Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaitasaini committed Jan 22, 2025
1 parent b4590a8 commit 3efba89
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
92 changes: 92 additions & 0 deletions services/app-api/handlers/reports/submit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { StatusCodes } from "../../libs/response-lib";
import { proxyEvent } from "../../testing/proxyEvent";
import { APIGatewayProxyEvent, UserRoles } from "../../types/types";
import { canWriteState } from "../../utils/authorization";
import { submitReport } from "./submit";

jest.mock("../../utils/authentication", () => ({
authenticatedUser: jest.fn().mockResolvedValue({
role: UserRoles.STATE_USER,
state: "PA",
}),
}));

jest.mock("../../utils/authorization", () => ({
canWriteState: jest.fn().mockReturnValue(true),
}));

jest.mock("../../storage/reports", () => ({
putReport: () => jest.fn(),
}));

const reportObj = { type: "QMS", state: "PA", id: "QMSPA123" };
const report = JSON.stringify(reportObj);

const testEvent: APIGatewayProxyEvent = {
...proxyEvent,
pathParameters: { reportType: "QMS", state: "PA", id: "QMSPA123" },
headers: { "cognito-identity-id": "test" },
body: report,
};

describe("Test submit report handler", () => {
beforeEach(() => {
jest.clearAllMocks();
});

test("Test missing path params", async () => {
const badTestEvent = {
...proxyEvent,
pathParameters: {},
} as APIGatewayProxyEvent;
const res = await submitReport(badTestEvent);
expect(res.statusCode).toBe(StatusCodes.BadRequest);
});

it("should return 403 if user is not authorized", async () => {
(canWriteState as jest.Mock).mockReturnValueOnce(false);
const response = await submitReport(testEvent);
expect(response.statusCode).toBe(StatusCodes.Forbidden);
});

test("Test missing body", async () => {
const emptyBodyEvent = {
...proxyEvent,
pathParameters: { reportType: "QMS", state: "PA", id: "QMSPA123" },
body: null,
} as APIGatewayProxyEvent;
const res = await submitReport(emptyBodyEvent);
expect(res.statusCode).toBe(StatusCodes.BadRequest);
});

test("Test body + param mismatch", async () => {
const badType = {
...proxyEvent,
pathParameters: { reportType: "ZZ", state: "PA", id: "QMSPA123" },
body: report,
} as APIGatewayProxyEvent;
const badState = {
...proxyEvent,
pathParameters: { reportType: "QMS", state: "PA", id: "QMSPA123" },
body: JSON.stringify({ ...reportObj, state: "OR" }),
} as APIGatewayProxyEvent;
const badId = {
...proxyEvent,
pathParameters: { reportType: "QMS", state: "PA", id: "ZZOR1234" },
body: report,
} as APIGatewayProxyEvent;

const resType = await submitReport(badType);
expect(resType.statusCode).toBe(StatusCodes.BadRequest);
const resState = await submitReport(badState);
expect(resState.statusCode).toBe(StatusCodes.BadRequest);
const resId = await submitReport(badId);
expect(resId.statusCode).toBe(StatusCodes.BadRequest);
});

test("Test Successful submit", async () => {
const res = await submitReport(testEvent);

expect(res.statusCode).toBe(StatusCodes.Ok);
});
});
4 changes: 4 additions & 0 deletions services/app-api/handlers/reports/submit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export const submitReport = handler(parseReportParameters, async (request) => {
return forbidden(error.UNAUTHORIZED);
}

if (!request?.body) {
return badRequest("Invalid request");
}

// get the report that's being submitted
const report = request.body as Report;
if (
Expand Down

0 comments on commit 3efba89

Please sign in to comment.