Skip to content

Commit

Permalink
Merge pull request #4384 from CMSgov/val
Browse files Browse the repository at this point in the history
Val -> Prod
  • Loading branch information
Nick Summers committed Oct 12, 2022
2 parents 8b17b70 + 06e3c58 commit a9003a2
Show file tree
Hide file tree
Showing 112 changed files with 4,900 additions and 3,498 deletions.
13 changes: 1 addition & 12 deletions services/app-api/handlers/banners/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ import { proxyEvent } from "../../utils/testing/proxyEvent";
import { StatusCodes } from "../../utils/types/types";
import error from "../../utils/constants/constants";

jest.mock("../../utils/dynamo/dynamodb-lib", () => ({
__esModule: true,
default: {
put: jest.fn(),
},
}));

jest.mock("../../utils/auth/authorization", () => ({
isAuthorized: jest.fn().mockReturnValue(true),
hasPermissions: jest.fn().mockReturnValueOnce(false).mockReturnValue(true),
Expand All @@ -36,10 +29,6 @@ const testEventWithInvalidData: APIGatewayProxyEvent = {
};

describe("Test createBanner API method", () => {
beforeEach(() => {
process.env["BANNER_TABLE_NAME"] = "fakeBannerTable";
});

test("Test unauthorized banner creation throws 403 error", async () => {
const res = await createBanner(testEvent, null);
expect(res.statusCode).toBe(403);
Expand All @@ -48,7 +37,7 @@ describe("Test createBanner API method", () => {

test("Test Successful Run of Banner Creation", async () => {
const res = await createBanner(testEvent, null);
expect(res.statusCode).toBe(StatusCodes.SUCCESS);
expect(res.statusCode).toBe(StatusCodes.CREATED);
expect(res.body).toContain("test banner");
expect(res.body).toContain("test description");
});
Expand Down
2 changes: 1 addition & 1 deletion services/app-api/handlers/banners/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const createBanner = handler(async (event, _context) => {
},
};
await dynamoDb.put(params);
return { status: StatusCodes.SUCCESS, body: params };
return { status: StatusCodes.CREATED, body: params };
}
}
});
12 changes: 0 additions & 12 deletions services/app-api/handlers/banners/delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ import { proxyEvent } from "../../utils/testing/proxyEvent";
import { StatusCodes } from "../../utils/types/types";
import error from "../../utils/constants/constants";

jest.mock("../../utils/dynamo/dynamodb-lib", () => ({
__esModule: true,
default: {
delete: jest.fn(),
},
}));

jest.mock("../../utils/auth/authorization", () => ({
isAuthorized: jest.fn().mockReturnValue(true),
hasPermissions: jest.fn().mockReturnValueOnce(false).mockReturnValue(true),
Expand All @@ -28,10 +21,6 @@ const testEvent: APIGatewayProxyEvent = {
};

describe("Test deleteBanner API method", () => {
beforeEach(() => {
process.env["BANNER_TABLE_NAME"] = "fakeBannerTable";
});

test("Test not authorized to delete banner throws 403 error", async () => {
const res = await deleteBanner(testEvent, null);

Expand All @@ -43,7 +32,6 @@ describe("Test deleteBanner API method", () => {
const res = await deleteBanner(testEvent, null);

expect(res.statusCode).toBe(StatusCodes.SUCCESS);
expect(res.body).toContain("fakeBannerTable");
expect(res.body).toContain("testKey");
});

Expand Down
30 changes: 11 additions & 19 deletions services/app-api/handlers/banners/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,10 @@ import { APIGatewayProxyEvent } from "aws-lambda";
import { proxyEvent } from "../../utils/testing/proxyEvent";
import { StatusCodes } from "../../utils/types/types";
import error from "../../utils/constants/constants";

jest.mock("../../utils/dynamo/dynamodb-lib", () => ({
__esModule: true,
default: {
get: jest.fn().mockReturnValue({
Item: {
createdAt: 1654198665696,
endDate: 1657252799000,
lastAltered: 1654198665696,
description: "testDesc",
title: "testTitle",
key: "admin-banner-id",
startDate: 1641013200000,
},
}),
},
}));
import {
mockBannerResponse,
mockDocumentClient,
} from "../../utils/testing/setupJest";

jest.mock("../../utils/auth/authorization", () => ({
isAuthorized: jest.fn().mockReturnValue(true),
Expand All @@ -38,11 +25,16 @@ const testEvent: APIGatewayProxyEvent = {
};

describe("Test fetchBanner API method", () => {
beforeEach(() => {
process.env["BANNER_TABLE_NAME"] = "fakeBannerTable";
test("Test Report not found Fetch", async () => {
mockDocumentClient.get.promise.mockReturnValueOnce({ Item: undefined });
const res = await fetchBanner(testEvent, null);
expect(res.statusCode).toBe(StatusCodes.NOT_FOUND);
});

test("Test Successful Banner Fetch", async () => {
mockDocumentClient.get.promise.mockReturnValueOnce({
Item: mockBannerResponse,
});
const res = await fetchBanner(testEvent, null);

expect(res.statusCode).toBe(StatusCodes.SUCCESS);
Expand Down
9 changes: 7 additions & 2 deletions services/app-api/handlers/banners/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export const fetchBanner = handler(async (event, _context) => {
key: event?.pathParameters?.bannerId!,
},
};
const queryResponse = await dynamoDb.get(params);
return { status: StatusCodes.SUCCESS, body: queryResponse };
const response = await dynamoDb.get(params);

let status = StatusCodes.SUCCESS;
if (!response?.Item) {
status = StatusCodes.NOT_FOUND;
}
return { status: status, body: response };
});
9 changes: 1 addition & 8 deletions services/app-api/handlers/reports/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ import { StatusCodes } from "../../utils/types/types";
import { mockReport } from "../../utils/testing/setupJest";
import error from "../../utils/constants/constants";

jest.mock("../../utils/dynamo/dynamodb-lib", () => ({
__esModule: true,
default: {
put: jest.fn(),
},
}));

jest.mock("../../utils/auth/authorization", () => ({
isAuthorized: jest.fn().mockResolvedValue(true),
hasPermissions: jest.fn().mockReturnValueOnce(false).mockReturnValue(true),
Expand Down Expand Up @@ -55,7 +48,7 @@ describe("Test createReport API method", () => {
const res = await createReport(creationEvent, null);

const body = JSON.parse(res.body);
expect(res.statusCode).toBe(StatusCodes.SUCCESS);
expect(res.statusCode).toBe(StatusCodes.CREATED);
expect(body.status).toContain("Not started");
});

Expand Down
2 changes: 1 addition & 1 deletion services/app-api/handlers/reports/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const createReport = handler(async (event, _context) => {
};
await dynamoDb.put(reportParams);
return {
status: StatusCodes.SUCCESS,
status: StatusCodes.CREATED,
body: { ...reportParams.Item },
};
} else throw new Error(error.MISSING_DATA);
Expand Down
19 changes: 11 additions & 8 deletions services/app-api/handlers/reports/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@ import { APIGatewayProxyEvent } from "aws-lambda";
import { proxyEvent } from "../../utils/testing/proxyEvent";
import { StatusCodes } from "../../utils/types/types";
import error from "../../utils/constants/constants";
import { mockReport } from "../../utils/testing/setupJest";

jest.mock("../../utils/dynamo/dynamodb-lib", () => ({
__esModule: true,
default: {
query: jest.fn(() => ({ Items: [mockReport] })),
},
}));
import { mockDocumentClient, mockReport } from "../../utils/testing/setupJest";

jest.mock("../../utils/auth/authorization", () => ({
isAuthorized: jest.fn().mockReturnValue(true),
Expand All @@ -35,7 +28,14 @@ const testReadEventByState: APIGatewayProxyEvent = {
};

describe("Test fetchReport API method", () => {
test("Test Report not found Fetch", async () => {
mockDocumentClient.get.promise.mockReturnValueOnce({ Item: undefined });
const res = await fetchReport(testReadEvent, null);
expect(res.statusCode).toBe(StatusCodes.NOT_FOUND);
});

test("Test Successful Report Fetch", async () => {
mockDocumentClient.get.promise.mockReturnValueOnce({ Item: mockReport });
const res = await fetchReport(testReadEvent, null);
expect(res.statusCode).toBe(StatusCodes.SUCCESS);
const body = JSON.parse(res.body);
Expand Down Expand Up @@ -66,6 +66,9 @@ describe("Test fetchReport API method", () => {

describe("Test fetchReportsByState API method", () => {
test("Test successful call", async () => {
mockDocumentClient.query.promise.mockReturnValueOnce({
Items: [mockReport],
});
const res = await fetchReportsByState(testReadEventByState, null);
expect(res.statusCode).toBe(StatusCodes.SUCCESS);
const body = JSON.parse(res.body);
Expand Down
26 changes: 13 additions & 13 deletions services/app-api/handlers/reports/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@ export const fetchReport = handler(async (event, _context) => {
if (!event?.pathParameters?.state! || !event?.pathParameters?.id!) {
throw new Error(error.NO_KEY);
}
const queryParams = {
const params = {
TableName: process.env.MCPAR_REPORT_TABLE_NAME!,
KeyConditionExpression: "#state = :state AND #id = :id",
ExpressionAttributeValues: {
":state": event.pathParameters.state,
":id": event.pathParameters.id,
},
ExpressionAttributeNames: {
"#state": "state",
"#id": "id",
Key: {
state: event.pathParameters.state,
id: event.pathParameters.id,
},
};
const reportQueryResponse = await dynamoDb.query(queryParams);
const responseBody = reportQueryResponse.Items![0] ?? {};
const response = await dynamoDb.get(params);

let status = StatusCodes.SUCCESS;
if (!response?.Item) {
status = StatusCodes.NOT_FOUND;
}
return {
status: StatusCodes.SUCCESS,
body: responseBody,
status: status,
body: response.Item,
};
});

Expand All @@ -42,6 +41,7 @@ export const fetchReportsByState = handler(async (event, _context) => {
},
};
const reportQueryResponse = await dynamoDb.query(queryParams);

return {
status: StatusCodes.SUCCESS,
body: reportQueryResponse.Items,
Expand Down
70 changes: 30 additions & 40 deletions services/app-api/handlers/reports/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ import { StatusCodes } from "../../utils/types/types";
import { mockReport } from "../../utils/testing/setupJest";
import error from "../../utils/constants/constants";

jest.mock("../../utils/dynamo/dynamodb-lib", () => ({
__esModule: true,
default: {
put: jest.fn(),
},
}));

jest.mock("../../utils/auth/authorization", () => ({
isAuthorized: jest.fn().mockResolvedValue(true),
hasPermissions: jest.fn().mockReturnValueOnce(false).mockReturnValue(true),
Expand Down Expand Up @@ -60,11 +53,6 @@ const updateEventWithInvalidData: APIGatewayProxyEvent = {
body: `{"programName":{}}`,
};

const submissionEventWithInvalidData: APIGatewayProxyEvent = {
...mockProxyEvent,
body: ``,
};

describe("Test updateReport API method", () => {
test("Test unauthorized report status creation throws 403 error", async () => {
const res = await updateReport(updateEvent, null);
Expand All @@ -90,7 +78,20 @@ describe("Test updateReport API method", () => {
expect(body.fieldData.text).toContain("text-input");
});

test("Test attempted report update with invalid data fails", async () => {
test("Test report update submission succeeds", async () => {
mockedFetchReport.mockResolvedValue({
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "string",
"Access-Control-Allow-Credentials": true,
},
body: JSON.stringify(mockReport),
});
const response = await updateReport(submissionEvent, null);
expect(response.statusCode).toBe(StatusCodes.SUCCESS);
});

test("Test attempted report update with invalid data throws 400", async () => {
mockedFetchReport.mockResolvedValue({
statusCode: 200,
headers: {
Expand All @@ -100,7 +101,22 @@ describe("Test updateReport API method", () => {
body: JSON.stringify(mockReport),
});
const res = await updateReport(updateEventWithInvalidData, null);
expect(res.statusCode).toBe(StatusCodes.SERVER_ERROR);
expect(res.statusCode).toBe(StatusCodes.BAD_REQUEST);
expect(res.body).toContain(error.MISSING_DATA);
});

test("Test attempted report update with no existing record throws 404", async () => {
mockedFetchReport.mockResolvedValue({
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "string",
"Access-Control-Allow-Credentials": true,
},
body: undefined!,
});
const res = await updateReport(updateEventWithInvalidData, null);
expect(res.statusCode).toBe(StatusCodes.NOT_FOUND);
expect(res.body).toContain(error.NO_MATCHING_RECORD);
});

test("Test reportKey not provided throws 500 error", async () => {
Expand All @@ -124,30 +140,4 @@ describe("Test updateReport API method", () => {
expect(res.statusCode).toBe(500);
expect(res.body).toContain(error.NO_KEY);
});

test("Test report update submission succeeds", async () => {
mockedFetchReport.mockResolvedValue({
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "string",
"Access-Control-Allow-Credentials": true,
},
body: JSON.stringify(mockReport),
});
const response = await updateReport(submissionEvent, null);
expect(response.statusCode).toBe(StatusCodes.SUCCESS);
});

test("Report update submission fails with invalid data", async () => {
mockedFetchReport.mockResolvedValue({
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "string",
"Access-Control-Allow-Credentials": true,
},
body: JSON.stringify(mockReport),
});
const response = await updateReport(submissionEventWithInvalidData, null);
expect(response.statusCode).toBe(StatusCodes.SERVER_ERROR);
});
});
21 changes: 15 additions & 6 deletions services/app-api/handlers/reports/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const updateReport = handler(async (event, context) => {
const getCurrentReport = await fetchReport(reportEvent, context);
const { fieldData: unvalidatedFieldData } = unvalidatedPayload;

let status, body;
if (getCurrentReport?.body) {
if (unvalidatedFieldData) {
// validate report metadata
Expand Down Expand Up @@ -59,10 +60,18 @@ export const updateReport = handler(async (event, context) => {
},
};
await dynamoDb.put(reportParams);
return {
status: StatusCodes.SUCCESS,
body: { ...reportParams.Item },
};
} else throw new Error(error.MISSING_DATA);
} else throw new Error(error.NO_MATCHING_RECORD);
status = StatusCodes.SUCCESS;
body = reportParams.Item;
} else {
status = StatusCodes.BAD_REQUEST;
body = error.MISSING_DATA;
}
} else {
status = StatusCodes.NOT_FOUND;
body = error.NO_MATCHING_RECORD;
}
return {
status: status,
body: body,
};
});
Loading

0 comments on commit a9003a2

Please sign in to comment.