Skip to content

Commit

Permalink
Add trash commands (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
sujaygarlanka authored Feb 20, 2020
1 parent e6e1120 commit 74db947
Show file tree
Hide file tree
Showing 10 changed files with 729 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Next Release

- Added `--quiet` flag to suppress any non-error output to stderr
- Fixed a bug for the `--restrict-collaboration` flag for `box folders:update` where previously the flag would not restrict the collaborations when passed as true and would restrict collaborations when passed as false
- Added `box trash:restore` to restore a trashed item and `box trash:get` to get information on a trashed item

## 2.4.0 [2019-08-29]

- Fixed an issue where the CSV formatting of commands that return multiple different object types (e.g
Expand Down
52 changes: 52 additions & 0 deletions src/commands/trash/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

const BoxCommand = require('../../box-command');

class TrashGetCommand extends BoxCommand {
async run() {
const { flags, args } = this.parse(TrashGetCommand);
let options = {};

if (flags.fields) {
options.fields = flags.fields;
}
let item;
if (args.type === 'file') {
item = await this.client.files.getTrashedFile(args.id, options);
} else if (args.type === 'folder') {
item = await this.client.folders.getTrashedFolder(args.id, options);
} else if (args.type === 'web_link') {
item = await this.client.wrapWithDefaultHandler(this.client.get)(`/web_links/${args.id}/trash`, {qs: options});
}
await this.output(item);
}
}

TrashGetCommand.description = 'Get information about an item in trash';
TrashGetCommand.examples = ['box trash:get folder 22222'];

TrashGetCommand.flags = {
...BoxCommand.flags
};

TrashGetCommand.args = [
{
name: 'type',
required: true,
hidden: false,
description: 'Type of the item to get',
options: [
'file',
'folder',
'web_link'
],
},
{
name: 'id',
required: true,
hidden: false,
description: 'ID of the item to get',
}
];

module.exports = TrashGetCommand;
64 changes: 64 additions & 0 deletions src/commands/trash/restore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict';

const BoxCommand = require('../../box-command');
const { flags } = require('@oclif/command');

class TrashRestoreCommand extends BoxCommand {
async run() {
const { flags, args } = this.parse(TrashRestoreCommand);
let options = {};

if (flags.name) {
options.name = flags.name;
}
if (flags['parent-id']) {
options.parent = {
id: flags['parent-id']
};
}
let item;
if (args.type === 'file') {
item = await this.client.files.restoreFromTrash(args.id, options);
} else if (args.type === 'folder') {
item = await this.client.folders.restoreFromTrash(args.id, options);
} else if (args.type === 'web_link') {
item = await this.client.wrapWithDefaultHandler(this.client.post)(`/web_links/${args.id}`, {body: options});
}
await this.output(item);
}
}

TrashRestoreCommand.description = 'Restore an item from trash';
TrashRestoreCommand.examples = ['box trash:restore folder 22222'];

TrashRestoreCommand.flags = {
...BoxCommand.flags,
name: flags.string({
description: 'The new name for the item'
}),
'parent-id': flags.string({
description: 'ID of a folder to restore the item to only when the original folder no longer exists'
})
};

TrashRestoreCommand.args = [
{
name: 'type',
required: true,
hidden: false,
description: 'Type of the item to restore',
options: [
'file',
'folder',
'web_link'
],
},
{
name: 'id',
required: true,
hidden: false,
description: 'ID of the item to restore',
}
];

module.exports = TrashRestoreCommand;
141 changes: 141 additions & 0 deletions test/commands/trash.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,145 @@ describe('Trash', () => {
.it('should send fields param to the API when --fiels flag is passed');
});
});

describe('trash:get', () => {
let folderFixture = getFixture('trash/get_folders_id_trash'),
fileFixture = getFixture('trash/get_files_id_trash'),
webLinkFixture = getFixture('trash/get_folders_id_trash');
let itemId = '12345';

test
.nock(TEST_API_ROOT, api => api
.get(`/2.0/folders/${itemId}/trash`)
.reply(200, folderFixture)
)
.stdout()
.command([
'trash:get',
'folder',
itemId,
'--json',
'--token=test'
])
.it('should get information on a folder in trash (JSON Output)', ctx => {
let fixtureJSON = JSON.parse(folderFixture);
let outputJSON = JSON.parse(ctx.stdout);
assert.deepEqual(outputJSON, fixtureJSON);
});

test
.nock(TEST_API_ROOT, api => api
.get(`/2.0/files/${itemId}/trash`)
.reply(200, fileFixture)
)
.stdout()
.command([
'trash:get',
'file',
itemId,
'--json',
'--token=test'
])
.it('should get information on a file in trash (JSON Output)', ctx => {
let fixtureJSON = JSON.parse(fileFixture);
let outputJSON = JSON.parse(ctx.stdout);
assert.deepEqual(outputJSON, fixtureJSON);
});

test
.nock(TEST_API_ROOT, api => api
.get(`/2.0/web_links/${itemId}/trash`)
.reply(200, webLinkFixture)
)
.stdout()
.command([
'trash:get',
'web_link',
itemId,
'--json',
'--token=test'
])
.it('should get information on a web link in trash (JSON Output)', ctx => {
let fixtureJSON = JSON.parse(webLinkFixture);
let outputJSON = JSON.parse(ctx.stdout);
assert.deepEqual(outputJSON, fixtureJSON);
});
});

describe('trash:restore', () => {
let folderFixture = getFixture('trash/post_folders_id'),
fileFixture = getFixture('trash/post_files_id'),
webLinkFixture = getFixture('trash/post_web_links_id');

let itemId = '1234',
name = 'Contracts',
parentId = '0';

let expectedBody = {
name,
parent: { id: parentId }
};
test
.nock(TEST_API_ROOT, api => api
.post(`/2.0/folders/${itemId}`, expectedBody)
.reply(201, folderFixture)
)
.stdout()
.command([
'trash:restore',
'folder',
itemId,
`--name=${name}`,
`--parent-id=${parentId}`,
'--json',
'--token=test'
])
.it('should restore a folder from trash (JSON Output)', ctx => {
let fixtureJSON = JSON.parse(folderFixture);
let outputJSON = JSON.parse(ctx.stdout);
assert.deepEqual(outputJSON, fixtureJSON);
});

test
.nock(TEST_API_ROOT, api => api
.post(`/2.0/files/${itemId}`, expectedBody)
.reply(201, fileFixture)
)
.stdout()
.command([
'trash:restore',
'file',
itemId,
`--name=${name}`,
`--parent-id=${parentId}`,
'--json',
'--token=test'
])
.it('should restore a file from trash (JSON Output)', ctx => {
let fixtureJSON = JSON.parse(fileFixture);
let outputJSON = JSON.parse(ctx.stdout);
assert.deepEqual(outputJSON, fixtureJSON);
});

test
.nock(TEST_API_ROOT, api => api
.post(`/2.0/web_links/${itemId}`, expectedBody)
.reply(201, webLinkFixture)
)
.stdout()
.command([
'trash:restore',
'web_link',
itemId,
`--name=${name}`,
`--parent-id=${parentId}`,
'--json',
'--token=test'
])
.it('should restore a web link from trash (JSON Output)', ctx => {
let fixtureJSON = JSON.parse(webLinkFixture);
let outputJSON = JSON.parse(ctx.stdout);
assert.deepEqual(outputJSON, fixtureJSON);
});
});
});
60 changes: 60 additions & 0 deletions test/fixtures/trash/get_files_id_trash.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"type": "file",
"id": "12345",
"file_version": {
"type": "file_version",
"id": "11223344556677",
"sha1": "97b3dbba6eab7ad0f058240744c8636b7c7bea93"
},
"sequence_id": "1",
"etag": "1",
"sha1": "97b3dbba6eab7ad0f058240744c8636b7c7bea93",
"name": "test_file_download.txt",
"description": "",
"size": 106833,
"path_collection": {
"total_count": 2,
"entries": [
{
"type": "folder",
"id": "0",
"sequence_id": null,
"etag": null,
"name": "All Files"
}
]
},
"created_at": "2016-11-16T22:01:44-08:00",
"modified_at": "2016-11-16T22:01:51-08:00",
"trashed_at": "2012-12-12T10:53:43-08:00",
"purged_at": "2012-12-12T10:53:43-08:00",
"content_created_at": "2016-10-29T18:33:50-07:00",
"content_modified_at": "2016-10-29T18:33:50-07:00",
"created_by": {
"type": "user",
"id": "1357924680",
"name": "Owner",
"login": "owner@example.com"
},
"modified_by": {
"type": "user",
"id": "1357924680",
"name": "Owner",
"login": "owner@example.com"
},
"owned_by": {
"type": "user",
"id": "1357924680",
"name": "Owner",
"login": "owner@example.com"
},
"shared_link": null,
"parent": {
"type": "folder",
"id": "0",
"sequence_id": "0",
"etag": "0",
"name": "All files"
},
"item_status": "active"
}
Loading

0 comments on commit 74db947

Please sign in to comment.