auth-service.service.spec.ts107 lines, 3.0 KB
| 1 | import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | import { AuthServiceService } from './auth-service.service'; |
| 3 | import { JwtService } from '@nestjs/jwt'; |
| 4 | import { KAFKA_SERVICE } from '@app/kafka'; |
| 5 | import { DatabaseService } from '@app/database'; |
| 6 | import * as bcrypt from 'bcrypt'; |
| 7 | |
| 8 | jest.mock('bcrypt', () => ({ |
| 9 | hash: jest.fn().mockResolvedValue('hashed-password'), |
| 10 | compare: jest.fn(), |
| 11 | })); |
| 12 | |
| 13 | describe('AuthServiceService', () => { |
| 14 | let service: AuthServiceService; |
| 15 | |
| 16 | // create mock objects |
| 17 | |
| 18 | const mockKafkaClient = { |
| 19 | emit: jest.fn(), |
| 20 | connect: jest.fn(), |
| 21 | }; |
| 22 | |
| 23 | const mockDbService = { |
| 24 | db: { |
| 25 | select: jest.fn().mockReturnThis(), |
| 26 | from: jest.fn().mockReturnThis(), |
| 27 | where: jest.fn().mockReturnThis(), |
| 28 | limit: jest.fn().mockReturnValue([]), |
| 29 | insert: jest.fn().mockReturnThis(), |
| 30 | values: jest.fn().mockReturnThis(), |
| 31 | returning: jest.fn().mockReturnValue([]), |
| 32 | }, |
| 33 | }; |
| 34 | |
| 35 | const mockJwtService = { |
| 36 | sign: jest.fn().mockReturnValue('mocked-jwt-token'), |
| 37 | }; |
| 38 | |
| 39 | beforeEach(async () => { |
| 40 | const module: TestingModule = await Test.createTestingModule({ |
| 41 | providers: [ |
| 42 | AuthServiceService, |
| 43 | { provide: KAFKA_SERVICE, useValue: mockKafkaClient }, |
| 44 | { provide: DatabaseService, useValue: mockDbService }, |
| 45 | { provide: JwtService, useValue: mockJwtService }, |
| 46 | ], |
| 47 | }).compile(); |
| 48 | |
| 49 | service = module.get<AuthServiceService>(AuthServiceService); |
| 50 | |
| 51 | // Clear mock call history before each test |
| 52 | jest.clearAllMocks(); |
| 53 | }); |
| 54 | |
| 55 | describe('getHello', () => { |
| 56 | it("should return 'Hello World!'", () => { |
| 57 | const result = service.getHello(); |
| 58 | expect(result).toBe('Hello World!'); |
| 59 | }); |
| 60 | }); |
| 61 | |
| 62 | describe('register', () => { |
| 63 | it('should register a new user successfully', async () => { |
| 64 | // Mock no existing user |
| 65 | mockDbService.db.limit.mockReturnValueOnce([]); |
| 66 | |
| 67 | const mockUser = { |
| 68 | id: 'user-id-123', |
| 69 | email: 'fiston@withcodewise.com', |
| 70 | name: 'Fiston', |
| 71 | }; |
| 72 | mockDbService.db.returning.mockReturnValueOnce([mockUser]); |
| 73 | |
| 74 | const result = await service.register( |
| 75 | 'fiston@withcodewise.com', |
| 76 | 'securePassword', |
| 77 | 'Fiston', |
| 78 | ); |
| 79 | |
| 80 | expect(result).toEqual({ |
| 81 | message: 'User registered successfully', |
| 82 | userId: 'user-id-123', |
| 83 | }); |
| 84 | |
| 85 | expect(bcrypt.hash).toHaveBeenCalledWith('securePassword', 10); |
| 86 | |
| 87 | expect(mockKafkaClient.emit).toHaveBeenCalledWith( |
| 88 | 'user.registered', |
| 89 | expect.objectContaining({ |
| 90 | userId: 'user-id-123', |
| 91 | email: 'fiston@withcodewise.com', |
| 92 | name: 'Fiston', |
| 93 | }), |
| 94 | ); |
| 95 | }); |
| 96 | |
| 97 | it('should throw ConflictException if user already exists', async () => { |
| 98 | mockDbService.db.limit.mockReturnValueOnce([ |
| 99 | { id: 'existing-user-id', email: 'fiston@withcodewise.com' }, |
| 100 | ]); |
| 101 | |
| 102 | await expect( |
| 103 | service.register('fiston@withcodewise.com', 'securePassword', 'Fiston'), |
| 104 | ).rejects.toThrow('User already exists'); |
| 105 | }); |
| 106 | }); |
| 107 | }); |