From cc09a61c05022e3c07d8444df6335a934f8bec35 Mon Sep 17 00:00:00 2001 From: Tony Jin Date: Wed, 7 Mar 2018 13:00:08 -0800 Subject: [PATCH] Fix: Preview error viewer in platform and IE11 (#696) - Contact Us redirect to Box support page should only show up in the Box web application - Fix non-wrapping error text in IE11 - Consolidate download checks into util function - Move file-specific util method into file.js --- src/lib/Preview.js | 29 ++-- src/lib/__tests__/Preview-test.js | 136 ++++++------------ src/lib/__tests__/RepStatus-test.js | 2 +- src/lib/__tests__/file-test.js | 65 ++++++++- src/lib/__tests__/util-test.js | 38 ++--- src/lib/file.js | 31 +++- src/lib/util.js | 22 ++- src/lib/viewers/error/PreviewError.scss | 1 + src/lib/viewers/error/PreviewErrorViewer.js | 10 +- .../__tests__/PreviewErrorViewer-test.js | 21 +-- src/lib/viewers/text/TextLoader.js | 2 +- .../viewers/text/__tests__/TextLoader-test.js | 14 +- 12 files changed, 187 insertions(+), 184 deletions(-) diff --git a/src/lib/Preview.js b/src/lib/Preview.js index ea902c06d..150049c2a 100644 --- a/src/lib/Preview.js +++ b/src/lib/Preview.js @@ -24,7 +24,8 @@ import { appendQueryParams, replacePlaceholders, stripAuthFromString, - isValidFileId + isValidFileId, + isBoxWebApp } from './util'; import { isDownloadHostBlocked, @@ -42,13 +43,13 @@ import { uncacheFile, isWatermarked, getCachedFile, - normalizeFileVersion + normalizeFileVersion, + canDownload } from './file'; import { API_HOST, APP_HOST, CLASS_NAVIGATION_VISIBILITY, - PERMISSION_DOWNLOAD, PERMISSION_PREVIEW, PREVIEW_SCRIPT_NAME, X_REP_HINT_BASE, @@ -475,7 +476,7 @@ class Preview extends EventEmitter { * @return {void} */ print() { - if (checkPermission(this.file, PERMISSION_DOWNLOAD) && checkFeature(this.viewer, 'print')) { + if (canDownload(this.file, this.options) && checkFeature(this.viewer, 'print')) { this.viewer.print(); } } @@ -489,7 +490,7 @@ class Preview extends EventEmitter { download() { const { apiHost, queryParams } = this.options; - if (!checkPermission(this.file, PERMISSION_DOWNLOAD)) { + if (!canDownload(this.file, this.options)) { return; } @@ -961,10 +962,13 @@ class Preview extends EventEmitter { // If file is not downloadable, trigger an error if (file.is_download_available === false) { - const error = new PreviewError(ERROR_CODE.NOT_DOWNLOADABLE, __('error_not_downloadable'), { - linkText: __('link_contact_us'), - linkUrl: SUPPORT_URL - }); + const details = isBoxWebApp() + ? { + linkText: __('link_contact_us'), + linkUrl: SUPPORT_URL + } + : {}; + const error = new PreviewError(ERROR_CODE.NOT_DOWNLOADABLE, __('error_not_downloadable'), details); throw error; } @@ -1021,7 +1025,7 @@ class Preview extends EventEmitter { } // Show download button if download permissions exist, options allow, and browser has ability - if (checkPermission(this.file, PERMISSION_DOWNLOAD) && this.options.showDownload && Browser.canDownload()) { + if (canDownload(this.file, this.options)) { this.ui.showLoadingDownloadButton(this.download); } @@ -1170,11 +1174,10 @@ class Preview extends EventEmitter { this.emitLoadMetrics(); // Show or hide print/download buttons - // canDownload is not supported by all of our browsers, so for now we need to check isMobile - if (checkPermission(this.file, PERMISSION_DOWNLOAD) && this.options.showDownload && Browser.canDownload()) { + if (canDownload(this.file, this.options)) { this.ui.showDownloadButton(this.download); - if (checkFeature(this.viewer, 'print') && !Browser.isMobile()) { + if (checkFeature(this.viewer, 'print')) { this.ui.showPrintButton(this.print); } } diff --git a/src/lib/__tests__/Preview-test.js b/src/lib/__tests__/Preview-test.js index d4b2f84ec..4338ecfbd 100644 --- a/src/lib/__tests__/Preview-test.js +++ b/src/lib/__tests__/Preview-test.js @@ -9,7 +9,7 @@ import PreviewError from '../PreviewError'; import * as file from '../file'; import * as util from '../util'; import * as dr from '../downloadReachability'; -import { API_HOST, CLASS_NAVIGATION_VISIBILITY } from '../constants'; +import { API_HOST, CLASS_NAVIGATION_VISIBILITY, PERMISSION_PREVIEW } from '../constants'; import { VIEWER_EVENT, ERROR_CODE, LOAD_METRIC, PREVIEW_METRIC } from '../events'; import Timer from '../Timer'; @@ -703,36 +703,33 @@ describe('lib/Preview', () => { describe('print()', () => { beforeEach(() => { - stubs.checkPermission = sandbox.stub(file, 'checkPermission').returns(false); - stubs.checkFeature = sandbox.stub(file, 'checkFeature').returns(false); + stubs.canDownload = sandbox.stub(file, 'canDownload'); + stubs.checkFeature = sandbox.stub(file, 'checkFeature'); preview.viewer = { print: sandbox.stub() }; }); - it('should print if the feature and permissions exist', () => { - stubs.checkPermission.returns(true); + it('should print if file can be downloaded and feature exists', () => { + stubs.canDownload.returns(true); stubs.checkFeature.returns(true); preview.print(); expect(preview.viewer.print).to.be.called; }); - it('should not print if feature does not exists', () => { - stubs.checkFeature.returns(true); + it('should not print if feature does not exist', () => { + stubs.canDownload.returns(true); + stubs.checkFeature.returns(false); preview.print(); expect(preview.viewer.print).to.not.be.called; }); - it('should not print if permissions do not exist', () => { - stubs.checkPermission.returns(true); - - preview.print(); - expect(preview.viewer.print).to.not.be.called; - }); + it('should not print if file cannot be downloaded', () => { + stubs.canDownload.returns(false); + stubs.checkFeature.returns(false); - it('should not print if permissions or feature do not exist', () => { preview.print(); expect(preview.viewer.print).to.not.be.called; }); @@ -747,8 +744,7 @@ describe('lib/Preview', () => { }); stubs.reachabilityPromise = Promise.resolve(true); - - stubs.checkPermission = sandbox.stub(file, 'checkPermission'); + stubs.canDownload = sandbox.stub(file, 'canDownload'); stubs.get = sandbox.stub(util, 'get').returns(stubs.promise); stubs.get = sandbox.stub(dr, 'setDownloadReachability').returns(stubs.reachabilityPromise); stubs.openUrlInsideIframe = sandbox.stub(util, 'openUrlInsideIframe'); @@ -759,15 +755,14 @@ describe('lib/Preview', () => { stubs.replaceDownloadHostWithDefault = sandbox.stub(dr, 'replaceDownloadHostWithDefault').returns('default'); }); - it('should not do anything if there is no download permission', () => { - stubs.checkPermission.returns(false); - + it('should not do anything if file cannot be downloaded', () => { + stubs.canDownload.returns(false); preview.download(); expect(stubs.openUrlInsideIframe).to.not.be.called; }); it('open the default download URL in an iframe if the custom host is blocked or if we were given the default', () => { - stubs.checkPermission.returns(true); + stubs.canDownload.returns(true); stubs.isDownloadHostBlocked.returns(true); stubs.isCustomDownloadHost.returns(true); @@ -787,7 +782,7 @@ describe('lib/Preview', () => { it('should check download reachability and fallback if we do not know the status of our custom host', () => { - stubs.checkPermission.returns(true); + stubs.canDownload.returns(true); stubs.isCustomDownloadHost.returns(true); preview.download(); @@ -1449,7 +1444,7 @@ describe('lib/Preview', () => { stubs.destroy = sandbox.stub(preview, 'destroy'); stubs.checkPermission = sandbox.stub(file, 'checkPermission').returns(true); - stubs.canDownload = sandbox.stub(Browser, 'canDownload').returns(false); + stubs.canDownload = sandbox.stub(file, 'canDownload').returns(false); stubs.showLoadingDownloadButton = sandbox.stub(preview.ui, 'showLoadingDownloadButton'); stubs.loadPromiseResolve = Promise.resolve(); stubs.determineRepresentationStatusPromise = Promise.resolve(); @@ -1484,49 +1479,24 @@ describe('lib/Preview', () => { expect(stubs.destroy).to.not.be.called; }); - it('should throw an error if there is no preview permission', () => { - stubs.checkPermission.returns(false); + 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( PreviewError, /We're sorry, you don't have permission to preview this file./ ); }); - it('should show the loading download button if there are sufficient permissions and support', () => { - stubs.checkPermission.withArgs(sinon.match.any, 'can_download').returns(false); - preview.options.showDownload = false; - - preview.loadViewer({}); - expect(stubs.showLoadingDownloadButton).to.not.be.called; - preview.destroy(); - - stubs.checkPermission.withArgs(sinon.match.any, 'can_download').returns(true); - - preview.loadViewer({}); - expect(stubs.showLoadingDownloadButton).to.not.be.called; - preview.destroy(); - - stubs.checkPermission.withArgs(sinon.match.any, 'can_download').returns(false); - preview.options.showDownload = true; - + it('should show the loading download button if file can be downloaded', () => { + stubs.canDownload.returns(true); preview.loadViewer({}); - expect(stubs.showLoadingDownloadButton).to.not.be.called; - preview.destroy(); + expect(stubs.showLoadingDownloadButton).to.be.called; + }); - stubs.checkPermission.withArgs(sinon.match.any, 'can_download').returns(true); - preview.options.showDownload = true; + it('should not show the loading download button if file can\'t be downloaded', () => { stubs.canDownload.returns(false); - preview.destroy(); - preview.loadViewer({}); expect(stubs.showLoadingDownloadButton).to.not.be.called; - - stubs.checkPermission.withArgs(sinon.match.any, 'can_download').returns(true); - preview.options.showDownload = true; - stubs.canDownload.returns(true); - - preview.loadViewer({}); - expect(stubs.showLoadingDownloadButton).to.be.called; }); it('should throw an unsupported error if there is no loader for general file types', () => { @@ -1701,10 +1671,9 @@ describe('lib/Preview', () => { describe('finishLoading()', () => { beforeEach(() => { - stubs.checkPermission = sandbox.stub(file, 'checkPermission'); + stubs.canDownload = sandbox.stub(file, 'canDownload'); stubs.checkFeature = sandbox.stub(file, 'checkFeature'); stubs.isMobile = sandbox.stub(Browser, 'isMobile'); - stubs.canDownload = sandbox.stub(Browser, 'canDownload'); stubs.showDownloadButton = sandbox.stub(preview.ui, 'showDownloadButton'); stubs.showPrintButton = sandbox.stub(preview.ui, 'showPrintButton'); stubs.hideLoadingIndicator = sandbox.stub(preview.ui, 'hideLoadingIndicator'); @@ -1727,64 +1696,39 @@ describe('lib/Preview', () => { }; preview.logger = stubs.logger; - preview.options.showDownload = true; - stubs.canDownload.returns(true); - stubs.checkPermission.returns(true); - stubs.checkFeature.returns(true); }); - it('should only show download button if there is download permission', () => { - stubs.checkPermission.returns(false); - - preview.finishLoading(); - expect(stubs.showDownloadButton).to.not.be.called; - - stubs.checkPermission.returns(true); - - preview.finishLoading(); - expect(stubs.showDownloadButton).to.be.calledWith(preview.download); - }); - - it('should show download button if it is requested in the options', () => { - preview.options.showDownload = false; - - preview.finishLoading(); - expect(stubs.showDownloadButton).to.not.be.called; - - preview.options.showDownload = true; - + it('should show download button if file can be downloaded', () => { + stubs.canDownload.returns(true); preview.finishLoading(); - expect(stubs.showDownloadButton).to.be.calledWith(preview.download); + expect(stubs.showDownloadButton).to.be.called; }); - it('should show download button if download is supported by browser', () => { + it('should not show download button if file can\'t be downloaded', () => { stubs.canDownload.returns(false); - preview.finishLoading(); expect(stubs.showDownloadButton).to.not.be.called; + }); + it('should show print button if print is supported', () => { + stubs.checkFeature.withArgs(sinon.match.any, 'print').returns(true); stubs.canDownload.returns(true); - preview.finishLoading(); - expect(stubs.showDownloadButton).to.be.called; + expect(stubs.showPrintButton).to.be.called; + }); + it('should not show print button if print is not supported', () => { + stubs.checkFeature.withArgs(sinon.match.any, 'print').returns(false); stubs.canDownload.returns(true); - stubs.isMobile.returns(false); - preview.finishLoading(); - expect(stubs.showDownloadButton).to.be.calledWith(preview.download); + expect(stubs.showPrintButton).to.not.be.called; }); - it('should show print button if print is supported', () => { - stubs.checkFeature.returns(false); - + it('should not show print button if file can\'t be downloaded', () => { + stubs.checkFeature.withArgs(sinon.match.any, 'print').returns(true); + stubs.canDownload.returns(false); preview.finishLoading(); expect(stubs.showPrintButton).to.not.be.called; - - stubs.checkFeature.returns(true); - - preview.finishLoading(); - expect(stubs.showPrintButton).to.be.called; }); it('should increment the preview count', () => { diff --git a/src/lib/__tests__/RepStatus-test.js b/src/lib/__tests__/RepStatus-test.js index c7102508a..476d4dbd7 100644 --- a/src/lib/__tests__/RepStatus-test.js +++ b/src/lib/__tests__/RepStatus-test.js @@ -109,7 +109,7 @@ describe('lib/RepStatus', () => { }) ); - sandbox.mock(window).expects('clearTimeout').withArgs(repStatus.statusTimeout); + sandbox.mock(window).expects('clearTimeout'); return repStatus.updateStatus().then(() => { expect(repStatus.representation.status.state).to.equal(state); diff --git a/src/lib/__tests__/file-test.js b/src/lib/__tests__/file-test.js index ab07cfd45..4a8d4f93e 100644 --- a/src/lib/__tests__/file-test.js +++ b/src/lib/__tests__/file-test.js @@ -1,5 +1,6 @@ /* eslint-disable no-unused-expressions */ import Cache from '../Cache'; +import Browser from '../Browser'; import { getURL, getDownloadURL, @@ -11,7 +12,9 @@ import { uncacheFile, getRepresentation, normalizeFileVersion, - getCachedFile + getCachedFile, + isVeraProtectedFile, + canDownload } from '../file'; const sandbox = sinon.sandbox.create(); @@ -324,4 +327,64 @@ describe('lib/file', () => { expect(cache.get).to.not.be.called; }); }); + + describe('isVeraProtectedFile()', () => { + [ + 'some.vera.pdf.html', + '.vera.test.html', + 'blah.vera..html', + 'another.vera.3.html', + 'test.vera.html' + ].forEach((fileName) => { + it('should return true if file is named like a Vera-protected file', () => { + expect(isVeraProtectedFile({ name: fileName })).to.be.true; + }); + }); + + [ + 'vera.pdf.html', + 'test.vera1.pdf.html', + 'blah.vera..htm', + 'another.verahtml', + ].forEach((fileName) => { + it('should return false if file is not named like a Vera-protected file', () => { + expect(isVeraProtectedFile({ name: fileName })).to.be.false; + }); + }); + }); + + describe('canDownload()', () => { + let file; + let options; + + beforeEach(() => { + file = { + is_download_available: false, + permissions: { + can_download: false + } + }; + options = { + showDownload: false + }; + }); + + [ + [false, false, false, false, false], + [false, false, false, true, false], + [false, false, true, false, false], + [false, true, false, false, false], + [true, false, false, false, false], + [true, true, true, true, true], + ].forEach((isDownloadable, isDownloadEnabled, havePermission, isBrowserSupported, expectedResult) => { + it('should only return true if all of: file is downloadable, download is enabled, user has permissions, and browser can download is true', () => { + file.permissions.can_download = havePermission; + file.is_download_available = isDownloadable + options.showDownload = isDownloadable; + sandbox.stub(Browser, 'canDownload').returns(isBrowserSupported); + + expect(canDownload(file, options)).to.equal(expectedResult); + }); + }); + }); }); diff --git a/src/lib/__tests__/util-test.js b/src/lib/__tests__/util-test.js index a6b451515..747dec5b0 100644 --- a/src/lib/__tests__/util-test.js +++ b/src/lib/__tests__/util-test.js @@ -2,7 +2,6 @@ import 'whatwg-fetch'; import fetchMock from 'fetch-mock'; import * as util from '../util'; -import { stripAuthFromString } from '../util'; const sandbox = sinon.sandbox.create(); @@ -621,31 +620,6 @@ describe('lib/util', () => { }); }); - describe('isVeraProtectedFile()', () => { - [ - 'some.vera.pdf.html', - '.vera.test.html', - 'blah.vera..html', - 'another.vera.3.html', - 'test.vera.html' - ].forEach((fileName) => { - it('should return true if file is named like a Vera-protected file', () => { - expect(util.isVeraProtectedFile({ name: fileName })).to.be.true; - }); - }); - - [ - 'vera.pdf.html', - 'test.vera1.pdf.html', - 'blah.vera..htm', - 'another.verahtml', - ].forEach((fileName) => { - it('should return false if file is not named like a Vera-protected file', () => { - expect(util.isVeraProtectedFile({ name: fileName })).to.be.false; - }); - }); - }); - describe('setDimensions()', () => { it('should set dimensions for the specified element', () => { const element = document.createElement('div'); @@ -797,13 +771,13 @@ describe('lib/util', () => { const random = `here's my string ${accessToken} khjfsadlkjfsad`; const randomFiltered = `here's my string ${accessFiltered}`; // It strips everything starting at 'access_token=' - expect(stripAuthFromString(query)).to.equal(queryFiltered); - expect(stripAuthFromString(random)).to.equal(randomFiltered); + expect(util.stripAuthFromString(query)).to.equal(queryFiltered); + expect(util.stripAuthFromString(random)).to.equal(randomFiltered); }); it('should return passed in param if not string', () => { const obj = { foo: 'bar' }; - expect(stripAuthFromString(obj)).to.equal(obj); + expect(util.stripAuthFromString(obj)).to.equal(obj); }); }); @@ -859,4 +833,10 @@ describe('lib/util', () => { expect(util.isValidFileId('1234foo')).to.be.false; }); }); + + describe('isBoxWebApp()', () => { + it('should return false when window location is not a Box domain', () => { + expect(util.isBoxWebApp()).to.be.false; + }); + }); }); diff --git a/src/lib/file.js b/src/lib/file.js index c2aa3b8c3..0b5d4b847 100644 --- a/src/lib/file.js +++ b/src/lib/file.js @@ -1,5 +1,6 @@ +import Browser from './Browser'; import { getProp, appendQueryParams } from './util'; -import { ORIGINAL_REP_NAME } from './constants'; +import { ORIGINAL_REP_NAME, PERMISSION_DOWNLOAD } from './constants'; // List of Box Content API fields that the Preview library requires for every file. Updating this list is most likely // a breaking change and should be done with care. Clients that leverage functionality dependent on this format @@ -249,3 +250,31 @@ export function getCachedFile(cache, { fileId, fileVersionId }) { return null; } + +/** + * Check to see if file is a Vera-protected file. + * + * @public + * @param {Object} file - File to check + * @return {boolean} Whether file is a Vera-protected HTML file + */ +export function isVeraProtectedFile(file) { + // Vera protected files will match this regex + return /.*\.(vera\..*|vera)\.html/i.test(file.name); +} + +/** + * Helper to determine whether a file can be downloaded based on permissions, file status, browser capability, and + * Preview options. + * + * @param {Object} file - Box file object + * @param {Object} previewOptions - Preview options + * @return {boolean} Whether file can be downloaded + */ +export function canDownload(file, previewOptions) { + const { is_download_available: isFileDownloadable } = file; + const { showDownload: isDownloadEnabled } = previewOptions; + const havePermission = checkPermission(file, PERMISSION_DOWNLOAD); + + return havePermission && isFileDownloadable && isDownloadEnabled && Browser.canDownload(); +} diff --git a/src/lib/util.js b/src/lib/util.js index a6b00c69b..e70f576b2 100644 --- a/src/lib/util.js +++ b/src/lib/util.js @@ -683,18 +683,6 @@ export function requires360Viewer(file) { return basename.endsWith('.360'); } -/** - * Check to see if file is a Vera-protected file. - * - * @public - * @param {Object} file - File to check - * @return {boolean} Whether file is a Vera-protected HTML file - */ -export function isVeraProtectedFile(file) { - // Vera protected files will match this regex - return /.*\.(vera\..*|vera)\.html/i.test(file.name); -} - /** * Set width/height for an element. * @@ -905,3 +893,13 @@ export function isValidFileId(fileId) { // Tests that the string or number contains all numbers return /^\d+$/.test(fileId); } + +/** + * Returns whether Preview is running inside the Box web application. This should be used sparingly since Preview + * should function based only on the provided options and not change functionality depending on environment. + * + * @return {boolean} Is Preview running in the Box WebApp + */ +export function isBoxWebApp() { + return (window.location.hostname || '').indexOf('app.box.com') !== -1; +} diff --git a/src/lib/viewers/error/PreviewError.scss b/src/lib/viewers/error/PreviewError.scss index cfba5cfa9..b32ed62c7 100644 --- a/src/lib/viewers/error/PreviewError.scss +++ b/src/lib/viewers/error/PreviewError.scss @@ -5,6 +5,7 @@ font-size: 14px; height: 247px; // Error icon + text + optional download button text-align: center; + width: 100%; // IE11 flex width bug - https://caniuse.com/#search=flex .bp-error-text { padding: 0 80px; diff --git a/src/lib/viewers/error/PreviewErrorViewer.js b/src/lib/viewers/error/PreviewErrorViewer.js index 9d01be37e..dfb639029 100644 --- a/src/lib/viewers/error/PreviewErrorViewer.js +++ b/src/lib/viewers/error/PreviewErrorViewer.js @@ -1,8 +1,6 @@ import BaseViewer from '../BaseViewer'; -import Browser from '../../Browser'; import PreviewError from '../../PreviewError'; -import { checkPermission } from '../../file'; -import { PERMISSION_DOWNLOAD } from '../../constants'; +import { canDownload } from '../../file'; import { getIconFromExtension, getIconFromName } from '../../icons/icons'; import { ERROR_CODE, VIEWER_EVENT } from '../../events'; import { stripAuthFromString } from '../../util'; @@ -32,7 +30,7 @@ class PreviewErrorViewer extends BaseViewer { this.iconEl = this.infoEl.appendChild(document.createElement('div')); this.iconEl.className = 'bp-icon bp-icon-file'; - this.messageEl = this.infoEl.appendChild(document.createElement('div')); + this.messageEl = this.infoEl.appendChild(document.createElement('p')); this.messageEl.className = 'bp-error-text'; } @@ -75,7 +73,7 @@ class PreviewErrorViewer extends BaseViewer { : new PreviewError(ERROR_CODE.GENERIC, __('error_generic'), {}, err.message); const { displayMessage, details, message } = error; - const { file, showDownload } = this.options; + const { file } = this.options; this.icon = getIconFromName('FILE_DEFAULT'); @@ -100,7 +98,7 @@ class PreviewErrorViewer extends BaseViewer { // Add optional link or download button if (details && details.linkText && details.linkUrl) { this.addLinkButton(details.linkText, details.linkUrl); - } else if (checkPermission(file, PERMISSION_DOWNLOAD) && showDownload && Browser.canDownload()) { + } else if (canDownload(file, this.options)) { this.addDownloadButton(); } diff --git a/src/lib/viewers/error/__tests__/PreviewErrorViewer-test.js b/src/lib/viewers/error/__tests__/PreviewErrorViewer-test.js index 254ab7b5b..3f8f61afd 100644 --- a/src/lib/viewers/error/__tests__/PreviewErrorViewer-test.js +++ b/src/lib/viewers/error/__tests__/PreviewErrorViewer-test.js @@ -4,7 +4,6 @@ import BaseViewer from '../../BaseViewer'; import Browser from '../../../Browser'; import PreviewError from '../../../PreviewError'; import * as file from '../../../file'; -import { PERMISSION_DOWNLOAD } from '../../../constants'; import * as icons from '../../../icons/icons'; import { VIEWER_EVENT } from '../../../events'; @@ -94,30 +93,18 @@ describe('lib/viewers/error/PreviewErrorViewer', () => { expect(error.addDownloadButton).to.not.be.called; }); - it('should add download button if file has permissions and showDownload option is set', () => { + it('should add download button if file can be downloaded', () => { sandbox.stub(error, 'addDownloadButton'); - sandbox.stub(file, 'checkPermission').withArgs(error.options.file, PERMISSION_DOWNLOAD).returns(true); - error.options.showDownload = true; + sandbox.stub(file, 'canDownload').returns(true); error.load('reason'); expect(error.addDownloadButton).to.be.called; }); - it('should not add download button if file does not have download permissions', () => { + it('should not add download button if file can\'t be downloaded', () => { sandbox.stub(error, 'addDownloadButton'); - sandbox.stub(file, 'checkPermission').withArgs(error.options.file, PERMISSION_DOWNLOAD).returns(false); - error.options.showDownload = true; - - error.load('reason'); - - expect(error.addDownloadButton).to.not.be.called; - }); - - it('should not add download button if showDownload option is not set', () => { - sandbox.stub(error, 'addDownloadButton'); - sandbox.stub(file, 'checkPermission').withArgs(error.options.file, PERMISSION_DOWNLOAD).returns(true); - error.options.showDownload = false; + sandbox.stub(file, 'canDownload').returns(false); error.load('reason'); diff --git a/src/lib/viewers/text/TextLoader.js b/src/lib/viewers/text/TextLoader.js index 418c1d219..3a4d68f5d 100644 --- a/src/lib/viewers/text/TextLoader.js +++ b/src/lib/viewers/text/TextLoader.js @@ -2,7 +2,7 @@ import AssetLoader from '../AssetLoader'; import PlainTextViewer from './PlainTextViewer'; import MarkdownViewer from './MarkdownViewer'; import CSVViewer from './CSVViewer'; -import { isVeraProtectedFile } from '../../util'; +import { isVeraProtectedFile } from '../../file'; import { ORIGINAL_REP_NAME } from '../../constants'; import { HTML_EXTENSIONS, TXT_EXTENSIONS } from '../../extensions'; diff --git a/src/lib/viewers/text/__tests__/TextLoader-test.js b/src/lib/viewers/text/__tests__/TextLoader-test.js index a56eeac99..b37129f4c 100644 --- a/src/lib/viewers/text/__tests__/TextLoader-test.js +++ b/src/lib/viewers/text/__tests__/TextLoader-test.js @@ -1,8 +1,8 @@ /* eslint-disable no-unused-expressions */ import TextLoader from '../TextLoader'; -import * as util from '../../../util'; +import * as file from '../../../file'; -let file; +let stubFile; const sandbox = sinon.sandbox.create(); describe('lib/viewers/text/TextLoader', () => { @@ -14,7 +14,7 @@ describe('lib/viewers/text/TextLoader', () => { } ]; - file = { + stubFile = { extension: 'html', representations: { entries: [ @@ -32,13 +32,13 @@ describe('lib/viewers/text/TextLoader', () => { describe('determineViewer()', () => { it('should return viewer if file is not a Vera-protected file', () => { - sandbox.stub(util, 'isVeraProtectedFile').returns(false); - expect(TextLoader.determineViewer(file)).to.equal(TextLoader.viewers[0]); + sandbox.stub(file, 'isVeraProtectedFile').returns(false); + expect(TextLoader.determineViewer(stubFile)).to.equal(TextLoader.viewers[0]); }); it('should return undefined if file is a Vera-protected file', () => { - sandbox.stub(util, 'isVeraProtectedFile').returns(true); - expect(TextLoader.determineViewer(file)).to.equal(undefined); + sandbox.stub(file, 'isVeraProtectedFile').returns(true); + expect(TextLoader.determineViewer(stubFile)).to.equal(undefined); }); }); });