Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NRPTI-1045] TESTS** Fix broken tests in API #1121

Merged
merged 14 commits into from
Oct 12, 2023
Merged
445 changes: 350 additions & 95 deletions api/package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"scripts": {
"start": "node app",
"test": "node_modules/.bin/jest --verbose --forceExit",
"test-mac": "env MONGOMS_ARCH=x64 npm run test --detectOpenHandles",
"dev": "nodemon app.js",
"lint": "npm-run-all -l -s -c lint:*",
"lint:1": "eslint . --ignore-pattern 'node_modules' --ext .js",
"lint:2": "prettier ./**/*.js --warn",
Expand Down Expand Up @@ -79,6 +81,7 @@
"mockingoose": "^2.15.2",
"mongodb-memory-server": "5.2.8",
"nock": "11.4.0",
"nodemon": "3.0.1",
"npm-run-all": "^4.1.5",
"prettier": "~1.19.1",
"shelljs": "^0.8.5",
Expand Down
35 changes: 17 additions & 18 deletions api/src/controllers/collection-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ exports.protectedOptions = function (args, res, next) {

exports.protectedGet = async function (args, res, next) {
let collectionId = null;
if (args.swagger.params.collectionId && args.swagger.params.collectionId.value) {
if (args.swagger.params.collectionId?.value) {
collectionId = args.swagger.params.collectionId.value
} else {
defaultLog.info(`protectedGet - you must provide an id to get`);
Expand Down Expand Up @@ -47,15 +47,15 @@ exports.protectedPut = async function (args, res, next) {
}

let collectionId = null;
if (args.swagger.params.collectionId && args.swagger.params.collectionId.value) {
if (args.swagger.params.collectionId?.value) {
collectionId = args.swagger.params.collectionId.value;
} else {
defaultLog.info(`protectedPut - you must provide an id to update`);
queryActions.sendResponse(res, 400, {});
next();
}
let incomingObj = {};
if (args.swagger.params.collection && args.swagger.params.collection.value) {
if (args.swagger.params.collection?.value) {
incomingObj = args.swagger.params.collection.value;
} else {
defaultLog.info(`protectedPut - you must provide an object`);
Expand Down Expand Up @@ -101,19 +101,18 @@ const updateCollection = async function(incomingObj, collectionId, displayName)
let recordsToAdd = [];
let arrayOfObjIds = [];

for (let i = 0; i < incomingObj.records.length; i++) {
if (!recordIds.includes(incomingObj.records[i])) {
// These are new records that need to be added to the collection.
recordsToAdd.push(incomingObj.records[i]);
for(const record of incomingObj.records){
if(!recordIds.includes(record)){
recordsToAdd.push(record);
arrayOfObjIds.push(new ObjectId(record));
}
arrayOfObjIds.push(new ObjectId(incomingObj.records[i]))
}

// Add records
if (recordsToAdd.length > 0) {
// Need to know the status of the collection in order to make sure the records match.
const collection = await collectionDB.findOne({ _id: new ObjectId(collectionId) });
if (!collection || !collection.read) {
if (!collection?.read) {
defaultLog.info(`protectedPut - error locating collection`);
throw new Error(`protectedPut - error locating collection`)
}
Expand Down Expand Up @@ -157,7 +156,7 @@ const updateCollection = async function(incomingObj, collectionId, displayName)
const CollectionBCMI = mongoose.model(RECORD_TYPE.CollectionBCMI._schemaName);

const sanitizedObj = PutUtils.validateObjectAgainstModel(CollectionBCMI, incomingObj);
if (!sanitizedObj || sanitizedObj === {}) {
if (!sanitizedObj || Object.keys(sanitizedObj).length === 0) {
LocalNewsTV marked this conversation as resolved.
Show resolved Hide resolved
// skip, as there are no changes to master record
return;
}
Expand Down Expand Up @@ -199,7 +198,7 @@ exports.protectedPost = async function (args, res, next) {
}

let incomingObj = {};
if (args.swagger.params.collection && args.swagger.params.collection.value) {
if (args.swagger.params.collection?.value) {
incomingObj = args.swagger.params.collection.value
} else {
defaultLog.info(`protectedPost - you must provide an id to post`);
Expand Down Expand Up @@ -228,7 +227,7 @@ exports.protectedPost = async function (args, res, next) {

exports.protectedDelete = async function (args, res, next) {
let collectionId = null;
if (args.swagger.params.collectionId && args.swagger.params.collectionId.value) {
if (args.swagger.params.collectionId?.value) {
collectionId = args.swagger.params.collectionId.value
} else {
defaultLog.info(`protectedDelete - you must provide an id to delete`);
Expand Down Expand Up @@ -263,7 +262,7 @@ exports.unpublishCollections = async function (mineId, auth_payload) {

try {
for (const collection of collections) {
if (collection.records && collection.records.length) {
if (collection.records?.length) {
defaultLog.debug('collection : ', JSON.stringify(collection._id));
const recordAggregate = [
{
Expand All @@ -288,12 +287,12 @@ exports.unpublishCollections = async function (mineId, auth_payload) {

const records = await nrpti.aggregate(recordAggregate).toArray();

let error = null;
let error;
// runs each function, last arg is callback, others are results from previous step
// any error in a step should cause the rest of the steps to be skipped and log that error
for (const record of records) {
defaultLog.debug('publishing record docs, recordId: ', JSON.stringify(record._id) );
if (record.documents && record.documents.length) {
if (record.documents?.length) {

for (const document of record.documents) {
if (document.key) {
Expand Down Expand Up @@ -371,7 +370,7 @@ exports.publishCollections = async function (mineId, auth_payload) {
// Publish every collection, their records, and their documents.
try {
for (const collection of collections) {
if (collection.records && collection.records.length) {
if (collection.records?.length) {
defaultLog.debug('collection : ', JSON.stringify(collection._id));
const recordAggregate = [
{
Expand Down Expand Up @@ -400,7 +399,7 @@ exports.publishCollections = async function (mineId, auth_payload) {
// Publish all documents of all records.
for (const record of records) {
// Update the S3 object properties for each document.
if (record.documents && record.documents.length) {
if (record.documents?.length) {
defaultLog.debug('publishing record s3 docs, recordId: ', JSON.stringify(record._id))
for (const document of record.documents) {
if (document.key) {
Expand Down Expand Up @@ -532,7 +531,7 @@ const createCollection = async function (collectionObj, user) {
collectionObj.date && (collection.date = collectionObj.date);
collectionObj.type && (collection.type = collectionObj.type);
collectionObj.agency && (collection.agency = collectionObj.agency);
collectionObj.records && collectionObj.records.length && (collection.records = collectionObj.records);
collectionObj.records?.length && (collection.records = collectionObj.records);

// If incoming object has addRole: 'public' then read will look like ['sysadmin', 'public']
if (collectionObj.addRole && collectionObj.addRole === 'public') {
Expand Down
76 changes: 39 additions & 37 deletions api/src/controllers/post/inspection.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ exports.createMaster = function(args, res, next, incomingObj, flavourIds) {
inspection.write = utils.ApplicationAdminRoles;

// set forward references
if (flavourIds && flavourIds.length) {
if (flavourIds?.length) {
flavourIds.forEach(id => {
if (ObjectId.isValid(id)) {
inspection._flavourRecords.push(new ObjectId(id));
Expand Down Expand Up @@ -263,22 +263,22 @@ exports.createLNG = function(args, res, next, incomingObj) {

inspectionLNG.issuedTo.read = utils.ApplicationAdminRoles;
inspectionLNG.issuedTo.write = [utils.ApplicationRoles.ADMIN, utils.ApplicationRoles.ADMIN_LNG];
incomingObj.issuedTo && incomingObj.issuedTo.type && (inspectionLNG.issuedTo.type = incomingObj.issuedTo.type);
incomingObj.issuedTo &&
incomingObj.issuedTo.companyName &&
incomingObj.issuedTo?.type && (inspectionLNG.issuedTo.type = incomingObj.issuedTo.type);

incomingObj.issuedTo?.companyName &&
(inspectionLNG.issuedTo.companyName = incomingObj.issuedTo.companyName);
incomingObj.issuedTo &&
incomingObj.issuedTo.firstName &&

incomingObj.issuedTo?.firstName &&
(inspectionLNG.issuedTo.firstName = incomingObj.issuedTo.firstName);
incomingObj.issuedTo &&
incomingObj.issuedTo.middleName &&

incomingObj.issuedTo?.middleName &&
(inspectionLNG.issuedTo.middleName = incomingObj.issuedTo.middleName);
incomingObj.issuedTo &&
incomingObj.issuedTo.lastName &&

incomingObj.issuedTo?.lastName &&
(inspectionLNG.issuedTo.lastName = incomingObj.issuedTo.lastName);
incomingObj.issuedTo && (inspectionLNG.issuedTo.fullName = postUtils.getIssuedToFullNameValue(incomingObj.issuedTo));
incomingObj.issuedTo &&
incomingObj.issuedTo.dateOfBirth &&

incomingObj.issuedTo?.dateOfBirth &&
(inspectionLNG.issuedTo.dateOfBirth = incomingObj.issuedTo.dateOfBirth);

incomingObj.projectName && (inspectionLNG.projectName = incomingObj.projectName);
Expand Down Expand Up @@ -354,7 +354,7 @@ exports.createNRCED = function(args, res, next, incomingObj) {
args.swagger.params.auth_payload.client_roles
)
) {
throw new Error('Missing valid user role.');
throw new Error('@func createNRCED: Missing valid user role.');
}

let InspectionNRCED = mongoose.model('InspectionNRCED');
Expand Down Expand Up @@ -395,23 +395,25 @@ exports.createNRCED = function(args, res, next, incomingObj) {

inspectionNRCED.issuedTo.read = utils.ApplicationAdminRoles;
inspectionNRCED.issuedTo.write = [utils.ApplicationRoles.ADMIN, utils.ApplicationRoles.ADMIN_NRCED];
incomingObj.issuedTo && incomingObj.issuedTo.type && (inspectionNRCED.issuedTo.type = incomingObj.issuedTo.type);
incomingObj.issuedTo &&
incomingObj.issuedTo.companyName &&

incomingObj?.issuedTo?.type && (inspectionNRCED.issuedTo.type = incomingObj.issuedTo.type);

incomingObj?.issuedTo?.companyName &&
(inspectionNRCED.issuedTo.companyName = incomingObj.issuedTo.companyName);
incomingObj.issuedTo &&
incomingObj.issuedTo.firstName &&

incomingObj?.issuedTo?.firstName &&
(inspectionNRCED.issuedTo.firstName = incomingObj.issuedTo.firstName);
incomingObj.issuedTo &&
incomingObj.issuedTo.middleName &&

incomingObj?.issuedTo?.middleName &&
(inspectionNRCED.issuedTo.middleName = incomingObj.issuedTo.middleName);
incomingObj.issuedTo &&
incomingObj.issuedTo.lastName &&

incomingObj?.issuedTo?.lastName &&
(inspectionNRCED.issuedTo.lastName = incomingObj.issuedTo.lastName);
incomingObj.issuedTo &&

incomingObj.issuedTo &&
(inspectionNRCED.issuedTo.fullName = postUtils.getIssuedToFullNameValue(incomingObj.issuedTo));
incomingObj.issuedTo &&
incomingObj.issuedTo.dateOfBirth &&

incomingObj?.issuedTo?.dateOfBirth &&
(inspectionNRCED.issuedTo.dateOfBirth = incomingObj.issuedTo.dateOfBirth);

incomingObj.projectName && (inspectionNRCED.projectName = incomingObj.projectName);
Expand Down Expand Up @@ -487,7 +489,7 @@ exports.createBCMI = function(args, res, next, incomingObj) {
args.swagger.params.auth_payload.client_roles
)
) {
throw new Error('Missing valid user role.');
throw new Error('@func inspection.js createBCMI: Missing valid user role.');
}

let InspectionNRCED = mongoose.model('InspectionBCMI');
Expand Down Expand Up @@ -532,22 +534,22 @@ exports.createBCMI = function(args, res, next, incomingObj) {

inspectionBCMI.issuedTo.read = utils.ApplicationAdminRoles;
inspectionBCMI.issuedTo.write = [utils.ApplicationRoles.ADMIN, utils.ApplicationRoles.ADMIN_NRCED];
incomingObj.issuedTo && incomingObj.issuedTo.type && (inspectionBCMI.issuedTo.type = incomingObj.issuedTo.type);
incomingObj.issuedTo &&
incomingObj.issuedTo.companyName &&
incomingObj.issuedTo?.type && (inspectionBCMI.issuedTo.type = incomingObj.issuedTo.type);

incomingObj.issuedTo?.companyName &&
(inspectionBCMI.issuedTo.companyName = incomingObj.issuedTo.companyName);
incomingObj.issuedTo &&
incomingObj.issuedTo.firstName &&

incomingObj.issuedTo?.firstName &&
(inspectionBCMI.issuedTo.firstName = incomingObj.issuedTo.firstName);
incomingObj.issuedTo &&
incomingObj.issuedTo.middleName &&

incomingObj.issuedTo?.middleName &&
(inspectionBCMI.issuedTo.middleName = incomingObj.issuedTo.middleName);
incomingObj.issuedTo &&
incomingObj.issuedTo.lastName &&

incomingObj.issuedTo?.lastName &&
(inspectionBCMI.issuedTo.lastName = incomingObj.issuedTo.lastName);
incomingObj.issuedTo && (inspectionBCMI.issuedTo.fullName = postUtils.getIssuedToFullNameValue(incomingObj.issuedTo));
incomingObj.issuedTo &&
incomingObj.issuedTo.dateOfBirth &&

incomingObj.issuedTo?.dateOfBirth &&
(inspectionBCMI.issuedTo.dateOfBirth = incomingObj.issuedTo.dateOfBirth);

incomingObj.projectName && (inspectionBCMI.projectName = incomingObj.projectName);
Expand Down
18 changes: 9 additions & 9 deletions api/src/controllers/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -665,14 +665,14 @@ exports.publicGet = async function (args, res, next) {
args.swagger.params.subset.value = ['redactedRecord'];

// if we are searching for data that does not require redaction, we should search on the main database subset.
if (args.swagger.params.dataset && args.swagger.params.dataset.value) {
if(SKIP_REDACTION_SCHEMA_NAMES.includes(String(args.swagger.params.dataset.value))){
if (args.swagger.params.dataset?.value) {
if (SKIP_REDACTION_SCHEMA_NAMES.includes(String(args.swagger.params.dataset.value))) {
args.swagger.params.subset.value = ['nrpti'];
defaultLog.info(`Searching on non-redacted database despite public search query: '${args.swagger.params.dataset.value}' is not a redacted dataset. `);
}
}
if (args.swagger.params._schemaName && args.swagger.params._schemaName.value) {
if (SKIP_REDACTION_SCHEMA_NAMES.includes(String(args.swagger.params._schemaName.value))){
if (args.swagger.params._schemaName?.value) {
if (SKIP_REDACTION_SCHEMA_NAMES.includes(String(args.swagger.params._schemaName.value))) {
args.swagger.params.subset.value = ['nrpti'];
defaultLog.info(`Searching on non-redacted database despite public search query: '${args.swagger.params._schemaName.value}' is not a redacted schema. `);
}
Expand Down Expand Up @@ -731,8 +731,8 @@ const generateMatchesForAggregation = async function (and, or, nor, searchProper
let match = {
_schemaName: Array.isArray(schemaName) ? { $in: schemaName } : schemaName,
...(isEmpty(modifier) ? undefined : modifier),
...(searchProperties ? searchProperties : undefined),
...(properties ? properties : undefined)
...(searchProperties || undefined),
...(properties || undefined)
};

return match;
Expand Down Expand Up @@ -774,7 +774,7 @@ const executeQuery = async function (args, res, next) {
defaultLog.info('populate:', populate);
defaultLog.info('subset:', subset);

let roles = args.swagger.params.auth_payload ? args.swagger.params.auth_payload.client_roles : ['public'];
let roles = args.swagger.params.auth_payload?.client_roles || ['public'];

defaultLog.info('Searching Collection:', dataset);

Expand All @@ -789,8 +789,8 @@ const executeQuery = async function (args, res, next) {
: { idir_userid: null, displayName: 'public', preferred_username: 'public' }
);

let sortDirection = undefined;
let sortField = undefined;
let sortDirection;
let sortField;

let sortingValue = {};
sortBy.map(value => {
Expand Down
4 changes: 2 additions & 2 deletions api/src/importers/alc/utils/csv-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ describe('getEntityType', () => {
expect(result).toBe(null);
});

it('returns "Company" if csvRow "Inspection Property Owner" is not empty', async () => {
const result = await CsvUtils.getEntityType({ 'inspection property owner': 'test' });
it('returns "Company" if csvRow "Inspection Property Owner" ends with Ltd, Corp, Inc, etc', async () => {
const result = await CsvUtils.getEntityType({ 'inspection property owner': 'test Ltd.' });

expect(result).toEqual(MiscConstants.IssuedToEntityTypes.Company);
});
Expand Down
2 changes: 1 addition & 1 deletion api/src/importers/coors/court-conviction-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class CourtConvictions extends BaseRecordUtils {
};
}

if (csvRow['enforcement_outcome'] && csvRow['enforcement_outcome'] === 'GTYJ') {
if (csvRow?.enforcement_outcome === 'GTYJ') {
conviction['dateIssued'] = csvRow['ticket_date'] || null;
conviction['recordName'] = (csvRow['case_number'] && `Case Number ${csvRow['case_number']}`) || '';
conviction['location'] = csvRow['location_of_violation'] || '';
Expand Down
6 changes: 3 additions & 3 deletions api/src/importers/coors/court-conviction-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('transformRecord', () => {
issuingAgency: '',
author: '',
legislation: [{ act: '', paragraph: '', regulation: '', section: '', subSection: '', offence: '' }],
location: '',
location_of_violation: '',
recordName: '',
penalties: [{ description: '', penalty: { type: null, value: null }, type: null }],

Expand Down Expand Up @@ -53,7 +53,7 @@ describe('transformRecord', () => {
_sourceRefCoorsId: '123-456',

recordType: 'Court Conviction',
dateIssued: expect.any(String),
dateIssued: "12/30/2019",
issuedTo: {
companyName: 'businessName123',
type: 'Company'
Expand All @@ -70,7 +70,7 @@ describe('transformRecord', () => {
offence: 'description123'
}
],
location: 'location123',
location_of_violation: 'location123',
recordName: 'Case Number P-123456',
penalties: [
{
Expand Down
2 changes: 0 additions & 2 deletions api/src/importers/coors/utils/csv-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ exports.getIssuingAgency = function(csvRow) {
let act = '';
if (csvRow['act']) {
act = csvRow['act'];
} else {
return null;
}

// Act == Water Sustainability Act, in which case Issuing Agency = BC Energy Regulator
Expand Down
2 changes: 1 addition & 1 deletion api/src/importers/coors/utils/csv-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('getIssuingAgency', () => {

it('returns "Conservation Officer Service" if csvRow "case_number" does not start with a "P-"', async () => {
const result = await CsvUtils.getIssuingAgency({ case_number: '123123' });

expect(result).toEqual(MiscConstants.CoorsCsvIssuingAgencies.Conservation_Officer_Service);
});
});
Loading
Loading