|
| 1 | +// import { Request, Response } from "express"; |
| 2 | +// import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +// import { z } from "zod"; |
| 4 | +// import { AuthController } from "../../../../src/modules/auth/auth.controller"; |
| 5 | + |
| 6 | +// vi.mock("../../../../src/types/auth.types.js", () => ({ |
| 7 | +// OAuthProviderSchema: { |
| 8 | +// parse: vi.fn((val) => { |
| 9 | +// if (val === "invalid") throw new z.ZodError([]); |
| 10 | +// return val; |
| 11 | +// }), |
| 12 | +// }, |
| 13 | +// AuthCallbackParamsSchema: { |
| 14 | +// parse: vi.fn((val) => { |
| 15 | +// if (val.provider === "invalid") throw new z.ZodError([]); |
| 16 | +// return val; |
| 17 | +// }), |
| 18 | +// }, |
| 19 | +// })); |
| 20 | + |
| 21 | +// describe("AuthController", () => { |
| 22 | +// let authServiceMock: any; |
| 23 | +// let authController: AuthController; |
| 24 | +// let reqMock: Partial<Request>; |
| 25 | +// let resMock: Partial<Response>; |
| 26 | +// let sessionMock: any; |
| 27 | + |
| 28 | +// beforeEach(() => { |
| 29 | +// vi.stubEnv("SESSION_SECRET", "um-password-longo-com-mais-de-32-caracteres"); |
| 30 | +// vi.stubEnv("FRONTEND_URL", "http://localhost:5173"); |
| 31 | +// vi.clearAllMocks(); |
| 32 | + |
| 33 | +// authServiceMock = { |
| 34 | +// getAuthUrl: vi.fn().mockResolvedValue("https://provider.com/auth"), |
| 35 | +// handleCallback: vi |
| 36 | +// .fn() |
| 37 | +// .mockResolvedValue({ user: { id: "1" }, session: { userId: "1" } }), |
| 38 | +// }; |
| 39 | + |
| 40 | +// authController = new AuthController(authServiceMock); |
| 41 | + |
| 42 | +// sessionMock = { |
| 43 | +// save: vi.fn().mockResolvedValue(undefined), |
| 44 | +// oauth_state: undefined, |
| 45 | +// userId: undefined, |
| 46 | +// }; |
| 47 | + |
| 48 | +// reqMock = { |
| 49 | +// params: {}, |
| 50 | +// query: {}, |
| 51 | +// protocol: "http", |
| 52 | +// get: vi.fn().mockReturnValue("localhost:3000"), |
| 53 | +// originalUrl: "/auth/callback", |
| 54 | +// session: sessionMock, |
| 55 | +// }; |
| 56 | + |
| 57 | +// resMock = { |
| 58 | +// json: vi.fn().mockReturnThis(), |
| 59 | +// status: vi.fn().mockReturnThis(), |
| 60 | +// redirect: vi.fn().mockReturnThis(), |
| 61 | +// }; |
| 62 | +// }); |
| 63 | + |
| 64 | +// describe("getUrl", () => { |
| 65 | +// it("deve gerar a URL de autenticação com sucesso e salvar o state na sessão", async () => { |
| 66 | +// reqMock.params = { provider: "google" }; |
| 67 | + |
| 68 | +// await authController.getUrl(reqMock as Request, resMock as Response); |
| 69 | + |
| 70 | +// expect(sessionMock.oauth_state).toBeDefined(); |
| 71 | +// expect(typeof sessionMock.oauth_state).toBe("string"); |
| 72 | +// expect(sessionMock.save).toHaveBeenCalled(); |
| 73 | +// expect(authServiceMock.getAuthUrl).toHaveBeenCalledWith( |
| 74 | +// "google", |
| 75 | +// sessionMock.oauth_state, |
| 76 | +// ); |
| 77 | +// expect(resMock.json).toHaveBeenCalledWith({ |
| 78 | +// url: "https://provider.com/auth", |
| 79 | +// }); |
| 80 | +// }); |
| 81 | + |
| 82 | +// it("deve retornar 400 se o provider for inválido (ZodError)", async () => { |
| 83 | +// reqMock.params = { provider: "invalid" }; |
| 84 | + |
| 85 | +// await authController.getUrl(reqMock as Request, resMock as Response); |
| 86 | + |
| 87 | +// expect(resMock.status).toHaveBeenCalledWith(400); |
| 88 | +// expect(resMock.json).toHaveBeenCalledWith( |
| 89 | +// expect.objectContaining({ error: "Provider inválido" }), |
| 90 | +// ); |
| 91 | +// }); |
| 92 | +// }); |
| 93 | + |
| 94 | +// describe("callback", () => { |
| 95 | +// it("deve redirecionar ao frontend apos callback valido", async () => { |
| 96 | +// sessionMock.oauth_state = "state_secreto_123"; |
| 97 | +// reqMock.params = { provider: "google" }; |
| 98 | +// reqMock.query = { code: "auth_code_abc", state: "state_secreto_123" }; |
| 99 | + |
| 100 | +// await authController.callback(reqMock as Request, resMock as Response); |
| 101 | + |
| 102 | +// expect(sessionMock.oauth_state).toBeUndefined(); |
| 103 | +// expect(sessionMock.userId).toBe("1"); |
| 104 | +// expect(sessionMock.save).toHaveBeenCalled(); |
| 105 | +// expect(authServiceMock.handleCallback).toHaveBeenCalledWith( |
| 106 | +// expect.objectContaining({ |
| 107 | +// provider: "google", |
| 108 | +// code: "auth_code_abc", |
| 109 | +// state: "state_secreto_123", |
| 110 | +// callbackUrl: "http://localhost:3000/auth/callback", |
| 111 | +// }), |
| 112 | +// ); |
| 113 | +// expect(resMock.redirect).toHaveBeenCalledWith( |
| 114 | +// "http://localhost:5173/auth/callback", |
| 115 | +// ); |
| 116 | +// }); |
| 117 | + |
| 118 | +// it("deve redirecionar ao login se o oauth_state estiver ausente na sessão", async () => { |
| 119 | +// sessionMock.oauth_state = undefined; |
| 120 | +// reqMock.params = { provider: "google" }; |
| 121 | +// reqMock.query = { code: "code", state: "any_state" }; |
| 122 | + |
| 123 | +// await authController.callback(reqMock as Request, resMock as Response); |
| 124 | + |
| 125 | +// expect(resMock.redirect).toHaveBeenCalledWith( |
| 126 | +// "http://localhost:5173/login?error=oauth_state_missing", |
| 127 | +// ); |
| 128 | +// }); |
| 129 | + |
| 130 | +// it("deve redirecionar ao login se o state da query for diferente do state da sessão", async () => { |
| 131 | +// sessionMock.oauth_state = "state_original"; |
| 132 | +// reqMock.params = { provider: "google" }; |
| 133 | +// reqMock.query = { code: "code", state: "state_ataque_csrf" }; |
| 134 | + |
| 135 | +// await authController.callback(reqMock as Request, resMock as Response); |
| 136 | + |
| 137 | +// expect(resMock.redirect).toHaveBeenCalledWith( |
| 138 | +// "http://localhost:5173/login?error=oauth_state_invalid", |
| 139 | +// ); |
| 140 | +// }); |
| 141 | + |
| 142 | +// it("deve redirecionar ao login com erro se o serviço falhar", async () => { |
| 143 | +// sessionMock.oauth_state = "valid_state"; |
| 144 | +// reqMock.params = { provider: "google" }; |
| 145 | +// reqMock.query = { code: "code", state: "valid_state" }; |
| 146 | + |
| 147 | +// authServiceMock.handleCallback.mockRejectedValue( |
| 148 | +// new Error("Falha na comunicação com o provedor"), |
| 149 | +// ); |
| 150 | + |
| 151 | +// await authController.callback(reqMock as Request, resMock as Response); |
| 152 | + |
| 153 | +// expect(resMock.redirect).toHaveBeenCalledWith( |
| 154 | +// "http://localhost:5173/login?error=oauth_failed", |
| 155 | +// ); |
| 156 | +// }); |
| 157 | +// }); |
| 158 | +// }); |
| 159 | + |
1 | 160 | import { Request, Response } from "express"; |
2 | 161 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
3 | 162 | import { z } from "zod"; |
| 163 | +import "../../../../src/middleware/withSession"; |
4 | 164 | import { AuthController } from "../../../../src/modules/auth/auth.controller"; |
5 | 165 |
|
6 | 166 | vi.mock("../../../../src/types/auth.types.js", () => ({ |
@@ -28,6 +188,7 @@ describe("AuthController", () => { |
28 | 188 | beforeEach(() => { |
29 | 189 | vi.stubEnv("SESSION_SECRET", "um-password-longo-com-mais-de-32-caracteres"); |
30 | 190 | vi.stubEnv("FRONTEND_URL", "http://localhost:5173"); |
| 191 | + vi.stubEnv("APP_URL", "http://localhost:3001/api"); |
31 | 192 | vi.clearAllMocks(); |
32 | 193 |
|
33 | 194 | authServiceMock = { |
@@ -107,7 +268,8 @@ describe("AuthController", () => { |
107 | 268 | provider: "google", |
108 | 269 | code: "auth_code_abc", |
109 | 270 | state: "state_secreto_123", |
110 | | - callbackUrl: "http://localhost:3000/auth/callback", |
| 271 | + callbackUrl: |
| 272 | + "http://localhost:3001/api/auth/google/callback?code=auth_code_abc&state=state_secreto_123", |
111 | 273 | }), |
112 | 274 | ); |
113 | 275 | expect(resMock.redirect).toHaveBeenCalledWith( |
|
0 commit comments