mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
tests: Clean up styling of auth update tests
This commit is contained in:
@@ -3,20 +3,21 @@ import {
|
|||||||
translate as t,
|
translate as t,
|
||||||
} from '../../../../../helpers/api-v3-integration.helper';
|
} from '../../../../../helpers/api-v3-integration.helper';
|
||||||
|
|
||||||
describe('PUT /user/auth/update-email', () => {
|
const ENDPOINT = '/user/auth/update-email';
|
||||||
let user;
|
|
||||||
let fbUser;
|
describe('PUT /user/auth/update-email', () => {
|
||||||
let endpoint = '/user/auth/update-email';
|
let newEmail = 'some-new-email_2@example.net';
|
||||||
let newEmail = 'some-new-email_2@example.net';
|
let oldPassword = 'password'; // from habitrpg/test/helpers/api-integration/v3/object-generators.js
|
||||||
let thePassword = 'password'; // from habitrpg/test/helpers/api-integration/v3/object-generators.js
|
|
||||||
|
context('Local Authenticaion User', async () => {
|
||||||
|
let user;
|
||||||
|
|
||||||
describe('local user', async () => {
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
user = await generateUser();
|
user = await generateUser();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('does not change email if one is not provided', async () => {
|
it('does not change email if email is not provided', async () => {
|
||||||
await expect(user.put(endpoint)).to.eventually.be.rejected.and.eql({
|
await expect(user.put(ENDPOINT)).to.eventually.be.rejected.and.eql({
|
||||||
code: 400,
|
code: 400,
|
||||||
error: 'BadRequest',
|
error: 'BadRequest',
|
||||||
message: t('invalidReqParams'),
|
message: t('invalidReqParams'),
|
||||||
@@ -24,7 +25,7 @@ describe('PUT /user/auth/update-email', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('does not change email if password is not provided', async () => {
|
it('does not change email if password is not provided', async () => {
|
||||||
await expect(user.put(endpoint, {
|
await expect(user.put(ENDPOINT, {
|
||||||
newEmail,
|
newEmail,
|
||||||
})).to.eventually.be.rejected.and.eql({
|
})).to.eventually.be.rejected.and.eql({
|
||||||
code: 400,
|
code: 400,
|
||||||
@@ -34,7 +35,7 @@ describe('PUT /user/auth/update-email', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('does not change email if wrong password is provided', async () => {
|
it('does not change email if wrong password is provided', async () => {
|
||||||
await expect(user.put(endpoint, {
|
await expect(user.put(ENDPOINT, {
|
||||||
newEmail,
|
newEmail,
|
||||||
password: 'wrong password',
|
password: 'wrong password',
|
||||||
})).to.eventually.be.rejected.and.eql({
|
})).to.eventually.be.rejected.and.eql({
|
||||||
@@ -45,9 +46,9 @@ describe('PUT /user/auth/update-email', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('changes email if new email and existing password are provided', async () => {
|
it('changes email if new email and existing password are provided', async () => {
|
||||||
let response = await user.put(endpoint, {
|
let response = await user.put(ENDPOINT, {
|
||||||
newEmail,
|
newEmail,
|
||||||
password: thePassword,
|
password: oldPassword,
|
||||||
});
|
});
|
||||||
expect(response).to.eql({ email: 'some-new-email_2@example.net' });
|
expect(response).to.eql({ email: 'some-new-email_2@example.net' });
|
||||||
|
|
||||||
@@ -56,16 +57,18 @@ describe('PUT /user/auth/update-email', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('facebook user', async () => {
|
context('Social Login User', async () => {
|
||||||
|
let socialUser;
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
fbUser = await generateUser();
|
socialUser = await generateUser();
|
||||||
await fbUser.update({ 'auth.local': { ok: true } });
|
await socialUser.update({ 'auth.local': { ok: true } });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('does not change email if user.auth.local.email does not exist for this user', async () => {
|
it('does not change email if user.auth.local.email does not exist for this user', async () => {
|
||||||
await expect(fbUser.put(endpoint, {
|
await expect(socialUser.put(ENDPOINT, {
|
||||||
newEmail,
|
newEmail,
|
||||||
password: thePassword,
|
password: oldPassword,
|
||||||
})).to.eventually.be.rejected.and.eql({
|
})).to.eventually.be.rejected.and.eql({
|
||||||
code: 400,
|
code: 400,
|
||||||
error: 'BadRequest',
|
error: 'BadRequest',
|
||||||
|
|||||||
@@ -3,10 +3,11 @@ import {
|
|||||||
translate as t,
|
translate as t,
|
||||||
} from '../../../../../helpers/api-v3-integration.helper';
|
} from '../../../../../helpers/api-v3-integration.helper';
|
||||||
|
|
||||||
|
const ENDPOINT = '/user/auth/update-password';
|
||||||
|
|
||||||
describe('PUT /user/auth/update-password', async () => {
|
describe('PUT /user/auth/update-password', async () => {
|
||||||
let endpoint = '/user/auth/update-password';
|
|
||||||
let user;
|
let user;
|
||||||
let password = 'password';
|
let password = 'password'; // from habitrpg/test/helpers/api-integration/v3/object-generators.js
|
||||||
let wrongPassword = 'wrong-password';
|
let wrongPassword = 'wrong-password';
|
||||||
let newPassword = 'new-password';
|
let newPassword = 'new-password';
|
||||||
|
|
||||||
@@ -16,7 +17,7 @@ describe('PUT /user/auth/update-password', async () => {
|
|||||||
|
|
||||||
it('successfully changes the password', async () => {
|
it('successfully changes the password', async () => {
|
||||||
let previousHashedPassword = user.auth.local.hashed_password;
|
let previousHashedPassword = user.auth.local.hashed_password;
|
||||||
let response = await user.put(endpoint, {
|
let response = await user.put(ENDPOINT, {
|
||||||
password,
|
password,
|
||||||
newPassword,
|
newPassword,
|
||||||
confirmPassword: newPassword,
|
confirmPassword: newPassword,
|
||||||
@@ -26,8 +27,8 @@ describe('PUT /user/auth/update-password', async () => {
|
|||||||
expect(user.auth.local.hashed_password).to.not.eql(previousHashedPassword);
|
expect(user.auth.local.hashed_password).to.not.eql(previousHashedPassword);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('new passwords mismatch', async () => {
|
it('returns an error when confirmPassword does not match newPassword', async () => {
|
||||||
await expect(user.put(endpoint, {
|
await expect(user.put(ENDPOINT, {
|
||||||
password,
|
password,
|
||||||
newPassword,
|
newPassword,
|
||||||
confirmPassword: `${newPassword}-wrong-confirmation`,
|
confirmPassword: `${newPassword}-wrong-confirmation`,
|
||||||
@@ -38,8 +39,8 @@ describe('PUT /user/auth/update-password', async () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('existing password is wrong', async () => {
|
it('returns an error when existing password is wrong', async () => {
|
||||||
await expect(user.put(endpoint, {
|
await expect(user.put(ENDPOINT, {
|
||||||
password: wrongPassword,
|
password: wrongPassword,
|
||||||
newPassword,
|
newPassword,
|
||||||
confirmPassword: newPassword,
|
confirmPassword: newPassword,
|
||||||
|
|||||||
@@ -3,20 +3,19 @@ import {
|
|||||||
translate as t,
|
translate as t,
|
||||||
} from '../../../../../helpers/api-v3-integration.helper';
|
} from '../../../../../helpers/api-v3-integration.helper';
|
||||||
|
|
||||||
|
const ENDPOINT = '/user/auth/update-username';
|
||||||
|
|
||||||
describe('PUT /user/auth/update-username', async () => {
|
describe('PUT /user/auth/update-username', async () => {
|
||||||
let endpoint = '/user/auth/update-username';
|
|
||||||
let user;
|
let user;
|
||||||
let newUsername = 'new-username';
|
let newUsername = 'new-username';
|
||||||
let existingUsername = 'existing-username';
|
|
||||||
let password = 'password'; // from habitrpg/test/helpers/api-integration/v3/object-generators.js
|
let password = 'password'; // from habitrpg/test/helpers/api-integration/v3/object-generators.js
|
||||||
let wrongPassword = 'wrong-password';
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
user = await generateUser();
|
user = await generateUser();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('successfully changes username', async () => {
|
it('successfully changes username', async () => {
|
||||||
let response = await user.put(endpoint, {
|
let response = await user.put(ENDPOINT, {
|
||||||
username: newUsername,
|
username: newUsername,
|
||||||
password,
|
password,
|
||||||
});
|
});
|
||||||
@@ -26,14 +25,11 @@ describe('PUT /user/auth/update-username', async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
context('errors', async () => {
|
context('errors', async () => {
|
||||||
describe('new username is unavailable', async () => {
|
it('prevents username update if new username is already taken', async () => {
|
||||||
beforeEach(async () => {
|
let existingUsername = 'existing-username';
|
||||||
user = await generateUser();
|
await generateUser({'auth.local.username': existingUsername, 'auth.local.lowerCaseUsername': existingUsername });
|
||||||
await user.update({'auth.local.username': existingUsername, 'auth.local.lowerCaseUsername': existingUsername });
|
|
||||||
});
|
|
||||||
|
|
||||||
it('prevents username update', async () => {
|
await expect(user.put(ENDPOINT, {
|
||||||
await expect(user.put(endpoint, {
|
|
||||||
username: existingUsername,
|
username: existingUsername,
|
||||||
password,
|
password,
|
||||||
})).to.eventually.be.rejected.and.eql({
|
})).to.eventually.be.rejected.and.eql({
|
||||||
@@ -42,12 +38,11 @@ describe('PUT /user/auth/update-username', async () => {
|
|||||||
message: t('usernameTaken'),
|
message: t('usernameTaken'),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
it('password is wrong', async () => {
|
it('errors if password is wrong', async () => {
|
||||||
await expect(user.put(endpoint, {
|
await expect(user.put(ENDPOINT, {
|
||||||
username: newUsername,
|
username: newUsername,
|
||||||
password: wrongPassword,
|
password: 'wrong-password',
|
||||||
})).to.eventually.be.rejected.and.eql({
|
})).to.eventually.be.rejected.and.eql({
|
||||||
code: 401,
|
code: 401,
|
||||||
error: 'NotAuthorized',
|
error: 'NotAuthorized',
|
||||||
@@ -55,14 +50,11 @@ describe('PUT /user/auth/update-username', async () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('social-only user', async () => {
|
|
||||||
beforeEach(async () => {
|
|
||||||
user = await generateUser();
|
|
||||||
await user.update({ 'auth.local': { ok: true } });
|
|
||||||
});
|
|
||||||
|
|
||||||
it('prevents username update', async () => {
|
it('prevents social-only user from changing username', async () => {
|
||||||
await expect(user.put(endpoint, {
|
let socialUser = await generateUser({ 'auth.local': { ok: true } });
|
||||||
|
|
||||||
|
await expect(socialUser.put(ENDPOINT, {
|
||||||
username: newUsername,
|
username: newUsername,
|
||||||
password,
|
password,
|
||||||
})).to.eventually.be.rejected.and.eql({
|
})).to.eventually.be.rejected.and.eql({
|
||||||
@@ -71,10 +63,9 @@ describe('PUT /user/auth/update-username', async () => {
|
|||||||
message: t('userHasNoLocalRegistration'),
|
message: t('userHasNoLocalRegistration'),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
it('new username is not provided', async () => {
|
it('errors if new username is not provided', async () => {
|
||||||
await expect(user.put(endpoint, {
|
await expect(user.put(ENDPOINT, {
|
||||||
password,
|
password,
|
||||||
})).to.eventually.be.rejected.and.eql({
|
})).to.eventually.be.rejected.and.eql({
|
||||||
code: 400,
|
code: 400,
|
||||||
|
|||||||
Reference in New Issue
Block a user