auth.controller.test.ts359 lines, 11 KB
| 1 | import request from "supertest"; |
| 2 | import { app } from "../../index"; |
| 3 | import { AuthService } from "../../services/auth.service"; |
| 4 | import { StatusCodes } from "http-status-codes"; |
| 5 | |
| 6 | // Mock AuthService |
| 7 | jest.mock("../../services/auth.service"); |
| 8 | |
| 9 | describe("AuthController", () => { |
| 10 | const mockUser = { |
| 11 | id: "test-uuid", |
| 12 | email: "test@example.com", |
| 13 | name: "Test User", |
| 14 | }; |
| 15 | |
| 16 | const mockToken = "test-token"; |
| 17 | |
| 18 | beforeEach(() => { |
| 19 | jest.clearAllMocks(); |
| 20 | }); |
| 21 | |
| 22 | describe("POST /api/v1/auth/register", () => { |
| 23 | it("should register a new user with valid data", async () => { |
| 24 | // Mock data |
| 25 | const userData = { |
| 26 | email: "test@example.com", |
| 27 | password: "Test1234!", |
| 28 | name: "Test User", |
| 29 | }; |
| 30 | |
| 31 | // Mock service |
| 32 | (AuthService.register as jest.Mock).mockResolvedValueOnce({ |
| 33 | user: mockUser, |
| 34 | token: mockToken, |
| 35 | }); |
| 36 | |
| 37 | // Act |
| 38 | const response = await request(app) |
| 39 | .post("/api/v1/auth/register") |
| 40 | .send(userData); |
| 41 | |
| 42 | // Assert |
| 43 | expect(response.status).toBe(StatusCodes.CREATED); |
| 44 | expect(response.body).toHaveProperty("status", "success"); |
| 45 | expect(response.body.data).toHaveProperty("user"); |
| 46 | expect(response.body.data).toHaveProperty("token", mockToken); |
| 47 | expect(AuthService.register).toHaveBeenCalledWith( |
| 48 | userData.email, |
| 49 | userData.password, |
| 50 | userData.name |
| 51 | ); |
| 52 | }); |
| 53 | |
| 54 | it("should handle registration error", async () => { |
| 55 | // Mock data |
| 56 | const userData = { |
| 57 | email: "test@example.com", |
| 58 | password: "Test1234!", |
| 59 | name: "Test User", |
| 60 | }; |
| 61 | |
| 62 | // Mock service to throw error |
| 63 | (AuthService.register as jest.Mock).mockRejectedValueOnce( |
| 64 | new Error("Registration failed") |
| 65 | ); |
| 66 | |
| 67 | // Act |
| 68 | const response = await request(app) |
| 69 | .post("/api/v1/auth/register") |
| 70 | .send(userData); |
| 71 | |
| 72 | // Assert |
| 73 | expect(response.status).toBe(StatusCodes.INTERNAL_SERVER_ERROR); |
| 74 | expect(response.body).toHaveProperty("status", "error"); |
| 75 | }); |
| 76 | |
| 77 | it("should validate email format", async () => { |
| 78 | // Act |
| 79 | const response = await request(app).post("/api/v1/auth/register").send({ |
| 80 | email: "invalid-email", |
| 81 | password: "Test1234!", |
| 82 | name: "Test User", |
| 83 | }); |
| 84 | |
| 85 | // Assert |
| 86 | expect(response.status).toBe(StatusCodes.BAD_REQUEST); |
| 87 | expect(response.body).toHaveProperty("status", "error"); |
| 88 | expect(response.body).toHaveProperty("errors"); |
| 89 | expect(response.body.errors).toContainEqual( |
| 90 | expect.objectContaining({ |
| 91 | path: "email", |
| 92 | msg: expect.stringContaining("valid email"), |
| 93 | }) |
| 94 | ); |
| 95 | }); |
| 96 | |
| 97 | it("should validate password strength", async () => { |
| 98 | // Act |
| 99 | const response = await request(app).post("/api/v1/auth/register").send({ |
| 100 | email: "test@example.com", |
| 101 | password: "weak", |
| 102 | name: "Test User", |
| 103 | }); |
| 104 | |
| 105 | // Assert |
| 106 | expect(response.status).toBe(StatusCodes.BAD_REQUEST); |
| 107 | expect(response.body).toHaveProperty("status", "error"); |
| 108 | expect(response.body).toHaveProperty("errors"); |
| 109 | expect(response.body.errors).toContainEqual( |
| 110 | expect.objectContaining({ |
| 111 | path: "password", |
| 112 | }) |
| 113 | ); |
| 114 | }); |
| 115 | |
| 116 | it("should require name field", async () => { |
| 117 | // Act |
| 118 | const response = await request(app).post("/api/v1/auth/register").send({ |
| 119 | email: "test@example.com", |
| 120 | password: "Test1234!", |
| 121 | }); |
| 122 | |
| 123 | // Assert |
| 124 | expect(response.status).toBe(StatusCodes.BAD_REQUEST); |
| 125 | expect(response.body).toHaveProperty("status", "error"); |
| 126 | expect(response.body).toHaveProperty("errors"); |
| 127 | expect(response.body.errors).toContainEqual( |
| 128 | expect.objectContaining({ |
| 129 | path: "name", |
| 130 | }) |
| 131 | ); |
| 132 | }); |
| 133 | }); |
| 134 | |
| 135 | describe("POST /api/v1/auth/login", () => { |
| 136 | it("should login user with valid credentials", async () => { |
| 137 | // Setup |
| 138 | const loginData = { |
| 139 | email: "test@example.com", |
| 140 | password: "password123", |
| 141 | }; |
| 142 | |
| 143 | (AuthService.login as jest.Mock).mockResolvedValueOnce({ |
| 144 | user: mockUser, |
| 145 | token: mockToken, |
| 146 | }); |
| 147 | |
| 148 | // Execute |
| 149 | const response = await request(app) |
| 150 | .post("/api/v1/auth/login") |
| 151 | .send(loginData); |
| 152 | |
| 153 | // Assert |
| 154 | expect(response.status).toBe(StatusCodes.OK); |
| 155 | expect(response.body).toHaveProperty("status", "success"); |
| 156 | expect(response.body.data).toHaveProperty("user"); |
| 157 | expect(response.body.data).toHaveProperty("token", mockToken); |
| 158 | expect(AuthService.login).toHaveBeenCalledWith( |
| 159 | loginData.email, |
| 160 | loginData.password |
| 161 | ); |
| 162 | }); |
| 163 | |
| 164 | it("should validate email format during login", async () => { |
| 165 | // Execute |
| 166 | const response = await request(app).post("/api/v1/auth/login").send({ |
| 167 | email: "invalid-email", |
| 168 | password: "password123", |
| 169 | }); |
| 170 | |
| 171 | // Assert |
| 172 | expect(response.status).toBe(StatusCodes.BAD_REQUEST); |
| 173 | expect(response.body).toHaveProperty("status", "error"); |
| 174 | expect(response.body).toHaveProperty("errors"); |
| 175 | expect(response.body.errors).toContainEqual( |
| 176 | expect.objectContaining({ |
| 177 | path: "email", |
| 178 | }) |
| 179 | ); |
| 180 | }); |
| 181 | |
| 182 | it("should require password field", async () => { |
| 183 | // Execute |
| 184 | const response = await request(app).post("/api/v1/auth/login").send({ |
| 185 | email: "test@example.com", |
| 186 | }); |
| 187 | |
| 188 | // Assert |
| 189 | expect(response.status).toBe(StatusCodes.BAD_REQUEST); |
| 190 | expect(response.body).toHaveProperty("status", "error"); |
| 191 | expect(response.body).toHaveProperty("errors"); |
| 192 | expect(response.body.errors).toContainEqual( |
| 193 | expect.objectContaining({ |
| 194 | path: "password", |
| 195 | }) |
| 196 | ); |
| 197 | }); |
| 198 | |
| 199 | it("should handle login error", async () => { |
| 200 | // Setup |
| 201 | (AuthService.login as jest.Mock).mockRejectedValueOnce( |
| 202 | new Error("Invalid credentials") |
| 203 | ); |
| 204 | |
| 205 | // Execute |
| 206 | const response = await request(app).post("/api/v1/auth/login").send({ |
| 207 | email: "test@example.com", |
| 208 | password: "wrongpassword", |
| 209 | }); |
| 210 | |
| 211 | // Assert |
| 212 | expect(response.status).toBe(StatusCodes.INTERNAL_SERVER_ERROR); |
| 213 | expect(response.body).toHaveProperty("status", "error"); |
| 214 | }); |
| 215 | }); |
| 216 | |
| 217 | describe("GET /api/v1/auth/verify-email", () => { |
| 218 | it("should verify email with valid token", async () => { |
| 219 | // Setup |
| 220 | const validToken = "valid-token"; |
| 221 | (AuthService.verifyEmail as jest.Mock).mockResolvedValueOnce({ |
| 222 | message: "Email verified successfully", |
| 223 | }); |
| 224 | |
| 225 | // Execute |
| 226 | const response = await request(app) |
| 227 | .get("/api/v1/auth/verify-email") |
| 228 | .query({ token: validToken }); |
| 229 | |
| 230 | // Assert |
| 231 | expect(response.status).toBe(StatusCodes.OK); |
| 232 | expect(response.body).toHaveProperty("status", "success"); |
| 233 | expect(response.body.data).toHaveProperty( |
| 234 | "message", |
| 235 | "Email verified successfully" |
| 236 | ); |
| 237 | expect(AuthService.verifyEmail).toHaveBeenCalledWith(validToken); |
| 238 | }); |
| 239 | |
| 240 | it("should require token parameter", async () => { |
| 241 | // Act |
| 242 | const response = await request(app).get("/api/v1/auth/verify-email"); |
| 243 | |
| 244 | // Assert |
| 245 | expect(response.status).toBe(StatusCodes.BAD_REQUEST); |
| 246 | expect(response.body).toHaveProperty("status", "error"); |
| 247 | expect(response.body).toHaveProperty("errors"); |
| 248 | expect(response.body.errors).toContainEqual( |
| 249 | expect.objectContaining({ |
| 250 | path: "token", |
| 251 | }) |
| 252 | ); |
| 253 | }); |
| 254 | |
| 255 | it("should handle invalid token", async () => { |
| 256 | // Setup |
| 257 | const invalidToken = "invalid-token"; |
| 258 | (AuthService.verifyEmail as jest.Mock).mockRejectedValueOnce( |
| 259 | new Error("Invalid or expired token") |
| 260 | ); |
| 261 | |
| 262 | // Execute |
| 263 | const response = await request(app) |
| 264 | .get("/api/v1/auth/verify-email") |
| 265 | .query({ token: invalidToken }); |
| 266 | |
| 267 | // Assert |
| 268 | expect(response.status).toBe(StatusCodes.INTERNAL_SERVER_ERROR); |
| 269 | expect(response.body).toHaveProperty("status", "error"); |
| 270 | }); |
| 271 | }); |
| 272 | |
| 273 | describe("POST /api/v1/auth/resend-verification", () => { |
| 274 | it("should resend verification email with valid email", async () => { |
| 275 | // Setup |
| 276 | const email = "test@example.com"; |
| 277 | (AuthService.resendVerificationEmail as jest.Mock).mockResolvedValueOnce({ |
| 278 | message: "Verification email sent", |
| 279 | }); |
| 280 | |
| 281 | // Execute |
| 282 | const response = await request(app) |
| 283 | .post("/api/v1/auth/resend-verification") |
| 284 | .send({ email }); |
| 285 | |
| 286 | // Assert |
| 287 | expect(response.status).toBe(StatusCodes.OK); |
| 288 | expect(response.body).toHaveProperty("status", "success"); |
| 289 | expect(response.body.data).toHaveProperty( |
| 290 | "message", |
| 291 | "Verification email sent" |
| 292 | ); |
| 293 | expect(AuthService.resendVerificationEmail).toHaveBeenCalledWith(email); |
| 294 | }); |
| 295 | |
| 296 | it("should validate email format", async () => { |
| 297 | // Execute |
| 298 | const response = await request(app) |
| 299 | .post("/api/v1/auth/resend-verification") |
| 300 | .send({ email: "invalid-email" }); |
| 301 | |
| 302 | // Assert |
| 303 | expect(response.status).toBe(StatusCodes.BAD_REQUEST); |
| 304 | expect(response.body).toHaveProperty("status", "error"); |
| 305 | expect(response.body).toHaveProperty("errors"); |
| 306 | expect(response.body.errors).toContainEqual( |
| 307 | expect.objectContaining({ |
| 308 | path: "email", |
| 309 | }) |
| 310 | ); |
| 311 | }); |
| 312 | |
| 313 | it("should require email field", async () => { |
| 314 | // Execute |
| 315 | const response = await request(app) |
| 316 | .post("/api/v1/auth/resend-verification") |
| 317 | .send({}); |
| 318 | |
| 319 | // Assert |
| 320 | expect(response.status).toBe(StatusCodes.BAD_REQUEST); |
| 321 | expect(response.body).toHaveProperty("status", "error"); |
| 322 | expect(response.body).toHaveProperty("errors"); |
| 323 | }); |
| 324 | }); |
| 325 | |
| 326 | describe("GET /api/v1/auth/me", () => { |
| 327 | it("should get user profile when authenticated", async () => { |
| 328 | // Setup |
| 329 | (AuthService.verifyToken as jest.Mock).mockReturnValueOnce({ |
| 330 | userId: mockUser.id, |
| 331 | email: mockUser.email, |
| 332 | }); |
| 333 | (AuthService.getProfile as jest.Mock).mockResolvedValueOnce({ |
| 334 | user: mockUser, |
| 335 | token: mockToken, |
| 336 | }); |
| 337 | |
| 338 | // Execute |
| 339 | const response = await request(app) |
| 340 | .get("/api/v1/auth/me") |
| 341 | .set("Authorization", `Bearer ${mockToken}`); |
| 342 | |
| 343 | // Assert |
| 344 | expect(response.status).toBe(StatusCodes.OK); |
| 345 | expect(response.body).toHaveProperty("status", "success"); |
| 346 | expect(response.body.data).toHaveProperty("user"); |
| 347 | expect(response.body.data).toHaveProperty("token", mockToken); |
| 348 | }); |
| 349 | |
| 350 | it("should return unauthorized when no token is provided", async () => { |
| 351 | // Execute |
| 352 | const response = await request(app).get("/api/v1/auth/me"); |
| 353 | |
| 354 | // Assert |
| 355 | expect(response.status).toBe(StatusCodes.UNAUTHORIZED); |
| 356 | expect(response.body).toHaveProperty("status", "error"); |
| 357 | }); |
| 358 | }); |
| 359 | }); |