|
| 1 | +import type { Request, Response } from 'express'; |
| 2 | +import { AuthenticationJWT } from './auth-jwt.js'; |
| 3 | +import type { UserRepository } from '@/features/user/repositories/user-repository/user-repository.js'; |
| 4 | +import { JWTHelper } from '@/shared/infra/jwt/jwt.js'; |
| 5 | + |
| 6 | +const secretKey = '321'; |
| 7 | + |
| 8 | +const makeSut = () => { |
| 9 | + class UserRepositoryStub implements UserRepository { |
| 10 | + create({ email, name, password, username }: any) { |
| 11 | + return Promise.resolve({ |
| 12 | + createdAt: new Date(2024, 5, 1), |
| 13 | + deletedAt: null, |
| 14 | + email, |
| 15 | + id: 'valid_id', |
| 16 | + name, |
| 17 | + password, |
| 18 | + updatedAt: new Date(2024, 5, 1), |
| 19 | + username, |
| 20 | + }); |
| 21 | + } |
| 22 | + |
| 23 | + findById(id: string): Promise<{ |
| 24 | + email: string; |
| 25 | + id: string; |
| 26 | + name: null | string; |
| 27 | + username: string; |
| 28 | + } | null> { |
| 29 | + const user = { |
| 30 | + |
| 31 | + id: 'fakeUserId', |
| 32 | + name: 'FakeName', |
| 33 | + username: 'FakeUserName', |
| 34 | + }; |
| 35 | + if (id === 'fakeUserId') { |
| 36 | + return Promise.resolve(user); |
| 37 | + } |
| 38 | + return Promise.resolve(null); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + const userRepository = new UserRepositoryStub(); |
| 43 | + const jwtHelper = new JWTHelper(secretKey as string); |
| 44 | + const auth = new AuthenticationJWT(jwtHelper, userRepository); |
| 45 | + |
| 46 | + return { auth, jwtHelper, userRepository }; |
| 47 | +}; |
| 48 | + |
| 49 | +describe('jwtAuth middleware', () => { |
| 50 | + let req: Partial<Request>; |
| 51 | + let res: Partial<Response>; |
| 52 | + let next: ReturnType<typeof vi.fn>; |
| 53 | + const { auth, jwtHelper } = makeSut(); |
| 54 | + |
| 55 | + beforeEach(() => { |
| 56 | + req = { headers: { authorization: 'Bearer' } }; |
| 57 | + res = { json: vi.fn(), status: vi.fn().mockReturnThis() }; |
| 58 | + next = vi.fn(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should call next if token is valid and user is found', async () => { |
| 62 | + const token = jwtHelper.createToken({ userId: 'fakeUserId' }); |
| 63 | + |
| 64 | + req = { headers: { authorization: `Bearer ${token}` } }; |
| 65 | + |
| 66 | + await auth.jwtAuth(req as Request, res as Response, next); |
| 67 | + |
| 68 | + expect(res.json).not.toHaveBeenCalled(); |
| 69 | + expect(res.status).not.toHaveBeenCalled(); |
| 70 | + expect(next).toHaveBeenCalled(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should return status code 401 with error message token missing', async () => { |
| 74 | + req.headers!.authorization = undefined; |
| 75 | + |
| 76 | + await auth.jwtAuth(req as Request, res as Response, next); |
| 77 | + |
| 78 | + expect(res.status).toHaveBeenCalledWith(401); |
| 79 | + expect(res.json).toHaveBeenCalledWith({ error: 'Token missing' }); |
| 80 | + expect(next).not.toHaveBeenCalled(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should return status code 401 with error message invalid token', async () => { |
| 84 | + const token = 'invalidToken'; |
| 85 | + |
| 86 | + req = { headers: { authorization: `Bearer ${token}` } }; |
| 87 | + |
| 88 | + await auth.jwtAuth(req as Request, res as Response, next); |
| 89 | + |
| 90 | + expect(res.status).toHaveBeenCalledWith(401); |
| 91 | + expect(res.json).toHaveBeenCalledWith({ error: 'Invalid token' }); |
| 92 | + expect(next).not.toHaveBeenCalled(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should return status code 401 with error message invalid user', async () => { |
| 96 | + const token = jwtHelper.createToken({ userId: '2' }); |
| 97 | + |
| 98 | + req = { headers: { authorization: `Bearer ${token}` } }; |
| 99 | + |
| 100 | + await auth.jwtAuth(req as Request, res as Response, next); |
| 101 | + expect(res.status).toHaveBeenCalledWith(401); |
| 102 | + expect(res.json).toHaveBeenCalledWith({ error: 'Invalid user' }); |
| 103 | + expect(next).not.toHaveBeenCalled(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should handle errors', async () => { |
| 107 | + const mockedError = new Error('fakeError'); |
| 108 | + vi.spyOn(jwtHelper, 'parseToken').mockImplementation(() => { |
| 109 | + throw mockedError; |
| 110 | + }); |
| 111 | + |
| 112 | + req = { headers: { authorization: 'Bearer 23123' } }; |
| 113 | + |
| 114 | + await auth.jwtAuth(req as Request, res as Response, next); |
| 115 | + expect(res.status).toHaveBeenCalledWith(500); |
| 116 | + expect(res.json).toHaveBeenCalledWith({ error: 'Internal Server Error' }); |
| 117 | + expect(next).not.toHaveBeenCalled(); |
| 118 | + }); |
| 119 | +}); |
0 commit comments