Skip to content

Commit

Permalink
Fix: Show file undownloadable error for cached previews (#802)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyjin authored May 29, 2018
1 parent 70be84f commit 29318d8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
23 changes: 11 additions & 12 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -1028,18 +1028,6 @@ class Preview extends EventEmitter {
this.file = file;
this.logger.setFile(file);

// If file is not downloadable, trigger an error
if (file.is_download_available === false) {
const details = isBoxWebApp()
? {
linkText: __('link_contact_us'),
linkUrl: SUPPORT_URL
}
: {};
const error = new PreviewError(ERROR_CODE.NOT_DOWNLOADABLE, __('error_not_downloadable'), details);
throw error;
}

// Keep reference to previously cached file version
const cachedFile = getCachedFile(this.cache, { fileVersionId: responseFileVersionId });

Expand Down Expand Up @@ -1087,6 +1075,17 @@ class Preview extends EventEmitter {
return;
}

// Check if file is downloadable
if (this.file.is_download_available === false) {
const details = isBoxWebApp()
? {
linkText: __('link_contact_us'),
linkUrl: SUPPORT_URL
}
: {};
throw new PreviewError(ERROR_CODE.NOT_DOWNLOADABLE, __('error_not_downloadable'), details);
}

// Check if preview permissions exist
if (!checkPermission(this.file, PERMISSION_PREVIEW)) {
throw new PreviewError(ERROR_CODE.PERMISSIONS_PREVIEW, __('error_permissions'));
Expand Down
22 changes: 12 additions & 10 deletions src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1423,15 +1423,6 @@ describe('lib/Preview', () => {
expect(preview.logger.setFile).to.be.called;
});

it('should trigger error if file is not downloadable', () => {
stubs.file.is_download_available = false;
preview.handleFileInfoResponse(stubs.file);

const error = stubs.triggerError.getCall(0).args[0];
expect(error).to.be.instanceof(PreviewError);
expect(error.code).to.equal('error_file_not_downloadable');
});

it('should get the latest cache, then update it with the new file', () => {
stubs.getCachedFile.returns({
file_version: {
Expand Down Expand Up @@ -1570,18 +1561,29 @@ describe('lib/Preview', () => {
setType: sandbox.stub()
};

preview.file = {
is_download_available: true
};

stubs.emit = sandbox.stub(preview, 'emit');
stubs.attachViewerListeners = sandbox.stub(preview, 'attachViewerListeners');
preview.open = true;
});

it('should do nothing if the preview is closed', () => {
preview.open = false;

preview.loadViewer();
expect(stubs.destroy).to.not.be.called;
});

it('should trigger error if file is not downloadable', () => {
preview.file.is_download_available = false;
expect(() => preview.loadViewer()).to.throw(
PreviewError,
/Oops! It looks like something is wrong with this file./
);
});

it('should throw an error if user does not have permission to preview', () => {
stubs.checkPermission.withArgs(sinon.match.any, PERMISSION_PREVIEW).returns(false);
expect(() => preview.loadViewer()).to.throw(
Expand Down

0 comments on commit 29318d8

Please sign in to comment.