Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#ICC-86] Add Third Party Middleware #208

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions CreateMessage/__tests__/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { readableReport } from "@pagopa/ts-commons/lib/reporters";
import { ApiNewMessageWithContentOf } from "../types";
import { PaymentData } from "@pagopa/io-functions-commons/dist/generated/definitions/PaymentData";
import { json } from "express";
import { ThirdPartyData } from "../../generated/definitions/ThirdPartyData";

import { pipe } from "fp-ts/lib/function";
import * as E from "fp-ts/lib/Either";
Expand Down Expand Up @@ -50,6 +51,51 @@ describe("ApiNewMessageWithContentOf", () => {
);
});

it("should decode Third Party Data", () => {
const aSubject = "my specific subject";
const aThirdPartyData = {
id: "ID"
};
const aMessageWithSuchSubject = {
content: { ...aMessageContent, subject: aSubject }
};

const aMessageWithThirdParty = {
...aMessageWithSuchSubject,
content: {
...aMessageContent,
subject: aSubject,
third_party_data: aThirdPartyData
}
};

const pattern = t.interface({ third_party_data: ThirdPartyData });

const codec = ApiNewMessageWithContentOf(pattern);

// positive scenario: we expect a match
pipe(
codec.decode(aMessageWithThirdParty),
E.fold(
e => fail(`Should have decoded the value: ${readableReport(e)}`),
e => {
expect(e.content.subject).toBe(aSubject);
}
)
);

// negative scenario: we expect a no-match
pipe(
codec.decode(aMessageWithSuchSubject),
E.fold(
_ => {
expect(true).toBe(true);
},
e => fail(`Should have not decoded the value: ${toString(e)}`)
)
);
});

it("should decode a specific payment data", () => {
const aPaymentData = { amount: 2, notice_number: "011111111111111111" };
const aMessageWithSuchPaymentData = {
Expand Down
10 changes: 8 additions & 2 deletions CreateMessage/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import {
} from "../utils/events/message";
import { commonCreateMessageMiddlewares } from "../utils/message_middlewares";
import { LegalData } from "../generated/definitions/LegalData";
import { ThirdPartyData } from "../generated/definitions/ThirdPartyData";
import {
ApiNewMessageWithAdvancedFeatures,
ApiNewMessageWithContentOf,
Expand Down Expand Up @@ -459,7 +460,6 @@ export function CreateMessageHandler(
)();
};
}

/**
* Wraps a CreateMessage handler inside an Express request handler.
*/
Expand Down Expand Up @@ -506,10 +506,16 @@ export function CreateMessage(
AzureAllowBodyPayloadMiddleware(
ApiNewMessageWithAdvancedFeatures,
new Set([UserGroup.ApiMessageWriteAdvanced])
),
// Allow only users in the ApiThirdPartyMessageWrite group to send messages with ThirdPartyData
AzureAllowBodyPayloadMiddleware(
ApiNewMessageWithContentOf(
t.interface({ third_party_data: ThirdPartyData })
),
new Set([UserGroup.ApiThirdPartyMessageWrite])
)
] as const)
);

return wrapRequestHandler(
middlewaresWrap(
// eslint-disable-next-line max-params
Expand Down
15 changes: 11 additions & 4 deletions CreateMessage/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,25 @@ export const ApiNewMessageWithAdvancedFeatures = t.intersection([
})
]);

/**
* Codec that matches a Message with a specific content pattern
*
* @param contentPattern a coded that matches a content pattern
* @returns a codec that specialize ApiNewMessage
*/

type PartialMessageContent = Partial<typeof ApiNewMessage._A["content"]>;

/**
* Codec that matches a Message with a specific content pattern
*
* @param contentPattern a coded that matches a content pattern
* @returns a codec that specialize ApiNewMessage
*/
export type ApiNewMessageWithContentOf<
T extends Partial<typeof ApiNewMessage._O["content"]>
T extends PartialMessageContent
> = ApiNewMessage & { readonly content: T };
export const ApiNewMessageWithContentOf = <
T extends Partial<typeof ApiNewMessage._O["content"]>
>(
export const ApiNewMessageWithContentOf = <T extends PartialMessageContent>(
contentPattern: t.Type<T, Partial<typeof ApiNewMessage._O["content"]>>
): t.Type<ApiNewMessage & { readonly content: T }, typeof ApiNewMessage._O> =>
t.intersection([
Expand Down
78 changes: 78 additions & 0 deletions __integrations__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ const aValidLegalMessageContent = {
}
};

const aValidThirdPartyMessageContent = {
id: "ID"
};

// Must correspond to an existing serviceId within "services" colletion
const aSubscriptionKey = "aSubscriptionKey";

Expand Down Expand Up @@ -197,6 +201,26 @@ describe("Create Message |> Middleware errors", () => {
expect(response.status).toEqual(403);
});

it("should return 403 when creating a third party message without right permission", async () => {
const nodeFetch = getNodeFetch({
"x-user-groups": "ApiMessageWrite"
});

const body = {
message: {
fiscal_code: anAutoFiscalCode,
content: {
...aMessageContent,
third_party_data: aValidThirdPartyMessageContent
}
}
};

const response = await postCreateMessage(nodeFetch)(body);

expect(response.status).toEqual(403);
});

it("should return 403 when creating an advanced message without right permission", async () => {
const nodeFetch = getNodeFetch({
"x-user-groups": "ApiMessageWrite"
Expand Down Expand Up @@ -341,6 +365,60 @@ describe("Create Message", () => {
);
});

describe("Create Third Party Message", () => {
it.each`
profileType | fiscalCode | serviceId
${"LEGACY Profile"} | ${aLegacyInboxEnabledFiscalCode} | ${anEnabledServiceId}
${"AUTO Profile"} | ${anAutoFiscalCode} | ${anEnabledServiceId}
${"MANUAL Profile"} | ${aManualFiscalCode} | ${anEnabledServiceId}
`(
"$profileType |> should return the message in PROCESSED status when service is allowed to send",
async ({ fiscalCode, serviceId }) => {
const body = {
message: {
fiscal_code: fiscalCode,
content: {
aMessageContent,
third_party_data: aValidThirdPartyMessageContent
}
}
};

const nodeFetch = getNodeFetch({
"x-subscription-id": serviceId,
"x-user-groups":
customHeaders["x-user-groups"] + ",ApiThirdPartyMessageWrite"
});

const result = await postCreateMessage(nodeFetch)(body);

expect(result.status).toEqual(201);

const messageId = ((await result.json()) as CreatedMessage).id;
expect(messageId).not.toBeUndefined();

// Wait the process to complete
await delay(WAIT_MS);

const resultGet = await getSentMessage(nodeFetch)(fiscalCode, messageId);

expect(resultGet.status).toEqual(200);
const detail = (await resultGet.json()) as ExternalMessageResponseWithContent;

expect(detail).toEqual(
expect.objectContaining({
message: expect.objectContaining({
id: messageId,
feature_level_type: FeatureLevelTypeEnum.STANDARD,
...body.message
}),
status: MessageStatusValueEnum.PROCESSED
})
);
}
);
});

describe("Create Advanced Message", () => {
it.each`
profileType | fiscalCode | serviceId
Expand Down
2 changes: 1 addition & 1 deletion __integrations__/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"dependencies": {
"@azure/cosmos": "^3.15.1",
"@azure/storage-queue": "^12.7.0",
"@pagopa/io-functions-commons": "^24.1.2",
"@pagopa/io-functions-commons": "^24.5.0",
"@types/jest": "^27.0.2",
"@types/node": "^13.11.0",
"azure-storage": "^2.10.5",
Expand Down
8 changes: 4 additions & 4 deletions __integrations__/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -609,10 +609,10 @@
resolved "https://registry.yarnpkg.com/@opentelemetry/api/-/api-1.0.3.tgz#13a12ae9e05c2a782f7b5e84c3cbfda4225eaf80"
integrity sha512-puWxACExDe9nxbBB3lOymQFrLYml2dVOrd7USiVRnSbgXE+KwBu+HxFvxrzfqsiSda9IWsXJG1ef7C1O2/GmKQ==

"@pagopa/io-functions-commons@^24.1.2":
version "24.1.2"
resolved "https://registry.yarnpkg.com/@pagopa/io-functions-commons/-/io-functions-commons-24.1.2.tgz#2197eb7c71e92a54c1ba1a547ee143ad843dd528"
integrity sha512-8SoeklN5oMGhgzsg9dNHEtDMkZvsbg4aKzmUX1LGyGcO/6fBecmvl6I6ww738JzvYcaJ6ymT6s356jbSBSZkXQ==
"@pagopa/io-functions-commons@^24.5.0":
version "24.5.0"
resolved "https://registry.yarnpkg.com/@pagopa/io-functions-commons/-/io-functions-commons-24.5.0.tgz#07b5c10a54d5fc0b196b6cdd7b3c4a7368f57f28"
integrity sha512-+LYDH9ZF7b/H7yriiY+aIV/3gsgnKJdF8bHifWYJ44ESIda8GSyqxTTkPr4Hn4hs9JPFiyt5Zgfs8h2NhHJjxQ==
dependencies:
"@azure/cosmos" "^3.15.1"
"@pagopa/ts-commons" "^10.3.0"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"dependencies": {
"@azure/cosmos": "^3.15.1",
"@pagopa/express-azure-functions": "^2.0.0",
"@pagopa/io-functions-commons": "^24.4.0",
"@pagopa/io-functions-commons": "^24.5.0",
"@pagopa/ts-commons": "^10.2.0",
"applicationinsights": "^1.7.4",
"azure-storage": "^2.10.4",
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -743,10 +743,10 @@
resolved "https://registry.yarnpkg.com/@pagopa/express-azure-functions/-/express-azure-functions-2.0.0.tgz#eb52a0b997d931c1509372e2a9bea22a8ca85c17"
integrity sha512-IFZqtk0e2sfkMZIxYqPORzxcKRkbIrVJesR6eMLNwzh1rA4bl2uh9ZHk1m55LNq4ZmaxREDu+1JcGlIaZQgKNQ==

"@pagopa/io-functions-commons@^24.4.0":
version "24.4.0"
resolved "https://registry.yarnpkg.com/@pagopa/io-functions-commons/-/io-functions-commons-24.4.0.tgz#0e40b7524e4945204ba1ccaae162370f4b95aa0a"
integrity sha512-6aMgjUV5WQTXzolQquPLMOMlWrN6+BpuLz2lNnUrOlRSULRV2pF9zSxx5uT5CMOLCQAcpXchAyon3BDBJdgPZg==
"@pagopa/io-functions-commons@^24.5.0":
version "24.5.0"
resolved "https://registry.yarnpkg.com/@pagopa/io-functions-commons/-/io-functions-commons-24.5.0.tgz#07b5c10a54d5fc0b196b6cdd7b3c4a7368f57f28"
integrity sha512-+LYDH9ZF7b/H7yriiY+aIV/3gsgnKJdF8bHifWYJ44ESIda8GSyqxTTkPr4Hn4hs9JPFiyt5Zgfs8h2NhHJjxQ==
dependencies:
"@azure/cosmos" "^3.15.1"
"@pagopa/ts-commons" "^10.3.0"
Expand Down