diff --git a/src/commands/search.js b/src/commands/search.js index eb83c842..535b7a79 100644 --- a/src/commands/search.js +++ b/src/commands/search.js @@ -28,11 +28,15 @@ class SearchCommand extends BoxCommand { async run() { const { flags, args } = this.parse(SearchCommand); - if (flags.all && flags.limit) { - throw new BoxCLIError('--all and --limit flags cannot be used together.'); + if (flags.all && (flags.limit || flags['max-items'])) { + throw new BoxCLIError('--all and --limit(--max-items) flags cannot be used together.'); } - if (!flags.all) { + if (flags.limit && flags['max-items'] && flags.limit !== flags['max-items']) { + throw new BoxCLIError(' --limit and --max-items flags cannot be used together.'); + } + + if (!flags.all && !flags['max-items']) { flags['max-items'] = flags.limit || RESULTS_LIMIT; this.flags['max-items'] = flags['max-items']; } @@ -289,6 +293,10 @@ SearchCommand.flags = { limit: flags.integer({ description: 'Defines the maximum number of items to return. Default value is 100.', }), + 'max-items': flags.integer({ + description: 'A value that indicates the maximum number of results to return.', + hidden: true + }), all: flags.boolean({ description: 'Returns all search results.', }), diff --git a/test/commands/search.test.js b/test/commands/search.test.js index 25ffcaae..29ede172 100644 --- a/test/commands/search.test.js +++ b/test/commands/search.test.js @@ -4,10 +4,12 @@ const { test } = require('@oclif/test'); const { assert } = require('chai'); const { getFixture, TEST_API_ROOT } = require('../helpers/test-helper'); const leche = require('leche'); - +const os = require('os'); +const path = require('path'); describe('Search', () => { let query = 'Test', + bulkInputFilePath = path.join(__dirname, '../fixtures/search/bulk/bulk_get_search_query_input.csv'), fixture = getFixture('search/get_search_query_page_1'), fixture2 = getFixture('search/get_search_query_page_2'), jsonOutput = getFixture('output/search_json.txt'), @@ -365,7 +367,61 @@ describe('Search', () => { .it('should return limited results when --limit flag provided', ctx => { assert.equal(ctx.stdout, jsonOutputLimitedTo5); }); + + test + .nock(TEST_API_ROOT, api => api + .get('/2.0/search') + .query({ + query, + limit: 5 + }) + .reply(200, fixture) + ) + .stdout() + .command([ + 'search', + query, + '--json', + '--max-items=5', + '--token=test' + ]) + .it('should return same limited results when --max-items flag provided instead of --limit', ctx => { + assert.equal(ctx.stdout, jsonOutputLimitedTo5); + }); }); + describe('bulk', () => { + test + .nock(TEST_API_ROOT, api => api + .get('/2.0/search') + .query({ + limit: 100, + query: '', + scope: 'enterprise_content', + type: 'file' + }) + .reply(200, { entries: [] }) + .get('/2.0/search') + .query({ + limit: 100, + query: '', + scope: 'enterprise_content', + type: 'folder', + }) + .reply(200, { entries: [] }) + ) + .stdout() + .stderr() + .command([ + 'search', + `--bulk-file-path=${bulkInputFilePath}`, + '--json', + '--token=test' + ]) + .it('should make a successful search call for each entry in a bulk file', ctx => { + let expectedMessage = `All bulk input entries processed successfully.${os.EOL}`; + assert.equal(ctx.stderr, expectedMessage); + }); + }); describe('fails', () => { test .stderr() @@ -377,7 +433,7 @@ describe('Search', () => { '--token=test' ]) .it('when both --all and --limit flag provided', ctx => { - assert.include(ctx.stderr, '--all and --limit flags cannot be used together.'); + assert.include(ctx.stderr, '--all and --limit(--max-items) flags cannot be used together.'); }); - }); + }); }); diff --git a/test/fixtures/search/bulk/bulk_get_search_query_input.csv b/test/fixtures/search/bulk/bulk_get_search_query_input.csv new file mode 100644 index 00000000..c1590fd1 --- /dev/null +++ b/test/fixtures/search/bulk/bulk_get_search_query_input.csv @@ -0,0 +1,3 @@ +scope,type +enterprise_content,file +enterprise_content,folder