|
| 1 | +/** |
| 2 | + * @jest-environment ./test/fastify-test-env.ts |
| 3 | + */ |
| 4 | +import { faker } from "@faker-js/faker/."; |
| 5 | +import { prismaClient } from "../../../../prisma/client"; |
| 6 | +import { authorizationHeaderForUser } from "../../../../test/helpers/authorization-helper"; |
| 7 | +import { createCandidacyHelper } from "../../../../test/helpers/entities/create-candidacy-helper"; |
| 8 | +import { createCertificationHelper } from "../../../../test/helpers/entities/create-certification-helper"; |
| 9 | +import { createFeasibilityUploadedPdfHelper } from "../../../../test/helpers/entities/create-feasibility-uploaded-pdf-helper"; |
| 10 | +import { injectGraphql } from "../../../../test/helpers/graphql-helper"; |
| 11 | + |
| 12 | +const getActiveFeasibilityByCandidacyId = async ({ |
| 13 | + candidacyId, |
| 14 | + keycloakId, |
| 15 | +}: { |
| 16 | + candidacyId: string; |
| 17 | + keycloakId: string; |
| 18 | +}) => |
| 19 | + injectGraphql({ |
| 20 | + fastify: (global as any).fastify, |
| 21 | + authorization: authorizationHeaderForUser({ |
| 22 | + role: "manage_feasibility", |
| 23 | + keycloakId, |
| 24 | + }), |
| 25 | + payload: { |
| 26 | + requestType: "query", |
| 27 | + endpoint: "feasibility_getActiveFeasibilityByCandidacyId", |
| 28 | + arguments: { candidacyId }, |
| 29 | + returnFields: "{id}", |
| 30 | + }, |
| 31 | + }); |
| 32 | + |
| 33 | +const ERROR_MESSAGES = { |
| 34 | + UNAUTHORIZED: "Vous n'êtes pas autorisé à gérer cette candidature.", |
| 35 | + CANDIDACY_NOT_FOUND: "Candidature inexistante.", |
| 36 | +} as const; |
| 37 | + |
| 38 | +describe("isFeasibilityManager", () => { |
| 39 | + describe("manage_feasibility role", () => { |
| 40 | + test("should allow access when user manages the certification authority of the feasibility", async () => { |
| 41 | + const certification = await createCertificationHelper(); |
| 42 | + const certificationAuthority = |
| 43 | + certification.certificationAuthorityStructure |
| 44 | + ?.oldCertificationAuthorities[0]; |
| 45 | + const candidacy = await createCandidacyHelper({ |
| 46 | + candidacyArgs: { |
| 47 | + certificationId: certification.id, |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + if ( |
| 52 | + !certificationAuthority?.id || |
| 53 | + !certificationAuthority.Account[0].id || |
| 54 | + !candidacy.departmentId |
| 55 | + ) { |
| 56 | + throw new Error("Required IDs are undefined"); |
| 57 | + } |
| 58 | + |
| 59 | + await prismaClient.certificationAuthorityLocalAccount.create({ |
| 60 | + data: { |
| 61 | + certificationAuthorityId: certificationAuthority.id, |
| 62 | + accountId: certificationAuthority.Account[0].id, |
| 63 | + certificationAuthorityLocalAccountOnCertification: { |
| 64 | + create: { |
| 65 | + certificationId: certification.id, |
| 66 | + }, |
| 67 | + }, |
| 68 | + certificationAuthorityLocalAccountOnDepartment: { |
| 69 | + create: { |
| 70 | + departmentId: candidacy.departmentId, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }); |
| 75 | + |
| 76 | + await createFeasibilityUploadedPdfHelper({ |
| 77 | + certificationAuthorityId: certificationAuthority?.id, |
| 78 | + candidacyId: candidacy.id, |
| 79 | + }); |
| 80 | + |
| 81 | + const resp = await getActiveFeasibilityByCandidacyId({ |
| 82 | + candidacyId: candidacy.id, |
| 83 | + keycloakId: certificationAuthority?.Account[0].keycloakId, |
| 84 | + }); |
| 85 | + |
| 86 | + expect(resp.statusCode).toEqual(200); |
| 87 | + expect(resp.json()).not.toHaveProperty("errors"); |
| 88 | + }); |
| 89 | + |
| 90 | + test("should deny access when user does not manage the certification authority", async () => { |
| 91 | + const feasibility = await createFeasibilityUploadedPdfHelper(); |
| 92 | + |
| 93 | + const resp = await getActiveFeasibilityByCandidacyId({ |
| 94 | + candidacyId: feasibility.candidacyId, |
| 95 | + keycloakId: faker.string.uuid(), |
| 96 | + }); |
| 97 | + |
| 98 | + expect(resp.json()).toHaveProperty("errors"); |
| 99 | + expect(resp.json().errors[0].message).toBe(ERROR_MESSAGES.UNAUTHORIZED); |
| 100 | + }); |
| 101 | + |
| 102 | + test("should deny access when feasibility does not exist", async () => { |
| 103 | + const resp = await getActiveFeasibilityByCandidacyId({ |
| 104 | + candidacyId: faker.string.uuid(), |
| 105 | + keycloakId: faker.string.uuid(), |
| 106 | + }); |
| 107 | + |
| 108 | + expect(resp.json()).toHaveProperty("errors"); |
| 109 | + expect(resp.json().errors[0].message).toBe( |
| 110 | + ERROR_MESSAGES.CANDIDACY_NOT_FOUND, |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + test("should deny access when account is not found", async () => { |
| 115 | + const feasibility = await createFeasibilityUploadedPdfHelper(); |
| 116 | + |
| 117 | + const resp = await getActiveFeasibilityByCandidacyId({ |
| 118 | + candidacyId: feasibility.candidacyId, |
| 119 | + keycloakId: faker.string.uuid(), |
| 120 | + }); |
| 121 | + |
| 122 | + expect(resp.json()).toHaveProperty("errors"); |
| 123 | + expect(resp.json().errors[0].message).toBe(ERROR_MESSAGES.UNAUTHORIZED); |
| 124 | + }); |
| 125 | + |
| 126 | + test("should deny access when feasibility is not active", async () => { |
| 127 | + const certification = await createCertificationHelper(); |
| 128 | + const certificationAuthority = |
| 129 | + certification.certificationAuthorityStructure |
| 130 | + ?.oldCertificationAuthorities[0]; |
| 131 | + const candidacy = await createCandidacyHelper({ |
| 132 | + candidacyArgs: { |
| 133 | + certificationId: certification.id, |
| 134 | + }, |
| 135 | + }); |
| 136 | + |
| 137 | + await createFeasibilityUploadedPdfHelper({ |
| 138 | + certificationAuthorityId: certificationAuthority?.id, |
| 139 | + candidacyId: candidacy.id, |
| 140 | + isActive: false, |
| 141 | + }); |
| 142 | + |
| 143 | + const resp = await getActiveFeasibilityByCandidacyId({ |
| 144 | + candidacyId: candidacy.id, |
| 145 | + keycloakId: |
| 146 | + certificationAuthority?.Account[0].keycloakId ?? faker.string.uuid(), |
| 147 | + }); |
| 148 | + |
| 149 | + expect(resp.json()).toHaveProperty("errors"); |
| 150 | + expect(resp.json().errors[0].message).toBe( |
| 151 | + ERROR_MESSAGES.CANDIDACY_NOT_FOUND, |
| 152 | + ); |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + describe("manage_feasibility role with certification and department checks", () => { |
| 157 | + test("should allow access when user matches certification, authority and department", async () => { |
| 158 | + const certification = await createCertificationHelper(); |
| 159 | + const certificationAuthority = |
| 160 | + certification.certificationAuthorityStructure |
| 161 | + ?.oldCertificationAuthorities[0]; |
| 162 | + const candidacy = await createCandidacyHelper({ |
| 163 | + candidacyArgs: { |
| 164 | + certificationId: certification.id, |
| 165 | + }, |
| 166 | + }); |
| 167 | + |
| 168 | + if ( |
| 169 | + !certificationAuthority?.id || |
| 170 | + !certificationAuthority.Account[0].id || |
| 171 | + !candidacy.departmentId |
| 172 | + ) { |
| 173 | + throw new Error("Required IDs are undefined"); |
| 174 | + } |
| 175 | + |
| 176 | + await prismaClient.certificationAuthorityLocalAccount.create({ |
| 177 | + data: { |
| 178 | + certificationAuthorityId: certificationAuthority.id, |
| 179 | + accountId: certificationAuthority.Account[0].id, |
| 180 | + certificationAuthorityLocalAccountOnCertification: { |
| 181 | + create: { |
| 182 | + certificationId: certification.id, |
| 183 | + }, |
| 184 | + }, |
| 185 | + certificationAuthorityLocalAccountOnDepartment: { |
| 186 | + create: { |
| 187 | + departmentId: candidacy.departmentId, |
| 188 | + }, |
| 189 | + }, |
| 190 | + }, |
| 191 | + }); |
| 192 | + |
| 193 | + await createFeasibilityUploadedPdfHelper({ |
| 194 | + certificationAuthorityId: certificationAuthority?.id, |
| 195 | + candidacyId: candidacy.id, |
| 196 | + }); |
| 197 | + |
| 198 | + const resp = await getActiveFeasibilityByCandidacyId({ |
| 199 | + candidacyId: candidacy.id, |
| 200 | + keycloakId: certificationAuthority?.Account[0].keycloakId, |
| 201 | + }); |
| 202 | + |
| 203 | + expect(resp.statusCode).toEqual(200); |
| 204 | + expect(resp.json()).not.toHaveProperty("errors"); |
| 205 | + }); |
| 206 | + |
| 207 | + test("should deny access when certification does not match", async () => { |
| 208 | + const feasibility = await createFeasibilityUploadedPdfHelper(); |
| 209 | + const differentCertification = await createCertificationHelper(); |
| 210 | + |
| 211 | + const resp = await getActiveFeasibilityByCandidacyId({ |
| 212 | + candidacyId: feasibility.candidacyId, |
| 213 | + keycloakId: |
| 214 | + differentCertification.certificationAuthorityStructure |
| 215 | + ?.oldCertificationAuthorities[0].Account[0].keycloakId ?? |
| 216 | + faker.string.uuid(), |
| 217 | + }); |
| 218 | + |
| 219 | + expect(resp.json()).toHaveProperty("errors"); |
| 220 | + expect(resp.json().errors[0].message).toBe(ERROR_MESSAGES.UNAUTHORIZED); |
| 221 | + }); |
| 222 | + |
| 223 | + test("should deny access when department does not match", async () => { |
| 224 | + const department = await prismaClient.department.create({ |
| 225 | + data: { |
| 226 | + code: faker.string.numeric(3), |
| 227 | + label: faker.lorem.sentence(), |
| 228 | + region: { |
| 229 | + create: { |
| 230 | + code: faker.string.numeric(3), |
| 231 | + label: faker.lorem.sentence(), |
| 232 | + }, |
| 233 | + }, |
| 234 | + }, |
| 235 | + }); |
| 236 | + const differentDepartmentCandidacy = await createCandidacyHelper({ |
| 237 | + candidacyArgs: { |
| 238 | + departmentId: department.id, |
| 239 | + }, |
| 240 | + }); |
| 241 | + await createFeasibilityUploadedPdfHelper({ |
| 242 | + candidacyId: differentDepartmentCandidacy.id, |
| 243 | + }); |
| 244 | + |
| 245 | + const certificationAuthority = await createCertificationHelper(); |
| 246 | + |
| 247 | + const resp = await getActiveFeasibilityByCandidacyId({ |
| 248 | + candidacyId: differentDepartmentCandidacy.id, |
| 249 | + keycloakId: |
| 250 | + certificationAuthority.certificationAuthorityStructure |
| 251 | + ?.oldCertificationAuthorities[0].Account[0].keycloakId ?? |
| 252 | + faker.string.uuid(), |
| 253 | + }); |
| 254 | + |
| 255 | + expect(resp.json()).toHaveProperty("errors"); |
| 256 | + expect(resp.json().errors[0].message).toBe(ERROR_MESSAGES.UNAUTHORIZED); |
| 257 | + }); |
| 258 | + |
| 259 | + test("should deny access when certification authority does not match", async () => { |
| 260 | + const feasibility = await createFeasibilityUploadedPdfHelper(); |
| 261 | + const differentAuthority = await createCertificationHelper(); |
| 262 | + |
| 263 | + const resp = await getActiveFeasibilityByCandidacyId({ |
| 264 | + candidacyId: feasibility.candidacyId, |
| 265 | + keycloakId: |
| 266 | + differentAuthority.certificationAuthorityStructure |
| 267 | + ?.oldCertificationAuthorities[0].Account[0].keycloakId ?? |
| 268 | + faker.string.uuid(), |
| 269 | + }); |
| 270 | + |
| 271 | + expect(resp.json()).toHaveProperty("errors"); |
| 272 | + expect(resp.json().errors[0].message).toBe(ERROR_MESSAGES.UNAUTHORIZED); |
| 273 | + }); |
| 274 | + |
| 275 | + test("should deny access when feasibility is not active", async () => { |
| 276 | + const certification = await createCertificationHelper(); |
| 277 | + const certificationAuthority = |
| 278 | + certification.certificationAuthorityStructure |
| 279 | + ?.oldCertificationAuthorities[0]; |
| 280 | + const candidacy = await createCandidacyHelper({ |
| 281 | + candidacyArgs: { |
| 282 | + certificationId: certification.id, |
| 283 | + }, |
| 284 | + }); |
| 285 | + |
| 286 | + await createFeasibilityUploadedPdfHelper({ |
| 287 | + certificationAuthorityId: certificationAuthority?.id, |
| 288 | + candidacyId: candidacy.id, |
| 289 | + isActive: false, |
| 290 | + }); |
| 291 | + |
| 292 | + const resp = await getActiveFeasibilityByCandidacyId({ |
| 293 | + candidacyId: candidacy.id, |
| 294 | + keycloakId: |
| 295 | + certificationAuthority?.Account[0].keycloakId ?? faker.string.uuid(), |
| 296 | + }); |
| 297 | + |
| 298 | + expect(resp.json()).toHaveProperty("errors"); |
| 299 | + expect(resp.json().errors[0].message).toBe( |
| 300 | + ERROR_MESSAGES.CANDIDACY_NOT_FOUND, |
| 301 | + ); |
| 302 | + }); |
| 303 | + |
| 304 | + test("should deny access when only certification authority matches", async () => { |
| 305 | + const certification = await createCertificationHelper(); |
| 306 | + const differentCertification = await createCertificationHelper(); |
| 307 | + const differentDepartment = await prismaClient.department.create({ |
| 308 | + data: { |
| 309 | + code: faker.string.numeric(3), |
| 310 | + label: faker.lorem.sentence(), |
| 311 | + region: { |
| 312 | + create: { |
| 313 | + code: faker.string.numeric(3), |
| 314 | + label: faker.lorem.sentence(), |
| 315 | + }, |
| 316 | + }, |
| 317 | + }, |
| 318 | + }); |
| 319 | + |
| 320 | + const candidacy = await createCandidacyHelper({ |
| 321 | + candidacyArgs: { |
| 322 | + certificationId: differentCertification.id, |
| 323 | + departmentId: differentDepartment.id, |
| 324 | + }, |
| 325 | + }); |
| 326 | + |
| 327 | + await createFeasibilityUploadedPdfHelper({ |
| 328 | + certificationAuthorityId: |
| 329 | + certification.certificationAuthorityStructure |
| 330 | + ?.oldCertificationAuthorities[0].id, |
| 331 | + candidacyId: candidacy.id, |
| 332 | + }); |
| 333 | + |
| 334 | + const resp = await getActiveFeasibilityByCandidacyId({ |
| 335 | + candidacyId: candidacy.id, |
| 336 | + keycloakId: |
| 337 | + certification.certificationAuthorityStructure |
| 338 | + ?.oldCertificationAuthorities[0].Account[0].keycloakId ?? |
| 339 | + faker.string.uuid(), |
| 340 | + }); |
| 341 | + |
| 342 | + expect(resp.json()).toHaveProperty("errors"); |
| 343 | + expect(resp.json().errors[0].message).toBe(ERROR_MESSAGES.UNAUTHORIZED); |
| 344 | + }); |
| 345 | + }); |
| 346 | + |
| 347 | + describe("other roles", () => { |
| 348 | + test("should deny access for unauthorized roles", async () => { |
| 349 | + const feasibility = await createFeasibilityUploadedPdfHelper(); |
| 350 | + |
| 351 | + const resp = await getActiveFeasibilityByCandidacyId({ |
| 352 | + candidacyId: feasibility.candidacyId, |
| 353 | + keycloakId: faker.string.uuid(), |
| 354 | + }); |
| 355 | + |
| 356 | + expect(resp.json()).toHaveProperty("errors"); |
| 357 | + expect(resp.json().errors[0].message).toBe(ERROR_MESSAGES.UNAUTHORIZED); |
| 358 | + }); |
| 359 | + }); |
| 360 | +}); |
0 commit comments