Skip to content

Commit

Permalink
[#IOPID-1604] refactor based on feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
arcogabbo committed Feb 23, 2024
1 parent e9da6ab commit ecefbcb
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 174 deletions.
86 changes: 37 additions & 49 deletions ValidateProfileEmail/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { ResourceNotFoundCode } from "@pagopa/io-functions-commons/dist/src/utils/azure_storage";

import {
TokenQueryParam,
ValidateProfileEmailHandler,
ValidationErrors
} from "../handler";
import { ValidateProfileEmailHandler } from "../handler";
import { EmailString, FiscalCode } from "@pagopa/ts-commons/lib/strings";
import { IProfileEmailReader } from "@pagopa/io-functions-commons/dist/src/utils/unique_email_enforcement";
import { constTrue } from "fp-ts/lib/function";
Expand All @@ -13,6 +9,12 @@ import * as O from "fp-ts/Option";
import { ProfileModel } from "@pagopa/io-functions-commons/dist/src/models/profile";
import { aFiscalCode, aRetrievedProfile, anEmail } from "../__mocks__/profile";
import { ValidUrl } from "@pagopa/ts-commons/lib/url";
import { FlowChoiceEnum, TokenQueryParam } from "../../utils/middleware";
import {
confirmChoicePageUrl,
validationFailureUrl,
validationSuccessUrl
} from "../../utils/redirect_url";

const VALIDATION_TOKEN = "01DPT9QAZ6N0FJX21A86FRCWB3:8c652f8566ba53bd8cf0b1b9" as TokenQueryParam;

Expand All @@ -38,38 +40,14 @@ const contextMock = {

const validationCallbackUrl = {
href: "localhost/validation"
};
} as ValidUrl;

const confirmChoiceUrl = {
href: "localhost/confirm-choice"
} as ValidUrl;

const timestampGeneratorMock = () => 1234567890;

const errorUrl = (
error: keyof typeof ValidationErrors,
timestampGenerator: () => number
) => {
return `${
validationCallbackUrl.href
}?result=failure&error=${error}&time=${timestampGenerator()}`;
};

const successUrl = (timestampGenerator: () => number) => {
return `${
validationCallbackUrl.href
}?result=success&time=${timestampGenerator()}`;
};

const confirmPageUrl = (
token: string,
email: EmailString,
timestampGenerator: () => number
) =>
`${
confirmChoiceUrl.href
}?token=${token}&email=${email}&time=${timestampGenerator()}`;

const mockRetrieveEntity = jest
.fn()
.mockImplementation((_, __, ___, ____, f) => {
Expand Down Expand Up @@ -119,16 +97,16 @@ describe("ValidateProfileEmailHandler", () => {
});

it.each`
scenario | expectedError | callbackInputs | isConfirmStep
${"GENERIC_ERROR in case the query versus the table storage fails during confirm step"} | ${"GENERIC_ERROR"} | ${[new Error()]} | ${true}
scenario | expectedError | callbackInputs | isConfirmFlow
${"GENERIC_ERROR in case the query versus the table storage fails during confirm flow"} | ${"GENERIC_ERROR"} | ${[new Error()]} | ${true}
${"GENERIC_ERROR in case the query versus the table storage fails"} | ${"GENERIC_ERROR"} | ${[new Error()]} | ${false}
${"INVALID_TOKEN error in case the token if not found in the table during confirm step"} | ${"INVALID_TOKEN"} | ${[{ code: ResourceNotFoundCode }]} | ${true}
${"INVALID_TOKEN error in case the token if not found in the table during confirm flow"} | ${"INVALID_TOKEN"} | ${[{ code: ResourceNotFoundCode }]} | ${true}
${"INVALID_TOKEN error in case the token if not found in the table"} | ${"INVALID_TOKEN"} | ${[{ code: ResourceNotFoundCode }]} | ${false}
${"TOKEN_EXPIRED error in case the token is expired during confirm step"} | ${"TOKEN_EXPIRED"} | ${[undefined, expiredTokenEntity]} | ${true}
${"TOKEN_EXPIRED error in case the token is expired during confirm flow"} | ${"TOKEN_EXPIRED"} | ${[undefined, expiredTokenEntity]} | ${true}
${"TOKEN_EXPIRED error in case the token is expired"} | ${"TOKEN_EXPIRED"} | ${[undefined, expiredTokenEntity]} | ${false}
`(
"should return a redirect with a $scenario",
async ({ callbackInputs, expectedError, isConfirmStep }) => {
async ({ callbackInputs, expectedError, isConfirmFlow }) => {
mockRetrieveEntity.mockImplementationOnce((_, __, ___, ____, f) => {
f(...callbackInputs);
});
Expand All @@ -147,27 +125,31 @@ describe("ValidateProfileEmailHandler", () => {
const response = await verifyProfileEmailHandler(
contextMock as any,
VALIDATION_TOKEN,
isConfirmStep ? O.some(true) : O.none
isConfirmFlow ? FlowChoiceEnum.CONFIRM : FlowChoiceEnum.VALIDATE
);

expect(response.kind).toBe("IResponseSeeOtherRedirect");
expect(response.detail).toBe(
errorUrl(expectedError, timestampGeneratorMock)
validationFailureUrl(
validationCallbackUrl,
expectedError,
timestampGeneratorMock
).href
);
expect(mockFindLastVersionByModelId).not.toBeCalled();
expect(mockUpdate).not.toBeCalled();
}
);

it.each`
scenario | expectedError | isThrowing | isConfirmStep
${"should return IResponseSeeOtherRedirect if the e-mail is already taken (unique email enforcement = %uee) WHEN a citizen changes e-mail during confirm step"} | ${"EMAIL_ALREADY_TAKEN"} | ${undefined} | ${true}
scenario | expectedError | isThrowing | isConfirmFlow
${"should return IResponseSeeOtherRedirect if the e-mail is already taken (unique email enforcement = %uee) WHEN a citizen changes e-mail during confirm flow"} | ${"EMAIL_ALREADY_TAKEN"} | ${undefined} | ${true}
${"should return IResponseSeeOtherRedirect if the e-mail is already taken (unique email enforcement = %uee) WHEN a citizen changes e-mail"} | ${"EMAIL_ALREADY_TAKEN"} | ${undefined} | ${false}
${"return 500 WHEN the unique e-mail enforcement check fails during confirm step"} | ${"GENERIC_ERROR"} | ${true} | ${true}
${"return 500 WHEN the unique e-mail enforcement check fails during confirm flow"} | ${"GENERIC_ERROR"} | ${true} | ${true}
${"return 500 WHEN the unique e-mail enforcement check fails"} | ${"GENERIC_ERROR"} | ${true} | ${false}
`(
"should $scenario",
async ({ expectedError, isThrowing, isConfirmStep }) => {
async ({ expectedError, isThrowing, isConfirmFlow }) => {
const verifyProfileEmailHandler = ValidateProfileEmailHandler(
tableServiceMock as any,
"",
Expand All @@ -184,19 +166,23 @@ describe("ValidateProfileEmailHandler", () => {
const response = await verifyProfileEmailHandler(
contextMock as any,
VALIDATION_TOKEN,
isConfirmStep ? O.some(true) : O.none
isConfirmFlow ? FlowChoiceEnum.CONFIRM : FlowChoiceEnum.VALIDATE
);

expect(response.kind).toBe("IResponseSeeOtherRedirect");
expect(response.detail).toBe(
errorUrl(expectedError, timestampGeneratorMock)
validationFailureUrl(
validationCallbackUrl,
expectedError,
timestampGeneratorMock
).href
);
expect(mockFindLastVersionByModelId).toBeCalledWith([aFiscalCode]);
expect(mockUpdate).not.toBeCalled();
}
);

it("should validate the email in profile if all the condition are verified and we are in the confirm step", async () => {
it("should validate the email in profile if all the condition are verified and we are in the confirm flow", async () => {
const verifyProfileEmailHandler = ValidateProfileEmailHandler(
tableServiceMock as any,
"",
Expand All @@ -213,18 +199,20 @@ describe("ValidateProfileEmailHandler", () => {
const response = await verifyProfileEmailHandler(
contextMock as any,
VALIDATION_TOKEN,
O.some(true)
FlowChoiceEnum.VALIDATE
);

expect(response.kind).toBe("IResponseSeeOtherRedirect");
expect(response.detail).toBe(successUrl(timestampGeneratorMock));
expect(response.detail).toBe(
validationSuccessUrl(validationCallbackUrl, timestampGeneratorMock).href
);
expect(mockFindLastVersionByModelId).toBeCalledWith([aFiscalCode]);
expect(mockUpdate).toBeCalledWith(
expect.objectContaining({ isEmailValidated: true })
);
});

it("should NOT validate the email in profile if we are NOT in the confirm step", async () => {
it("should NOT validate the email in profile if we are in the CONFIRM flow", async () => {
const verifyProfileEmailHandler = ValidateProfileEmailHandler(
tableServiceMock as any,
"",
Expand All @@ -241,12 +229,12 @@ describe("ValidateProfileEmailHandler", () => {
const response = await verifyProfileEmailHandler(
contextMock as any,
VALIDATION_TOKEN,
O.none
FlowChoiceEnum.CONFIRM
);

expect(response.kind).toBe("IResponseSeeOtherRedirect");
expect(response.detail).toBe(
confirmPageUrl(VALIDATION_TOKEN, anEmail, timestampGeneratorMock)
confirmChoicePageUrl(confirmChoiceUrl, VALIDATION_TOKEN, anEmail).href
);
expect(mockFindLastVersionByModelId).toBeCalledWith([aFiscalCode]);
expect(mockUpdate).not.toHaveBeenCalled();
Expand Down
Loading

0 comments on commit ecefbcb

Please sign in to comment.