userService.test.ts178 lines, 5.3 KB
| 1 | import { UserService } from "../src/userService"; |
| 2 | |
| 3 | // Mock external dependencies |
| 4 | jest.mock("../src/authClient"); |
| 5 | |
| 6 | // Import mocked modules |
| 7 | import axios from "axios"; |
| 8 | import { AuthClient } from "../src/authClient"; |
| 9 | import { ServiceError } from "../../../shared/types"; |
| 10 | import { |
| 11 | resetAllMocks, |
| 12 | testUpdateProfileRequest, |
| 13 | testUserProfile, |
| 14 | } from "./setup"; |
| 15 | |
| 16 | const MockedAuthClient = AuthClient as jest.MockedClass<typeof AuthClient>; |
| 17 | |
| 18 | // Helper function to test ServiceError |
| 19 | async function expectServiceError( |
| 20 | asyncFn: () => Promise<any>, |
| 21 | expectedMessage: string, |
| 22 | expectedStatusCode: number |
| 23 | ) { |
| 24 | try { |
| 25 | await asyncFn(); |
| 26 | fail("Expected function to throw ServiceError"); |
| 27 | } catch (error) { |
| 28 | expect(error).toBeInstanceOf(ServiceError); |
| 29 | expect(error.message).toBe(expectedMessage); |
| 30 | expect(error.statusCode).toBe(expectedStatusCode); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | describe("UserService", () => { |
| 35 | let userService: UserService; |
| 36 | let mockAuthClient: jest.Mocked<AuthClient>; |
| 37 | |
| 38 | beforeAll(() => { |
| 39 | resetAllMocks(); |
| 40 | |
| 41 | // Create mock AuthClient instance |
| 42 | mockAuthClient = { |
| 43 | validateToken: jest.fn(), |
| 44 | } as any; |
| 45 | |
| 46 | MockedAuthClient.mockImplementation(() => mockAuthClient); |
| 47 | |
| 48 | userService = new UserService(); |
| 49 | }); |
| 50 | |
| 51 | describe("createProfile", () => { |
| 52 | const userId = "test-user-id"; |
| 53 | const profileData = { |
| 54 | firstName: "Test", |
| 55 | lastName: "User", |
| 56 | bio: "This is a test user profile.", |
| 57 | avatarUrl: "http://example.com/avatar.jpg", |
| 58 | preferences: { theme: "dark", notifications: true }, |
| 59 | }; |
| 60 | |
| 61 | it("should create a user profile successfully", async () => { |
| 62 | global.mockPrisma.userProfile.findUnique.mockResolvedValue(null); |
| 63 | global.mockPrisma.userProfile.create.mockResolvedValue(testUserProfile); |
| 64 | |
| 65 | const result = await userService.createProfile(userId, profileData); |
| 66 | expect(global.mockPrisma.userProfile.findUnique).toHaveBeenCalledWith({ |
| 67 | where: { userId }, |
| 68 | }); |
| 69 | expect(global.mockPrisma.userProfile.create).toHaveBeenCalledWith({ |
| 70 | data: { |
| 71 | userId, |
| 72 | ...profileData, |
| 73 | }, |
| 74 | }); |
| 75 | expect(result).toEqual(testUserProfile); |
| 76 | }); |
| 77 | |
| 78 | it("should throw an error if profile already exists", async () => { |
| 79 | global.mockPrisma.userProfile.findUnique.mockResolvedValue( |
| 80 | testUserProfile |
| 81 | ); |
| 82 | |
| 83 | await expectServiceError( |
| 84 | () => userService.createProfile(userId, profileData), |
| 85 | "User profile already exists", |
| 86 | 409 |
| 87 | ); |
| 88 | |
| 89 | expect(global.mockPrisma.userProfile.create).not.toHaveBeenCalled(); |
| 90 | }); |
| 91 | |
| 92 | it("should sanitize input data before creating profile", async () => { |
| 93 | const unsanitizedData = { |
| 94 | firstName: "<script>alert('xss')</script>Test", |
| 95 | lastName: "User", |
| 96 | bio: "This is a test user profile.", |
| 97 | avatarUrl: "http://example.com/avatar.jpg", |
| 98 | preferences: { theme: "dark", notifications: true }, |
| 99 | }; |
| 100 | |
| 101 | global.mockPrisma.userProfile.findUnique.mockResolvedValue(null); |
| 102 | global.mockPrisma.userProfile.create.mockResolvedValue(testUserProfile); |
| 103 | |
| 104 | await userService.createProfile(userId, unsanitizedData); |
| 105 | |
| 106 | expect(global.mockPrisma.userProfile.create).toHaveBeenCalledWith({ |
| 107 | data: { |
| 108 | userId, |
| 109 | firstName: "scriptalert('xss')/scriptTest", |
| 110 | lastName: "User", |
| 111 | bio: "This is a test user profile.", |
| 112 | avatarUrl: "http://example.com/avatar.jpg", |
| 113 | preferences: { theme: "dark", notifications: true }, |
| 114 | }, |
| 115 | }); |
| 116 | }); |
| 117 | }); |
| 118 | |
| 119 | describe("getProfile", () => { |
| 120 | const userId = "test-user-id"; |
| 121 | |
| 122 | it("should retrieve an existing user profile successfully", async () => { |
| 123 | global.mockPrisma.userProfile.findUnique.mockResolvedValue( |
| 124 | testUserProfile |
| 125 | ); |
| 126 | |
| 127 | const result = await userService.getProfile(userId); |
| 128 | expect(global.mockPrisma.userProfile.findUnique).toHaveBeenCalledWith({ |
| 129 | where: { userId }, |
| 130 | }); |
| 131 | expect(result).toEqual(testUserProfile); |
| 132 | }); |
| 133 | |
| 134 | it("should throw an error if profile does not exist", async () => { |
| 135 | global.mockPrisma.userProfile.findUnique.mockResolvedValue(null); |
| 136 | |
| 137 | await expectServiceError( |
| 138 | () => userService.getProfile(userId), |
| 139 | "User profile not found", |
| 140 | 404 |
| 141 | ); |
| 142 | }); |
| 143 | }); |
| 144 | |
| 145 | describe("updateProfile", () => { |
| 146 | const userId = "test-user-id"; |
| 147 | |
| 148 | it("should update an existing user profile successfully", async () => { |
| 149 | global.mockPrisma.userProfile.findUnique.mockResolvedValue( |
| 150 | testUserProfile |
| 151 | ); |
| 152 | global.mockPrisma.userProfile.update.mockResolvedValue({ |
| 153 | ...testUserProfile, |
| 154 | ...testUpdateProfileRequest, |
| 155 | }); |
| 156 | }); |
| 157 | |
| 158 | it("should create a profile if it does not exist", async () => { |
| 159 | global.mockPrisma.userProfile.findUnique.mockResolvedValue(null); |
| 160 | global.mockPrisma.userProfile.create.mockResolvedValue(testUserProfile); |
| 161 | |
| 162 | const result = await userService.updateProfile( |
| 163 | userId, |
| 164 | testUpdateProfileRequest |
| 165 | ); |
| 166 | expect(global.mockPrisma.userProfile.findUnique).toHaveBeenCalledWith({ |
| 167 | where: { userId }, |
| 168 | }); |
| 169 | expect(global.mockPrisma.userProfile.create).toHaveBeenCalledWith({ |
| 170 | data: { |
| 171 | userId, |
| 172 | ...testUpdateProfileRequest, |
| 173 | }, |
| 174 | }); |
| 175 | expect(result).toEqual(testUserProfile); |
| 176 | }); |
| 177 | }); |
| 178 | }); |