authService.test.ts135 lines, 4.1 KB
| 1 | import { AuthService } from "../src/authService"; |
| 2 | |
| 3 | // Mock external dependencies |
| 4 | jest.mock("bcryptjs", () => ({ |
| 5 | hash: jest.fn(), |
| 6 | compare: jest.fn(), |
| 7 | })); |
| 8 | |
| 9 | jest.mock("jsonwebtoken", () => ({ |
| 10 | sign: jest.fn(), |
| 11 | verify: jest.fn(), |
| 12 | })); |
| 13 | |
| 14 | jest.mock("uuid", () => ({ |
| 15 | v4: jest.fn(), |
| 16 | })); |
| 17 | |
| 18 | // import mocked modules |
| 19 | import bcrypt from "bcryptjs"; |
| 20 | import jwt from "jsonwebtoken"; |
| 21 | import { v4 as uuidv4 } from "uuid"; |
| 22 | import { |
| 23 | resetAllMocks, |
| 24 | testJwtPayload, |
| 25 | testRefreshToken, |
| 26 | testUser, |
| 27 | } from "./setup"; |
| 28 | import { ServiceError } from "../../../shared/types"; |
| 29 | |
| 30 | const mockedUuidv4 = uuidv4 as unknown as jest.Mock<string, []>; |
| 31 | const mockedBcrypt = bcrypt as jest.Mocked<typeof bcrypt>; |
| 32 | const mockedJwt = jwt as jest.Mocked<typeof jwt>; |
| 33 | |
| 34 | // Helper function to test ServiceError |
| 35 | async function expectServiceError( |
| 36 | asyncFn: () => Promise<any>, |
| 37 | expectedMessage: string, |
| 38 | expectedStatusCode: number |
| 39 | ) { |
| 40 | try { |
| 41 | await asyncFn(); |
| 42 | fail("Expected function to throw ServiceError"); |
| 43 | } catch (error) { |
| 44 | expect(error).toBeInstanceOf(ServiceError); |
| 45 | expect(error.message).toBe(expectedMessage); |
| 46 | expect(error.statusCode).toBe(expectedStatusCode); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | describe("AuthService", () => { |
| 51 | let authService: AuthService; |
| 52 | |
| 53 | beforeAll(() => { |
| 54 | resetAllMocks(); |
| 55 | authService = new AuthService(); |
| 56 | |
| 57 | // setup default mock implementations |
| 58 | mockedUuidv4.mockReturnValue("test-uuid"); |
| 59 | (mockedBcrypt.hash as jest.Mock).mockResolvedValue("hashed-password"); |
| 60 | (mockedBcrypt.compare as jest.Mock).mockResolvedValue(true); |
| 61 | (mockedJwt.sign as jest.Mock).mockReturnValue("test-jwt-token"); |
| 62 | (mockedJwt.verify as jest.Mock).mockReturnValue(testJwtPayload); |
| 63 | }); |
| 64 | |
| 65 | describe("constructor", () => { |
| 66 | it("should initialize with environment variables", () => { |
| 67 | expect(authService).toBeInstanceOf(AuthService); |
| 68 | }); |
| 69 | |
| 70 | it("should throw an error if JWT_SECRET is not configured", () => { |
| 71 | delete process.env.JWT_SECRET; |
| 72 | expect(() => new AuthService()).toThrow( |
| 73 | "JWT secrets are not defined in environment variables" |
| 74 | ); |
| 75 | process.env.JWT_SECRET = "test-jwt-secret-key-for-testing-only"; // Reset for other tests |
| 76 | }); |
| 77 | |
| 78 | it("should throw an error if JWT_REFRESH_SECRET is not configured", () => { |
| 79 | delete process.env.JWT_REFRESH_SECRET; |
| 80 | expect(() => new AuthService()).toThrow( |
| 81 | "JWT secrets are not defined in environment variables" |
| 82 | ); |
| 83 | process.env.JWT_REFRESH_SECRET = |
| 84 | "test-jwt-refresh-secret-key-for-testing-only"; |
| 85 | }); |
| 86 | }); |
| 87 | |
| 88 | describe("register", () => { |
| 89 | const email = "sigmmmmma@user.com"; |
| 90 | const password = "testpassword"; |
| 91 | |
| 92 | it("should successfully register a new user", async () => { |
| 93 | // setup mocks |
| 94 | global.mockPrisma.user.findUnique.mockResolvedValue(null); |
| 95 | global.mockPrisma.user.create.mockResolvedValue(testUser); |
| 96 | global.mockPrisma.refreshToken.create.mockResolvedValue(testRefreshToken); |
| 97 | |
| 98 | const result = await authService.register(email, password); |
| 99 | |
| 100 | expect(global.mockPrisma.user.findUnique).toHaveBeenCalledWith({ |
| 101 | where: { email }, |
| 102 | }); |
| 103 | expect(mockedBcrypt.hash).toHaveBeenCalledWith(password, 4); |
| 104 | expect(global.mockPrisma.user.create).toHaveBeenCalledWith({ |
| 105 | data: { |
| 106 | email, |
| 107 | password: "hashed-password", |
| 108 | }, |
| 109 | }); |
| 110 | expect(result).toEqual({ |
| 111 | accessToken: "test-jwt-token", |
| 112 | refreshToken: "test-jwt-token", |
| 113 | }); |
| 114 | }); |
| 115 | |
| 116 | it("should throw an error if user already exists", async () => { |
| 117 | global.mockPrisma.user.findUnique.mockResolvedValue(testUser); |
| 118 | |
| 119 | await expectServiceError( |
| 120 | () => authService.register(email, password), |
| 121 | "User already exists", |
| 122 | 409 |
| 123 | ); |
| 124 | |
| 125 | expect(global.mockPrisma.user.create).not.toHaveBeenCalled(); |
| 126 | }); |
| 127 | |
| 128 | it("should handle database errors during creation", async () => { |
| 129 | global.mockPrisma.user.findUnique.mockResolvedValue(null); |
| 130 | global.mockPrisma.user.create.mockRejectedValue(new Error("DB Error")); |
| 131 | |
| 132 | await expect(authService.register(email, password)).rejects.toThrow("DB Error"); |
| 133 | }) |
| 134 | }); |
| 135 | }); |