import { authService } from '../auth.service'; import { AccountStatus } from '../auth.constants'; // Helper to clear localStorage and reset the mock DB before each test beforeEach(() => { // Clear all localStorage keys used by dbService and authService Object.keys(localStorage).forEach(key => localStorage.removeItem(key)); }); describe('Authentication Integration Tests', () => { const username = 'testuser'; const password = 'Passw0rd!'; const email = 'testuser@example.com'; test('User registration creates a pending account', async () => { const result = await authService.register(email, password, username); expect(result).toBeDefined(); expect(result.user.username).toBe(username); expect(result.user.email).toBe(email); expect(result.user.status).toBe(AccountStatus.PENDING); expect(result.user.emailVerified).toBeFalsy(); }); test('Login fails for unverified (pending) account', async () => { await expect(authService.login({ email, password })).rejects.toThrow(); }); test('Email verification activates the account', async () => { // Register a user first to get the verification token const result = await authService.register(email, password, username); const verificationToken = result.verificationToken.token; const verifiedUser = await authService.verifyEmail(verificationToken); expect(verifiedUser.status).toBe(AccountStatus.ACTIVE); expect(verifiedUser.emailVerified).toBeTruthy(); }); test('Login succeeds after email verification', async () => { const tokens = await authService.login({ email, password }); expect(tokens).toBeDefined(); expect(tokens.accessToken).toBeTruthy(); expect(tokens.refreshToken).toBeTruthy(); }); test('OAuth flow registers or logs in a user', async () => { const oauthEmail = 'oauthuser@example.com'; const oauthName = 'OAuth User'; const result = await authService.loginWithOAuth('google', { email: oauthEmail, username: oauthName, }); expect(result).toBeDefined(); expect(result.user.email).toBe(oauthEmail); // OAuth users should be active immediately expect(result.user.status).toBe(AccountStatus.ACTIVE); expect(result.user.emailVerified).toBeTruthy(); }); });