|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { Request, Response } from 'express'; |
| 3 | + |
| 4 | +import { AuthController } from './auth.controller'; |
| 5 | +import { AuthService } from './auth.service'; |
| 6 | + |
| 7 | +const mockAuthService = { |
| 8 | + githubLogin: jest.fn(), |
| 9 | + googleLogin: jest.fn(), |
| 10 | + discordLogin: jest.fn(), |
| 11 | + verifyToken: jest.fn(), |
| 12 | +}; |
| 13 | + |
| 14 | +describe('AuthController', () => { |
| 15 | + let controller: AuthController; |
| 16 | + let authService: AuthService; |
| 17 | + |
| 18 | + beforeEach(async () => { |
| 19 | + const module: TestingModule = await Test.createTestingModule({ |
| 20 | + controllers: [AuthController], |
| 21 | + providers: [ |
| 22 | + { |
| 23 | + provide: AuthService, |
| 24 | + useValue: mockAuthService, |
| 25 | + }, |
| 26 | + ], |
| 27 | + }).compile(); |
| 28 | + |
| 29 | + controller = module.get<AuthController>(AuthController); |
| 30 | + authService = module.get<AuthService>(AuthService); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should be defined', () => { |
| 34 | + expect(controller).toBeDefined(); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('githubRedirect', () => { |
| 38 | + it('should call AuthService.githubLogin', async () => { |
| 39 | + const req = {} as Request; |
| 40 | + const res = {} as Response; |
| 41 | + |
| 42 | + await controller.githubRedirect(req, res); |
| 43 | + |
| 44 | + expect(authService.githubLogin).toHaveBeenCalledWith(req, res); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should handle exceptions', async () => { |
| 48 | + const req = {} as Request; |
| 49 | + const res = {} as Response; |
| 50 | + const error = new Error('Test error'); |
| 51 | + (authService.githubLogin as jest.Mock).mockRejectedValueOnce(error); |
| 52 | + |
| 53 | + await expect(controller.githubRedirect(req, res)).rejects.toThrow( |
| 54 | + 'Test error', |
| 55 | + ); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('googleRedirect', () => { |
| 60 | + it('should call AuthService.googleLogin', async () => { |
| 61 | + const req = {} as Request; |
| 62 | + const res = {} as Response; |
| 63 | + |
| 64 | + await controller.googleRedirect(req, res); |
| 65 | + |
| 66 | + expect(authService.googleLogin).toHaveBeenCalledWith(req, res); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should handle exceptions', async () => { |
| 70 | + const req = {} as Request; |
| 71 | + const res = {} as Response; |
| 72 | + const error = new Error('Test error'); |
| 73 | + (authService.googleLogin as jest.Mock).mockRejectedValueOnce(error); |
| 74 | + |
| 75 | + await expect(controller.googleRedirect(req, res)).rejects.toThrow( |
| 76 | + 'Test error', |
| 77 | + ); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('discordRedirect', () => { |
| 82 | + it('should call AuthService.discordLogin', async () => { |
| 83 | + const req = {} as Request; |
| 84 | + const res = {} as Response; |
| 85 | + |
| 86 | + await controller.discordRedirect(req, res); |
| 87 | + |
| 88 | + expect(authService.discordLogin).toHaveBeenCalledWith(req, res); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should handle exceptions', async () => { |
| 92 | + const req = {} as Request; |
| 93 | + const res = {} as Response; |
| 94 | + const error = new Error('Test error'); |
| 95 | + (authService.discordLogin as jest.Mock).mockRejectedValueOnce(error); |
| 96 | + |
| 97 | + await expect(controller.discordRedirect(req, res)).rejects.toThrow( |
| 98 | + 'Test error', |
| 99 | + ); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe('verify', () => { |
| 104 | + it('should call AuthService.verifyToken', async () => { |
| 105 | + const req = {} as Request; |
| 106 | + const res = {} as Response; |
| 107 | + |
| 108 | + await controller.verify(req, res); |
| 109 | + |
| 110 | + expect(authService.verifyToken).toHaveBeenCalledWith(req, res); |
| 111 | + }); |
| 112 | + }); |
| 113 | +}); |
0 commit comments