auth.middleware.test.ts116 lines, 3.0 KB
| 1 | import { Request, Response, NextFunction } from "express"; |
| 2 | import { authenticate } from "../../middleware/auth.middleware"; |
| 3 | import { AuthService } from "../../services/auth.service"; |
| 4 | import { StatusCodes } from "http-status-codes"; |
| 5 | import { AppError } from "../../utils/errors"; |
| 6 | |
| 7 | // Mock dependencies |
| 8 | jest.mock("../../services/auth.service"); |
| 9 | jest.mock("../../services/email.service"); |
| 10 | |
| 11 | describe("Auth Middleware", () => { |
| 12 | let mockRequest: Partial<Request>; |
| 13 | let mockResponse: Partial<Response>; |
| 14 | let nextFunction: jest.Mock; |
| 15 | |
| 16 | beforeEach(() => { |
| 17 | mockRequest = { |
| 18 | headers: {}, |
| 19 | }; |
| 20 | mockResponse = { |
| 21 | json: jest.fn(), |
| 22 | status: jest.fn().mockReturnThis(), |
| 23 | }; |
| 24 | nextFunction = jest.fn(); |
| 25 | jest.clearAllMocks(); |
| 26 | }); |
| 27 | |
| 28 | it("should call next() when token is valid", async () => { |
| 29 | // Setup |
| 30 | const mockDecodedToken = { userId: "user-123", email: "test@example.com" }; |
| 31 | mockRequest.headers = { |
| 32 | authorization: "Bearer valid-token", |
| 33 | }; |
| 34 | (AuthService.verifyToken as jest.Mock).mockReturnValueOnce( |
| 35 | mockDecodedToken |
| 36 | ); |
| 37 | |
| 38 | // Execute |
| 39 | await authenticate( |
| 40 | mockRequest as Request, |
| 41 | mockResponse as Response, |
| 42 | nextFunction |
| 43 | ); |
| 44 | |
| 45 | // Assert |
| 46 | expect(AuthService.verifyToken).toHaveBeenCalledWith("valid-token"); |
| 47 | expect(mockRequest.user).toEqual(mockDecodedToken); |
| 48 | expect(nextFunction).toHaveBeenCalled(); |
| 49 | expect(nextFunction).not.toHaveBeenCalledWith(expect.any(Error)); |
| 50 | }); |
| 51 | |
| 52 | it("should call next with error when no authorization header", async () => { |
| 53 | // Execute |
| 54 | await authenticate( |
| 55 | mockRequest as Request, |
| 56 | mockResponse as Response, |
| 57 | nextFunction |
| 58 | ); |
| 59 | |
| 60 | // Assert |
| 61 | expect(nextFunction).toHaveBeenCalledWith( |
| 62 | expect.objectContaining({ |
| 63 | statusCode: StatusCodes.UNAUTHORIZED, |
| 64 | message: "Unauthorized", |
| 65 | }) |
| 66 | ); |
| 67 | }); |
| 68 | |
| 69 | it("should call next with error when token format is invalid", async () => { |
| 70 | // Setup |
| 71 | mockRequest.headers = { |
| 72 | authorization: "InvalidTokenFormat", |
| 73 | }; |
| 74 | |
| 75 | // Execute |
| 76 | await authenticate( |
| 77 | mockRequest as Request, |
| 78 | mockResponse as Response, |
| 79 | nextFunction |
| 80 | ); |
| 81 | |
| 82 | // Assert |
| 83 | expect(nextFunction).toHaveBeenCalledWith( |
| 84 | expect.objectContaining({ |
| 85 | statusCode: StatusCodes.UNAUTHORIZED, |
| 86 | message: "Unauthorized", |
| 87 | }) |
| 88 | ); |
| 89 | }); |
| 90 | |
| 91 | it("should call next with error when token verification fails", async () => { |
| 92 | // Setup |
| 93 | mockRequest.headers = { |
| 94 | authorization: "Bearer invalid-token", |
| 95 | }; |
| 96 | (AuthService.verifyToken as jest.Mock).mockImplementationOnce(() => { |
| 97 | throw new AppError(StatusCodes.UNAUTHORIZED, "Invalid token"); |
| 98 | }); |
| 99 | |
| 100 | // Execute |
| 101 | await authenticate( |
| 102 | mockRequest as Request, |
| 103 | mockResponse as Response, |
| 104 | nextFunction |
| 105 | ); |
| 106 | |
| 107 | // Assert |
| 108 | expect(AuthService.verifyToken).toHaveBeenCalledWith("invalid-token"); |
| 109 | expect(nextFunction).toHaveBeenCalledWith( |
| 110 | expect.objectContaining({ |
| 111 | statusCode: StatusCodes.UNAUTHORIZED, |
| 112 | message: "Invalid token", |
| 113 | }) |
| 114 | ); |
| 115 | }); |
| 116 | }); |