|
| 1 | +import { ERRORS_LOGIN } from "@vao/shared-bridge"; |
| 2 | +import crypto from "crypto"; |
| 3 | +import request from "supertest"; |
| 4 | + |
| 5 | +import app from "../../app"; |
| 6 | +import config from "../../config"; |
| 7 | +import { UsersRepository as AdminUsersRepository } from "../../repositories/admin/Users"; |
| 8 | +import { |
| 9 | + createTestContainer, |
| 10 | + removeTestContainer, |
| 11 | +} from "../helper/testContainer"; |
| 12 | + |
| 13 | +beforeAll(async () => { |
| 14 | + await createTestContainer(); |
| 15 | + |
| 16 | + config.tokenSecret_BO = crypto.randomBytes(32).toString("hex"); |
| 17 | + config.algorithm = "HS256"; |
| 18 | + config.accessToken.expiresIn = 60000; |
| 19 | + config.refreshToken.expiresIn = 120000; |
| 20 | +}); |
| 21 | + |
| 22 | +afterAll(async () => { |
| 23 | + await removeTestContainer(); |
| 24 | +}); |
| 25 | + |
| 26 | +describe("POST /bo-authentication/email/login", () => { |
| 27 | + it("should login a validated admin user and set BO cookies", async () => { |
| 28 | + const password = "HelloHello1!!"; |
| 29 | + const timestamp = Date.now(); |
| 30 | + const email = `bologin${timestamp}@example.com`; |
| 31 | + |
| 32 | + const { user: createdUsers } = await AdminUsersRepository.create({ |
| 33 | + user: { |
| 34 | + cgu_accepted: true, |
| 35 | + cgu_accepted_at: new Date(), |
| 36 | + deleted: false, |
| 37 | + email, |
| 38 | + nom: "BoNom", |
| 39 | + password, |
| 40 | + prenom: "BoPrenom", |
| 41 | + ter_code: "FRA", |
| 42 | + validated: true, |
| 43 | + }, |
| 44 | + }); |
| 45 | + |
| 46 | + expect(createdUsers[0]).toBeDefined(); |
| 47 | + |
| 48 | + const response = await request(app) |
| 49 | + .post("/bo-authentication/email/login") |
| 50 | + .send({ email, password }); |
| 51 | + |
| 52 | + expect(response.status).toBe(200); |
| 53 | + expect(response.body.user).toBeDefined(); |
| 54 | + expect(response.body.user.email).toBe(email); |
| 55 | + expect(response.body.user.featureFlags).toBeDefined(); |
| 56 | + |
| 57 | + const setCookieHeader = response.headers["set-cookie"] || []; |
| 58 | + expect(setCookieHeader).toEqual( |
| 59 | + expect.arrayContaining([ |
| 60 | + expect.stringContaining("VAO_BO_access_token="), |
| 61 | + expect.stringContaining("VAO_BO_refresh_token="), |
| 62 | + ]), |
| 63 | + ); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should return 404 when admin user does not exist", async () => { |
| 67 | + const password = "SomePassword1!!"; |
| 68 | + const timestamp = Date.now(); |
| 69 | + const email = `bo-login-not-found-${timestamp}@example.com`; |
| 70 | + |
| 71 | + const response = await request(app) |
| 72 | + .post("/bo-authentication/email/login") |
| 73 | + .send({ email, password }); |
| 74 | + |
| 75 | + expect(response.status).toBe(404); |
| 76 | + expect(response.body.name).toBe(ERRORS_LOGIN.WrongCredentials); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should return 400 when payload is invalid", async () => { |
| 80 | + const timestamp = Date.now(); |
| 81 | + const email = `bo-login-bad-payload-${timestamp}@example.com`; |
| 82 | + |
| 83 | + const response = await request(app) |
| 84 | + .post("/bo-authentication/email/login") |
| 85 | + // missing password |
| 86 | + .send({ email }); |
| 87 | + |
| 88 | + expect(response.status).toBe(400); |
| 89 | + expect(response.body.name).toBe("AppError"); |
| 90 | + }); |
| 91 | +}); |
0 commit comments