Merge pull request #6945 from piousbox/api-v3-test-attach-local-to-social-auth

[v3] working on attaching local to social auth
This commit is contained in:
Matteo Pagliazzi 2016-03-24 19:18:04 +01:00
commit 9452fd3128
2 changed files with 36 additions and 3 deletions

View file

@ -116,6 +116,40 @@ describe('POST /user/auth/local/register', () => {
});
});
context('attach to facebook user', () => {
let user;
let email = 'some@email.net';
let username = 'some-username';
let password = 'some-password';
beforeEach(async () => {
user = await generateUser();
});
it('checks onlySocialAttachLocal', async () => {
await expect(user.post('/user/auth/local/register', {
email,
username,
password,
confirmPassword: password,
})).to.eventually.be.rejected.and.eql({
code: 401,
error: 'NotAuthorized',
message: t('onlySocialAttachLocal'),
});
});
it('succeeds', async () => {
await user.update({ 'auth.facebook.id': 'some-fb-id', 'auth.local': { ok: true } });
await user.post('/user/auth/local/register', {
username,
email,
password,
confirmPassword: password,
});
await user.sync();
expect(user.auth.local.username).to.eql(username);
expect(user.auth.local.email).to.eql(email);
});
});
context('login is already taken', () => {
let username, email, api;

View file

@ -70,7 +70,7 @@ api.registerLocal = {
url: '/user/auth/local/register',
async handler (req, res) {
let fbUser = res.locals.user; // If adding local auth to social user
// TODO check user doesn't have local auth
req.checkBody({
email: {
notEmpty: {errorMessage: res.t('missingEmail')},
@ -82,7 +82,6 @@ api.registerLocal = {
equals: {options: [req.body.confirmPassword], errorMessage: res.t('passwordConfirmationMatch')},
},
});
let validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
@ -124,7 +123,7 @@ api.registerLocal = {
if (fbUser) {
if (!fbUser.auth.facebook.id) throw new NotAuthorized(res.t('onlySocialAttachLocal'));
fbUser.auth.local = newUser;
fbUser.auth.local = newUser.auth.local;
newUser = fbUser;
} else {
newUser = new User(newUser);