Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vladikoff committed Aug 29, 2019
1 parent b6776e7 commit 4fb0a6d
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,10 @@ export default BaseAuthenticationBroker.extend({
this.clearOriginalTabMarker();
return this.getOAuthResult(account).then(result => {
result = _.extend(result, additionalResultData);
result.declinedSyncEngines = account.get('declinedSyncEngines');
result.offeredSyncEngines = account.get('offeredSyncEngines');
if (account.get('declinedSyncEngines')) {
result.declinedSyncEngines = account.get('declinedSyncEngines');
result.offeredSyncEngines = account.get('offeredSyncEngines');
}

return this.sendOAuthResultToRelier(result);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
/**
* WebChannel OAuth broker that speaks 'v1' of the protocol.
*/

import _ from 'underscore';
import ChannelMixin from './mixins/channel';
import Cocktail from 'cocktail';
Expand Down Expand Up @@ -41,12 +40,9 @@ const OAuthWebChannelBroker = OAuthRedirectAuthenticationBroker.extend({
this._metrics = options.metrics;

proto.initialize.call(this, options);

this.request(
this.getCommand('FXA_STATUS', {
service: this.relier.get('service'),
})
).then(response => this.onFxaStatus(response));
this.request(this.getCommand('FXA_STATUS'), {
service: this.relier.get('service'),
}).then(response => this.onFxaStatus(response));
},

/**
Expand Down Expand Up @@ -94,16 +90,8 @@ const OAuthWebChannelBroker = OAuthRedirectAuthenticationBroker.extend({

DELAY_BROKER_RESPONSE_MS: 100,

sendOAuthResultToRelier(result, account) {
sendOAuthResultToRelier(result) {
return this._metrics.flush().then(() => {
const extraParams = {};
if (result.error) {
extraParams.error = result.error;
}
if (result.action) {
extraParams.action = result.action;
}

result.redirect = Constants.OAUTH_WEBCHANNEL_REDIRECT;

return this.send(this.getCommand('OAUTH_LOGIN'), result);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* 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/. */

import OAuthErrors from '../../../../lib/oauth-errors';
import PairingChannelClient from '../../../../lib/pairing-channel-client';
import SupplicantStateMachine from '../../../pairing/supplicant-state-machine';
import setRemoteMetaData from '../remote-metadata';

/**
* Shared functions of the supplicant auth brokers
*/

const SupplicantMixin = {
init(options) {
const { config, notifier, relier } = options;

if (!config.pairingClients.includes(relier.get('clientId'))) {
// only approved clients may pair
throw OAuthErrors.toError('INVALID_PAIRING_CLIENT');
}

const channelServerUri = config.pairingChannelServerUri;
const { channelId, channelKey } = relier.toJSON();
if (channelId && channelKey && channelServerUri) {
this.pairingChannelClient = new PairingChannelClient(
{
channelId,
channelKey,
channelServerUri,
},
{
importPairingChannel: options.importPairingChannel,
notifier,
}
);

this.suppStateMachine = new SupplicantStateMachine(
{},
{
broker: this,
notifier,
pairingChannelClient: this.pairingChannelClient,
relier,
}
);

this.pairingChannelClient.open();
} else {
throw new Error('Failed to initialize supplicant');
}
},
afterSupplicantApprove() {
return Promise.resolve().then(() => {
this.notifier.trigger('pair:supp:authorize');
});
},

setRemoteMetaData: setRemoteMetaData,
};

export default SupplicantMixin;
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,19 @@
* 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/. */

import OAuthErrors from '../../../lib/oauth-errors';
import Cocktail from 'cocktail';
import OAuthWebChannelBroker from '../oauth-webchannel-v1';
import PairingChannelClient from '../../../lib/pairing-channel-client';
import setRemoteMetaData from './remote-metadata';
import SupplicantStateMachine from '../../pairing/supplicant-state-machine';
import Url from '../../../lib/url';
import SupplicantMixin from './mixins/supplicant';

export default class SupplicantWebChannelBroker extends OAuthWebChannelBroker {
type = 'supplicant';
/**
* SupplicantWebChannelBroker extends OAuthWebChannelBroker to provide a WebChannel flow
*/
class SupplicantWebChannelBroker extends OAuthWebChannelBroker {
type = 'supplicant-webchannel';

initialize(options = {}) {
super.initialize(options);
const { config, notifier, relier } = options;

if (!config.pairingClients.includes(relier.get('clientId'))) {
// only approved clients may pair
throw OAuthErrors.toError('INVALID_PAIRING_CLIENT');
}

const channelServerUri = config.pairingChannelServerUri;
const { channelId, channelKey } = relier.toJSON();
if (channelId && channelKey && channelServerUri) {
this.pairingChannelClient = new PairingChannelClient(
{
channelId,
channelKey,
channelServerUri,
},
{
importPairingChannel: options.importPairingChannel,
notifier,
}
);

this.suppStateMachine = new SupplicantStateMachine(
{},
{
broker: this,
notifier,
pairingChannelClient: this.pairingChannelClient,
relier,
}
);

this.pairingChannelClient.open();
} else {
throw new Error('Failed to initialize supplicant');
}
}

afterSupplicantApprove() {
return Promise.resolve().then(() => {
this.notifier.trigger('pair:supp:authorize');
});
this.init(options);
}

sendCodeToRelier() {
Expand All @@ -70,6 +29,8 @@ export default class SupplicantWebChannelBroker extends OAuthWebChannelBroker {
this.sendOAuthResultToRelier(result);
});
}

setRemoteMetaData = setRemoteMetaData;
}

Cocktail.mixin(SupplicantWebChannelBroker, SupplicantMixin);

export default SupplicantWebChannelBroker;
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,20 @@
* 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/. */

import OAuthErrors from '../../../lib/oauth-errors';
import Cocktail from 'cocktail';
import OAuthRedirectBroker from '../oauth-redirect';
import PairingChannelClient from '../../../lib/pairing-channel-client';
import setRemoteMetaData from './remote-metadata';
import SupplicantStateMachine from '../../pairing/supplicant-state-machine';
import SupplicantMixin from './mixins/supplicant';
import Url from '../../../lib/url';

export default class SupplicantBroker extends OAuthRedirectBroker {
/**
* SupplicantBroker extends OAuthRedirectBroker to provide a redirect behaviour as an OAuth flow
*/
class SupplicantBroker extends OAuthRedirectBroker {
type = 'supplicant';

initialize(options = {}) {
super.initialize(options);

const { config, notifier, relier } = options;

if (!config.pairingClients.includes(relier.get('clientId'))) {
// only approved clients may pair
throw OAuthErrors.toError('INVALID_PAIRING_CLIENT');
}

const channelServerUri = config.pairingChannelServerUri;
const { channelId, channelKey } = relier.toJSON();
if (channelId && channelKey && channelServerUri) {
this.pairingChannelClient = new PairingChannelClient(
{
channelId,
channelKey,
channelServerUri,
},
{
importPairingChannel: options.importPairingChannel,
notifier,
}
);

this.suppStateMachine = new SupplicantStateMachine(
{},
{
broker: this,
notifier,
pairingChannelClient: this.pairingChannelClient,
relier,
}
);

this.pairingChannelClient.open();
} else {
throw new Error('Failed to initialize supplicant');
}
}

afterSupplicantApprove() {
return Promise.resolve().then(() => {
this.notifier.trigger('pair:supp:authorize');
});
this.init(options);
}

sendCodeToRelier() {
Expand All @@ -72,6 +31,8 @@ export default class SupplicantBroker extends OAuthRedirectBroker {
this.sendOAuthResultToRelier(result);
});
}

setRemoteMetaData = setRemoteMetaData;
}

Cocktail.mixin(SupplicantBroker, SupplicantMixin);

export default SupplicantBroker;
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('lib/channels/web', () => {
assert.ok(WebChannel.LOADED);
assert.ok(WebChannel.LOGIN);
assert.ok(WebChannel.LOGOUT);
assert.ok(WebChannel.OAUTH_LOGIN);
assert.ok(WebChannel.PAIR_AUTHORIZE);
assert.ok(WebChannel.PAIR_DECLINE);
assert.ok(WebChannel.PAIR_REQUEST_SUPPLICANT_METADATA);
Expand All @@ -54,7 +55,7 @@ describe('lib/channels/web', () => {
window: windowMock,
});

assert.lengthOf(Object.keys(channel.COMMANDS), 16);
assert.lengthOf(Object.keys(channel.COMMANDS), 17);
assert.ok(channel.COMMANDS.CAN_LINK_ACCOUNT);
assert.ok(channel.COMMANDS.CHANGE_PASSWORD);
assert.ok(channel.COMMANDS.DELETE);
Expand All @@ -63,6 +64,7 @@ describe('lib/channels/web', () => {
assert.ok(channel.COMMANDS.LOADED);
assert.ok(channel.COMMANDS.LOGIN);
assert.ok(channel.COMMANDS.LOGOUT);
assert.ok(channel.COMMANDS.OAUTH_LOGIN);
assert.ok(channel.COMMANDS.PAIR_AUTHORIZE);
assert.ok(channel.COMMANDS.PAIR_DECLINE);
assert.ok(channel.COMMANDS.PAIR_COMPLETE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,28 @@ describe('models/auth_brokers/oauth-redirect', () => {
);
});
});

it('handles declinedSyncEngines and offeredSyncEngines', () => {
account.set('declinedSyncEngines', ['history']);
account.set('offeredSyncEngines', ['history']);

sinon.stub(broker, 'getOAuthResult').callsFake(() => {
return Promise.resolve({});
});

sinon.stub(broker, 'sendOAuthResultToRelier').callsFake(() => {
return Promise.resolve();
});

return broker
.persistVerificationData(account)
.then(() => {
return broker.finishOAuthFlow(account);
})
.then(result => {
assert.equal(result, 'thing here!');
});
});
});

describe('afterResetPasswordConfirmationPoll', function() {
Expand Down
Loading

0 comments on commit 4fb0a6d

Please sign in to comment.