UNPKG

@wernerthiago/teo

Version:

Test Execution Optimizer

88 lines (66 loc) 2.83 kB
// Tests for authentication service import { jest } from '@jest/globals' import AuthService from '../../src/auth/auth-service.js' describe('AuthService', () => { let authService beforeEach(() => { authService = new AuthService() }) describe('register', () => { test('should register new user successfully', async () => { const result = await authService.register('testuser', 'password123', 'test@example.com') expect(result).toHaveProperty('id') expect(result).toHaveProperty('username', 'testuser') }) test('should throw error for duplicate username', async () => { await authService.register('testuser', 'password123', 'test@example.com') await expect( authService.register('testuser', 'password456', 'test2@example.com') ).rejects.toThrow('Username already exists') }) }) describe('login', () => { beforeEach(async () => { await authService.register('testuser', 'password123', 'test@example.com') }) test('should login with valid credentials', async () => { const result = await authService.login('testuser', 'password123') expect(result).toHaveProperty('sessionId') expect(result).toHaveProperty('user') expect(result.user.username).toBe('testuser') }) test('should throw error for invalid credentials', async () => { await expect( authService.login('testuser', 'wrongpassword') ).rejects.toThrow('Invalid credentials') }) test('should throw error for non-existent user', async () => { await expect( authService.login('nonexistent', 'password123') ).rejects.toThrow('Invalid credentials') }) }) describe('validateSession', () => { test('should validate active session', async () => { await authService.register('testuser', 'password123', 'test@example.com') const loginResult = await authService.login('testuser', 'password123') const session = await authService.validateSession(loginResult.sessionId) expect(session).toBeTruthy() expect(session).toHaveProperty('userId') }) test('should return null for invalid session', async () => { const session = await authService.validateSession('invalid-session-id') expect(session).toBeNull() }) }) describe('logout', () => { test('should logout successfully', async () => { await authService.register('testuser', 'password123', 'test@example.com') const loginResult = await authService.login('testuser', 'password123') const result = await authService.logout(loginResult.sessionId) expect(result).toBe(true) const session = await authService.validateSession(loginResult.sessionId) expect(session).toBeNull() }) }) })