2019-10-08 18:45:38 +00:00
|
|
|
import passport from 'passport';
|
2022-04-14 17:58:37 +00:00
|
|
|
import { v4 as generateUUID } from 'uuid';
|
2016-07-27 19:27:21 +00:00
|
|
|
import {
|
|
|
|
|
generateUser,
|
|
|
|
|
requester,
|
|
|
|
|
translate as t,
|
2016-09-16 17:13:21 +00:00
|
|
|
getProperty,
|
2016-07-27 19:27:21 +00:00
|
|
|
} from '../../../../../helpers/api-integration/v3';
|
|
|
|
|
|
|
|
|
|
describe('POST /user/auth/social', () => {
|
|
|
|
|
let api;
|
|
|
|
|
let user;
|
2019-10-08 18:45:38 +00:00
|
|
|
const endpoint = '/user/auth/social';
|
2022-04-14 17:58:37 +00:00
|
|
|
let randomAccessToken = '123456';
|
|
|
|
|
let randomGoogleId = 'googleId';
|
2016-09-28 10:11:10 +00:00
|
|
|
let network = 'NoNetwork';
|
2016-07-27 19:27:21 +00:00
|
|
|
|
2016-09-28 10:11:10 +00:00
|
|
|
beforeEach(async () => {
|
2016-07-27 19:27:21 +00:00
|
|
|
api = requester();
|
|
|
|
|
user = await generateUser();
|
2022-04-14 17:58:37 +00:00
|
|
|
randomAccessToken = generateUUID();
|
2016-07-27 19:27:21 +00:00
|
|
|
});
|
|
|
|
|
|
2016-09-28 10:11:10 +00:00
|
|
|
it('fails if network is not supported', async () => {
|
2016-07-27 19:27:21 +00:00
|
|
|
await expect(api.post(endpoint, {
|
2019-10-08 18:45:38 +00:00
|
|
|
authResponse: { access_token: randomAccessToken }, // eslint-disable-line camelcase
|
2016-09-28 10:11:10 +00:00
|
|
|
network,
|
2016-07-27 19:27:21 +00:00
|
|
|
})).to.eventually.be.rejected.and.eql({
|
2016-09-28 10:11:10 +00:00
|
|
|
code: 400,
|
|
|
|
|
error: 'BadRequest',
|
|
|
|
|
message: t('unsupportedNetwork'),
|
2016-07-27 19:27:21 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-09-28 10:11:10 +00:00
|
|
|
describe('google', () => {
|
2022-04-14 17:58:37 +00:00
|
|
|
beforeEach(async () => {
|
|
|
|
|
randomGoogleId = generateUUID();
|
|
|
|
|
const expectedResult = {
|
|
|
|
|
id: randomGoogleId,
|
|
|
|
|
displayName: 'a google user',
|
|
|
|
|
emails: [
|
|
|
|
|
{ value: `${user.auth.local.username}+google@example.com` },
|
|
|
|
|
],
|
|
|
|
|
};
|
2016-09-28 10:11:10 +00:00
|
|
|
sandbox.stub(passport._strategies.google, 'userProfile').yields(null, expectedResult);
|
|
|
|
|
network = 'google';
|
|
|
|
|
});
|
2016-07-27 19:27:21 +00:00
|
|
|
|
2022-04-14 17:58:37 +00:00
|
|
|
afterEach(async () => {
|
|
|
|
|
passport._strategies.google.userProfile.restore();
|
|
|
|
|
});
|
|
|
|
|
|
2016-09-28 10:11:10 +00:00
|
|
|
it('registers a new user', async () => {
|
2019-10-08 18:45:38 +00:00
|
|
|
const response = await api.post(endpoint, {
|
|
|
|
|
authResponse: { access_token: randomAccessToken }, // eslint-disable-line camelcase
|
2016-09-28 10:11:10 +00:00
|
|
|
network,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(response.apiToken).to.exist;
|
|
|
|
|
expect(response.id).to.exist;
|
|
|
|
|
expect(response.newUser).to.be.true;
|
2022-04-14 17:58:37 +00:00
|
|
|
await expect(getProperty('users', response.id, 'auth.google.id')).to.eventually.equal(randomGoogleId);
|
|
|
|
|
await expect(getProperty('users', response.id, 'auth.local.email')).to.eventually.equal(`${user.auth.local.username}+google@example.com`);
|
2017-01-02 23:00:01 +00:00
|
|
|
await expect(getProperty('users', response.id, 'profile.name')).to.eventually.equal('a google user');
|
2016-09-28 10:11:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('logs an existing user in', async () => {
|
2019-10-08 18:45:38 +00:00
|
|
|
const registerResponse = await api.post(endpoint, {
|
|
|
|
|
authResponse: { access_token: randomAccessToken }, // eslint-disable-line camelcase
|
2016-09-28 10:11:10 +00:00
|
|
|
network,
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-08 18:45:38 +00:00
|
|
|
const response = await api.post(endpoint, {
|
|
|
|
|
authResponse: { access_token: randomAccessToken }, // eslint-disable-line camelcase
|
2016-09-28 10:11:10 +00:00
|
|
|
network,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(response.apiToken).to.eql(registerResponse.apiToken);
|
|
|
|
|
expect(response.id).to.eql(registerResponse.id);
|
|
|
|
|
expect(response.newUser).to.be.false;
|
2022-04-14 17:58:37 +00:00
|
|
|
expect(registerResponse.newUser).to.be.true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('logs an existing user in if they have local auth with matching email', async () => {
|
|
|
|
|
passport._strategies.google.userProfile.restore();
|
|
|
|
|
const expectedResult = {
|
|
|
|
|
id: randomGoogleId,
|
|
|
|
|
displayName: 'a google user',
|
|
|
|
|
emails: [
|
|
|
|
|
{ value: user.auth.local.email },
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
sandbox.stub(passport._strategies.google, 'userProfile').yields(null, expectedResult);
|
|
|
|
|
|
|
|
|
|
const response = await api.post(endpoint, {
|
|
|
|
|
authResponse: { access_token: randomAccessToken }, // eslint-disable-line camelcase
|
|
|
|
|
network,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(response.apiToken).to.eql(user.apiToken);
|
|
|
|
|
expect(response.id).to.eql(user._id);
|
|
|
|
|
expect(response.newUser).to.be.false;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('logs an existing user into their social account if they have local auth with matching email', async () => {
|
|
|
|
|
const registerResponse = await api.post(endpoint, {
|
|
|
|
|
authResponse: { access_token: randomAccessToken }, // eslint-disable-line camelcase
|
|
|
|
|
network,
|
|
|
|
|
});
|
|
|
|
|
expect(registerResponse.newUser).to.be.true;
|
|
|
|
|
// This is important for existing accounts before the new social handling
|
|
|
|
|
passport._strategies.google.userProfile.restore();
|
|
|
|
|
const expectedResult = {
|
|
|
|
|
id: randomGoogleId,
|
|
|
|
|
displayName: 'a google user',
|
|
|
|
|
emails: [
|
|
|
|
|
{ value: user.auth.local.email },
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
sandbox.stub(passport._strategies.google, 'userProfile').yields(null, expectedResult);
|
|
|
|
|
|
|
|
|
|
const response = await api.post(endpoint, {
|
|
|
|
|
authResponse: { access_token: randomAccessToken }, // eslint-disable-line camelcase
|
|
|
|
|
network,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(response.apiToken).to.eql(registerResponse.apiToken);
|
|
|
|
|
expect(response.id).to.eql(registerResponse.id);
|
|
|
|
|
expect(response.apiToken).not.to.eql(user.apiToken);
|
|
|
|
|
expect(response.id).not.to.eql(user._id);
|
|
|
|
|
expect(response.newUser).to.be.false;
|
2016-07-27 19:27:21 +00:00
|
|
|
});
|
|
|
|
|
|
2016-09-28 10:11:10 +00:00
|
|
|
it('add social auth to an existing user', async () => {
|
2019-10-08 18:45:38 +00:00
|
|
|
const response = await user.post(endpoint, {
|
|
|
|
|
authResponse: { access_token: randomAccessToken }, // eslint-disable-line camelcase
|
2016-09-28 10:11:10 +00:00
|
|
|
network,
|
|
|
|
|
});
|
|
|
|
|
|
2022-04-14 17:58:37 +00:00
|
|
|
expect(response.apiToken).to.eql(user.apiToken);
|
|
|
|
|
expect(response.id).to.eql(user._id);
|
2016-09-28 10:11:10 +00:00
|
|
|
expect(response.newUser).to.be.false;
|
|
|
|
|
});
|
|
|
|
|
|
2022-04-14 17:58:37 +00:00
|
|
|
it('does not log into other account if social auth already exists', async () => {
|
|
|
|
|
const registerResponse = await api.post(endpoint, {
|
|
|
|
|
authResponse: { access_token: randomAccessToken }, // eslint-disable-line camelcase
|
|
|
|
|
network,
|
|
|
|
|
});
|
|
|
|
|
expect(registerResponse.newUser).to.be.true;
|
|
|
|
|
|
|
|
|
|
await expect(user.post(endpoint, {
|
|
|
|
|
authResponse: { access_token: randomAccessToken }, // eslint-disable-line camelcase
|
|
|
|
|
network,
|
|
|
|
|
})).to.eventually.be.rejected.and.eql({
|
|
|
|
|
code: 401,
|
|
|
|
|
error: 'NotAuthorized',
|
|
|
|
|
message: t('socialAlreadyExists'),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2018-10-13 18:03:20 +00:00
|
|
|
xit('enrolls a new user in an A/B test', async () => {
|
2016-09-28 10:11:10 +00:00
|
|
|
await api.post(endpoint, {
|
2019-10-08 18:45:38 +00:00
|
|
|
authResponse: { access_token: randomAccessToken }, // eslint-disable-line camelcase
|
2016-09-28 10:11:10 +00:00
|
|
|
network,
|
|
|
|
|
});
|
|
|
|
|
|
2017-05-19 19:45:11 +00:00
|
|
|
await expect(getProperty('users', user._id, '_ABtests')).to.eventually.be.a('object');
|
2016-09-28 10:11:10 +00:00
|
|
|
});
|
2025-08-19 19:04:33 +00:00
|
|
|
|
|
|
|
|
it('sets auth.timestamps.updated', async () => {
|
|
|
|
|
let oldUpdated = new Date(user.auth.timestamps.updated);
|
|
|
|
|
await user.post(endpoint, {
|
|
|
|
|
authResponse: { access_token: randomAccessToken }, // eslint-disable-line camelcase
|
|
|
|
|
network,
|
|
|
|
|
});
|
|
|
|
|
await user.sync();
|
|
|
|
|
expect(user.auth.timestamps.updated).to.be.greaterThan(oldUpdated);
|
|
|
|
|
oldUpdated = new Date(user.auth.timestamps.updated);
|
|
|
|
|
|
|
|
|
|
// Do it again to ensure it updates even when nothing else changes
|
|
|
|
|
await api.post(endpoint, {
|
|
|
|
|
authResponse: { access_token: randomAccessToken }, // eslint-disable-line camelcase
|
|
|
|
|
network,
|
|
|
|
|
});
|
|
|
|
|
await user.sync();
|
|
|
|
|
expect(user.auth.timestamps.updated).to.be.greaterThan(oldUpdated);
|
|
|
|
|
});
|
2016-07-27 19:27:21 +00:00
|
|
|
});
|
|
|
|
|
});
|