-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#IOPID-867] magic link on login email (#285)
- Loading branch information
Showing
16 changed files
with
801 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,26 @@ | ||
import { Millisecond } from "@pagopa/ts-commons/lib/units"; | ||
import { initTelemetryClient } from "../utils/appinsights"; | ||
import { magicLinkServiceClient } from "./utils"; | ||
import { getConfigOrThrow } from "../utils/config"; | ||
import { getTimeoutFetch } from "../utils/fetch"; | ||
import { getMagicLinkServiceClient } from "./utils"; | ||
import { getActivityHandler } from "./handler"; | ||
|
||
// HTTP external requests timeout in milliseconds | ||
const REQUEST_TIMEOUT_MS = 5000; | ||
|
||
const config = getConfigOrThrow(); | ||
|
||
const timeoutFetch = getTimeoutFetch(REQUEST_TIMEOUT_MS as Millisecond); | ||
|
||
// Initialize application insights | ||
initTelemetryClient(); | ||
|
||
const activityFunctionHandler = getActivityHandler(magicLinkServiceClient); | ||
const activityFunctionHandler = getActivityHandler( | ||
getMagicLinkServiceClient( | ||
config.MAGIC_LINK_SERVICE_PUBLIC_URL, | ||
config.MAGIC_LINK_SERVICE_API_KEY, | ||
timeoutFetch | ||
) | ||
); | ||
|
||
export default activityFunctionHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,27 @@ | ||
import { FiscalCode, NonEmptyString } from "@pagopa/ts-commons/lib/strings"; | ||
import { NonEmptyString } from "@pagopa/ts-commons/lib/strings"; | ||
import nodeFetch from "node-fetch"; | ||
import { | ||
Client, | ||
createClient | ||
} from "../generated/definitions/ioweb-function/client"; | ||
|
||
// TODO: instanciate an actual magicLinkServiceClient | ||
export const magicLinkServiceClient = { | ||
getMagicCodeForUser: ( | ||
fc: FiscalCode, | ||
n: NonEmptyString, | ||
f: NonEmptyString | ||
): Promise<never> => Promise.reject({ status: 501, value: { f, fc, n } }) | ||
}; | ||
export const getMagicLinkServiceClient = ( | ||
magicLinkServiceBaseUrl: NonEmptyString, | ||
token: NonEmptyString, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
fetchApi: typeof fetch = (nodeFetch as unknown) as typeof fetch | ||
): Client<"ApiKeyAuth"> => | ||
createClient({ | ||
baseUrl: magicLinkServiceBaseUrl, | ||
fetchApi, | ||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type | ||
withDefaults: op => params => | ||
op({ | ||
...params, | ||
ApiKeyAuth: token | ||
}) | ||
}); | ||
|
||
export type MagicLinkServiceClient = typeof magicLinkServiceClient; | ||
export type MagicLinkServiceClient = ReturnType< | ||
typeof getMagicLinkServiceClient | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,7 +57,7 @@ describe("NoticeLoginEmailOrchestratorHandler", () => { | |
|
||
mockCallActivityFunction.mockReturnValueOnce({ | ||
kind: "SUCCESS", | ||
value: { magic_code: "dummy" as NonEmptyString } | ||
value: { magic_link: "dummy" as NonEmptyString } | ||
}); | ||
|
||
mockCallActivityFunction.mockReturnValueOnce({ | ||
|
@@ -97,7 +97,7 @@ describe("NoticeLoginEmailOrchestratorHandler", () => { | |
date_time: aDate.getTime(), | ||
name: "foo", | ||
ip_address: anIPAddress, | ||
magic_code: "dummy", | ||
magic_link: "dummy", | ||
identity_provider: "idp", | ||
geo_location: "Rome", | ||
email: "[email protected]", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,26 @@ | ||
import { getSendLoginEmailActivityHandler } from "../handler"; | ||
import { ActivityInput, getSendLoginEmailActivityHandler } from "../handler"; | ||
import { context } from "../../__mocks__/durable-functions"; | ||
import { EmailString, NonEmptyString } from "@pagopa/ts-commons/lib/strings"; | ||
import { | ||
EmailString, | ||
IPString, | ||
NonEmptyString | ||
} from "@pagopa/ts-commons/lib/strings"; | ||
import { EmailDefaults } from "../index"; | ||
import * as ai from "applicationinsights"; | ||
import * as mailTemplate from "../../generated/templates/login/index"; | ||
import * as fallbackMailTemplate from "../../generated/templates/login-fallback/index"; | ||
|
||
const aDate = new Date("1970-01-01"); | ||
const aValidPayload = { | ||
const aValidPayload: ActivityInput = { | ||
date_time: aDate, | ||
name: "foo" as NonEmptyString, | ||
email: "[email protected]" as EmailString, | ||
identity_provider: "idp" as NonEmptyString, | ||
ip_address: "127.0.0.1" as NonEmptyString | ||
ip_address: "127.0.0.1" as IPString | ||
}; | ||
const aValidPayloadWithMagicLink: ActivityInput = { | ||
...aValidPayload, | ||
magic_link: "http://example.com/#token=abcde" as NonEmptyString | ||
}; | ||
const emailDefaults: EmailDefaults = { | ||
from: "[email protected]" as any, | ||
|
@@ -24,30 +34,40 @@ const mockMailerTransporter = { | |
}) | ||
}; | ||
|
||
const aPublicUrl = "https://localhost/" as NonEmptyString; | ||
const aHelpDeskRef = "[email protected]" as NonEmptyString; | ||
|
||
const mockTrackEvent = jest.fn(); | ||
const mockTracker = ({ | ||
trackEvent: mockTrackEvent | ||
} as unknown) as ai.TelemetryClient; | ||
|
||
const templateFunction = jest.spyOn(mailTemplate, "apply"); | ||
const fallbackTemplateFunction = jest.spyOn(fallbackMailTemplate, "apply"); | ||
|
||
describe("SendTemplatedLoginEmailActivity", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should send a login email with the data", async () => { | ||
it.each` | ||
title | payload | ||
${"fallback login email"} | ${aValidPayload} | ||
${"login email"} | ${aValidPayloadWithMagicLink} | ||
`("should send a $title with the data", async ({ payload }) => { | ||
const handler = getSendLoginEmailActivityHandler( | ||
mockMailerTransporter as any, | ||
emailDefaults, | ||
aPublicUrl, | ||
aHelpDeskRef, | ||
mockTracker | ||
); | ||
|
||
const result = await handler(context as any, aValidPayload); | ||
const result = await handler(context as any, payload); | ||
|
||
expect(result.kind).toEqual("SUCCESS"); | ||
expect(templateFunction).toHaveBeenCalledTimes(payload.magic_link ? 1 : 0); | ||
expect(fallbackTemplateFunction).toHaveBeenCalledTimes( | ||
payload.magic_link ? 0 : 1 | ||
); | ||
expect(mockMailerTransporter.sendMail).toHaveBeenCalledTimes(1); | ||
expect(mockMailerTransporter.sendMail).toHaveBeenCalledWith( | ||
{ | ||
|
@@ -66,7 +86,6 @@ describe("SendTemplatedLoginEmailActivity", () => { | |
const handler = getSendLoginEmailActivityHandler( | ||
mockMailerTransporter as any, | ||
emailDefaults, | ||
aPublicUrl, | ||
aHelpDeskRef, | ||
mockTracker | ||
); | ||
|
Oops, something went wrong.