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

test(email): proces email test #979

Merged
merged 7 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
152 changes: 152 additions & 0 deletions lib/lambda/processEmails.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import { describe, it, expect, vi } from "vitest";
import { Context } from "aws-lambda";
import { SESClient } from "@aws-sdk/client-ses";
import { sendEmail, validateEmailTemplate, handler } from "./processEmails";
import { KafkaRecord, KafkaEvent } from "node_modules/shared-types";

describe("process emails Handler", () => {
it("should return 200 with a proper email", async () => {
const params = {
Source: "[email protected]",
Destination: { ToAddresses: ["[email protected]"] },
Message: {
Subject: { Data: "Mocked Email", Charset: "UTF-8" },
Body: { Text: { Data: "This is a mocked email body.", Charset: "UTF-8" } },
},
};
const test = await sendEmail(params, "us-east-1");
expect(test.status).toStrictEqual(200);
});
it("should throw an error", async () => {
const params = {
Source: "[email protected]",
Destination: { ToAddresses: ["[email protected]"] },
Message: {
Subject: { Data: "Mocked Email", Charset: "UTF-8" },
Body: { Text: { Data: "This is a mocked email body.", Charset: "UTF-8" } },
},
};
await expect(sendEmail(params, "test")).rejects.toThrowError();
});
it("should validate the email template and throw an error", async () => {
const template = {
to: "Person",
from: "Other Guy",
body: "body",
};
expect(() => validateEmailTemplate(template)).toThrowError();
});
it("should make a handler", async () => {
const callback = vi.fn();
const secSPY = vi.spyOn(SESClient.prototype, "send");
const mockEvent: KafkaEvent = {
records: {
"mock-topic": [
{
key: Buffer.from("VA").toString("base64"),
value: Buffer.from(
JSON.stringify({
origin: "mako",
event: "new-medicaid-submission",
authority: "medicaid spa",
}),
).toString("base64"),
headers: {},
timestamp: 1732645041557,
offset: "0",
partition: 0,
topic: "mock-topic",
} as unknown as KafkaRecord,
],
},
eventSource: "",
bootstrapServers: "",
};

await handler(mockEvent, {} as Context, callback);
expect(secSPY).toHaveBeenCalledTimes(2);
});
it("should not be mako therefor not do an event", async () => {
const callback = vi.fn();
const mockEvent: KafkaEvent = {
records: {
"mock-topic": [
{
key: Buffer.from("VA").toString("base64"),
value: Buffer.from(
JSON.stringify({
origin: "not mako",
event: "new-medicaid-submission",
authority: "medicaid spa",
}),
).toString("base64"),
headers: {},
timestamp: 1732645041557,
offset: "0",
partition: 0,
topic: "mock-topic",
} as unknown as KafkaRecord,
],
},
eventSource: "",
bootstrapServers: "",
};
const secSPY = vi.spyOn(SESClient.prototype, "send");

await handler(mockEvent, {} as Context, callback);
expect(secSPY).toHaveBeenCalledTimes(0);
});
it("should be missing a value, so nothing sent", async () => {
const callback = vi.fn();
const mockEvent: KafkaEvent = {
records: {
"mock-topic": [
{
key: Buffer.from("VA").toString("base64"),
headers: {},
timestamp: 1732645041557,
offset: "0",
partition: 0,
topic: "mock-topic",
} as unknown as KafkaRecord,
],
},
eventSource: "",
bootstrapServers: "",
};
const secSPY = vi.spyOn(SESClient.prototype, "send");

await handler(mockEvent, {} as Context, callback);
expect(secSPY).toHaveBeenCalledTimes(0);
});
it("should be missing an environment variable", async () => {
const callback = vi.fn();
delete process.env.osDomain;
const mockEvent: KafkaEvent = {
records: {
"mock-topic": [
{
key: Buffer.from("VA").toString("base64"),
value: Buffer.from(
JSON.stringify({
origin: "mako",
event: "new-medicaid-submission",
authority: "medicaid spa",
}),
).toString("base64"),
headers: {},
timestamp: 1732645041557,
offset: "0",
partition: 0,
topic: "mock-topic",
} as unknown as KafkaRecord,
],
},
eventSource: "",
bootstrapServers: "",
};
await expect(handler(mockEvent, {} as Context, callback)).rejects.toThrowError(
"Missing required environment variables: osDomain",
);
});
});
223 changes: 0 additions & 223 deletions lib/lambda/processEmails.text.ts

This file was deleted.

2 changes: 1 addition & 1 deletion lib/lambda/processEmails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
const { key, value, timestamp } = kafkaRecord;
const id: string = decodeBase64WithUtf8(key);

if (!value) {

Check failure on line 112 in lib/lambda/processEmails.ts

View workflow job for this annotation

GitHub Actions / coverage-report

lambda/processEmails.test.ts > process emails Handler > should make a handler

Error: InvalidChecksumError ❯ ../node_modules/@aws-sdk/middleware-sdk-sqs/dist-cjs/index.js:142:11 ❯ ../node_modules/@aws-sdk/client-sqs/node_modules/@aws-sdk/middleware-logger/dist-cjs/index.js:34:22 ❯ Module.handler lambda/processEmails.ts:112:8 ❯ lambda/processEmails.test.ts:69:3
console.log("Tombstone detected. Doing nothing for this event");
return;
}
Expand All @@ -134,7 +134,7 @@
}
}

function validateEmailTemplate(template: any) {
export function validateEmailTemplate(template: any) {
const requiredFields = ["to", "subject", "body"];
const missingFields = requiredFields.filter((field) => !template[field]);

Expand Down
Loading
Loading