Skip to content

Commit

Permalink
test(email): proces email test (#979)
Browse files Browse the repository at this point in the history
* feat(test)proces email test

* Removed console.log

* linter correction

* removed a ine

* remove brackets

* add region

* Address change reqeust
  • Loading branch information
thwalker6 authored Jan 8, 2025
1 parent cf14feb commit 11ac25a
Show file tree
Hide file tree
Showing 10 changed files with 247 additions and 233 deletions.
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 @@ -134,7 +134,7 @@ export async function processRecord(kafkaRecord: KafkaRecord, config: ProcessEma
}
}

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

Expand Down
Loading

0 comments on commit 11ac25a

Please sign in to comment.