update social tests

This commit is contained in:
Phillip Thelen
2025-04-09 10:49:03 +02:00
parent a254c50a8e
commit 80825f3478
3 changed files with 46 additions and 4 deletions
@@ -7,7 +7,7 @@ import {
getProperty,
} from '../../../../../helpers/api-integration/v3';
describe('POST /user/auth/social', () => {
describe.only('POST /user/auth/social', () => {
let api;
let user;
const endpoint = '/user/auth/social';
@@ -64,6 +64,18 @@ describe('POST /user/auth/social', () => {
await expect(getProperty('users', response.id, 'profile.name')).to.eventually.equal('a google user');
});
it('fails if allowRegister is false and user does not exist', async () => {
await expect(api.post(endpoint, {
authResponse: { access_token: randomAccessToken }, // eslint-disable-line camelcase
network,
allowRegister: false,
})).to.eventually.be.rejected.and.eql({
code: 404,
error: 'NotFound',
message: t('userNotFound'),
});
});
it('logs an existing user in', async () => {
const registerResponse = await api.post(endpoint, {
authResponse: { access_token: randomAccessToken }, // eslint-disable-line camelcase
@@ -131,6 +143,36 @@ describe('POST /user/auth/social', () => {
expect(response.newUser).to.be.false;
});
it('logs an existing user into their social account if allowRegister is false', 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,
allowRegister: false,
});
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;
});
it('add social auth to an existing user', async () => {
const response = await user.post(endpoint, {
authResponse: { access_token: randomAccessToken }, // eslint-disable-line camelcase
+2 -2
View File
@@ -1,6 +1,6 @@
import passport from 'passport';
import common from '../../../common';
import { BadRequest, NotAuthorized } from '../errors';
import { BadRequest, NotAuthorized, NotFound } from '../errors';
import logger from '../logger';
import {
generateUsername,
@@ -75,7 +75,7 @@ export async function loginSocial (req, res) { // eslint-disable-line import/pre
}
if (!allowRegister) {
return res.respond(404, {});
throw new NotFound(res.t('userNotFound'));
}
let email;