Skip to content

Commit 8150c45

Browse files
committed
fix(api): refactor candidacy tests to use input and context objects
1 parent d2ae261 commit 8150c45

File tree

2 files changed

+94
-27
lines changed

2 files changed

+94
-27
lines changed

packages/reva-api/modules/candidacy/candidacy-contestation-caducite/features/createCandidacyContestationCaducite.test.ts

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,28 @@ import { createCandidacyHelper } from "../../../../test/helpers/entities/create-
1111
import { injectGraphql } from "../../../../test/helpers/graphql-helper";
1212
import { clearDatabase } from "../../../../test/jestClearDatabaseBeforeEachTestFile";
1313
import { createCandidacyContestationCaducite } from "./createCandidacyContestationCaducite";
14+
import { randomUUID } from "crypto";
1415

1516
const VALID_CONTESTATION_REASON = "Valid contestation reason";
1617
const FUTURE_DATE = addDays(new Date(), 30);
1718
const PAST_DATE = subDays(new Date(), 1);
19+
const CONTEXT = {
20+
auth: {
21+
userInfo: {
22+
sub: randomUUID(),
23+
email: "test@test.com",
24+
email_verified: true,
25+
preferred_username: "test",
26+
realm_access: { roles: ["candidate" as KeyCloakUserRole] },
27+
},
28+
hasRole: (_role: string) => true,
29+
},
30+
app: {
31+
keycloak: {
32+
hasRole: (_role: string) => true,
33+
},
34+
},
35+
};
1836

1937
const createContestationMutation = async ({
2038
keycloakId,
@@ -55,9 +73,12 @@ describe("createCandidacyContestationCaducite", () => {
5573
const candidacy = await createCandidacyHelper();
5674

5775
const createContestationPromise = createCandidacyContestationCaducite({
58-
candidacyId: candidacy.id,
59-
contestationReason: "",
60-
readyForJuryEstimatedAt: FUTURE_DATE,
76+
input: {
77+
candidacyId: candidacy.id,
78+
contestationReason: "",
79+
readyForJuryEstimatedAt: FUTURE_DATE,
80+
},
81+
context: CONTEXT,
6182
});
6283

6384
await expect(createContestationPromise).rejects.toThrow(
@@ -69,9 +90,12 @@ describe("createCandidacyContestationCaducite", () => {
6990
const candidacy = await createCandidacyHelper();
7091

7192
const createContestationPromise = createCandidacyContestationCaducite({
72-
candidacyId: candidacy.id,
73-
contestationReason: VALID_CONTESTATION_REASON,
74-
readyForJuryEstimatedAt: PAST_DATE,
93+
input: {
94+
candidacyId: candidacy.id,
95+
contestationReason: VALID_CONTESTATION_REASON,
96+
readyForJuryEstimatedAt: PAST_DATE,
97+
},
98+
context: CONTEXT,
7599
});
76100

77101
await expect(createContestationPromise).rejects.toThrow(
@@ -83,9 +107,12 @@ describe("createCandidacyContestationCaducite", () => {
83107
describe("Candidacy validation", () => {
84108
test("should fail when candidacy does not exist", async () => {
85109
const createContestationPromise = createCandidacyContestationCaducite({
86-
candidacyId: faker.string.uuid(),
87-
contestationReason: VALID_CONTESTATION_REASON,
88-
readyForJuryEstimatedAt: FUTURE_DATE,
110+
input: {
111+
candidacyId: faker.string.uuid(),
112+
contestationReason: VALID_CONTESTATION_REASON,
113+
readyForJuryEstimatedAt: FUTURE_DATE,
114+
},
115+
context: CONTEXT,
89116
});
90117

91118
await expect(createContestationPromise).rejects.toThrow(
@@ -106,9 +133,12 @@ describe("createCandidacyContestationCaducite", () => {
106133
});
107134

108135
const createContestationPromise = createCandidacyContestationCaducite({
109-
candidacyId: candidacy.id,
110-
contestationReason: VALID_CONTESTATION_REASON,
111-
readyForJuryEstimatedAt: FUTURE_DATE,
136+
input: {
137+
candidacyId: candidacy.id,
138+
contestationReason: VALID_CONTESTATION_REASON,
139+
readyForJuryEstimatedAt: FUTURE_DATE,
140+
},
141+
context: CONTEXT,
112142
});
113143

114144
await expect(createContestationPromise).rejects.toThrow(
@@ -129,9 +159,12 @@ describe("createCandidacyContestationCaducite", () => {
129159
});
130160

131161
const createContestationPromise = createCandidacyContestationCaducite({
132-
candidacyId: candidacy.id,
133-
contestationReason: VALID_CONTESTATION_REASON,
134-
readyForJuryEstimatedAt: FUTURE_DATE,
162+
input: {
163+
candidacyId: candidacy.id,
164+
contestationReason: VALID_CONTESTATION_REASON,
165+
readyForJuryEstimatedAt: FUTURE_DATE,
166+
},
167+
context: CONTEXT,
135168
});
136169

137170
await expect(createContestationPromise).rejects.toThrow(
@@ -145,9 +178,12 @@ describe("createCandidacyContestationCaducite", () => {
145178
const candidacy = await createCandidacyHelper();
146179

147180
const result = await createCandidacyContestationCaducite({
148-
candidacyId: candidacy.id,
149-
contestationReason: VALID_CONTESTATION_REASON,
150-
readyForJuryEstimatedAt: FUTURE_DATE,
181+
input: {
182+
candidacyId: candidacy.id,
183+
contestationReason: VALID_CONTESTATION_REASON,
184+
readyForJuryEstimatedAt: FUTURE_DATE,
185+
},
186+
context: CONTEXT,
151187
});
152188

153189
expect(result).toMatchObject({
@@ -176,9 +212,12 @@ describe("createCandidacyContestationCaducite", () => {
176212
});
177213

178214
const result = await createCandidacyContestationCaducite({
179-
candidacyId: candidacy.id,
180-
contestationReason: VALID_CONTESTATION_REASON,
181-
readyForJuryEstimatedAt: FUTURE_DATE,
215+
input: {
216+
candidacyId: candidacy.id,
217+
contestationReason: VALID_CONTESTATION_REASON,
218+
readyForJuryEstimatedAt: FUTURE_DATE,
219+
},
220+
context: CONTEXT,
182221
});
183222

184223
expect(result).toMatchObject({

packages/reva-api/modules/candidacy/features/updateLastActivityDate.test.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
1+
import { randomUUID } from "crypto";
12
import { createCandidacyHelper } from "../../../test/helpers/entities/create-candidacy-helper";
23
import { updateLastActivityDate } from "./updateLastActivityDate";
34

5+
const CONTEXT = {
6+
auth: {
7+
userInfo: {
8+
sub: randomUUID(),
9+
email: "test@test.com",
10+
email_verified: true,
11+
preferred_username: "test",
12+
realm_access: { roles: ["candidate" as KeyCloakUserRole] },
13+
},
14+
hasRole: (_role: string) => true,
15+
},
16+
app: {
17+
keycloak: {
18+
hasRole: (_role: string) => true,
19+
},
20+
},
21+
};
22+
423
describe("updateLastActivityDate", () => {
524
test("should fail when readyForJuryEstimatedAt is in the past", async () => {
625
const candidacy = await createCandidacyHelper();
@@ -9,8 +28,11 @@ describe("updateLastActivityDate", () => {
928

1029
await expect(async () => {
1130
await updateLastActivityDate({
12-
candidacyId: candidacy.id,
13-
readyForJuryEstimatedAt: pastDate,
31+
input: {
32+
candidacyId: candidacy.id,
33+
readyForJuryEstimatedAt: pastDate,
34+
},
35+
context: CONTEXT,
1436
});
1537
}).rejects.toThrow(
1638
"La date de préparation pour le jury ne peut être dans le passé",
@@ -23,8 +45,11 @@ describe("updateLastActivityDate", () => {
2345

2446
await expect(async () => {
2547
await updateLastActivityDate({
26-
candidacyId: "non-existent-id",
27-
readyForJuryEstimatedAt: futureDate,
48+
input: {
49+
candidacyId: "non-existent-id",
50+
readyForJuryEstimatedAt: futureDate,
51+
},
52+
context: CONTEXT,
2853
});
2954
}).rejects.toThrow();
3055
});
@@ -35,8 +60,11 @@ describe("updateLastActivityDate", () => {
3560
futureDate.setDate(futureDate.getDate() + 30);
3661

3762
const updatedCandidacy = await updateLastActivityDate({
38-
candidacyId: candidacy.id,
39-
readyForJuryEstimatedAt: futureDate,
63+
input: {
64+
candidacyId: candidacy.id,
65+
readyForJuryEstimatedAt: futureDate,
66+
},
67+
context: CONTEXT,
4068
});
4169

4270
expect(updatedCandidacy.readyForJuryEstimatedAt?.getTime()).toBe(

0 commit comments

Comments
 (0)