65 lines
2.4 KiB
JavaScript
65 lines
2.4 KiB
JavaScript
// Ce fichier ne charge pas app.js, donc pas le .env : le JWT a besoin d'un secret.
|
|
process.env.JWT_SECRET = process.env.JWT_SECRET || "test-secret";
|
|
|
|
const getFrontendUrl = require("../src/utils/frontendUrl");
|
|
const { oauthCallback } = require("../src/controllers/auth.controller");
|
|
|
|
describe("getFrontendUrl", () => {
|
|
const original = process.env.FRONTEND_URL;
|
|
afterEach(() => {
|
|
process.env.FRONTEND_URL = original;
|
|
});
|
|
|
|
test("ne garde que la premiere origine d une liste (CORS prod)", () => {
|
|
process.env.FRONTEND_URL = "https://site.fr, https://www.site.fr";
|
|
expect(getFrontendUrl()).toBe("https://site.fr");
|
|
});
|
|
|
|
test("retire le slash final", () => {
|
|
process.env.FRONTEND_URL = "https://site.fr/";
|
|
expect(getFrontendUrl()).toBe("https://site.fr");
|
|
});
|
|
|
|
test("retombe sur localhost sans configuration", () => {
|
|
delete process.env.FRONTEND_URL;
|
|
expect(getFrontendUrl()).toBe("http://localhost:5173");
|
|
});
|
|
});
|
|
|
|
describe("oauthCallback", () => {
|
|
const original = process.env.FRONTEND_URL;
|
|
afterEach(() => {
|
|
process.env.FRONTEND_URL = original;
|
|
});
|
|
|
|
test("redirige vers une URL valide avec un profil lisible par le front", () => {
|
|
process.env.FRONTEND_URL = "https://site.fr,https://www.site.fr";
|
|
const res = { redirect: jest.fn() };
|
|
// Accents (UTF-8) et « ~ » : ce profil produit un base64 contenant « + »,
|
|
// qui deviendrait un espace sans encodage.
|
|
const user = { id: 7, firstname: "Chloé", lastname: "Lefèvre", email: "ab~test@test.local", role: "CLIENT" };
|
|
expect(Buffer.from(JSON.stringify(user)).toString("base64")).toContain("+");
|
|
|
|
oauthCallback({ user }, res);
|
|
|
|
const url = new URL(res.redirect.mock.calls[0][0]);
|
|
expect(`${url.origin}${url.pathname}`).toBe("https://site.fr/auth/callback");
|
|
expect(url.searchParams.get("token")).toBeTruthy();
|
|
|
|
// Même décodage que frontend/src/pages/AuthCallback.jsx.
|
|
const bytes = Uint8Array.from(atob(url.searchParams.get("user")), (c) => c.charCodeAt(0));
|
|
expect(JSON.parse(new TextDecoder().decode(bytes))).toEqual(user);
|
|
});
|
|
|
|
test("renvoie une erreur au front quand passport n a pas d utilisateur", () => {
|
|
process.env.FRONTEND_URL = "https://site.fr,https://www.site.fr";
|
|
const res = { redirect: jest.fn() };
|
|
|
|
oauthCallback({}, res);
|
|
|
|
const url = new URL(res.redirect.mock.calls[0][0]);
|
|
expect(url.origin).toBe("https://site.fr");
|
|
expect(url.searchParams.get("error")).toMatch(/refusée/);
|
|
});
|
|
});
|