mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
Merge branch 'api-v3-user' of github.com:HabitRPG/habitrpg into api-v3-user
This commit is contained in:
@@ -36,8 +36,8 @@ let testCount = (stdout, regexp) => {
|
|||||||
return parseInt(match && match[1] || 0);
|
return parseInt(match && match[1] || 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
let testBin = (string) => {
|
let testBin = (string, additionalEnvVariables = '') => {
|
||||||
return `NODE_ENV=testing ./node_modules/.bin/${string}`;
|
return `NODE_ENV=testing ${additionalEnvVariables} ./node_modules/.bin/${string}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
gulp.task('test:prepare:mongo', (cb) => {
|
gulp.task('test:prepare:mongo', (cb) => {
|
||||||
@@ -354,7 +354,7 @@ gulp.task('test:api-v3:integration:watch', ['test:prepare:server'], () => {
|
|||||||
gulp.task('test:api-v3:safe', ['test:prepare:server'], (done) => {
|
gulp.task('test:api-v3:safe', ['test:prepare:server'], (done) => {
|
||||||
awaitPort(TEST_SERVER_PORT).then(() => {
|
awaitPort(TEST_SERVER_PORT).then(() => {
|
||||||
let runner = exec(
|
let runner = exec(
|
||||||
testBin(API_V3_TEST_COMMAND),
|
testBin(API_V3_TEST_COMMAND, 'API_VERSION=v3'),
|
||||||
(err, stdout, stderr) => {
|
(err, stdout, stderr) => {
|
||||||
testResults.push({
|
testResults.push({
|
||||||
suite: 'API V3 Specs\t',
|
suite: 'API V3 Specs\t',
|
||||||
|
|||||||
249
test/api/v3/integration/user/auth/POST-register_local.test.js
Normal file
249
test/api/v3/integration/user/auth/POST-register_local.test.js
Normal file
@@ -0,0 +1,249 @@
|
|||||||
|
import {
|
||||||
|
generateUser,
|
||||||
|
requester,
|
||||||
|
translate as t,
|
||||||
|
} from '../../../../../helpers/api-integration.helper';
|
||||||
|
import { v4 as generateRandomUserName } from 'uuid';
|
||||||
|
import { each } from 'lodash';
|
||||||
|
|
||||||
|
describe.skip('POST /user/register/local', () => {
|
||||||
|
context('username and email are free', () => {
|
||||||
|
it('registers a new user', () => {
|
||||||
|
let api = requester();
|
||||||
|
let username = generateRandomUserName();
|
||||||
|
let email = `${username}@example.com`;
|
||||||
|
let password = 'password';
|
||||||
|
|
||||||
|
return api.post('/user/register/local', {
|
||||||
|
username: username,
|
||||||
|
email: email,
|
||||||
|
password: password,
|
||||||
|
confirmPassword: password,
|
||||||
|
}).then((user) => {
|
||||||
|
expect(user._id).to.exist;
|
||||||
|
expect(user.apiToken).to.exist;
|
||||||
|
expect(user.auth.local.username).to.eql(username);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('requires password and confirmPassword to match', () => {
|
||||||
|
let api = requester();
|
||||||
|
let username = generateRandomUserName();
|
||||||
|
let email = `${username}@example.com`;
|
||||||
|
let password = 'password';
|
||||||
|
let confirmPassword = 'not password';
|
||||||
|
|
||||||
|
return expect(api.post('/user/register/local', {
|
||||||
|
username: username,
|
||||||
|
email: email,
|
||||||
|
password: password,
|
||||||
|
confirmPassword: confirmPassword,
|
||||||
|
})).to.eventually.be.rejected.and.eql({
|
||||||
|
code: 401,
|
||||||
|
error: 'NotAuthorized',
|
||||||
|
message: t('passwordConfirmationMatch'),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('requires a username', () => {
|
||||||
|
let api = requester();
|
||||||
|
let email = `${generateRandomUserName()}@example.com`;
|
||||||
|
let password = 'password';
|
||||||
|
let confirmPassword = 'password';
|
||||||
|
|
||||||
|
return expect(api.post('/user/register/local', {
|
||||||
|
email: email,
|
||||||
|
password: password,
|
||||||
|
confirmPassword: confirmPassword,
|
||||||
|
})).to.eventually.be.rejected.and.eql({
|
||||||
|
code: 401,
|
||||||
|
error: 'NotAuthorized',
|
||||||
|
message: t('missingUsernameEmail'),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('requires an email', () => {
|
||||||
|
let api = requester();
|
||||||
|
let username = generateRandomUserName();
|
||||||
|
let password = 'password';
|
||||||
|
let confirmPassword = 'password';
|
||||||
|
|
||||||
|
return expect(api.post('/user/register/local', {
|
||||||
|
username: username,
|
||||||
|
password: password,
|
||||||
|
confirmPassword: confirmPassword,
|
||||||
|
})).to.eventually.be.rejected.and.eql({
|
||||||
|
code: 401,
|
||||||
|
error: 'NotAuthorized',
|
||||||
|
message: t('missingUsernameEmail'),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('requires a password', () => {
|
||||||
|
let api = requester();
|
||||||
|
let username = generateRandomUserName();
|
||||||
|
let email = `${username}@example.com`;
|
||||||
|
let confirmPassword = 'password';
|
||||||
|
|
||||||
|
return expect(api.post('/user/register/local', {
|
||||||
|
username: username,
|
||||||
|
email: email,
|
||||||
|
confirmPassword: confirmPassword,
|
||||||
|
})).to.eventually.be.rejected.and.eql({
|
||||||
|
code: 401,
|
||||||
|
error: 'NotAuthorized',
|
||||||
|
message: t('missingPassword'),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
context('login is already taken', () => {
|
||||||
|
let username, email;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
username = generateRandomUserName();
|
||||||
|
email = `${username}@example.com`;
|
||||||
|
return generateUser({
|
||||||
|
'auth.local.username': username,
|
||||||
|
'auth.local.lowerCaseUsername': username,
|
||||||
|
'auth.local.email': email
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects if username is already taken', () => {
|
||||||
|
let api = requester();
|
||||||
|
let uniqueEmail = `${generateRandomUserName()}@exampe.com`;
|
||||||
|
let password = 'password';
|
||||||
|
|
||||||
|
return expect(api.post('/user/register/local', {
|
||||||
|
username: username,
|
||||||
|
email: uniqueEmail,
|
||||||
|
password: password,
|
||||||
|
confirmPassword: password,
|
||||||
|
})).to.eventually.be.rejected.and.eql({
|
||||||
|
code: 401,
|
||||||
|
error: 'NotAuthorized',
|
||||||
|
message: t('usernameTake'),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects if email is already taken', () => {
|
||||||
|
let api = requester();
|
||||||
|
let uniqueUsername = generateRandomUserName();
|
||||||
|
let password = 'password';
|
||||||
|
|
||||||
|
return expect(api.post('/user/register/local', {
|
||||||
|
username: uniqueUsername,
|
||||||
|
email: email,
|
||||||
|
password: password,
|
||||||
|
confirmPassword: password,
|
||||||
|
})).to.eventually.be.rejected.and.eql({
|
||||||
|
code: 401,
|
||||||
|
error: 'NotAuthorized',
|
||||||
|
message: t('emailTaken'),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
context('successful login via api', () => {
|
||||||
|
let api, username, email, password;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
api = requester();
|
||||||
|
username = generateRandomUserName();
|
||||||
|
email = `${username}@example.com`;
|
||||||
|
password = 'password';
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sets all site tour values to -2 (already seen)', () => {
|
||||||
|
return api.post('/user/register/local', {
|
||||||
|
username: username,
|
||||||
|
email: email,
|
||||||
|
password: password,
|
||||||
|
confirmPassword: password,
|
||||||
|
}).then((user) => {
|
||||||
|
expect(user.flags.tour).to.not.be.empty;
|
||||||
|
|
||||||
|
each(user.flags.tour, (value, attribute) => {
|
||||||
|
expect(value).to.eql(-2);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('populates user with default todos, not no other task types', () => {
|
||||||
|
return api.post('/user/register/local', {
|
||||||
|
username: username,
|
||||||
|
email: email,
|
||||||
|
password: password,
|
||||||
|
confirmPassword: password,
|
||||||
|
}).then((user) => {
|
||||||
|
expect(user.todos).to.not.be.empty;
|
||||||
|
expect(user.dailys).to.be.empty;
|
||||||
|
expect(user.habits).to.be.empty;
|
||||||
|
expect(user.rewards).to.be.empty;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('populates user with default tags', () => {
|
||||||
|
return api.post('/user/register/local', {
|
||||||
|
username: username,
|
||||||
|
email: email,
|
||||||
|
password: password,
|
||||||
|
confirmPassword: password,
|
||||||
|
}).then((user) => {
|
||||||
|
expect(user.tags).to.not.be.empty;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
context('successful login with habitica-web header', () => {
|
||||||
|
let api, username, email, password;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
api = requester({}, {'x-client': 'habitica-web'});
|
||||||
|
username = generateRandomUserName();
|
||||||
|
email = `${username}@example.com`;
|
||||||
|
password = 'password';
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sets all common tutorial flags to true', () => {
|
||||||
|
return api.post('/user/register/local', {
|
||||||
|
username: username,
|
||||||
|
email: email,
|
||||||
|
password: password,
|
||||||
|
confirmPassword: password,
|
||||||
|
}).then((user) => {
|
||||||
|
expect(user.flags.tour).to.not.be.empty;
|
||||||
|
|
||||||
|
each(user.flags.tutorial.common, (value, attribute) => {
|
||||||
|
expect(value).to.eql(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('populates user with default todos, habits, and rewards', () => {
|
||||||
|
return api.post('/user/register/local', {
|
||||||
|
username: username,
|
||||||
|
email: email,
|
||||||
|
password: password,
|
||||||
|
confirmPassword: password,
|
||||||
|
}).then((user) => {
|
||||||
|
expect(user.todos).to.not.be.empty;
|
||||||
|
expect(user.dailys).to.be.empty;
|
||||||
|
expect(user.habits).to.not.be.empty;
|
||||||
|
expect(user.rewards).to.not.be.empty;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('populates user with default tags', () => {
|
||||||
|
return api.post('/user/register/local', {
|
||||||
|
username: username,
|
||||||
|
email: email,
|
||||||
|
password: password,
|
||||||
|
confirmPassword: password,
|
||||||
|
}).then((user) => {
|
||||||
|
expect(user.tags).to.not.be.empty;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -5,7 +5,7 @@ import {
|
|||||||
NotAuthorized,
|
NotAuthorized,
|
||||||
} from '../../libs/api-v3/errors';
|
} from '../../libs/api-v3/errors';
|
||||||
import passwordUtils from '../../libs/api-v3/password';
|
import passwordUtils from '../../libs/api-v3/password';
|
||||||
import User from '../../models/user';
|
import { model as User } from '../../models/user';
|
||||||
import EmailUnsubscription from '../../models/emailUnsubscription';
|
import EmailUnsubscription from '../../models/emailUnsubscription';
|
||||||
import { sendTxn as sendTxnEmail } from '../../libs/api-v3/email';
|
import { sendTxn as sendTxnEmail } from '../../libs/api-v3/email';
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user