Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Show file undownloadable error for cached previews #802

Merged
merged 1 commit into from
May 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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