2019-10-08 18:45:38 +00:00
|
|
|
import passport from 'passport';
|
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';
|
|
|
|
|
const randomAccessToken = '123456';
|
|
|
|
|
const facebookId = 'facebookId';
|
|
|
|
|
const googleId = '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();
|
|
|
|
|
});
|
|
|
|
|
|
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('facebook', () => {
|
|
|
|
|
before(async () => {
|
2019-10-08 18:45:38 +00:00
|
|
|
const expectedResult = { id: facebookId, displayName: 'a facebook user' };
|
2016-09-28 10:11:10 +00:00
|
|
|
sandbox.stub(passport._strategies.facebook, 'userProfile').yields(null, expectedResult);
|
|
|
|
|
network = 'facebook';
|
2016-07-27 19:27:21 +00:00
|
|
|
});
|
|
|
|
|
|
2016-09-28 10:11:10 +00:00
|
|
|
it('registers a new user', async () => {
|
2018-10-02 21:17:06 +00:00
|
|
|
const response = 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,
|
|
|
|
|
});
|
2016-07-27 19:27:21 +00:00
|
|
|
|
2016-09-28 10:11:10 +00:00
|
|
|
expect(response.apiToken).to.exist;
|
|
|
|
|
expect(response.id).to.exist;
|
|
|
|
|
expect(response.newUser).to.be.true;
|
2018-10-02 21:17:06 +00:00
|
|
|
expect(response.username).to.exist;
|
|
|
|
|
|
2017-01-02 23:00:01 +00:00
|
|
|
await expect(getProperty('users', response.id, 'profile.name')).to.eventually.equal('a facebook user');
|
2018-10-02 21:17:06 +00:00
|
|
|
await expect(getProperty('users', response.id, 'auth.local.lowerCaseUsername')).to.exist;
|
2020-04-08 16:44:30 +00:00
|
|
|
await expect(getProperty('users', response.id, 'auth.facebook.id')).to.eventually.equal(facebookId);
|
2016-09-16 17:13:21 +00:00
|
|
|
});
|
|
|
|
|
|
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;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(response.apiToken).to.exist;
|
|
|
|
|
expect(response.id).to.exist;
|
|
|
|
|
expect(response.newUser).to.be.false;
|
|
|
|
|
});
|
|
|
|
|
|
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
|
|
|
});
|
2016-09-16 17:13:21 +00:00
|
|
|
});
|
|
|
|
|
|
2016-09-28 10:11:10 +00:00
|
|
|
describe('google', () => {
|
|
|
|
|
before(async () => {
|
2019-10-08 18:45:38 +00:00
|
|
|
const expectedResult = { id: googleId, displayName: 'a google user' };
|
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
|
|
|
|
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;
|
2020-04-08 16:44:30 +00:00
|
|
|
await expect(getProperty('users', response.id, 'auth.google.id')).to.eventually.equal(googleId);
|
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;
|
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,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(response.apiToken).to.exist;
|
|
|
|
|
expect(response.id).to.exist;
|
|
|
|
|
expect(response.newUser).to.be.false;
|
|
|
|
|
});
|
|
|
|
|
|
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
|
|
|
});
|
2016-07-27 19:27:21 +00:00
|
|
|
});
|
|
|
|
|
});
|