-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into less-verbiage
- Loading branch information
Showing
12 changed files
with
216 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,8 +32,8 @@ if ! which yarn > /dev/null ; then | |
fi | ||
|
||
# check serverless is installed globally. | ||
if ! which serverless > /dev/null ; then | ||
echo "installing serverless globally" | ||
if ! which serverless > /dev/null || [[ "$(serverless --version | cut -d'.' -f1)" != "4" ]]; then | ||
echo "installing serverless v4 globally" | ||
yarn global add [email protected] | ||
fi | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { handler } from "../../libs/handler-lib"; | ||
import { parseReportParameters } from "../../libs/param-lib"; | ||
import { badRequest, forbidden, ok } from "../../libs/response-lib"; | ||
import { putReport } from "../../storage/reports"; | ||
import { Report, ReportStatus } from "../../types/reports"; | ||
import { canWriteState } from "../../utils/authorization"; | ||
import { error } from "../../utils/constants"; | ||
|
||
export const submitReport = handler(parseReportParameters, async (request) => { | ||
const { reportType, state, id } = request.parameters; | ||
const user = request.user; | ||
|
||
if (!canWriteState(user, state)) { | ||
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 ( | ||
reportType !== report.type || | ||
state !== report.state || | ||
id !== report.id | ||
) { | ||
return badRequest("Invalid request"); | ||
} | ||
|
||
// collect the user info of the submitter | ||
report.status = ReportStatus.SUBMITTED; | ||
report.lastEdited = Date.now(); | ||
report.lastEditedBy = user.fullName; | ||
|
||
// leave space for validating that the report can be submitted | ||
|
||
// save the report that's being submitted (with the new information on top of it) | ||
await putReport(report); | ||
|
||
return ok(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters