Skip to content

Commit

Permalink
fix: Fix bulk action in search command
Browse files Browse the repository at this point in the history
  • Loading branch information
arjankowski committed Apr 2, 2024
1 parent 52497dc commit fdca249
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
14 changes: 11 additions & 3 deletions src/commands/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
Expand Down Expand Up @@ -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.',
}),
Expand Down
62 changes: 59 additions & 3 deletions test/commands/search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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()
Expand All @@ -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.');
});
});
});
});
3 changes: 3 additions & 0 deletions test/fixtures/search/bulk/bulk_get_search_query_input.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
scope,type
enterprise_content,file
enterprise_content,folder

0 comments on commit fdca249

Please sign in to comment.