|
| 1 | +/* eslint-disable @typescript-eslint/explicit-function-return-type */ |
| 2 | +import { describe, it, expect, vi } from "vitest"; |
| 3 | +import { Attribute, generateId } from "pagopa-interop-models"; |
| 4 | +import { generateToken, getMockAttribute } from "pagopa-interop-commons-test"; |
| 5 | +import { AuthRole, authRole } from "pagopa-interop-commons"; |
| 6 | +import request from "supertest"; |
| 7 | +import { attributeRegistryApi } from "pagopa-interop-api-clients"; |
| 8 | +import { api, attributeRegistryService } from "../vitest.api.setup.js"; |
| 9 | +import { toApiAttribute } from "../../src/model/domain/apiConverter.js"; |
| 10 | +import { attributeDuplicateByNameAndCode } from "../../src/model/domain/errors.js"; |
| 11 | + |
| 12 | +describe("API /certifiedAttributes authorization test", () => { |
| 13 | + const mockCertifiedAttributeSeed: attributeRegistryApi.CertifiedAttributeSeed = |
| 14 | + { |
| 15 | + name: "Certified Attribute", |
| 16 | + description: "This is a certified attribute", |
| 17 | + code: "001", |
| 18 | + }; |
| 19 | + |
| 20 | + const mockAttribute: Attribute = { |
| 21 | + ...getMockAttribute(), |
| 22 | + id: generateId(), |
| 23 | + kind: "Certified", |
| 24 | + creationTime: new Date(), |
| 25 | + }; |
| 26 | + |
| 27 | + const apiAttribute = attributeRegistryApi.Attribute.parse( |
| 28 | + toApiAttribute(mockAttribute) |
| 29 | + ); |
| 30 | + |
| 31 | + attributeRegistryService.createCertifiedAttribute = vi |
| 32 | + .fn() |
| 33 | + .mockResolvedValue(mockAttribute); |
| 34 | + |
| 35 | + const makeRequest = async (token: string) => |
| 36 | + request(api) |
| 37 | + .post("/certifiedAttributes") |
| 38 | + .set("Authorization", `Bearer ${token}`) |
| 39 | + .set("X-Correlation-Id", generateId()) |
| 40 | + .send(mockCertifiedAttributeSeed); |
| 41 | + |
| 42 | + const authorizedRoles: AuthRole[] = [authRole.ADMIN_ROLE, authRole.M2M_ROLE]; |
| 43 | + it.each(authorizedRoles)( |
| 44 | + "Should return 200 for user with role %s", |
| 45 | + async (role) => { |
| 46 | + const token = generateToken(role); |
| 47 | + const res = await makeRequest(token); |
| 48 | + expect(res.status).toBe(200); |
| 49 | + expect(res.body).toEqual(apiAttribute); |
| 50 | + } |
| 51 | + ); |
| 52 | + |
| 53 | + it.each( |
| 54 | + Object.values(authRole).filter((role) => !authorizedRoles.includes(role)) |
| 55 | + )("Should return 403 for user with role %s", async (role) => { |
| 56 | + const token = generateToken(role); |
| 57 | + const res = await makeRequest(token); |
| 58 | + expect(res.status).toBe(403); |
| 59 | + }); |
| 60 | + |
| 61 | + it("Should return 409 for conflict", async () => { |
| 62 | + attributeRegistryService.createCertifiedAttribute = vi |
| 63 | + .fn() |
| 64 | + .mockRejectedValue( |
| 65 | + attributeDuplicateByNameAndCode( |
| 66 | + mockCertifiedAttributeSeed.name, |
| 67 | + mockCertifiedAttributeSeed.code |
| 68 | + ) |
| 69 | + ); |
| 70 | + |
| 71 | + const res = await makeRequest(generateToken(authRole.ADMIN_ROLE)); |
| 72 | + |
| 73 | + expect(res.status).toBe(409); |
| 74 | + }); |
| 75 | + |
| 76 | + it("Should return 400 if passed an invalid certified attribute seed", async () => { |
| 77 | + const token = generateToken(authRole.ADMIN_ROLE); |
| 78 | + const res = await request(api) |
| 79 | + .post("/certifiedAttributes") |
| 80 | + .set("Authorization", `Bearer ${token}`) |
| 81 | + .set("X-Correlation-Id", generateId()) |
| 82 | + .send({ |
| 83 | + name: "Certified Attribute", |
| 84 | + code: "001", |
| 85 | + }); |
| 86 | + expect(res.status).toBe(400); |
| 87 | + }); |
| 88 | +}); |
0 commit comments