import { JwtService } from '@nestjs/jwt'; import { AuthService } from '../src/auth/auth.service'; import * as bcrypt from 'bcrypt'; describe('AuthService', () => { let service: AuthService; const mockPrisma = { user: { findUnique: jest.fn(), count: jest.fn(), create: jest.fn(), }, }; const mockJwt = { sign: jest.fn().mockReturnValue('mock-jwt-token'), }; beforeEach(() => { service = new AuthService(mockPrisma as any, mockJwt as any); jest.clearAllMocks(); }); it('returns token for valid credentials', async () => { const hash = await bcrypt.hash('password123', 10); mockPrisma.user.findUnique.mockResolvedValue({ id: 1, email: 'admin@wraith.local', passwordHash: hash, displayName: 'Admin', }); const result = await service.login('admin@wraith.local', 'password123'); expect(result).toEqual({ access_token: 'mock-jwt-token', user: { id: 1, email: 'admin@wraith.local', displayName: 'Admin' }, }); }); it('throws on wrong password', async () => { const hash = await bcrypt.hash('correct', 10); mockPrisma.user.findUnique.mockResolvedValue({ id: 1, email: 'admin@wraith.local', passwordHash: hash, }); await expect(service.login('admin@wraith.local', 'wrong')) .rejects.toThrow('Invalid credentials'); }); it('throws on unknown user', async () => { mockPrisma.user.findUnique.mockResolvedValue(null); await expect(service.login('nobody@wraith.local', 'pass')) .rejects.toThrow('Invalid credentials'); }); });