Skip to content

Commit

Permalink
[#IP-324] update tests + libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
gquadrati committed Sep 3, 2021
1 parent ffa8a09 commit b1eb10d
Show file tree
Hide file tree
Showing 36 changed files with 1,499 additions and 1,688 deletions.
2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
10.14.1
14.16.0
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
10.14.1
14.16.0
43 changes: 25 additions & 18 deletions CreateMessage/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {

import { none, some } from "fp-ts/lib/Option";

import { pipe } from "fp-ts/lib/function";
import * as E from "fp-ts/lib/Either";

import {
canDefaultAddresses,
canPaymentAmount,
Expand Down Expand Up @@ -41,7 +44,7 @@ import {
anIncompleteService,
anotherFiscalCode
} from "../../__mocks__/mocks";
import { initAppInsights } from "italia-ts-commons/lib/appinsights";
import { initAppInsights } from "@pagopa/ts-commons/lib/appinsights";
import { mockOrchestratorContext } from "../../__mocks__/durable-functions";
import { ApiNewMessageWithDefaults } from "../types";

Expand All @@ -64,9 +67,9 @@ describe("canWriteMessage", () => {
), // any authorized recipient, possibly also the current one
recipient // one random recipient
);
expect(response.isLeft()).toBeTruthy();
if (response.isLeft()) {
expect(response.value.kind).toEqual(
expect(E.isLeft(response)).toBeTruthy();
if (E.isLeft(response)) {
expect(response.left.kind).toEqual(
"IResponseErrorForbiddenNotAuthorizedForProduction"
);
}
Expand All @@ -86,9 +89,9 @@ describe("canWriteMessage", () => {
new Set(authorizedRecipients.filter(_ => _ !== recipient)), // current recipient is not authorized
recipient
);
expect(response.isLeft()).toBeTruthy();
if (response.isLeft()) {
expect(response.value.kind).toEqual(
expect(E.isLeft(response)).toBeTruthy();
if (E.isLeft(response)) {
expect(response.left.kind).toEqual(
"IResponseErrorForbiddenNotAuthorizedForRecipient"
);
}
Expand All @@ -108,7 +111,7 @@ describe("canWriteMessage", () => {
new Set([...authorizedRecipients, recipient]), // current recipient always authorized
recipient
);
expect(response.isRight()).toBeTruthy();
expect(E.isRight(response)).toBeTruthy();
}
)
);
Expand All @@ -125,7 +128,7 @@ describe("canWriteMessage", () => {
authorizedRecipients,
recipient
);
expect(response.isRight()).toBeTruthy();
expect(E.isRight(response)).toBeTruthy();
}
)
);
Expand All @@ -137,7 +140,7 @@ describe("canDefaultAddresses", () => {
fc.assert(
fc.property(newMessageWithDefaultEmailArb, m => {
const response = canDefaultAddresses(m);
expect(response.isLeft()).toBeTruthy();
expect(E.isLeft(response)).toBeTruthy();
})
);
});
Expand All @@ -150,11 +153,13 @@ describe("canPaymentAmount", () => {
newMessageWithPaymentDataArb,
maxAmountArb,
(message, maxAmount) => {
const p = message.content.payment_data;

const response = canPaymentAmount(message.content, maxAmount);
if (message.content.payment_data.amount <= maxAmount) {
expect(response.isRight()).toBeTruthy();
expect(E.isRight(response)).toBeTruthy();
} else {
expect(response.isLeft()).toBeTruthy();
expect(E.isLeft(response)).toBeTruthy();
}
}
)
Expand Down Expand Up @@ -188,11 +193,11 @@ describe("createMessageDocument", () => {
senderServiceId
);

const response = await responseTask.run();
const response = await responseTask();

expect(mockMessageModel.create).toHaveBeenCalledTimes(1);
expect(response.isRight()).toBeTruthy();
expect(response.getOrElse(undefined)).toMatchObject({
expect(E.isRight(response)).toBeTruthy();
expect(pipe(response, E.getOrElse(undefined))).toMatchObject({
fiscalCode,
id: messageId,
indexedId: messageId,
Expand Down Expand Up @@ -233,8 +238,8 @@ describe("forkOrchestrator", () => {
service,
newMessageWithoutContent,
serviceUserEmail
).run();
expect(response.isRight()).toBeTruthy();
)();
expect(E.isRight(response)).toBeTruthy();
expect(getDfClient).toHaveBeenCalledTimes(1);
expect(mockDfClient.startNew).toHaveBeenCalledTimes(1);
expect(mockDfClient.startNew).toHaveBeenCalledWith(
Expand All @@ -255,7 +260,9 @@ describe("forkOrchestrator", () => {
serviceVersion: service.version
})
);
expect(response.getOrElse(undefined)).toEqual("orchestratorId");
expect(pipe(response, E.getOrElse(undefined))).toEqual(
"orchestratorId"
);
}
)
);
Expand Down
78 changes: 49 additions & 29 deletions CreateMessage/__tests__/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { WithinRangeString } from "@pagopa/ts-commons/lib/strings";
import * as t from "io-ts";
import { toString } from "fp-ts/lib/function";
import { readableReport } from "italia-ts-commons/lib/reporters";
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 { pipe } from "fp-ts/lib/function";
import * as E from "fp-ts/lib/Either";

const toString = (x: any) => JSON.stringify(x);

const aMessageContent = { subject: "a".repeat(10), markdown: "a".repeat(80) };

Expand All @@ -23,19 +28,25 @@ describe("ApiNewMessageWithContentOf", () => {
const codec = ApiNewMessageWithContentOf(pattern);

// positive scenario: we expect a match
codec.decode(aMessageWithSuchSubject).fold(
e => fail(`Should have decoded the value: ${readableReport(e)}`),
e => {
expect(e.content.subject).toBe(aSubject);
}
pipe(
codec.decode(aMessageWithSuchSubject),
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
codec.decode(aMessageWithDifferentSubject).fold(
_ => {
expect(true).toBe(true);
},
e => fail(`Should have not decoded the value: ${toString(e)}`)
pipe(
codec.decode(aMessageWithDifferentSubject),
E.fold(
_ => {
expect(true).toBe(true);
},
e => fail(`Should have not decoded the value: ${toString(e)}`)
)
);
});

Expand Down Expand Up @@ -67,27 +78,36 @@ describe("ApiNewMessageWithContentOf", () => {
const codec = ApiNewMessageWithContentOf(pattern);

// positive scenario: we expect a match
codec.decode(aMessageWithSuchPaymentData).fold(
e => fail(`Should have decoded the value: ${readableReport(e)}`),
e => {
expect(e.content.payment_data).toEqual(
expect.objectContaining(aPaymentData)
);
}
pipe(
codec.decode(aMessageWithSuchPaymentData),
E.fold(
e => fail(`Should have decoded the value: ${readableReport(e)}`),
e => {
expect(e.content.payment_data).toEqual(
expect.objectContaining(aPaymentData)
);
}
)
);

// negative scenario: we expect a no-match
codec.decode(aMessageWithNoPaymentData).fold(
_ => {
expect(true).toBe(true);
},
e => fail(`Should have not decoded the value: ${toString(e)}`)
pipe(
codec.decode(aMessageWithNoPaymentData),
E.fold(
_ => {
expect(true).toBe(true);
},
e => fail(`Should have not decoded the value: ${toString(e)}`)
)
);
codec.decode(aMessageWithAnotherPaymentData).fold(
_ => {
expect(true).toBe(true);
},
e => fail(`Should have not decoded the value: ${toString(e)}`)
pipe(
codec.decode(aMessageWithAnotherPaymentData),
E.fold(
_ => {
expect(true).toBe(true);
},
e => fail(`Should have not decoded the value: ${toString(e)}`)
)
);
});
});
2 changes: 1 addition & 1 deletion CreateMessage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { secureExpressApp } from "@pagopa/io-functions-commons/dist/src/utils/express";
import { setAppContext } from "@pagopa/io-functions-commons/dist/src/utils/middlewares/context_middleware";

import createAzureFunctionHandler from "io-functions-express/dist/src/createAzureFunctionsHandler";
import createAzureFunctionHandler from "@pagopa/express-azure-functions/dist/src/createAzureFunctionsHandler";

import { withAppInsightsContext } from "@pagopa/io-functions-commons/dist/src/utils/application_insights";
import { cosmosdbInstance } from "../utils/cosmosdb";
Expand Down
6 changes: 3 additions & 3 deletions CreateService/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import {
} from "@pagopa/io-functions-commons/dist/src/utils/middlewares/azure_api_auth";
import { IAzureUserAttributes } from "@pagopa/io-functions-commons/dist/src/utils/middlewares/azure_user_attributes";

import { NonNegativeInteger } from "italia-ts-commons/lib/numbers";
import { NonNegativeInteger } from "@pagopa/ts-commons/lib/numbers";
import {
EmailString,
NonEmptyString,
OrganizationFiscalCode
} from "italia-ts-commons/lib/strings";
} from "@pagopa/ts-commons/lib/strings";

import { MaxAllowedPaymentAmount } from "@pagopa/io-functions-commons/dist/generated/definitions/MaxAllowedPaymentAmount";

import { left, right } from "fp-ts/lib/Either";
import { ServiceScopeEnum } from "@pagopa/io-functions-commons/dist/generated/definitions/ServiceScope";
import { ServiceMetadata } from "@pagopa/io-functions-commons/dist/src/models/service";
import * as reporters from "italia-ts-commons/lib/reporters";
import * as reporters from "@pagopa/ts-commons/lib/reporters";
import { Subscription } from "../../generated/api-admin/Subscription";
import { UserInfo } from "../../generated/api-admin/UserInfo";
import { ServicePayload } from "../../generated/definitions/ServicePayload";
Expand Down
2 changes: 1 addition & 1 deletion CreateService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { secureExpressApp } from "@pagopa/io-functions-commons/dist/src/utils/express";
import { setAppContext } from "@pagopa/io-functions-commons/dist/src/utils/middlewares/context_middleware";

import createAzureFunctionHandler from "io-functions-express/dist/src/createAzureFunctionsHandler";
import createAzureFunctionHandler from "@pagopa/express-azure-functions/dist/src/createAzureFunctionsHandler";
import { cosmosdbInstance } from "../utils/cosmosdb";

import { apiClient } from "../clients/admin";
Expand Down
14 changes: 8 additions & 6 deletions EmailNotificationActivity/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
import {
NonEmptyString,
OrganizationFiscalCode
} from "italia-ts-commons/lib/strings";
} from "@pagopa/ts-commons/lib/strings";

import {
EmailNotificationActivityInput,
getEmailNotificationActivityHandler
} from "../handler";

import { some } from "fp-ts/lib/Option";
import { fromLeft, taskEither } from "fp-ts/lib/TaskEither";
import { pipe } from "fp-ts/lib/function";
import * as E from "fp-ts/lib/Either";
import * as TE from "fp-ts/lib/TaskEither";
import * as O from "fp-ts/lib/Option";

import { EmailAddress } from "@pagopa/io-functions-commons/dist/generated/definitions/EmailAddress";
import { MessageBodyMarkdown } from "@pagopa/io-functions-commons/dist/generated/definitions/MessageBodyMarkdown";
Expand Down Expand Up @@ -72,7 +74,7 @@ const aRetrievedNotification: RetrievedNotification = {
};

const notificationModelMock = ({
find: jest.fn(() => taskEither.of(some(aRetrievedNotification)))
find: jest.fn(() => TE.of(O.some(aRetrievedNotification)))
} as unknown) as NotificationModel;

const aNotificationId = "A_NOTIFICATION_ID" as NonEmptyString;
Expand Down Expand Up @@ -129,7 +131,7 @@ const lMailerTransporterMock = ({} as unknown) as mail.MailerTransporter;

describe("getEmailNotificationActivityHandler", () => {
it("should respond with 'SUCCESS' if the mail is sent", async () => {
jest.spyOn(mail, "sendMail").mockReturnValueOnce(taskEither.of("SUCCESS"));
jest.spyOn(mail, "sendMail").mockReturnValueOnce(TE.of("SUCCESS"));

const GetEmailNotificationActivityHandler = getEmailNotificationActivityHandler(
lMailerTransporterMock,
Expand All @@ -150,7 +152,7 @@ describe("getEmailNotificationActivityHandler", () => {

jest
.spyOn(mail, "sendMail")
.mockReturnValueOnce(fromLeft(new Error(errorMessage)));
.mockReturnValueOnce(TE.left(new Error(errorMessage)));

const GetEmailNotificationActivityHandler = getEmailNotificationActivityHandler(
lMailerTransporterMock,
Expand Down
2 changes: 1 addition & 1 deletion GetLimitedProfile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { secureExpressApp } from "@pagopa/io-functions-commons/dist/src/utils/ex
import { setAppContext } from "@pagopa/io-functions-commons/dist/src/utils/middlewares/context_middleware";
import cors = require("cors");
import express = require("express");
import createAzureFunctionHandler from "io-functions-express/dist/src/createAzureFunctionsHandler";
import createAzureFunctionHandler from "@pagopa/express-azure-functions/dist/src/createAzureFunctionsHandler";
import { initTelemetryClient } from "../utils/appinsights";
import { getConfigOrThrow } from "../utils/config";
import { cosmosdbInstance } from "../utils/cosmosdb";
Expand Down
2 changes: 1 addition & 1 deletion GetLimitedProfileByPOST/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { secureExpressApp } from "@pagopa/io-functions-commons/dist/src/utils/ex
import { setAppContext } from "@pagopa/io-functions-commons/dist/src/utils/middlewares/context_middleware";
import cors = require("cors");
import express = require("express");
import createAzureFunctionHandler from "io-functions-express/dist/src/createAzureFunctionsHandler";
import createAzureFunctionHandler from "@pagopa/express-azure-functions/dist/src/createAzureFunctionsHandler";
import { initTelemetryClient } from "../utils/appinsights";
import { getConfigOrThrow } from "../utils/config";
import { cosmosdbInstance } from "../utils/cosmosdb";
Expand Down
Loading

0 comments on commit b1eb10d

Please sign in to comment.