From 9fea34e19b00f2916a7b044a0092e978d8dfabca Mon Sep 17 00:00:00 2001 From: Andrew Brazzatti Date: Wed, 4 Dec 2024 03:26:52 +0000 Subject: [PATCH] Added mocha tests for the parameter building functions for solr and named queries inside the vocab service. Added a feature to expose all service methods when in mocha testing mode --- core/src/CoreService.ts | 28 ++++++- .../docker-compose.mocha.yml | 2 +- test/unit/services/VocabService.test.js | 82 +++++++++++++++++++ typescript/api/services/VocabService.ts | 2 +- 4 files changed, 108 insertions(+), 6 deletions(-) create mode 100644 test/unit/services/VocabService.test.js diff --git a/core/src/CoreService.ts b/core/src/CoreService.ts index 458975d206..4850bb562b 100644 --- a/core/src/CoreService.ts +++ b/core/src/CoreService.ts @@ -67,11 +67,31 @@ export module Services.Core { * @returns {*} */ public exports(): any { + let exportedMethods: any = {}; + if(process.env["sails_redbox__mochaTesting"] === "true") { + const allProperties = [ + ...Object.getOwnPropertyNames(Object.getPrototypeOf(this)), // Prototype methods + ...Object.getOwnPropertyNames(this), // Instance properties + ]; + + + const uniqueProperties = Array.from(new Set(allProperties)); + uniqueProperties.forEach((property) => { + const value = (this as any)[property]; + + // Check if the property is a function + if (typeof value === "function" && property !== "constructor") { + exportedMethods[property] = value.bind(this); // Bind the method to maintain `this` context + } + + }); + console.error("Exported Methods for Mocha Testing: ", exportedMethods); + } else { // Merge default array and custom array from child. - var methods: any = this._defaultExportedMethods.concat(this._exportedMethods); - var exportedMethods: any = {}; + let methods: any = this._defaultExportedMethods.concat(this._exportedMethods); + - for (var i = 0; i < methods.length; i++) { + for (let i = 0; i < methods.length; i++) { // Check if the method exists. if (typeof this[methods[i]] !== 'undefined') { // Check that the method shouldn't be private. (Exception for _config, which is a sails config) @@ -89,7 +109,7 @@ export module Services.Core { console.error('The method "' + methods[i] + '" does not exist on the controller ' + this); } } - + } return exportedMethods; } diff --git a/support/integration-testing/docker-compose.mocha.yml b/support/integration-testing/docker-compose.mocha.yml index aff42ddb02..7b33a4d1e9 100755 --- a/support/integration-testing/docker-compose.mocha.yml +++ b/support/integration-testing/docker-compose.mocha.yml @@ -32,7 +32,7 @@ services: - sails_redbox__apiKey=c8e844fc-8550-497f-b970-7900ec8741ca - sails_record__baseUrl_redbox=http://redbox:9000/redbox - sails_record__baseUrl_mint=http://203.101.226.160/mint - # - sails_log__level=verbose + - sails_redbox__mochaTesting=true - sails_auth__default__local__default__token=d077835a-696b-4728-85cf-3ffd57152b1e - sails_security__csrf=false - datacite_username=${datacite_username} diff --git a/test/unit/services/VocabService.test.js b/test/unit/services/VocabService.test.js new file mode 100644 index 0000000000..3d3fd324e9 --- /dev/null +++ b/test/unit/services/VocabService.test.js @@ -0,0 +1,82 @@ + + +describe('The VocabService', function () { + before(function (done) { + done(); + }); + + + it('Build a named query parameter map that includes both the search string and logged in user attributes', function (done) { + const user = { + id: 1, + username: 'testuser', + email: 'test@redboxresearchdata.com.au', + roles: ['Guest','Researcher','Admin']}; + + const queryConfig = { + querySource: 'database', + databaseQuery: 'test', + queryField: { + property: 'title', + type: 'string' + }, + userQueryFields: [ + { + property: 'userEmail', + userValueProperty: 'email' + }, + { + property: 'userRole', + userValueProperty: 'roles' + } + ] + } + + const queryParamMap = VocabService.buildNamedQueryParamMap(queryConfig, 'test', user); + expect(queryParamMap).to.be.an('object'); + expect(queryParamMap).to.have.property('userEmail'); + expect(queryParamMap.userEmail).to.equal(user.email); + expect(queryParamMap).to.have.property('userRole'); + expect(queryParamMap.userRole).to.equal(user.roles); + + done(); + }); + + it('Build a solr query that includes both the search string and logged in user attributes', function (done) { + const user = { + id: 1, + username: 'testuser', + email: 'test@redboxresearchdata.com.au', + roles: ['Guest','Researcher','Admin']}; + + const queryConfig = { + querySource: 'solr', + searchQuery:{ + baseQuery: 'metaMetadata_type:rdmp' + }, + queryField: { + property: 'title', + type: 'text' + }, + userQueryFields: [ + { + property: 'userEmail', + userValueProperty: 'email' + }, + { + property: 'userRole', + userValueProperty: 'roles' + } + ] + } + const brand ={ + id: "1" + } + + const solrQuery = VocabService.buildSolrParams(brand, 'test',queryConfig, 1, 1, 'json', user); + expect(solrQuery).to.equal('metaMetadata_type:rdmp&sort=date_object_modified desc&version=2.2&start=1&rows=1&fq=metaMetadata_brandId:1&wt=json&fq=title:test*&fq=userEmail:test@redboxresearchdata.com.au&fq=userRole:Guest,Researcher,Admin'); + done(); + }); + + +}); diff --git a/typescript/api/services/VocabService.ts b/typescript/api/services/VocabService.ts index a723fbf473..95d8a20972 100644 --- a/typescript/api/services/VocabService.ts +++ b/typescript/api/services/VocabService.ts @@ -210,7 +210,7 @@ export module Services { if (queryConfig.userQueryFields != null) { for(let userQueryField of queryConfig.userQueryFields) { - let searchProperty = queryConfig.queryField.property; + let searchProperty = userQueryField.property; query = query + '&fq=' + searchProperty + ':'+ _.get(user, userQueryField.userValueProperty, null); } }