mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-14 05:07:22 +01:00
Merge branch 'piousbox-api-v3-email-3' into api-v3
This commit is contained in:
@@ -4,8 +4,10 @@
|
||||
"missingEmail": "Missing email.",
|
||||
"missingUsername": "Missing username.",
|
||||
"missingPassword": "Missing password.",
|
||||
"wrongPassword": "Wrong password.",
|
||||
"notAnEmail": "Invalid email address.",
|
||||
"emailTaken": "Email already taken.",
|
||||
"newEmailRequired": "The newEmail body parameter is required.",
|
||||
"usernameTaken": "Username already taken.",
|
||||
"passwordConfirmationMatch": "Password confirmation doesn't match password.",
|
||||
"invalidLoginCredentials": "Incorrect username / email and / or password.",
|
||||
@@ -64,6 +66,7 @@
|
||||
"userAlreadyPendingInvitation": "User already pending invitation.",
|
||||
"userAlreadyInAParty": "User already in a party.",
|
||||
"userWithIDNotFound": "User with id \"<%= userId %>\" not found.",
|
||||
"userHasNoLocalRegistration": "User does not have a local registration (username, email, password).",
|
||||
"uuidsMustBeAnArray": "UUIDs invites must be a an Array.",
|
||||
"emailsMustBeAnArray": "Email invites must be a an Array.",
|
||||
"canOnlyInviteMaxInvites": "You can only invite \"<%= maxInvites %>\" at a time",
|
||||
|
||||
77
test/api/v3/integration/user/POST-user-update-email.test.js
Normal file
77
test/api/v3/integration/user/POST-user-update-email.test.js
Normal file
@@ -0,0 +1,77 @@
|
||||
import {
|
||||
generateUser,
|
||||
translate as t,
|
||||
} from '../../../../helpers/api-v3-integration.helper';
|
||||
import { model as User } from '../../../../../website/src/models/user';
|
||||
|
||||
describe.only('POST /email/update', () => {
|
||||
let user;
|
||||
let fbUser;
|
||||
let endpoint = '/user/update-email';
|
||||
let newEmail = 'some-new-email_2@example.net';
|
||||
let thePassword = 'password'; // from habitrpg/test/helpers/api-integration/v3/object-generators.js
|
||||
|
||||
describe('local user', async () => {
|
||||
beforeEach(async () => {
|
||||
user = await generateUser();
|
||||
});
|
||||
|
||||
it('does not change email if one is not provided', async () => {
|
||||
await expect(user.post(endpoint)).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: 'Invalid request parameters.',
|
||||
});
|
||||
});
|
||||
|
||||
it('does not change email if password is not provided', async () => {
|
||||
await expect(user.post(endpoint, {
|
||||
newEmail,
|
||||
})).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: 'Invalid request parameters.',
|
||||
});
|
||||
});
|
||||
|
||||
it('does not change email if wrong password is provided', async () => {
|
||||
await expect(user.post(endpoint, {
|
||||
newEmail,
|
||||
password: 'wrong password',
|
||||
})).to.eventually.be.rejected.and.eql({
|
||||
code: 401,
|
||||
error: 'NotAuthorized',
|
||||
message: t('wrongPassword'),
|
||||
});
|
||||
});
|
||||
|
||||
it('changes email if new email and existing password are provided', async () => {
|
||||
let response = await user.post(endpoint, {
|
||||
newEmail,
|
||||
password: thePassword,
|
||||
});
|
||||
expect(response).to.eql({ email: 'some-new-email_2@example.net' });
|
||||
let id = user._id;
|
||||
user = await User.findOne({ _id: id });
|
||||
expect(user.auth.local.email).to.eql(newEmail);
|
||||
});
|
||||
});
|
||||
|
||||
describe('facebook user', async () => {
|
||||
beforeEach(async () => {
|
||||
fbUser = await generateUser();
|
||||
await fbUser.update({ 'auth.local': { ok: true } });
|
||||
});
|
||||
|
||||
it('does not change email if user.auth.local.email does not exist for this user', async () => {
|
||||
await expect(fbUser.post(endpoint, {
|
||||
newEmail,
|
||||
password: thePassword,
|
||||
})).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: t('userHasNoLocalRegistration'),
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -7,7 +7,7 @@ import { isEmpty, cloneDeep } from 'lodash';
|
||||
const API_TEST_SERVER_PORT = nconf.get('PORT');
|
||||
let apiVersion;
|
||||
|
||||
// Sets up an abject that can make all REST requests
|
||||
// Sets up an object that can make all REST requests
|
||||
// If a user is passed in, the uuid and api token of
|
||||
// the user are used to make the requests
|
||||
export function requester (user = {}, additionalSets = {}) {
|
||||
|
||||
@@ -8,10 +8,11 @@ import {
|
||||
let api = {};
|
||||
|
||||
/**
|
||||
* @api {post} /email/unsubscribe Unsubscribe an email or user from email notifications
|
||||
* @api {get} /email/unsubscribe Unsubscribe an email or user from email notifications
|
||||
* @apiVersion 3.0.0
|
||||
* @apiName UnsubscribeEmail
|
||||
* @apiGroup Unsubscribe
|
||||
* @apiDescription This is a GET method so that you can put the unsubscribe link in emails.
|
||||
*
|
||||
* @apiParam {String} code An unsubscription code
|
||||
*
|
||||
|
||||
@@ -11,6 +11,7 @@ import { model as Group } from '../../models/group';
|
||||
import { model as User } from '../../models/user';
|
||||
import Q from 'q';
|
||||
import _ from 'lodash';
|
||||
import * as passwordUtils from '../../libs/api-v3/password';
|
||||
|
||||
let api = {};
|
||||
|
||||
@@ -29,7 +30,7 @@ api.getUser = {
|
||||
async handler (req, res) {
|
||||
let user = res.locals.user.toJSON();
|
||||
|
||||
// Remove apiToken from resonse TODO make it priavte at the user level? returned in signup/login
|
||||
// Remove apiToken from response TODO make it private at the user level? returned in signup/login
|
||||
delete user.apiToken;
|
||||
|
||||
// TODO move to model (maybe virtuals, maybe in toJSON)
|
||||
@@ -41,6 +42,41 @@ api.getUser = {
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* @api {post} /user/update-email
|
||||
* @apiVersion 3.0.0
|
||||
* @apiName UpdateEmail
|
||||
* @apiGroup User
|
||||
*
|
||||
* @apiParam {string} newEmail The new email address.
|
||||
* @apiParam {string} password The user password.
|
||||
*
|
||||
* @apiSuccess {Object} An object containing the new email address
|
||||
*/
|
||||
api.updateEmail = {
|
||||
method: 'POST',
|
||||
middlewares: [authWithHeaders(), cron],
|
||||
url: '/user/update-email',
|
||||
async handler (req, res) {
|
||||
let user = res.locals.user;
|
||||
|
||||
if (!user.auth.local.email) throw new BadRequest(res.t('userHasNoLocalRegistration'));
|
||||
|
||||
req.checkBody('newEmail', res.t('newEmailRequired')).notEmpty().isEmail();
|
||||
req.checkBody('password', res.t('missingPassword')).notEmpty();
|
||||
let validationErrors = req.validationErrors();
|
||||
if (validationErrors) throw validationErrors;
|
||||
|
||||
let candidatePassword = passwordUtils.encrypt(req.body.password, user.auth.local.salt);
|
||||
if (candidatePassword !== user.auth.local.hashed_password) throw new NotAuthorized(res.t('wrongPassword'));
|
||||
|
||||
user.auth.local.email = req.body.newEmail;
|
||||
await user.save();
|
||||
|
||||
return res.respond(200, { email: user.auth.local.email });
|
||||
},
|
||||
};
|
||||
|
||||
const partyMembersFields = 'profile.name stats achievements items.special';
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user