From 9d610fa71cc85454c5a7c3461ddd1fe0376e23ac Mon Sep 17 00:00:00 2001 From: LocalNewsTV <62873746+LocalNewsTV@users.noreply.github.com> Date: Fri, 6 Oct 2023 13:15:36 -0700 Subject: [PATCH] revert changes in search.js, format cleanup --- api/src/controllers/search.js | 18 ++++++------ api/test/tests/controllers/search.test.js | 36 ++++++++++++----------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/api/src/controllers/search.js b/api/src/controllers/search.js index d2126b9a6..d487b24ac 100644 --- a/api/src/controllers/search.js +++ b/api/src/controllers/search.js @@ -355,23 +355,23 @@ let searchCollection = async function ( pageNum, pageSize, project, + sortField = undefined, + sortDirection = undefined, caseSensitive, + populate = false, and, or, nor, subset, - _in, - populate = false, - sortField = undefined, - sortDirection = undefined, + _in ) { - let properties; + let properties = undefined; if (project) { properties = { project: mongoose.Types.ObjectId(project) }; } // optional search keys - let searchProperties; + let searchProperties = undefined; if (keywords) { // for now, limit fuzzy search to the mine search only. We can expand to all searches // later if desired @@ -389,7 +389,7 @@ let searchCollection = async function ( let hasCollection = null; if (or && Object.prototype.hasOwnProperty.call(or, 'hasCollection')) { hasCollectionTest = true; - hasCollection = or.hasCollection === 'true'; + hasCollection = or.hasCollection === 'true' ? true : false; delete or.hasCollection; } @@ -516,7 +516,7 @@ let searchCollection = async function ( // to finalize the facet searchResultAggregation.push({ $lookup: { - from: subset && subset?.includes('redactedRecord') ? 'redacted_record_subset' : 'nrpti', + from: subset && subset.includes('redactedRecord') ? 'redacted_record_subset' : 'nrpti', localField: '_id', foreignField: '_id', as: 'fullRecord' @@ -758,7 +758,7 @@ const executeQuery = async function (args, res, next) { let or = args.swagger.params.or ? args.swagger.params.or.value : ''; let nor = args.swagger.params.nor ? args.swagger.params.nor.value : ''; let _in = args.swagger.params._in ? args.swagger.params._in.value : ''; - let subset = args.swagger.params.subset ? args.swagger.params.subset.value : []; + let subset = args.swagger.params.subset ? args.swagger.params.subset.value : null; defaultLog.info('Searching keywords:', keywords); defaultLog.info('Searching datasets:', dataset); defaultLog.info('Searching project:', project); diff --git a/api/test/tests/controllers/search.test.js b/api/test/tests/controllers/search.test.js index 1a5c47067..ad240ddf6 100644 --- a/api/test/tests/controllers/search.test.js +++ b/api/test/tests/controllers/search.test.js @@ -26,7 +26,7 @@ let generated_Company; describe('Search Controller Testing', () => { const testUser = 'testUser'; - beforeAll( async () => { + beforeAll(async () => { generated_things = await generate_helper.generateSingleFactory( 'Order', 5, @@ -47,12 +47,12 @@ describe('Search Controller Testing', () => { {}, { genAdult: true } ), - generated_Company = await generate_helper.generateSingleFactory( - 'Order', - 2, - { }, - { genCompany: true } - ) + generated_Company = await generate_helper.generateSingleFactory( + 'Order', + 2, + {}, + { genCompany: true } + ) }); test('Invalid ObjectId returns 400 error', async (done) => { @@ -89,7 +89,7 @@ describe('Search Controller Testing', () => { request(app) .get(searchEndpoint) - .query(qs.stringify({ dataset: ['Item'], _schemaName: 'Order', _id: orderId})) + .query(qs.stringify({ dataset: ['Item'], _schemaName: 'Order', _id: orderId })) .expect(200) .expect('Content-Type', 'application/json') .end((err, res) => { @@ -114,7 +114,7 @@ describe('Search Controller Testing', () => { request(app) .get(searchEndpoint) - .query(qs.stringify({ dataset: ['Item'], _schemaName: 'Order', _id: orderId, populate: true})) + .query(qs.stringify({ dataset: ['Item'], _schemaName: 'Order', _id: orderId, populate: true })) .expect(200) .expect('Content-Type', 'application/json') .end((err, res) => { @@ -146,7 +146,7 @@ describe('Search Controller Testing', () => { request(app) .get(searchEndpoint) - .query(qs.stringify({ dataset: ['Item'], _schemaName: 'Order', _id: orderId, populate: true})) + .query(qs.stringify({ dataset: ['Item'], _schemaName: 'Order', _id: orderId, populate: true })) .expect(200) .expect('Content-Type', 'application/json') .end((err, res) => { @@ -177,7 +177,7 @@ describe('Search Controller Testing', () => { request(app) .get(searchEndpoint) - .query(qs.stringify({ dataset: ['Item'], _schemaName: 'Order', _id: orderId, populate: true})) + .query(qs.stringify({ dataset: ['Item'], _schemaName: 'Order', _id: orderId, populate: true })) .expect(200) .expect('Content-Type', 'application/json') .end((err, res) => { @@ -200,6 +200,8 @@ describe('Search Controller Testing', () => { const roles = ['admin:nrced']; const orderId = generated_Company[0]._id.toString(); const expected = generated_Company[0]; + const redactExpectation = 'Unpublished'; + app.get(searchEndpoint, (req, res) => { const params = test_util.buildParams(req.query) const paramsWithValues = test_util.createSwaggerParams(params, roles, testUser); @@ -208,7 +210,7 @@ describe('Search Controller Testing', () => { request(app) .get(searchEndpoint) - .query(qs.stringify({ dataset: ['Item'], _schemaName: 'Order', _id: orderId, populate: true})) + .query(qs.stringify({ dataset: ['Item'], _schemaName: 'Order', _id: orderId, populate: true })) .expect(200) .expect('Content-Type', 'application/json') .end((err, res) => { @@ -221,16 +223,16 @@ describe('Search Controller Testing', () => { expect(record._id).toMatch(orderId) expect(record.issuedTo.type).toBe(CONSTANTS.IssuedToEntityTypes.Company) expect(record.issuedTo.companyName).toBe(expected.issuedTo.companyName) - expect(record.issuedTo.firstName).toBe('') - expect(record.issuedTo.lastName).toBe('') - expect(record.issuedTo.fullName).toBe('') + expect(record.issuedTo.firstName).toBe(redactExpectation || '') + expect(record.issuedTo.lastName).toBe(redactExpectation || '') + expect(record.issuedTo.fullName).toBe(redactExpectation || '') expect(record.issuedTo.dateOfBirth).toBeFalsy() return done(); }) }) test('Lookup by dataset returns items', async (done) => { - const roles = ['sysadmin','admin:nrced']; + const roles = ['sysadmin', 'admin:nrced']; app.get(searchEndpoint, (req, res) => { const params = test_util.buildParams(req.query) const paramsWithValues = test_util.createSwaggerParams(params, roles, testUser); @@ -239,7 +241,7 @@ describe('Search Controller Testing', () => { request(app) .get(searchEndpoint) - .query({ dataset: ['Order','Inspection']}) + .query({ dataset: ['Order', 'Inspection'] }) .expect(200) .expect('Content-Type', 'application/json') .end((err, res) => {