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

Chore: Add util tests and fix file tests #709

Merged
merged 1 commit into from
Mar 13, 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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"optimize-css-assets-webpack-plugin": "^3.2.0",
"phantomjs-prebuilt": "^2.1.16",
"postcss-loader": "^2.0.9",
"postcss-sass": "^0.3.0",
"prettier": "^1.8.2",
"prettier-eslint-cli": "^4.4.2",
"raw-loader": "^0.5.1",
Expand Down Expand Up @@ -130,6 +131,5 @@
"prettier-eslint --print-width 120 --single-quote --tab-width 4 --write",
"git add"
]
},
"dependencies": {}
}
}
21 changes: 21 additions & 0 deletions src/lib/Location.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Location {
/**
* Returns window hostname to simplify testing.
*
* @return {string} Window hostname
*/
static getHostname() {
return window.location.hostname || '';
}

/**
* Returns window origin to simplify testing.
*
* @return {string} Window location
*/
static getOrigin() {
return window.location.origin || '';
}
}

export default Location;
2 changes: 1 addition & 1 deletion src/lib/__tests__/file-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ describe('lib/file', () => {
[false, true, false, false, false],
[true, false, false, false, false],
[true, true, true, true, true],
].forEach((isDownloadable, isDownloadEnabled, havePermission, isBrowserSupported, expectedResult) => {
].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
Expand Down
18 changes: 15 additions & 3 deletions src/lib/__tests__/util-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-unused-expressions */
import 'whatwg-fetch';
import fetchMock from 'fetch-mock';
import Location from '../Location';
import * as util from '../util';

const sandbox = sinon.sandbox.create();
Expand Down Expand Up @@ -835,8 +836,19 @@ describe('lib/util', () => {
});

describe('isBoxWebApp()', () => {
it('should return false when window location is not a Box domain', () => {
expect(util.isBoxWebApp()).to.be.false;
});
[
['https://test.app.box.com', true],
['https://foo.ent.box.com', true],
['https://bar.app.boxcn.net', true],
['https://baz.ent.boxenterprise.net', true],
['https://haha.box.net', false],
['https://some.other.domain', false]
]
.forEach(([hostname, expectedResult]) => {
it('should return true when window location is a Box domain', () => {
sandbox.stub(Location, 'getHostname').returns(hostname);
expect(util.isBoxWebApp()).to.equal(expectedResult);
});
})
});
});
4 changes: 3 additions & 1 deletion src/lib/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Uri from 'jsuri';
import 'whatwg-fetch';

import Location from './Location';
import { isDownloadHostBlocked, replaceDownloadHostWithDefault } from './downloadReachability';

const HEADER_CLIENT_NAME = 'X-Box-Client-Name';
Expand Down Expand Up @@ -901,5 +902,6 @@ export function isValidFileId(fileId) {
* @return {boolean} Is Preview running in the Box WebApp
*/
export function isBoxWebApp() {
return (window.location.hostname || '').indexOf('app.box.com') !== -1;
const boxHostnameRegex = /(app|ent)\.(box\.com|boxcn\.net|boxenterprise\.net)/;
return boxHostnameRegex.test(Location.getHostname());
}
5 changes: 3 additions & 2 deletions src/lib/viewers/office/OfficeViewer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import BaseViewer from '../BaseViewer';
import Browser from '../../Browser';
import Location from '../../Location';
import Popup from '../../Popup';
import { CLASS_HIDDEN } from '../../constants';
import { getRepresentation } from '../../file';
Expand Down Expand Up @@ -179,7 +180,7 @@ class OfficeViewer extends BaseViewer {
formEl.submit();

// Tell Office Online that we are ready to receive messages
iframeEl.contentWindow.postMessage(MESSAGE_HOST_READY, window.location.origin);
iframeEl.contentWindow.postMessage(MESSAGE_HOST_READY, Location.getOrigin());
} else {
iframeEl.src = this.setupRunmodeURL(appHost, file.id, sharedLink);
}
Expand Down Expand Up @@ -280,7 +281,7 @@ class OfficeViewer extends BaseViewer {
createFormElement(apiHost, fileId, sharedLink, locale) {
// Setting the action URL
const WOPISrc = this.setupWOPISrc(apiHost, fileId, sharedLink);
const origin = { origin: window.location.origin };
const origin = { origin: Location.getOrigin() };
const formEl = this.containerEl.appendChild(document.createElement('form'));
// We pass our origin in the sessionContext so that Microsoft will pass
// this to the checkFileInfo endpoint. From their we can set it as the
Expand Down
5 changes: 4 additions & 1 deletion src/lib/viewers/office/__tests__/OfficeViewer-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-unused-expressions */
import BaseViewer from '../../BaseViewer';
import Browser from '../../../Browser';
import Location from '../../../Location';
import OfficeViewer from '../OfficeViewer';
import * as util from '../../../util';
import { CLASS_HIDDEN } from '../../../constants';
Expand Down Expand Up @@ -283,8 +284,10 @@ describe('lib/viewers/office/OfficeViewer', () => {

describe('createFormElement()', () => {
beforeEach(() => {
const origin = 'someOrigin';
sandbox.stub(Location, 'getOrigin').returns(origin);
stubs.setupWOPISrc = sandbox.stub(office, 'setupWOPISrc').returns('src');
stubs.sessionContext = JSON.stringify({ origin: window.location.origin });
stubs.sessionContext = JSON.stringify({ origin });
stubs.formEl = office.createFormElement(
office.options.apiHost,
office.options.file.id,
Expand Down
Loading