Skip to content

Commit

Permalink
BRS-950: Updated unit tests (#108)
Browse files Browse the repository at this point in the history
* BRS-950: Updated unit tests

* BRS-950: added back removed park tests, added IS_TEST flag and updated permissionUtils, updated some tests.

* BRS-950: Update dynamoUtil test, update export test

* BRS-950: Fixing last tests
  • Loading branch information
wilwong89 authored Jan 16, 2023
1 parent 00caf3c commit 1c89556
Show file tree
Hide file tree
Showing 8 changed files with 538 additions and 70 deletions.
122 changes: 122 additions & 0 deletions __tests__/dynamoUtil.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
const AWS = require("aws-sdk");
const { DocumentClient } = require("aws-sdk/clients/dynamodb");
const { REGION, ENDPOINT, TABLE_NAME } = require("./global/settings");
const { PARKSLIST, SUBAREAS, SUBAREA_ENTRIES} = require("./global/data.json");

const utils = require("../lambda/dynamoUtil");

async function setupDb() {
new AWS.DynamoDB({
region: REGION,
endpoint: ENDPOINT,
});
docClient = new DocumentClient({
region: REGION,
endpoint: ENDPOINT,
convertEmptyValues: true,
});

for (const park of PARKSLIST) {
await docClient
.put({
TableName: TABLE_NAME,
Item: park,
})
.promise();
}

for (const subarea of SUBAREAS) {
await docClient
.put({
TableName: TABLE_NAME,
Item: subarea,
})
.promise();
}

for (const subEntry of SUBAREA_ENTRIES) {
await docClient
.put({
TableName: TABLE_NAME,
Item: subEntry,
})
.promise();
}
}

describe("Pass Succeeds", () => {
beforeAll(async () => {
return await setupDb();
});

test("dynamoUtil - runScan", async () => {
let queryObj = {
TableName: TABLE_NAME
};
queryObj.FilterExpression = "pk = :pk";
queryObj.ExpressionAttributeValues = {};
queryObj.ExpressionAttributeValues[':pk'] = { S: `park` };

const result = await utils.runScan(queryObj, null)

expect(result).toEqual(
expect.arrayContaining([
expect.objectContaining({
parkName: PARKSLIST[0].parkName
}),
expect.objectContaining({
parkName: PARKSLIST[1].parkName
}),
])
);
});

test("dynamoUtil - getParks", async () => {
const result = await utils.getParks()

expect(result).toEqual(
expect.arrayContaining([
expect.objectContaining({
parkName: PARKSLIST[0].parkName
}),
expect.objectContaining({
parkName: PARKSLIST[1].parkName
}),
])
);
});

test("dynamoUtil - getSubAreas", async () => {
let orc = '0041'
let specificSubAreas = [];
for (const area of SUBAREAS) {
if (area.pk === `park::${orc}`) {
specificSubAreas.push(area);
}
}
const result = await utils.getSubAreas(orc)

expect(result).toEqual(
expect.arrayContaining([
expect.objectContaining({
subAreaName: specificSubAreas[0].subAreaName
}),
expect.objectContaining({
subAreaName: specificSubAreas[1].subAreaName
}),
])
);
});

test("dynamoUtil - getRecords", async () => {
const result = await utils.getRecords(SUBAREAS[0])

expect(result).toEqual(
expect.not.arrayContaining([
expect.not.objectContaining({
orcs: SUBAREAS[0].pk.split("::")[1]
})
])
);
});
});
141 changes: 141 additions & 0 deletions __tests__/export.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
const AWS = require("aws-sdk");
const { DocumentClient } = require("aws-sdk/clients/dynamodb");
const { REGION, ENDPOINT, TABLE_NAME } = require("./global/settings");
const { PARKSLIST, SUBAREAS, JOBSLIST, MOCKJOB } = require("./global/data.json");

const exportGET = require("../lambda/export/GET/index");
const exportFUNCTIONS = require("../lambda/export/functions");
const utils = require("../lambda/dynamoUtil");


const jwt = require("jsonwebtoken");
const tokenContent = {
resource_access: { "attendance-and-revenue": { roles: ["sysadmin"] } },
};
const token = jwt.sign(tokenContent, "defaultSecret");


async function setupDb() {
new AWS.DynamoDB({
region: REGION,
endpoint: ENDPOINT,
});
docClient = new DocumentClient({
region: REGION,
endpoint: ENDPOINT,
convertEmptyValues: true,
});

for (const park of PARKSLIST) {
await docClient
.put({
TableName: TABLE_NAME,
Item: park,
})
.promise();
}

for (const subarea of SUBAREAS) {
await docClient
.put({
TableName: TABLE_NAME,
Item: subarea,
})
.promise();
}

for (const job of JOBSLIST) {
await docClient
.put({
TableName: TABLE_NAME,
Item: job,
})
.promise();
}
}

describe("Export Report", () => {
beforeAll(async () => {
return await setupDb();
});

test("Handler - 403 GET Invalid Auth", async () => {
const response = await exportGET.handler(
{
headers: {
Authorization: "Bearer " + token + "invalid",
PsuedoToken: "error",
},
},
null
);

expect(response.statusCode).toBe(403);
});

test("Handler - 200 GET, with no jobs", async () => {
const dateField = "dateGenerated"
const event = {
headers: {
Authorization: "Bearer " + token,
},
httpMethod: "GET",
queryStringParameters: {
getJob: "true"
},
};

const result = await exportGET.handler(event, null)
let body;
try {
body = JSON.parse(result.body)
} catch (e) {
body = 'fail'
}

expect(result).toEqual(
expect.objectContaining({
headers: {
"Access-Control-Allow-Headers":
"Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token",
"Access-Control-Allow-Methods": "OPTIONS,GET,POST",
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json",
},
statusCode: 200,
}),
);
expect(body.jobObj[dateField]).toMatch(JOBSLIST[0][dateField])
})

test("Handler - 200 GET, generate report", async () => {
const event = {
headers: {
Authorization: "Bearer " + token,
},
httpMethod: "GET",
};

const result = await exportGET.handler(event, null)

// Returns value below even with no job
// Update when invokable can be called
expect(result.body).toBe("{\"status\":\"Job is already running\"}")
});

test("Functions - updateJobEntry", async () => {
const query = {
TableName: TABLE_NAME,
KeyConditionExpression: "pk = :pk AND sk = :sk",
ExpressionAttributeValues: {
":pk": { S: "job" },
":sk": {S: "MOCK_JOB_ID"}
}
};

await exportFUNCTIONS.updateJobEntry(MOCKJOB, TABLE_NAME)
const result = await utils.runQuery(query)

expect(result).toMatchObject([MOCKJOB])
})
});
14 changes: 14 additions & 0 deletions __tests__/fiscalYearEnd.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,18 @@ describe('Fiscal Year End Test', () => {
expect(response.statusCode).toBe(200);
});

test('HandleLock - 403 unlock fiscal year without perms', async () => {
const response = await fiscalYearEndPOST.unlockFiscalYear({
headers: {
Authorization: "Bearer " + token,
PsuedoToken: { resource_access: { 'attendance-and-revenue': { roles: [''] } } }
},
queryStringParameters: {
fiscalYearEnd: "2018"
}
}, null);

expect(response.statusCode).toBe(403);
});

});
36 changes: 33 additions & 3 deletions __tests__/global/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@
],
"parkName": "Cultus Lake Park",
"orcs": 41,
"subAreaName": "Maple Bay"
"subAreaName": "Maple Bay",
"roles": [
"sysadmin",
"0041"
]
},
{
"pk": "park::0041",
Expand All @@ -60,7 +64,11 @@
],
"parkName": "Cultus Lake Park",
"orcs": 41,
"subAreaName": "Teapot Hill"
"subAreaName": "Teapot Hill",
"roles": [
"sysadmin",
"0041"
]
},
{
"pk": "park::6161",
Expand Down Expand Up @@ -159,5 +167,27 @@
"sk": "2018",
"isLocked": true
}
]
],
"JOBSLIST": [
{
"dateGenerated": "2023-01-05T22:12:49.314Z",
"lastSuccessfulJob": {
"dateGenerated": "2023-01-05T22:12:49.314Z",
"key": "8404d420fd0dd5dc00986111a6bb8d6d/A&R_Export.xlsx"
},
"progressDescription": "Job Complete. Your document is ready.",
"progressState": "complete",
"sk": "8404d420fd0dd5dc00986111a6bb8d6d",
"pk": "job",
"key": "8404d420fd0dd5dc00986111a6bb8d6d/A&R_Export.xlsx",
"progressPercentage": 100
}
],
"MOCKJOB": {
"sk": "MOCK_JOB_ID",
"progressPercentage": 0,
"key": "MOCK_S3_KEY",
"progressDescription": "",
"lastSuccessfulJob": {}
}
}
Loading

0 comments on commit 1c89556

Please sign in to comment.