Skip to content

Commit

Permalink
Add support to accounts that are identified by email instead of username
Browse files Browse the repository at this point in the history
  • Loading branch information
tprzytula committed Dec 3, 2018
1 parent 8782f07 commit 1189401
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 3 deletions.
24 changes: 24 additions & 0 deletions server/integration/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ export const getConnectionLastLoginToken = connectionId => Accounts._getLoginTok
*/
export const findUserByUsername = username => Accounts.findUserByUsername(username);

/**
* Returns user's record matching the email.
* @param {string} email.
* @returns {Object} user
*/
export const findUserByEmail = email => Accounts.findUserByEmail(email);

/**
* Finds user's record matching provided user details.
* @param {string} [username]
* @param {[Object]} [emails]
* @returns {Object || undefined} result
*/
export const findUser = user = ({ username, emails = [] } = {}) => {
if (username) {
return findUserByUsername(username);
}

const [ primaryEmail ] = emails;
if (primaryEmail && primaryEmail.address) {
return findUserByEmail(primaryEmail.address);
}
};

/**
* Hashes login token.
* @param {string} token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ class ShouldResumeBeAccepted extends Validator.default {
* @private
*/
_getUsersLoginTokens() {
const user = integrationAccounts.findUserByUsername(this.loginAttempt.user.username);
return (user.services && user.services.resume)
const user = integrationAccounts.findUser(this.loginAttempt.user);
return (user && user.services && user.services.resume)
? user.services.resume.loginTokens
: [];
}
Expand Down
84 changes: 84 additions & 0 deletions test/server/integration/accounts.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import sinon from 'sinon';
import {
addValidationStep,
findUserByUsername,
findUserByEmail,
findUser,
getConnectionLastLoginToken,
hashLoginToken
} from '../../../server/integration/accounts';
Expand Down Expand Up @@ -58,6 +60,88 @@ describe('Given findUserByUsername method', () => {
});
});

describe('Given findUserByEmail method', () => {
const sandbox = sinon.createSandbox();
let findUserByEmailStub;

beforeEach(() => {
findUserByEmailStub = sandbox.stub(Accounts, 'findUserByEmail');
});

afterEach(() => {
sandbox.restore();
});

describe('When a param is passed', () => {
const sampleEmail = '[email protected]';

beforeEach(() => {
findUserByEmail(sampleEmail);
});

it('should pass the param to the Accounts findUserByEmail method', () => {
expect(findUserByEmailStub).to.be.calledWith(sampleEmail);
});
});
});

describe('Given findUser method', () => {
const sandbox = sinon.createSandbox();
const sampleUsername = 'test';
const sampleEmail = '[email protected]';
let findUserByUsernameStub, findUserByEmailStub;

beforeEach(() => {
findUserByUsernameStub = sandbox.stub(Accounts, 'findUserByUsername');
findUserByEmailStub = sandbox.stub(Accounts, 'findUserByEmail');
});

afterEach(() => {
sandbox.restore();
});

describe('When the record contains username and email', () => {
beforeEach(() => {
findUser({ username: sampleUsername, emails: [ { address: sampleEmail } ] });
});

it('should pass the username to the Accounts findUserByUsernameStub method', () => {
expect(findUserByUsernameStub).to.be.calledWith(sampleUsername);
});
});

describe('When the record contains only username', () => {
beforeEach(() => {
findUser({ username: sampleUsername });
});

it('should pass the username to the Accounts findUserByUsernameStub method', () => {
expect(findUserByUsernameStub).to.be.calledWith(sampleUsername);
});
});

describe('When the record contains only email', () => {
beforeEach(() => {
findUser({ emails: [ { address: sampleEmail } ] });
});

it('should pass the email to the Accounts findUserByEmail method', () => {
expect(findUserByEmailStub).to.be.calledWith(sampleEmail);
});
});

describe('When the record does not contain username or email', () => {
beforeEach(() => {
findUser();
});

it('should not call the Accounts methods', () => {
expect(findUserByUsernameStub).to.not.be.called();
expect(findUserByEmailStub).to.not.be.called();
});
});
});

describe('Given getConnectionLastLoginToken method', () => {
const sandbox = sinon.createSandbox();
let _getLoginTokenStub;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('Given the shouldResumeBeAccepted validator', () => {
.stub(integrationAccounts, 'hashLoginToken')
.callsFake(token => token);
sandbox
.stub(integrationAccounts, 'findUserByUsername')
.stub(integrationAccounts, 'findUser')
.callsFake(() => ({
services: {
resume: {
Expand Down

0 comments on commit 1189401

Please sign in to comment.