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

[#175205379] Added mailing component from io-functions-commons #82

Merged
merged 7 commits into from
Oct 28, 2020
Merged
Changes from 1 commit
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
146 changes: 146 additions & 0 deletions EmailNotificationActivity/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/* tslint:disable:no-any */
/* tslint:disable:no-duplicate-string */
/* tslint:disable:no-big-function */
/* tslint:disable: no-identical-functions */

import {
NonEmptyString,
OrganizationFiscalCode
} from "italia-ts-commons/lib/strings";

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

import { some } from "fp-ts/lib/Option";
import { taskEither } from "fp-ts/lib/TaskEither";

import { EmailAddress } from "io-functions-commons/dist/generated/definitions/EmailAddress";
import { MessageBodyMarkdown } from "io-functions-commons/dist/generated/definitions/MessageBodyMarkdown";
import { MessageContent } from "io-functions-commons/dist/generated/definitions/MessageContent";
import { MessageSubject } from "io-functions-commons/dist/generated/definitions/MessageSubject";
import { TimeToLiveSeconds } from "io-functions-commons/dist/generated/definitions/TimeToLiveSeconds";

import { NotificationChannelEnum } from "io-functions-commons/dist/generated/definitions/NotificationChannel";
import { MailerTransporter } from "io-functions-commons/dist/src/mailer";
import { CreatedMessageEventSenderMetadata } from "io-functions-commons/dist/src/models/created_message_sender_metadata";
import {
NewNotification,
NotificationAddressSourceEnum,
NotificationModel,
RetrievedNotification
} from "io-functions-commons/dist/src/models/notification";

describe("getEmailNotificationActivityHandler", () => {
balanza marked this conversation as resolved.
Show resolved Hide resolved
const mockContext = {
balanza marked this conversation as resolved.
Show resolved Hide resolved
log: {
// tslint:disable-next-line: no-console
error: console.error,
// tslint:disable-next-line: no-console
info: console.log,
// tslint:disable-next-line: no-console
verbose: console.log,
// tslint:disable-next-line: no-console
warn: console.warn
}
} as any;

const aMessageId = "A_MESSAGE_ID" as NonEmptyString;

const aNewEmailNotification: NewNotification = {
channels: {
[NotificationChannelEnum.EMAIL]: {
addressSource: NotificationAddressSourceEnum.DEFAULT_ADDRESS,
toAddress: "to@example.com" as EmailAddress
}
},
fiscalCode: "FRLFRC74E04B157I" as any,
id: "A_NOTIFICATION_ID" as NonEmptyString,
kind: "INewNotification",
messageId: aMessageId
};

const aRetrievedNotification: RetrievedNotification = {
_etag: "_etag",
_rid: "_rid",
_self: "_self",
_ts: 1,
...aNewEmailNotification,
kind: "IRetrievedNotification"
};

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

const aNotificationId = "A_NOTIFICATION_ID" as NonEmptyString;
const anOrganizationFiscalCode = "00000000000" as OrganizationFiscalCode;

const aMessageBodyMarkdown = "test".repeat(80) as MessageBodyMarkdown;

const aMessageContent: MessageContent = {
markdown: aMessageBodyMarkdown,
subject: "test".repeat(10) as MessageSubject
};

const aMessage = {
createdAt: new Date(),
fiscalCode: "FRLFRC74E04B157I" as any,
id: aMessageId,
indexedId: aMessageId,
kind: "INewMessageWithoutContent" as "INewMessageWithoutContent",
senderServiceId: "s123" as NonEmptyString,
senderUserId: "u123" as NonEmptyString,
timeToLiveSeconds: 3600 as TimeToLiveSeconds
};

const aSenderMetadata: CreatedMessageEventSenderMetadata = {
departmentName: "IT" as NonEmptyString,
organizationFiscalCode: anOrganizationFiscalCode,
organizationName: "AgID" as NonEmptyString,
requireSecureChannels: false,
serviceName: "Test" as NonEmptyString,
serviceUserEmail: "email@example.com" as EmailAddress
};

const HTML_TO_TEXT_OPTIONS: HtmlToTextOptions = {
ignoreImage: true, // ignore all document images
tables: true
};

const MAIL_FROM = "IO - l’app dei servizi pubblici <no-reply@io.italia.it>" as NonEmptyString;
const defaultNotificationParams = {
HTML_TO_TEXT_OPTIONS,
MAIL_FROM
};

const input: EmailNotificationActivityInput = {
notificationEvent: {
content: aMessageContent,
message: aMessage,
notificationId: aNotificationId,
senderMetadata: aSenderMetadata
}
};

it("should respond with 'SUCCESS' if the mail is sent", async () => {
balanza marked this conversation as resolved.
Show resolved Hide resolved
const lMailerTransporterMock = ({
sendMail: jest.fn((_, f) => f(null, taskEither.of("SUCCESS")))
} as unknown) as MailerTransporter;
balanza marked this conversation as resolved.
Show resolved Hide resolved

const GetEmailNotificationActivityHandler = getEmailNotificationActivityHandler(
lMailerTransporterMock,
notificationModelMock,
defaultNotificationParams
);

const result = (await GetEmailNotificationActivityHandler(
mockContext,
input
)) as EmailNotificationActivityResult;
balanza marked this conversation as resolved.
Show resolved Hide resolved

expect(result.kind).toBe("SUCCESS");
});
});