diff --git a/src/managers/metadata.ts b/src/managers/metadata.ts index e57c7661..e3d70ab8 100644 --- a/src/managers/metadata.ts +++ b/src/managers/metadata.ts @@ -391,9 +391,11 @@ class Metadata { * @param {string} ancestorFolderId - The folder_id to which to restrain the query * @param {Object} [options] - Optional parameters * @param {string} [options.query] - The logical expression of the query - * @param {Object} [options.query_parameters] - Required if query present. The arguments for the query + * @param {Object} [options.query_params] - Required if query present. The arguments for the query * @param {Object} [options.order_by] - The field_key(s) to order on and the corresponding direction(s) * @param {Array} [options.fields] - An array of fields to return + * @param {int} [options.limit=100] - The number of results to return for a single request + * @param {string} [options.marker] - Pagination marker * @param {Function} [callback] - Passed a collection of items and their associated metadata * @returns {Promise} Promise resolving to a collection of items and their associated metadata */ @@ -402,9 +404,11 @@ class Metadata { ancestorFolderId: string, options?: { query?: string; - query_parameters?: Record; - order_by: Record; + query_params?: Record; + order_by?: Record; fields?: string[]; + limit?: number; + marker?: string; }, callback?: Function ) { diff --git a/tests/integration_test/__tests__/metadata.test.js b/tests/integration_test/__tests__/metadata.test.js new file mode 100644 index 00000000..c8744706 --- /dev/null +++ b/tests/integration_test/__tests__/metadata.test.js @@ -0,0 +1,70 @@ +'use strict'; + +const {getAppClient} = require('../context'); +const {createBoxTestFolder} = require('../objects/box-test-folder'); +const {createBoxTestFile} = require('../objects/box-test-file'); +const utils = require('../lib/utils'); +const path = require('path'); +const context = {}; + +beforeAll(async() => { + context.appClient = await getAppClient(); + let folder = await createBoxTestFolder(context.appClient); + context.folder = folder; +}); + +afterAll(async() => { + await context.folder.dispose(); + context.folder = null; +}); + +test('test metadata search', async() => { + const templateKey = `template_${utils.randomName()}`; + const fields = [ + { + type: 'float', + key: 'testFloatValue', + displayName: 'testFloatValue', + }, + ]; + + const metadataTemplate = await context.appClient.metadata.createTemplate(templateKey, fields, { + scope: 'enterprise', + templateKey, + }); + try { + expect(metadataTemplate.id).toBeDefined(); + expect(metadataTemplate.templateKey).toBe(templateKey); + expect(metadataTemplate.displayName).toBe(templateKey); + const file = await createBoxTestFile(context.appClient, path.join(__dirname, '../resources/blank.pdf'), 'blank_sign_1.pdf', context.folder.id); + try { + const metadata = await context.appClient.files.addMetadata(file.id, 'enterprise', templateKey, {testFloatValue: 150}); + expect(metadata.$template).toBe(templateKey); + expect(metadata.testFloatValue).toBe(150); + + const searchForm = `${metadataTemplate.scope}.${metadataTemplate.templateKey}`; + const ancestorFolderId = '0'; + const queryOptions = { + query: 'testFloatValue >= :arg', + query_params: {arg: '100'}, + limit: 1, + order_by: [ + { + field_key: 'testFloatValue', + direction: 'asc', + }, + ], + }; + const searchResults = await context.appClient.metadata.query(searchForm, ancestorFolderId, queryOptions); + // Sometimes, despite correctly sent metadata, the search does not return files because they probably haven't been properly indexed yet + expect(searchResults.entries.length).toBeGreaterThanOrEqual(0); + } + finally { + await file.dispose(); + } + } + finally { + await context.appClient.metadata.deleteTemplate('enterprise', metadataTemplate.templateKey); + } +}, 120000); + diff --git a/tests/lib/managers/metadata-test.js b/tests/lib/managers/metadata-test.js index 2fedf65f..ac3251be 100644 --- a/tests/lib/managers/metadata-test.js +++ b/tests/lib/managers/metadata-test.js @@ -334,4 +334,35 @@ describe('Metadata', function() { .then(data => assert.equal(data, response)); }); }); + + describe('query()', function() { + it('should make POST request to search the API when called', function() { + var from = 'enterprise_987654321.someTemplate'; + var ancestorFolderId = '0'; + var options = { + query: 'value >= :amount', + query_params: {amount: '100'}, + limit: 10, + marker: 'vwxyz', + order_by: [ + { + "field_key": "value", + "direction": "asc" + } + ] + }; + + var expectedParams = { + body: { + ancestor_folder_id: ancestorFolderId, + from: from, + ...options + }, + }; + + sandbox.stub(boxClientFake, 'wrapWithDefaultHandler').returnsArg(0); + sandbox.mock(boxClientFake).expects('post').withArgs('/metadata_queries/execute_read', expectedParams); + metadata.query(from, ancestorFolderId, options); + }); + }); });