Skip to content

Commit

Permalink
feat(test) test for withdraw-rai email (#1031)
Browse files Browse the repository at this point in the history
* feat(test) test for withdrwa rai

* remove attachments.ts on coverage

* add respondto rai and fix event in changelog

* change WithdrawRAI to withdraw-rai

* prettier issue

* clean the mock data

* add back in ignoring mock-data
  • Loading branch information
thwalker6 authored Jan 16, 2025
1 parent 61b4573 commit 15843f7
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 21 deletions.
158 changes: 147 additions & 11 deletions lib/lambda/processEmailsHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,112 +4,206 @@ import { SESClient } from "@aws-sdk/client-ses";
import { handler } from "./processEmails";
import { KafkaRecord, KafkaEvent } from "shared-types";
import { Authority } from "shared-types";

import { SIMPLE_ID, WITHDRAW_RAI_ITEM_B, WITHDRAW_RAI_ITEM_C } from "mocks";
const nms = "new-medicaid-submission";
const ncs = "new-chip-submission";
const tempExtension = "temp-extension";
const withdrawPackage = "withdraw-package";
const contractingInitial = "contracting-initial";
const capitatedInitial = "capitated-initial";
const withdrawRai = "withdraw-rai";
const respondToRai = "respond-to-rai";

describe("process emails Handler", () => {
it.each([
[`should send an email for ${nms} with ${Authority.MED_SPA}`, Authority.MED_SPA, nms],
[`should send an email for ${nms} with ${Authority.CHIP_SPA}`, Authority.CHIP_SPA, nms],
[`should send an email for ${nms} with ${Authority["1915b"]}`, Authority["1915b"], nms],
[`should send an email for ${nms} with ${Authority["1915c"]}`, Authority["1915c"], nms],
[`should send an email for ${ncs} with ${Authority.MED_SPA}`, Authority.MED_SPA, ncs],
[`should send an email for ${ncs} with ${Authority.CHIP_SPA}`, Authority.CHIP_SPA, ncs],
[`should send an email for ${ncs} with ${Authority["1915b"]}`, Authority["1915b"], ncs],
[`should send an email for ${ncs} with ${Authority["1915c"]}`, Authority["1915c"], ncs],
[
`should send an email for ${respondToRai} with ${Authority.MED_SPA}`,
Authority.MED_SPA,
respondToRai,
SIMPLE_ID,
],
[
`should send an email for ${respondToRai} with ${Authority.CHIP_SPA}`,
Authority.CHIP_SPA,
respondToRai,
SIMPLE_ID,
],
[
`should send an email for ${respondToRai} with ${Authority["1915b"]}`,
Authority["1915b"],
respondToRai,
SIMPLE_ID,
],
[
`should send an email for ${respondToRai} with ${Authority["1915c"]}`,
Authority["1915c"],
respondToRai,
SIMPLE_ID,
],
[
`should send an email for ${nms} with ${Authority.MED_SPA}`,
Authority.MED_SPA,
nms,
SIMPLE_ID,
],
[
`should send an email for ${nms} with ${Authority.CHIP_SPA}`,
Authority.CHIP_SPA,
nms,
SIMPLE_ID,
],
[
`should send an email for ${nms} with ${Authority["1915b"]}`,
Authority["1915b"],
nms,
SIMPLE_ID,
],
[
`should send an email for ${nms} with ${Authority["1915c"]}`,
Authority["1915c"],
nms,
SIMPLE_ID,
],
[
`should send an email for ${ncs} with ${Authority.MED_SPA}`,
Authority.MED_SPA,
ncs,
SIMPLE_ID,
],
[
`should send an email for ${ncs} with ${Authority.CHIP_SPA}`,
Authority.CHIP_SPA,
ncs,
SIMPLE_ID,
],
[
`should send an email for ${ncs} with ${Authority["1915b"]}`,
Authority["1915b"],
ncs,
SIMPLE_ID,
],
[
`should send an email for ${ncs} with ${Authority["1915c"]}`,
Authority["1915c"],
ncs,
SIMPLE_ID,
],
[
`should send an email for ${tempExtension} with ${Authority.MED_SPA}`,
Authority.MED_SPA,
tempExtension,
SIMPLE_ID,
],
[
`should send an email for ${tempExtension} with ${Authority.CHIP_SPA}`,
Authority.CHIP_SPA,
tempExtension,
SIMPLE_ID,
],
[
`should send an email for ${tempExtension} with ${Authority["1915b"]}`,
Authority["1915b"],
tempExtension,
SIMPLE_ID,
],
[
`should send an email for ${tempExtension} with ${Authority["1915c"]}`,
Authority["1915c"],
tempExtension,
SIMPLE_ID,
],
[
`should send an email for ${withdrawPackage} with ${Authority.MED_SPA}`,
Authority.MED_SPA,
withdrawPackage,
SIMPLE_ID,
],
[
`should send an email for ${withdrawPackage} with ${Authority.CHIP_SPA}`,
Authority.CHIP_SPA,
withdrawPackage,
SIMPLE_ID,
],
[
`should send an email for ${withdrawPackage} for ${ncs} with ${Authority["1915b"]}`,
Authority["1915b"],
withdrawPackage,
SIMPLE_ID,
],
[
`should send an email for ${withdrawPackage} with ${Authority["1915c"]}`,
Authority["1915c"],
withdrawPackage,
SIMPLE_ID,
],
[
`should send an email for ${contractingInitial} with ${Authority.MED_SPA}`,
Authority.MED_SPA,
contractingInitial,
SIMPLE_ID,
],
[
`should send an email for ${contractingInitial} with ${Authority.CHIP_SPA}`,
Authority.CHIP_SPA,
contractingInitial,
SIMPLE_ID,
],
[
`should send an email for ${contractingInitial} with ${Authority["1915b"]}`,
Authority["1915b"],
contractingInitial,
SIMPLE_ID,
],
[
`should send an email for ${contractingInitial} with ${Authority["1915c"]}`,
Authority["1915c"],
contractingInitial,
SIMPLE_ID,
],
[
`should send an email for ${capitatedInitial} with ${Authority.MED_SPA}`,
Authority.MED_SPA,
capitatedInitial,
SIMPLE_ID,
],
[
`should send an email for ${capitatedInitial} with ${Authority.CHIP_SPA}`,
Authority.CHIP_SPA,
capitatedInitial,
SIMPLE_ID,
],
[
`should send an email for ${capitatedInitial} with ${Authority["1915b"]}`,
Authority["1915b"],
capitatedInitial,
SIMPLE_ID,
],
[
`should send an email for ${capitatedInitial} with ${Authority["1915c"]}`,
Authority["1915c"],
capitatedInitial,
SIMPLE_ID,
],
])("%s", async (_, auth, eventType) => {
[
`should send an email for ${withdrawRai} with ${Authority["1915b"]}`,
Authority["1915b"],
withdrawRai,
WITHDRAW_RAI_ITEM_B,
],
[
`should send an email for ${withdrawRai} with ${Authority["1915c"]}`,
Authority["1915c"],
withdrawRai,
WITHDRAW_RAI_ITEM_C,
],
])("%s", async (_, auth, eventType, id) => {
const callback = vi.fn();
const secSPY = vi.spyOn(SESClient.prototype, "send");
const mockEvent: KafkaEvent = {
records: {
"mock-topic": [
{
key: Buffer.from("VA").toString("base64"),
key: Buffer.from(id).toString("base64"),
value: Buffer.from(
JSON.stringify({
origin: "mako",
Expand All @@ -132,3 +226,45 @@ describe("process emails Handler", () => {
expect(secSPY).toHaveBeenCalledTimes(2);
});
});
describe("process emails Handler failures", () => {
it.each([
[
`should send an email for ${withdrawRai} with ${Authority["1915b"]} and fail due to not finding it`,
Authority["1915b"],
withdrawRai,
SIMPLE_ID,
],
[
`should send an email for ${withdrawRai} with ${Authority["1915c"]} and fail due to not finding it`,
Authority["1915c"],
withdrawRai,
SIMPLE_ID,
],
])("%s", async (_, auth, eventType, id = SIMPLE_ID) => {
const callback = vi.fn();
const mockEvent: KafkaEvent = {
records: {
"mock-topic": [
{
key: Buffer.from(id).toString("base64"),
value: Buffer.from(
JSON.stringify({
origin: "mako",
event: eventType,
authority: auth,
}),
).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.toThrow();
});
});
2 changes: 1 addition & 1 deletion lib/lambda/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe("getSearchData Handler", () => {
const body = JSON.parse(res.body);
expect(body).toBeTruthy();
expect(body?.hits?.hits).toBeTruthy();
expect(body?.hits?.hits?.length).toEqual(14);
expect(body?.hits?.hits?.length).toEqual(16);
});

it("should handle errors during processing", async () => {
Expand Down
10 changes: 5 additions & 5 deletions lib/lambda/update/updatePackage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
EXISTING_ITEM_PENDING_ID,
CAPITATED_INITIAL_ITEM_ID,
CAPITATED_INITIAL_NEW_ITEM_ID,
WEIRD_ID,
SIMPLE_ID,
} from "mocks";
vi.mock("libs/handler-lib", () => ({
response: vi.fn((data) => data),
Expand Down Expand Up @@ -204,7 +204,7 @@ describe("handler", () => {
it("should fail to update a package with bad existing id format", async () => {
const noActionevent = {
body: JSON.stringify({
packageId: WEIRD_ID,
packageId: SIMPLE_ID,
action: "update-id",
changeReason: "Nunya",
updatedId: "SS-120",
Expand All @@ -223,7 +223,7 @@ describe("handler", () => {
process.env.topicName = "";
const noActionevent = {
body: JSON.stringify({
packageId: WEIRD_ID,
packageId: SIMPLE_ID,
action: "update-values",
changeReason: "Nunya",
updatedFields: {},
Expand All @@ -240,7 +240,7 @@ describe("handler", () => {
it("should fail to update a package - No valid fields ", async () => {
const noActionevent = {
body: JSON.stringify({
packageId: WEIRD_ID,
packageId: SIMPLE_ID,
action: "update-values",
changeReason: "Nunya",
updatedFields: { badfield: "nothing" },
Expand All @@ -257,7 +257,7 @@ describe("handler", () => {
it("should fail to update a package - Id can not be updated ", async () => {
const noActionevent = {
body: JSON.stringify({
packageId: WEIRD_ID,
packageId: SIMPLE_ID,
action: "update-values",
changeReason: "Nunya",
updatedFields: { id: "cant update ID here" },
Expand Down
2 changes: 1 addition & 1 deletion lib/libs/email/content/withdrawRai/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { render } from "@react-email/render";
import { EmailProcessingError } from "libs/email/errors";

const getWithdrawRaiEvent = async (id: string) => {
const event = await getLatestMatchingEvent(id, "WithdrawRai");
const event = await getLatestMatchingEvent(id, "withdraw-rai");

if (!event) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion lib/libs/email/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export async function getLatestMatchingEvent(
}

// Filter matching events
const events = item.hits.hits.filter((event) => event._source.actionType === actionType);
const events = item.hits.hits.filter((event) => event._source.event === actionType);

// Check if any matching events were found
if (!events.length) {
Expand Down
Loading

0 comments on commit 15843f7

Please sign in to comment.