|
| 1 | +import * as emailModule from "@/modules/shared/email"; |
| 2 | +import { prismaClient } from "@/prisma/client"; |
| 3 | +import { authorizationHeaderForUser } from "@/test/helpers/authorization-helper"; |
| 4 | +import { getGraphQLClient } from "@/test/test-graphql-client"; |
| 5 | + |
| 6 | +import { graphql } from "../graphql/generated"; |
| 7 | +import * as getKeycloakAdminModule from "../shared/auth/getKeycloakAdmin"; |
| 8 | + |
| 9 | +const createCommanditaireVaeCollective = async ({ |
| 10 | + raisonSociale, |
| 11 | + gestionnaireFirstname, |
| 12 | + gestionnaireLastname, |
| 13 | + gestionnaireEmail, |
| 14 | +}: { |
| 15 | + raisonSociale: string; |
| 16 | + gestionnaireFirstname: string; |
| 17 | + gestionnaireLastname: string; |
| 18 | + gestionnaireEmail: string; |
| 19 | +}) => { |
| 20 | + const graphqlClient = getGraphQLClient({ |
| 21 | + headers: { |
| 22 | + authorization: authorizationHeaderForUser({ |
| 23 | + role: "admin", |
| 24 | + keycloakId: "1b0e7046-ca61-4259-b716-785f36ab79b2", |
| 25 | + }), |
| 26 | + }, |
| 27 | + }); |
| 28 | + |
| 29 | + const createCommanditaireVaeCollectiveMutation = graphql(` |
| 30 | + mutation createCommanditaireVaeCollective( |
| 31 | + $raisonSociale: String! |
| 32 | + $gestionnaireFirstname: String! |
| 33 | + $gestionnaireLastname: String! |
| 34 | + $gestionnaireEmail: String! |
| 35 | + ) { |
| 36 | + vaeCollective_createCommanditaireVaeCollective( |
| 37 | + raisonSociale: $raisonSociale |
| 38 | + gestionnaireFirstname: $gestionnaireFirstname |
| 39 | + gestionnaireLastname: $gestionnaireLastname |
| 40 | + gestionnaireEmail: $gestionnaireEmail |
| 41 | + ) { |
| 42 | + id |
| 43 | + raisonSociale |
| 44 | + } |
| 45 | + } |
| 46 | + `); |
| 47 | + |
| 48 | + return graphqlClient.request(createCommanditaireVaeCollectiveMutation, { |
| 49 | + raisonSociale, |
| 50 | + gestionnaireFirstname, |
| 51 | + gestionnaireLastname, |
| 52 | + gestionnaireEmail, |
| 53 | + }); |
| 54 | +}; |
| 55 | + |
| 56 | +describe("create commanditaire vae collective", () => { |
| 57 | + test("should create a commanditaire, an account and send an email to the account owner", async () => { |
| 58 | + vi.spyOn(getKeycloakAdminModule, "getKeycloakAdmin").mockImplementation( |
| 59 | + () => |
| 60 | + Promise.resolve({ |
| 61 | + users: { |
| 62 | + find: vi.fn().mockResolvedValue([]), |
| 63 | + create: vi.fn().mockResolvedValue({ |
| 64 | + id: "02c1b842-c889-4db7-a4a6-2fad38e3d1fe", |
| 65 | + }), |
| 66 | + }, |
| 67 | + }), |
| 68 | + ); |
| 69 | + |
| 70 | + const emailSpy = vi.spyOn(emailModule, "sendEmailUsingTemplate"); |
| 71 | + |
| 72 | + await createCommanditaireVaeCollective({ |
| 73 | + raisonSociale: "Test Commanditaire", |
| 74 | + gestionnaireFirstname: "John", |
| 75 | + gestionnaireLastname: "Doe", |
| 76 | + gestionnaireEmail: "john.doe@example.com", |
| 77 | + }); |
| 78 | + |
| 79 | + const commanditaire = |
| 80 | + await prismaClient.commanditaireVaeCollective.findFirst({ |
| 81 | + where: { |
| 82 | + raisonSociale: "Test Commanditaire", |
| 83 | + }, |
| 84 | + }); |
| 85 | + |
| 86 | + expect(commanditaire).toBeDefined(); |
| 87 | + |
| 88 | + const account = await prismaClient.account.findUnique({ |
| 89 | + where: { |
| 90 | + id: commanditaire?.gestionnaireAccountId || "", |
| 91 | + }, |
| 92 | + }); |
| 93 | + |
| 94 | + expect(account).toBeDefined(); |
| 95 | + expect(account?.email).toBe("john.doe@example.com"); |
| 96 | + expect(account?.firstname).toBe("John"); |
| 97 | + expect(account?.lastname).toBe("Doe"); |
| 98 | + |
| 99 | + expect(emailSpy).toHaveBeenCalledWith( |
| 100 | + expect.objectContaining({ |
| 101 | + to: { email: "john.doe@example.com" }, |
| 102 | + }), |
| 103 | + ); |
| 104 | + }); |
| 105 | +}); |
0 commit comments