Skip to content

Commit

Permalink
Add auth test
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Aug 13, 2024
1 parent cf4cf81 commit f94e72f
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
165 changes: 165 additions & 0 deletions app/assets/tests/stores/auth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import { setActivePinia, createPinia } from 'pinia'
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { useAuthStore } from '../../stores/auth'
import axios from 'axios'
import type { LoginForm, UserInterface } from 'app/assets/interfaces'
import { AlertStyle } from '@userfrosting/sprinkle-core/types'

const testUser: UserInterface = {
id: 1,
user_name: 'JohnDoe',
first_name: 'John',
last_name: 'Doe',
full_name: 'John Doe',
email: '[email protected]',
avatar: '',
flag_enabled: true,
flag_verified: true,
group_id: null,
locale: 'en_US',
created_at: '',
updated_at: '',
deleted_at: null
}

const form: LoginForm = {
user_name: 'john',
password: 'password'
}

describe('authStore', () => {
beforeEach(() => {
setActivePinia(createPinia())
})

test('should set, get and unset the user', () => {
// Arrange
const authStore = useAuthStore()

// Assert initial state
expect(authStore.user).toBeNull()
expect(authStore.isAuthenticated).toBe(false)

// Assert set user
authStore.setUser(testUser)
expect(authStore.user).toStrictEqual(testUser)
expect(authStore.isAuthenticated).toBe(true)

// Assert unset user
authStore.unsetUser()
expect(authStore.user).toBeNull()
expect(authStore.isAuthenticated).toBe(false)
})

test('should login successfully', async () => {
// Arrange
const authStore = useAuthStore()
const response = { data: testUser }
vi.spyOn(axios, 'post').mockResolvedValue(response as any)

// Assert initial state
expect(authStore.user).toBeNull()

// Act
const result = await authStore.login(form)

// Assert
expect(axios.post).toHaveBeenCalledWith('/account/login', form)
expect(result).toStrictEqual(testUser)
expect(authStore.user).toStrictEqual(testUser)
})

test('should throw an error when login fails', async () => {
// Arrange
const authStore = useAuthStore()
const error = { response: { data: { description: 'Bad password' } } }
vi.spyOn(axios, 'post').mockRejectedValue(error as any)

// Assert initial state
expect(authStore.user).toBeNull()

// Act & Assert
await expect(authStore.login(form)).rejects.toEqual({
description: 'Bad password',
style: AlertStyle.Danger,
closeBtn: true
})
expect(axios.post).toHaveBeenCalledWith('/account/login', form)
expect(authStore.user).toBeNull()
})

test('should check authentication successfully', async () => {
// Arrange
const authStore = useAuthStore()
const response = { data: { auth: true, user: testUser } }
vi.spyOn(axios, 'get').mockResolvedValue(response as any)

// Assert initial state
expect(authStore.user).toBeNull()

// Act
const result = await authStore.check()

// Assert
expect(axios.get).toHaveBeenCalledWith('/account/auth-check')
expect(result).toStrictEqual(testUser)
expect(authStore.user).toStrictEqual(testUser)
})

test('should unset the user when authentication check fails', async () => {
// Arrange
const authStore = useAuthStore()
const error = { response: { data: {} } }
vi.spyOn(axios, 'get').mockRejectedValue(error as any)

// Assert initial state
authStore.setUser(testUser)
expect(authStore.user).toStrictEqual(testUser)

// Act & Assert
await expect(authStore.check()).rejects.toEqual({
description: 'An error as occurred',
style: AlertStyle.Danger,
closeBtn: true
})
expect(axios.get).toHaveBeenCalledWith('/account/auth-check')
expect(authStore.user).toBeNull()
})

test('should logout successfully', async () => {
// Arrange
const authStore = useAuthStore()
vi.spyOn(axios, 'get').mockResolvedValue({} as any)

// Assert initial state
authStore.setUser(testUser)
expect(authStore.user).toStrictEqual(testUser)

// Act
await authStore.logout()

// Assert
expect(axios.get).toHaveBeenCalledWith('/account/logout')
expect(authStore.user).toBeNull()
})

test('should throw an error when logout fails', async () => {
// Arrange
const authStore = useAuthStore()
const error = { response: { data: {} } }
vi.spyOn(axios, 'get').mockRejectedValue(error as any)

// Assert initial state
authStore.setUser(testUser)
expect(authStore.user).toStrictEqual(testUser)

// Act & Assert
await expect(authStore.logout()).rejects.toEqual({
description: 'An error as occurred',
style: AlertStyle.Danger,
closeBtn: true
})
expect(axios.get).toHaveBeenCalledWith('/account/logout')
expect(authStore.user).toBeNull() // User will be unset even if logout fails
})
})
1 change: 1 addition & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default defineConfig({
},
test: {
coverage: {
include: ['app/assets/**/*.*'],
reportsDirectory: './_meta/_coverage'
},
environment: 'happy-dom'
Expand Down

0 comments on commit f94e72f

Please sign in to comment.