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

Commit

Permalink
fix(tests): Update service-mixin tests.
Browse files Browse the repository at this point in the history
Fixes #1400.
  • Loading branch information
vladikoff committed Aug 11, 2014
1 parent 9a033cf commit 37d6c37
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 16 deletions.
33 changes: 20 additions & 13 deletions app/scripts/views/mixins/service-mixin.js
Expand Up @@ -38,7 +38,7 @@ define([
return hasServiceView || this.isOAuthSameBrowser();
}

function notifyChannel(message, data) {
function _notifyChannel(message, data) {
/*jshint validthis: true*/
var self = this;
// Assume the receiver of the channel's notification will either
Expand All @@ -48,7 +48,7 @@ define([
if (data && data.timeout) {
self._expectResponseTimeout = self.setTimeout(function () {
self.displayError(OAuthErrors.toError('TRY_AGAIN'), OAuthErrors);
}, EXPECT_CHANNEL_RESPONSE_TIMEOUT);
}, data.timeout);
}

return Channels.sendExpectResponse(message, data, {
Expand All @@ -67,16 +67,16 @@ define([
* @param {Object} options
* @returns {Object}
*/
function decorateOAuthResult(result, options) {
function _decorateOAuthResult(result, options) {
options = options || {};

// if specific to the WebChannel flow
if (Channels.getWebChannelId(options.context)) {
// set closeWindow
result.closeWindow = options.viewOptions && options.viewOptions.source === 'signin';
// if the source is "signin" then set a timeout for a successful WebChannel signin
if (options.viewOptions.source === 'signin') {
result.timeout = 3000;
if (options.viewOptions && options.viewOptions.source === 'signin') {
result.timeout = EXPECT_CHANNEL_RESPONSE_TIMEOUT;
}
}

Expand All @@ -89,7 +89,7 @@ define([
* @param {Object} result
* @returns {Object}
*/
function formatOAuthResult(result) {
function _formatOAuthResult(result) {
// get code and state from redirect params
var redirectParams = result.redirect.split('?')[1];
result.state = Url.searchParam('state', redirectParams);
Expand Down Expand Up @@ -118,6 +118,10 @@ define([
// to the 'client_id'.
this.service = params.client_id || Session.service;

// assertion library to use to generate assertions
// can be substituted for testing
this.assertionLibrary = Assertion;

Session.set('service', this.service);
// A hint that allows Session to determine whether the user
// is in the OAuth flow.
Expand All @@ -144,36 +148,35 @@ define([
},

finishOAuthFlowDifferentBrowser: function () {
return notifyChannel.call(this, 'oauth_complete', {
return _notifyChannel.call(this, 'oauth_complete', {
redirect: this.serviceRedirectURI,
error: RP_DIFFERENT_BROWSER_ERROR_CODE
});
},

finishOAuthFlow: buttonProgressIndicator(function (viewOptions) {
var self = this;

return this._configLoader.fetch().then(function(config) {
return Assertion.generate(config.oauthUrl);
return self.assertionLibrary.generate(config.oauthUrl);
})
.then(function(assertion) {
self._oAuthParams.assertion = assertion;
return self._oAuthClient.getCode(self._oAuthParams);
})
.then(function(result) {

return formatOAuthResult(result);
return _formatOAuthResult(result);
})
.then(function(result) {

return decorateOAuthResult(result, {
return _decorateOAuthResult(result, {
context: self.window,
viewOptions: viewOptions
});
})
.then(function(result) {

return notifyChannel.call(self, 'oauth_complete', result);
return _notifyChannel.call(self, 'oauth_complete', result);
})
.then(function() {
Session.clear('oauth');
Expand Down Expand Up @@ -230,6 +233,10 @@ define([

isSync: function () {
return Session.service === SYNC_SERVICE;
}
},
// exported for testing purposes
_decorateOAuthResult: _decorateOAuthResult,
_formatOAuthResult: _formatOAuthResult,
_notifyChannel: _notifyChannel
};
});
141 changes: 138 additions & 3 deletions app/tests/spec/views/mixins/service-mixin.js
Expand Up @@ -8,23 +8,26 @@ define([
'chai',
'backbone',
'underscore',
'lib/promise',
'../../../mocks/oauth_servers',
'../../../mocks/window',
'../../../mocks/channel',
'views/mixins/service-mixin',
'views/base',
'lib/session',
'stache!templates/test_template'
], function (Chai, Backbone, _, MockOAuthServers, WindowMock, ChannelMock, ServiceMixin, BaseView, Session, TestTemplate) {
], function (Chai, Backbone, _, p, MockOAuthServers, WindowMock, ChannelMock, ServiceMixin, BaseView, Session, TestTemplate) {
var assert = Chai.assert;


var CLIENT_ID = 'dcdb5ae7add825d2';
var STATE = '123';
var CODE = 'code1';
var SCOPE = 'profile:email';
var CLIENT_NAME = '123Done';
var BASE_REDIRECT_URL = 'http://127.0.0.1:8080/api/oauth';
var DEFAULT_SEARCH_STRING = '?client_id=' + CLIENT_ID + '&state=' + STATE + '&scope=' + SCOPE;
var DEFAULT_REDIRECT_STRING = '?code=' + CODE + '&state=' + STATE;


var OAuthView = BaseView.extend({
Expand Down Expand Up @@ -113,9 +116,141 @@ define([
});
});

describe('_decorateOAuthResult', function () {
it('decorates nothing by default', function (done) {
view._decorateOAuthResult({}, {})
.then(function (result) {
assert.notOk(result.closeWindow);
assert.notOk(result.timeout);
return done();
});
});

it('decorates WebChannel from Session with signin source', function (done) {
Session.set('oauth', {
webChannelId: 'id'
});

view._decorateOAuthResult({}, {
context: windowMock,
viewOptions: {
source: 'signin'
}
}).then(function (result) {
assert.isTrue(result.closeWindow);
assert.ok(result.timeout);
Session.clear('oauth');
done();
});
});

it('decorates WebChannel from location param with signin source', function (done) {
windowMock.location.search = DEFAULT_SEARCH_STRING + '&webChannelId=id';

view._decorateOAuthResult({}, {
context: windowMock,
viewOptions: {
source: 'signin'
}
}).then(function (result) {
assert.isTrue(result.closeWindow);
assert.ok(result.timeout);
done();
});
});
});

describe('_formatOAuthResult', function () {
it('formats the redirect params', function (done) {
view._formatOAuthResult({
redirect: DEFAULT_REDIRECT_STRING
}).then(function (result) {
assert.equal(result.redirect, DEFAULT_REDIRECT_STRING);
assert.equal(result.code, CODE);
assert.equal(result.state, STATE);
done();
});
});
});

describe('_notifyChannel', function () {
var mockChannel = {
init: function() {},
send: function(msg, data, done) { done(); },
teardown: function() {}
};

it('throws timeout errors', function (done) {
view.channel = mockChannel;
view.setTimeout = function(func) { func(); };

var displayedError = false;
view.displayError = function() {
displayedError = true;
done();
};

view._notifyChannel({}, {timeout: 1});
});

it('throws channel errors', function () {
view.channel = mockChannel;
view.channel.send = function (msg, data, done) {
done(new Error('Cannot send events'));
};

return view._notifyChannel({})
.then(assert.fail, function() { assert.ok('Error thrown'); });
});
});

describe('finishOAuthFlow', function () {
it('notifies the channel', function () {
// TODO - fill this in with #1141
var mockAssertionLibrary = {
generate: function() {
return p('mockAssertion');
}
};
var mockConfigLoader = {
fetch: function() {
return p({oauthUrl: ''});
}
};

it('notifies the channel', function (done) {
view.assertionLibrary = mockAssertionLibrary;
view._configLoader = mockConfigLoader;
view._oAuthParams = {};
view._oAuthClient = {
getCode: function() {
return p({redirect: DEFAULT_REDIRECT_STRING});
}
};

view.finishOAuthFlow()
.then(function () {
assert.equal(Session.oauth, null);
done();
});
});

it('throws errors', function () {
view.assertionLibrary = mockAssertionLibrary;
view._configLoader = mockConfigLoader;
view._oAuthParams = {};
view._oAuthClient = {
getCode: function() {
return p({});
}
};
var displayedError = false;
view.displayError = function() {
displayedError = true;
};

return view.finishOAuthFlow()
.then(function () {
assert.isTrue(displayedError);
});
});
});

Expand Down

0 comments on commit 37d6c37

Please sign in to comment.