Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
Split off FxDesktopBroker related redirection tests into their own mo…
Browse files Browse the repository at this point in the history
…dule.
  • Loading branch information
Shane Tomlinson committed Sep 8, 2014
1 parent dabf10e commit 4151dac
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 67 deletions.
36 changes: 22 additions & 14 deletions app/scripts/lib/app-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ function (

this._window = options.window || window;
this._router = options.router;
this._relier = options.relier;
this._broker = options.broker;

this._history = options.history || Backbone.history;
this._configLoader = new ConfigLoader();
Expand Down Expand Up @@ -115,24 +117,30 @@ function (
},

initializeRelier: function () {
this._relier = new Relier({
window: this._window
});
return this._relier.fetch();
if (! this._relier) {
this._relier = new Relier({
window: this._window
});

return this._relier.fetch();
}
},

initializeBroker: function () {
if (this._searchParam('context') === 'fx_desktop_v1') {
this._broker = new FxDesktopBroker({
window: this._window,
relier: this._relier
});
} else {
this._broker = new Broker({
relier: this._relier
});
if (! this._broker) {
if (this._searchParam('context') === 'fx_desktop_v1') {
this._broker = new FxDesktopBroker({
window: this._window,
relier: this._relier
});
} else {
this._broker = new Broker({
relier: this._relier
});
}

return this._broker.fetch();
}
return this._broker.fetch();
},

initializeRouter: function () {
Expand Down
5 changes: 4 additions & 1 deletion app/scripts/lib/auth-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ function () {
1012: t('Year of birth required'),
1013: t('A usable image was not found'),
1014: t('Could not initialize camera'),
1015: t('Valid URL required')
1015: t('Valid URL required'),

// No response from browser, the user string is Unexpected Error.
1016: t('Unexpected Error')
};

return {
Expand Down
6 changes: 5 additions & 1 deletion app/scripts/models/brokers/fx-desktop.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
* A broker that knows how to communicate with Firefox when used for Sync.
*/

'use strict';

define([
Expand All @@ -20,7 +24,7 @@ define([

this._window = options.window || window;

this._channel = new FxDesktopChannel();
this._channel = options.channel || new FxDesktopChannel();
this._channel.init({
window: this._window
});
Expand Down
65 changes: 15 additions & 50 deletions app/tests/spec/lib/app-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,38 @@

define([
'chai',
'sinon',
'lib/app-start',
'lib/session',
'lib/constants',
'lib/promise',
'models/brokers/broker',
'../../mocks/window',
'../../mocks/router',
'../../mocks/history'
],
function (chai, AppStart, Session, Constants, WindowMock, RouterMock, HistoryMock) {
function (chai, sinon, AppStart, Session, Constants, p, Broker, WindowMock, RouterMock, HistoryMock) {
/*global describe, beforeEach, it*/
var assert = chai.assert;

describe('lib/app-start', function () {
var windowMock;
var routerMock;
var historyMock;
var brokerMock;
var appStart;


function getFxDesktopContextSearchString() {
return '?context=' + Constants.FX_DESKTOP_CONTEXT;
}

function dispatchEventFromWindowMock(status, data) {
windowMock.dispatchEvent({
detail: {
command: 'message',
data: {
status: status,
data: data
}
}
});
}

beforeEach(function () {
windowMock = new WindowMock();
routerMock = new RouterMock();
historyMock = new HistoryMock();
brokerMock = new Broker();

appStart = new AppStart({
window: windowMock,
router: routerMock,
history: historyMock
history: historyMock,
broker: brokerMock
});
});

Expand Down Expand Up @@ -79,13 +68,9 @@ function (chai, AppStart, Session, Constants, WindowMock, RouterMock, HistoryMoc
});
});

it('redirects to /settings if the context is FXA_DESKTOP and user is signed in', function () {
windowMock.location.search = getFxDesktopContextSearchString();

windowMock.on('session_status', function () {
dispatchEventFromWindowMock('session_status', {
email: 'testuser@testuser.com'
});
it('redirects to the start page specified by the broker', function () {
sinon.stub(brokerMock, 'selectStartPage', function () {
return p('settings');
});

return appStart.startApp()
Expand All @@ -94,38 +79,18 @@ function (chai, AppStart, Session, Constants, WindowMock, RouterMock, HistoryMoc
});
});

it('redirects to /signup if the context is FXA_DESKTOP, no email is set, and no pathname is specified', function () {
windowMock.location.search = getFxDesktopContextSearchString();

windowMock.on('session_status', function () {
// no data from session_status signifies no user is signed in.
dispatchEventFromWindowMock('session_status', {
});
it('does not redirect if the broker does not return a start page', function () {
sinon.stub(brokerMock, 'selectStartPage', function () {
return p();
});

routerMock.page = 'signup';
return appStart.startApp()
.then(function () {
assert.equal(routerMock.page, 'signup');
});
});

it('does not redirect the user if a route is present in the path', function () {
windowMock.location.search = getFxDesktopContextSearchString();
windowMock.location.pathname = '/signin';
routerMock.page = 'signin';

windowMock.on('session_status', function () {
// no data from session_status signifies no user is signed in.
dispatchEventFromWindowMock('session_status', {
});
});

return appStart.startApp()
.then(function () {
assert.equal(routerMock.page, 'signin');
});
});

it('sets the session service from a client_id query parameter', function () {
windowMock.location.search = '?client_id=testing';
return appStart.startApp()
Expand Down
83 changes: 83 additions & 0 deletions app/tests/spec/models/brokers/fx-desktop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

'use strict';

define([
'chai',
'underscore',
'models/brokers/fx-desktop',
'lib/constants',
'../../../mocks/window'
], function (chai, _, FxDesktopBroker, Constants, WindowMock) {
var assert = chai.assert;

describe('models/brokers/fx-desktop', function () {
var windowMock;
var broker;

function dispatchEventFromWindowMock(status, data) {
windowMock.dispatchEvent({
detail: {
command: 'message',
data: {
status: status,
data: data
}
}
});
}

beforeEach(function () {
windowMock = new WindowMock();
broker = new FxDesktopBroker({
window: windowMock
});
});

describe('selectStartPage', function () {
it('returns /settings if is signed in', function () {
windowMock.on('session_status', function () {
dispatchEventFromWindowMock('session_status', {
email: 'testuser@testuser.com'
});
});

return broker.fetch()
.then(_.bind(broker.selectStartPage, broker))
.then(function (page) {
assert.equal(page, 'settings');
});
});

it('returns /signup if no email is set, and no pathname is specified', function () {
windowMock.on('session_status', function () {
// no data from session_status signifies no user is signed in.
dispatchEventFromWindowMock('session_status', {});
});

return broker.fetch()
.then(_.bind(broker.selectStartPage, broker))
.then(function (page) {
assert.equal(page, 'signup');
});
});

it('returns nothing if a route is present in the path', function () {
windowMock.location.pathname = '/signin';

windowMock.on('session_status', function () {
// no data from session_status signifies no user is signed in.
dispatchEventFromWindowMock('session_status', {});
});

return broker.fetch()
.then(_.bind(broker.selectStartPage, broker))
.then(function (page) {
assert.isUndefined(page);
});
});
});
});
});
3 changes: 2 additions & 1 deletion app/tests/test_start.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ function (Translator, Session, FxaClientWrapper) {
'../tests/spec/views/mixins/floating-placeholder-mixin',
'../tests/spec/views/mixins/timer-mixin',
'../tests/spec/views/mixins/service-mixin',
'../tests/spec/models/reliers/relier'
'../tests/spec/models/reliers/relier',
'../tests/spec/models/brokers/fx-desktop'
];

/*global mocha */
Expand Down

0 comments on commit 4151dac

Please sign in to comment.