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

feat(test) Unit test for updating pacakges #1007

Merged
merged 3 commits into from
Jan 10, 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
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(13);
expect(body?.hits?.hits?.length).toEqual(14);
});

it("should handle errors during processing", async () => {
Expand Down
288 changes: 288 additions & 0 deletions lib/lambda/update/updatePackage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { handler } from "./updatePackage";
import { APIGatewayEvent } from "node_modules/shared-types";

import {
EXISTING_ITEM_ID,
EXISTING_ITEM_PENDING_ID,
CAPITATED_INITIAL_ITEM_ID,
CAPITATED_INITIAL_NEW_ITEM_ID,
WEIRD_ID,
} from "mocks";
vi.mock("libs/handler-lib", () => ({
response: vi.fn((data) => data),
}));

describe("handler", () => {
beforeEach(() => {
vi.clearAllMocks();
process.env.topicName = "test-topic";
});

it("should return 400 if event body is missing", async () => {
const event = {} as APIGatewayEvent;
const result = await handler(event);
const expectedResult = { statusCode: 400, body: { message: "Event body required" } };

expect(result).toStrictEqual(expectedResult);
});

it("should return 400 if package ID is not found", async () => {
const noActionevent = {
body: JSON.stringify({ packageId: "123", changeReason: "Nunya" }),
} as APIGatewayEvent;

const resultPackage = await handler(noActionevent);

expect(resultPackage.statusCode).toBe(400);
});
it("should return 400 if action is not found", async () => {
const noApackageEvent = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should probably split these into two tests just for clarity

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split

body: JSON.stringify({ action: "123", changeReason: "Nunya" }),
} as APIGatewayEvent;

const resultAction = await handler(noApackageEvent);

expect(resultAction.statusCode).toBe(400);
});
it("should get a package", async () => {
const noActionevent = {
body: JSON.stringify({ packageId: "123", action: "delete", changeReason: "Nunya" }),
} as APIGatewayEvent;

const result = await handler(noActionevent);

const expectedResult = {
statusCode: 404,
body: { message: "No record found for the given id" },
};
expect(result).toStrictEqual(expectedResult);
});
it("should delete a package", async () => {
const noActionevent = {
body: JSON.stringify({
packageId: EXISTING_ITEM_ID,
action: "delete",
changeReason: "Nunya",
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);

const expectedResult = {
statusCode: 200,
body: { message: "MD-00-0000 has been deleted." },
};
expect(result).toStrictEqual(expectedResult);
});
it("should delete a package but no topic name", async () => {
process.env.topicName = "";
const noActionevent = {
body: JSON.stringify({
packageId: EXISTING_ITEM_ID,
action: "delete",
changeReason: "Nunya",
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);

const expectedResult = {
statusCode: 500,
body: { message: "Topic name is not defined" },
};
expect(result).toStrictEqual(expectedResult);
});

it("should fail to update an ID on a package - missing new iD", async () => {
const noActionevent = {
body: JSON.stringify({
packageId: EXISTING_ITEM_ID,
action: "update-id",
changeReason: "Nunya",
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);

const expectedResult = {
statusCode: 400,
body: { message: "New ID required to update package" },
};
expect(result).toStrictEqual(expectedResult);
});
it("should fail to update an ID on a package - missing or same ID", async () => {
const noActionevent = {
body: JSON.stringify({
packageId: EXISTING_ITEM_ID,
action: "update-id",
changeReason: "Nunya",
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);

const expectedResult = {
statusCode: 400,
body: { message: "New ID required to update package" },
};
expect(result).toStrictEqual(expectedResult);
});
it("should fail to update an ID on a package - missing or same ID", async () => {
const noActionevent = {
body: JSON.stringify({
packageId: EXISTING_ITEM_ID,
action: "update-id",
changeReason: "Nunya",
updatedId: EXISTING_ITEM_PENDING_ID,
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);

const expectedResult = {
statusCode: 400,
body: { message: "This ID already exists" },
};
expect(result).toStrictEqual(expectedResult);
});
it("should update an ID on a package", async () => {
const noActionevent = {
body: JSON.stringify({
packageId: CAPITATED_INITIAL_ITEM_ID,
action: "update-id",
changeReason: "Nunya",
updatedId: CAPITATED_INITIAL_NEW_ITEM_ID,
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);

const expectedResult = {
statusCode: 200,
body: {
message: `The ID of package ${CAPITATED_INITIAL_ITEM_ID} has been updated to ${CAPITATED_INITIAL_NEW_ITEM_ID}.`,
},
};
expect(result).toStrictEqual(expectedResult);
});
it("should fail to update a package ID with no topic name", async () => {
process.env.topicName = "";
const noActionevent = {
body: JSON.stringify({
packageId: CAPITATED_INITIAL_ITEM_ID,
action: "update-id",
changeReason: "Nunya",
updatedId: "SS-1235.R00.00",
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);
const expectedResult = {
statusCode: 500,
body: { message: "Topic name is not defined" },
};
expect(result).toStrictEqual(expectedResult);
});

it("should fail to update a package ID with bad new id format", async () => {
const noActionevent = {
body: JSON.stringify({
packageId: CAPITATED_INITIAL_ITEM_ID,
action: "update-id",
changeReason: "Nunya",
updatedId: "SS-120",
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);
const expectedResult =
"The Initial Waiver Number must be in the format of SS-####.R00.00 or SS-#####.R00.00";

expect(JSON.parse(result?.body)[0].message).toStrictEqual(expectedResult);
});
it("should fail to update a package with bad existing id format", async () => {
const noActionevent = {
body: JSON.stringify({
packageId: WEIRD_ID,
action: "update-id",
changeReason: "Nunya",
updatedId: "SS-120",
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);
const expectedResult = "Cannot read properties of undefined (reading 'baseSchema')";
expect(result?.statusCode).toStrictEqual(500);
expect(result?.body.message).toStrictEqual(expectedResult);
});
it("should fail to update a package - no topic name ", async () => {
process.env.topicName = "";
const noActionevent = {
body: JSON.stringify({
packageId: WEIRD_ID,
action: "update-values",
changeReason: "Nunya",
updatedFields: {},
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);
const expectedResult = {
statusCode: 500,
body: { message: "Topic name is not defined" },
};
expect(result).toStrictEqual(expectedResult);
});
it("should fail to update a package - No valid fields ", async () => {
const noActionevent = {
body: JSON.stringify({
packageId: WEIRD_ID,
action: "update-values",
changeReason: "Nunya",
updatedFields: { badfield: "nothing" },
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);
const expectedResult = {
statusCode: 400,
body: { message: "Cannot update invalid field(s): badfield" },
};
expect(result).toStrictEqual(expectedResult);
});
it("should fail to update a package - Id can not be updated ", async () => {
const noActionevent = {
body: JSON.stringify({
packageId: WEIRD_ID,
action: "update-values",
changeReason: "Nunya",
updatedFields: { id: "cant update ID here" },
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);
const expectedResult = {
statusCode: 400,
body: { message: "ID is not a valid field to update" },
};
expect(result).toStrictEqual(expectedResult);
});
it("should fail to update a package - nothing to update ", async () => {
const noActionevent = {
body: JSON.stringify({
packageId: CAPITATED_INITIAL_ITEM_ID,
action: "update-values",
changeReason: "Nunya",
updatedFields: { state: "TX" },
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);
const expectedResult = {
statusCode: 200,
body: { message: `state has been updated in package ${CAPITATED_INITIAL_ITEM_ID}.` },
};
expect(result).toStrictEqual(expectedResult);
});
});
41 changes: 18 additions & 23 deletions lib/lambda/update/updatePackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const sendUpdateIdMessage = async ({
...remainingFields
} = currentPackage._source;

if (!updatedId) {
if (updatedId === currentPackage._id) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be if (!updatedId || updatedId === currentPackage._id) {?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its impossible for updatedId to not exist. Its set to default to packageId if one is not provided on line 192.

return response({
statusCode: 400,
body: { message: "New ID required to update package" },
Expand All @@ -111,7 +111,7 @@ const sendUpdateIdMessage = async ({

// check if a package with this new ID already exists
const packageExists = await getPackage(updatedId);
if (packageExists) {
if (packageExists?.found) {
return response({
statusCode: 400,
body: { message: "This ID already exists" },
Expand All @@ -121,13 +121,6 @@ const sendUpdateIdMessage = async ({
const packageEvent = await getPackageType(currentPackage._id);
const packageSubmissionTypeSchema = events[packageEvent as keyof typeof events].baseSchema;

if (!packageSubmissionTypeSchema) {
return response({
statusCode: 500,
body: { message: "Could not validate the ID of this type of package." },
});
}

const idSchema = packageSubmissionTypeSchema.shape.id;
const parsedId = idSchema.safeParse(updatedId);

Expand Down Expand Up @@ -178,7 +171,21 @@ export const handler = async (event: APIGatewayEvent) => {
const parseEventBody = (body: unknown) => {
return updatePackageEventBodySchema.parse(typeof body === "string" ? JSON.parse(body) : body);
};

let body = {
packageId: "",
action: "",
};
if (typeof event.body === "string") {
body = JSON.parse(event.body);
} else {
body = event.body;
}
if (!body.packageId || !body.action) {
return response({
statusCode: 400,
body: { message: "Package ID and action are required" },
});
}
const {
packageId,
action,
Expand All @@ -187,16 +194,8 @@ export const handler = async (event: APIGatewayEvent) => {
changeReason,
} = parseEventBody(event.body);

if (!packageId || !action) {
return response({
statusCode: 400,
body: { message: "Package ID and action are required" },
});
}

const currentPackage = await getPackage(packageId);

if (!currentPackage) {
if (!currentPackage || currentPackage.found == false) {
return response({
statusCode: 404,
body: { message: "No record found for the given id" },
Expand All @@ -218,10 +217,6 @@ export const handler = async (event: APIGatewayEvent) => {
changeReason,
});
}
return response({
statusCode: 400,
body: { message: "Could not update package." },
});
} catch (err) {
console.error("Error has occured modifying package:", err);
return response({
Expand Down
Loading
Loading