Skip to content
This repository has been archived by the owner on Jan 28, 2024. It is now read-only.

Commit

Permalink
test: use body alias for injections
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs committed May 27, 2023
1 parent 89e1492 commit 8ba9f10
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 128 deletions.
8 changes: 4 additions & 4 deletions src/plugins/clean-object/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@ describe("Clean-Object plugin", () => {
headers: {
"content-type": "application/json",
},
payload: {
body: {
test1: undefined,
test2: null,
test3: [{ testobj: null }],
test4: 2,
},
});

expect(JSON.parse(response.payload)).toEqual({ test3: [{}], test4: 2 });
expect(JSON.parse(response.body)).toEqual({ test3: [{}], test4: 2 });
expect(response.statusCode).toBe(200);
});

it("Returns empty object if request payload not passed to plugin function", async () => {
it("Returns empty object if request body not passed to plugin function", async () => {
const response = await server.inject({
method: "PUT",
url: "/empty",
});

expect(JSON.parse(response.payload)).toEqual({});
expect(JSON.parse(response.body)).toEqual({});
expect(response.statusCode).toBe(200);
});
});
4 changes: 2 additions & 2 deletions src/plugins/convert-date-param-operator/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ describe("Convert-Date-Param-Operator plugin", () => {
headers: {
"content-type": "text/plain",
},
payload: `${key}`,
body: `${key}`,
})
.then((response) => {
// eslint-disable-next-line security/detect-object-injection
expect(response.payload).toEqual(values[key]);
expect(response.body).toEqual(values[key]);
expect(response.statusCode).toBe(200);

return response.statusCode;
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/db/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("DB plugin", () => {
url: "/",
});

expect(JSON.parse(response.payload).recordsets[0][0].example).toBe(
expect(JSON.parse(response.body).recordsets[0][0].example).toBe(
"test"
);
expect(response.statusCode).toBe(200);
Expand Down Expand Up @@ -77,7 +77,7 @@ describe("DB plugin", () => {
url: "/",
});

expect(JSON.parse(response.payload)[0].example).toBe("test");
expect(JSON.parse(response.body)[0].example).toBe("test");
expect(response.statusCode).toBe(200);
});
});
Expand Down
74 changes: 37 additions & 37 deletions src/routes/admin/access/bearer-token/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const testDate2 = "2022-11-07";

const testId = randomUUID();

const testReqPayload = {
const testReqBody = {
name: "Test Clinical System Supplier Product",
email: "[email protected]",
expires: "2022-11-07",
Expand All @@ -23,10 +23,10 @@ const testReqPayload = {

const testDbResult = {
id: testId,
name: testReqPayload.name,
email: testReqPayload.email,
name: testReqBody.name,
email: testReqBody.email,
hash: "testhash",
expires: testReqPayload.expires,
expires: testReqBody.expires,
created: "2022-01-18T14:07:48.190Z",
last_updated: "2022-01-18T14:07:48.190Z",
};
Expand All @@ -38,7 +38,7 @@ const testResRecord = {
email: testDbResult.email,
expires: testDbResult.expires,
hash: testDbResult.hash,
scopes: testReqPayload.scopes,
scopes: testReqBody.scopes,
},
meta: {
created: testDbResult.created,
Expand Down Expand Up @@ -87,7 +87,7 @@ describe("Access route", () => {
{
...testDbResult,
scopes: JSON.stringify(
testReqPayload.scopes
testReqBody.scopes
),
},
],
Expand All @@ -105,7 +105,7 @@ describe("Access route", () => {
{
...testDbResult,
scopes: JSON.stringify(
testReqPayload.scopes
testReqBody.scopes
),
},
],
Expand All @@ -122,7 +122,7 @@ describe("Access route", () => {
{
id: testId,
scopes: JSON.stringify(
testReqPayload.scopes
testReqBody.scopes
),
},
],
Expand Down Expand Up @@ -159,7 +159,7 @@ describe("Access route", () => {
rows: [
{
...testDbResult,
scopes: testReqPayload.scopes,
scopes: testReqBody.scopes,
},
],
},
Expand All @@ -174,7 +174,7 @@ describe("Access route", () => {
rows: [
{
...testDbResult,
scopes: testReqPayload.scopes,
scopes: testReqBody.scopes,
},
],
},
Expand All @@ -188,7 +188,7 @@ describe("Access route", () => {
rows: [
{
id: testId,
scopes: testReqPayload.scopes,
scopes: testReqBody.scopes,
},
],
},
Expand Down Expand Up @@ -236,7 +236,7 @@ describe("Access route", () => {
});

expect(mockQueryFn).toHaveBeenCalledTimes(1);
expect(response.payload).toBe("");
expect(response.body).toBe("");
expect(response.statusCode).toBe(204);
});

Expand All @@ -255,7 +255,7 @@ describe("Access route", () => {
});

expect(mockQueryFn).toHaveBeenCalledTimes(1);
expect(JSON.parse(response.payload)).toEqual({
expect(JSON.parse(response.body)).toEqual({
error: "Not Found",
message:
"Bearer token record does not exist or has already been deleted",
Expand All @@ -279,7 +279,7 @@ describe("Access route", () => {
});

expect(mockQueryFn).toHaveBeenCalledTimes(1);
expect(JSON.parse(response.payload)).toEqual({
expect(JSON.parse(response.body)).toEqual({
error: "Internal Server Error",
message: "Error: Failed to connect to DB",
statusCode: 500,
Expand All @@ -304,7 +304,7 @@ describe("Access route", () => {
});

expect(mockQueryFn).toHaveBeenCalledTimes(1);
expect(JSON.parse(response.payload)).toEqual(testResRecord);
expect(JSON.parse(response.body)).toEqual(testResRecord);
expect(response.statusCode).toBe(200);
});

Expand All @@ -323,7 +323,7 @@ describe("Access route", () => {
});

expect(mockQueryFn).toHaveBeenCalledTimes(1);
expect(JSON.parse(response.payload)).toEqual({
expect(JSON.parse(response.body)).toEqual({
error: "Not Found",
message: "Bearer token record not found",
statusCode: 404,
Expand All @@ -346,7 +346,7 @@ describe("Access route", () => {
});

expect(mockQueryFn).toHaveBeenCalledTimes(1);
expect(JSON.parse(response.payload)).toEqual({
expect(JSON.parse(response.body)).toEqual({
error: "Internal Server Error",
message: "Error: Failed to connect to DB",
statusCode: 500,
Expand Down Expand Up @@ -381,7 +381,7 @@ describe("Access route", () => {
});

expect(mockQueryFn).toHaveBeenCalledTimes(1);
expect(JSON.parse(response.payload)).toEqual(expSearchResult);
expect(JSON.parse(response.body)).toEqual(expSearchResult);
expect(response.statusCode).toBe(200);
});

Expand All @@ -406,7 +406,7 @@ describe("Access route", () => {
});

expect(mockQueryFn).toHaveBeenCalledTimes(1);
expect(JSON.parse(response.payload)).toEqual(expSearchResult);
expect(JSON.parse(response.body)).toEqual(expSearchResult);
expect(response.statusCode).toBe(200);
});

Expand All @@ -430,7 +430,7 @@ describe("Access route", () => {
});

expect(mockQueryFn).toHaveBeenCalledTimes(1);
expect(JSON.parse(response.payload)).toEqual(expSearchResult);
expect(JSON.parse(response.body)).toEqual(expSearchResult);
expect(response.statusCode).toBe(200);
});

Expand All @@ -450,7 +450,7 @@ describe("Access route", () => {
});

expect(mockQueryFn).toHaveBeenCalledTimes(1);
expect(JSON.parse(response.payload)).toEqual({
expect(JSON.parse(response.body)).toEqual({
link: expect.any(String),
meta: {
pagination: {
Expand Down Expand Up @@ -480,7 +480,7 @@ describe("Access route", () => {
});

expect(mockQueryFn).toHaveBeenCalledTimes(0);
expect(JSON.parse(response.payload)).toEqual({
expect(JSON.parse(response.body)).toEqual({
error: "Bad Request",
message: "No valid query string parameters provided",
statusCode: 400,
Expand All @@ -506,7 +506,7 @@ describe("Access route", () => {
});

expect(mockQueryFn).toHaveBeenCalledTimes(1);
expect(JSON.parse(response.payload)).toEqual({
expect(JSON.parse(response.body)).toEqual({
error: "Internal Server Error",
message: "Error: Failed to connect to DB",
statusCode: 500,
Expand All @@ -531,15 +531,15 @@ describe("Access route", () => {
headers: {
"content-type": "application/json",
},
payload: testReqPayload,
body: testReqBody,
});

expect(mockQueryFn).toHaveBeenCalledTimes(1);
expect(JSON.parse(response.payload)).toEqual({
expect(JSON.parse(response.body)).toEqual({
id: testId,
access: {
token: expect.stringMatching(/^ydhcc_/i),
scopes: testReqPayload.scopes,
scopes: testReqBody.scopes,
},
});
expect(response.headers).toMatchObject({
Expand All @@ -559,8 +559,8 @@ describe("Access route", () => {
query: mockQueryFn,
};

const trimmedTestReqPayload = {
...testReqPayload,
const trimmedtestReqBody = {
...testReqBody,
email: undefined,
expires: undefined,
};
Expand All @@ -571,15 +571,15 @@ describe("Access route", () => {
headers: {
"content-type": "application/json",
},
payload: trimmedTestReqPayload,
body: trimmedtestReqBody,
});

expect(mockQueryFn).toHaveBeenCalledTimes(1);
expect(JSON.parse(response.payload)).toEqual({
expect(JSON.parse(response.body)).toEqual({
id: testId,
access: {
token: expect.stringMatching(/^ydhcc_/i),
scopes: testReqPayload.scopes,
scopes: testReqBody.scopes,
},
});
expect(response.statusCode).toBe(201);
Expand All @@ -600,11 +600,11 @@ describe("Access route", () => {
headers: {
"content-type": "application/javascript",
},
payload: testReqPayload,
body: testReqBody,
});

expect(mockQueryFn).toHaveBeenCalledTimes(0);
expect(JSON.parse(response.payload)).toEqual({
expect(JSON.parse(response.body)).toEqual({
error: "Unsupported Media Type",
message: "Unsupported Media Type: application/javascript",
statusCode: 415,
Expand All @@ -627,11 +627,11 @@ describe("Access route", () => {
headers: {
"content-type": "application/json",
},
payload: testReqPayload,
body: testReqBody,
});

expect(mockQueryFn).toHaveBeenCalledTimes(1);
expect(JSON.parse(response.payload)).toEqual({
expect(JSON.parse(response.body)).toEqual({
error: "Internal Server Error",
message: "Error",
statusCode: 500,
Expand All @@ -654,11 +654,11 @@ describe("Access route", () => {
headers: {
"content-type": "application/json",
},
payload: testReqPayload,
body: testReqBody,
});

expect(mockQueryFn).toHaveBeenCalledTimes(1);
expect(JSON.parse(response.payload)).toEqual({
expect(JSON.parse(response.body)).toEqual({
error: "Internal Server Error",
message: "Error: Failed to connect to DB",
statusCode: 500,
Expand Down
4 changes: 2 additions & 2 deletions src/routes/admin/healthcheck/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe("Healthcheck route", () => {
},
});

expect(response.payload).toBe("ok");
expect(response.body).toBe("ok");
expect(response.statusCode).toBe(200);
});

Expand All @@ -48,7 +48,7 @@ describe("Healthcheck route", () => {
},
});

expect(JSON.parse(response.payload)).toEqual({
expect(JSON.parse(response.body)).toEqual({
error: "Not Acceptable",
message: "Not Acceptable",
statusCode: 406,
Expand Down
Loading

0 comments on commit 8ba9f10

Please sign in to comment.