2020-05-25 11:49:38 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2020-04-29 18:44:27 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const testUtils = require('../../utils');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
// Stuff we are testing
|
2021-04-27 16:18:04 +03:00
|
|
|
const events = require('../../../core/server/lib/common/events');
|
2020-04-29 18:44:27 +03:00
|
|
|
|
|
|
|
const imageLib = require('../../../core/server/lib/image');
|
|
|
|
const UserModel = require('../../../core/server/models/user').User;
|
|
|
|
const RoleModel = require('../../../core/server/models/role').Role;
|
|
|
|
const context = testUtils.context.admin;
|
2013-09-06 20:07:25 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
describe('User Model', function run() {
|
2020-04-29 18:44:27 +03:00
|
|
|
let eventsTriggered = {};
|
2017-01-25 16:47:49 +03:00
|
|
|
|
2020-02-24 23:51:09 +03:00
|
|
|
before(testUtils.teardownDb);
|
|
|
|
afterEach(testUtils.teardownDb);
|
2014-07-21 21:50:04 +04:00
|
|
|
afterEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2014-07-21 21:50:04 +04:00
|
|
|
});
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2014-08-14 01:58:12 +04:00
|
|
|
before(function () {
|
|
|
|
should.exist(UserModel);
|
|
|
|
});
|
2013-08-25 00:51:58 +04:00
|
|
|
|
|
|
|
describe('Registration', function runRegistration() {
|
2014-07-30 19:40:30 +04:00
|
|
|
beforeEach(testUtils.setup('roles'));
|
2013-08-25 00:51:58 +04:00
|
|
|
|
|
|
|
it('can add first', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const userData = testUtils.DataGenerator.forModel.users[0];
|
2014-07-21 21:50:04 +04:00
|
|
|
|
2014-07-15 15:03:12 +04:00
|
|
|
UserModel.add(userData, context).then(function (createdUser) {
|
2013-06-25 15:43:15 +04:00
|
|
|
should.exist(createdUser);
|
2014-07-21 13:29:03 +04:00
|
|
|
createdUser.attributes.password.should.not.equal(userData.password, 'password was hashed');
|
|
|
|
createdUser.attributes.email.should.eql(userData.email, 'email address correct');
|
2014-07-21 21:50:04 +04:00
|
|
|
|
2013-11-11 23:55:22 +04:00
|
|
|
done();
|
2014-05-06 00:58:58 +04:00
|
|
|
}).catch(done);
|
2013-11-11 23:55:22 +04:00
|
|
|
});
|
|
|
|
|
2014-09-30 02:45:58 +04:00
|
|
|
it('shortens slug if possible', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const userData = testUtils.DataGenerator.forModel.users[2];
|
2014-09-30 02:45:58 +04:00
|
|
|
|
|
|
|
UserModel.add(userData, context).then(function (createdUser) {
|
|
|
|
should.exist(createdUser);
|
|
|
|
createdUser.has('slug').should.equal(true);
|
|
|
|
createdUser.attributes.slug.should.equal('jimothy');
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not short slug if not possible', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const userData = testUtils.DataGenerator.forModel.users[2];
|
2014-09-30 02:45:58 +04:00
|
|
|
|
|
|
|
UserModel.add(userData, context).then(function (createdUser) {
|
|
|
|
should.exist(createdUser);
|
|
|
|
createdUser.has('slug').should.equal(true);
|
|
|
|
createdUser.attributes.slug.should.equal('jimothy');
|
|
|
|
}).then(function () {
|
|
|
|
userData.email = 'newmail@mail.com';
|
|
|
|
UserModel.add(userData, context).then(function (createdUser) {
|
|
|
|
should.exist(createdUser);
|
|
|
|
createdUser.has('slug').should.equal(true);
|
|
|
|
createdUser.attributes.slug.should.equal('jimothy-bogendath');
|
|
|
|
}).then(function () {
|
|
|
|
userData.email = 'newmail2@mail.com';
|
|
|
|
UserModel.add(userData, context).then(function (createdUser) {
|
|
|
|
should.exist(createdUser);
|
|
|
|
createdUser.has('slug').should.equal(true);
|
|
|
|
createdUser.attributes.slug.should.equal('jimothy-bogendath-2');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2014-01-15 02:47:17 +04:00
|
|
|
it('does NOT lowercase email', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const userData = testUtils.DataGenerator.forModel.users[2];
|
2014-07-21 21:50:04 +04:00
|
|
|
|
2014-07-15 15:03:12 +04:00
|
|
|
UserModel.add(userData, context).then(function (createdUser) {
|
2013-11-18 04:34:02 +04:00
|
|
|
should.exist(createdUser);
|
2014-07-21 13:29:03 +04:00
|
|
|
createdUser.attributes.email.should.eql(userData.email, 'email address correct');
|
2013-11-18 04:34:02 +04:00
|
|
|
done();
|
2014-05-06 00:58:58 +04:00
|
|
|
}).catch(done);
|
2013-11-18 04:34:02 +04:00
|
|
|
});
|
|
|
|
|
2013-11-11 23:55:22 +04:00
|
|
|
it('can find gravatar', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const userData = testUtils.DataGenerator.forModel.users[4];
|
2014-07-21 21:50:04 +04:00
|
|
|
|
2020-10-20 03:19:33 +03:00
|
|
|
sinon.stub(imageLib.gravatar, 'lookup').callsFake(function (data) {
|
|
|
|
data.image = 'http://www.gravatar.com/avatar/2fab21a4c4ed88e76add10650c73bae1?d=404';
|
|
|
|
return Promise.resolve(data);
|
2014-07-21 21:50:04 +04:00
|
|
|
});
|
2013-06-15 02:12:04 +04:00
|
|
|
|
2014-07-15 15:03:12 +04:00
|
|
|
UserModel.add(userData, context).then(function (createdUser) {
|
2013-11-11 23:55:22 +04:00
|
|
|
should.exist(createdUser);
|
2017-04-24 20:21:47 +03:00
|
|
|
createdUser.attributes.profile_image.should.eql(
|
2014-07-21 13:29:03 +04:00
|
|
|
'http://www.gravatar.com/avatar/2fab21a4c4ed88e76add10650c73bae1?d=404', 'Gravatar found'
|
|
|
|
);
|
2013-11-11 23:55:22 +04:00
|
|
|
done();
|
2014-05-06 00:58:58 +04:00
|
|
|
}).catch(done);
|
2013-11-11 23:55:22 +04:00
|
|
|
});
|
|
|
|
|
2014-01-15 02:47:17 +04:00
|
|
|
it('can handle no gravatar', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const userData = testUtils.DataGenerator.forModel.users[0];
|
2014-07-21 21:50:04 +04:00
|
|
|
|
2020-10-20 03:19:33 +03:00
|
|
|
sinon.stub(imageLib.gravatar, 'lookup').callsFake(function (data) {
|
|
|
|
return Promise.resolve(data);
|
2014-07-21 21:50:04 +04:00
|
|
|
});
|
2013-11-11 23:55:22 +04:00
|
|
|
|
2014-07-15 15:03:12 +04:00
|
|
|
UserModel.add(userData, context).then(function (createdUser) {
|
2013-11-11 23:55:22 +04:00
|
|
|
should.exist(createdUser);
|
|
|
|
should.not.exist(createdUser.image);
|
2013-06-25 15:43:15 +04:00
|
|
|
done();
|
2014-05-06 00:58:58 +04:00
|
|
|
}).catch(done);
|
2013-06-15 02:12:04 +04:00
|
|
|
});
|
2014-01-15 02:47:17 +04:00
|
|
|
|
|
|
|
it('can find by email and is case insensitive', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const userData = testUtils.DataGenerator.forModel.users[2];
|
|
|
|
const email = testUtils.DataGenerator.forModel.users[2].email;
|
2014-01-15 02:47:17 +04:00
|
|
|
|
2014-07-15 15:03:12 +04:00
|
|
|
UserModel.add(userData, context).then(function () {
|
2014-01-15 02:47:17 +04:00
|
|
|
// Test same case
|
|
|
|
return UserModel.getByEmail(email).then(function (user) {
|
|
|
|
should.exist(user);
|
|
|
|
user.attributes.email.should.eql(email);
|
|
|
|
});
|
|
|
|
}).then(function () {
|
|
|
|
// Test entered in lowercase
|
|
|
|
return UserModel.getByEmail(email.toLowerCase()).then(function (user) {
|
|
|
|
should.exist(user);
|
|
|
|
user.attributes.email.should.eql(email);
|
|
|
|
});
|
|
|
|
}).then(function () {
|
|
|
|
// Test entered in uppercase
|
|
|
|
return UserModel.getByEmail(email.toUpperCase()).then(function (user) {
|
|
|
|
should.exist(user);
|
|
|
|
user.attributes.email.should.eql(email);
|
|
|
|
});
|
|
|
|
}).then(function () {
|
|
|
|
// Test incorrect email address - swapped capital O for number 0
|
|
|
|
return UserModel.getByEmail('jb0gendAth@example.com').then(null, function (error) {
|
|
|
|
should.exist(error);
|
|
|
|
error.message.should.eql('NotFound');
|
|
|
|
});
|
|
|
|
}).then(function () {
|
|
|
|
done();
|
2014-05-06 00:58:58 +04:00
|
|
|
}).catch(done);
|
2014-01-15 02:47:17 +04:00
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-06-15 02:12:04 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
describe('Basic Operations', function () {
|
2014-07-30 18:02:25 +04:00
|
|
|
beforeEach(testUtils.setup('users:roles'));
|
2013-06-15 02:12:04 +04:00
|
|
|
|
2017-01-25 16:47:49 +03:00
|
|
|
beforeEach(function () {
|
|
|
|
eventsTriggered = {};
|
2020-05-25 11:49:38 +03:00
|
|
|
sinon.stub(events, 'emit').callsFake(function (eventName, eventObj) {
|
2017-01-25 16:47:49 +03:00
|
|
|
if (!eventsTriggered[eventName]) {
|
|
|
|
eventsTriggered[eventName] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
eventsTriggered[eventName].push(eventObj);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-03-27 19:28:34 +04:00
|
|
|
it('sets last login time on successful login', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const userData = testUtils.DataGenerator.forModel.users[0];
|
2014-03-27 19:28:34 +04:00
|
|
|
|
2014-06-30 16:58:10 +04:00
|
|
|
UserModel.check({email: userData.email, password: userData.password}).then(function (activeUser) {
|
2017-04-05 22:45:55 +03:00
|
|
|
should.exist(activeUser.get('last_seen'));
|
2014-03-27 19:28:34 +04:00
|
|
|
done();
|
2014-05-06 00:58:58 +04:00
|
|
|
}).catch(done);
|
2014-03-27 19:28:34 +04:00
|
|
|
});
|
|
|
|
|
2014-07-05 18:57:56 +04:00
|
|
|
it('converts fetched dateTime fields to Date objects', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const userData = testUtils.DataGenerator.forModel.users[0];
|
2014-07-05 18:57:56 +04:00
|
|
|
|
2014-09-10 08:06:24 +04:00
|
|
|
UserModel.check({email: userData.email, password: userData.password}).then(function (user) {
|
|
|
|
return UserModel.findOne({id: user.id});
|
2014-07-05 18:57:56 +04:00
|
|
|
}).then(function (user) {
|
2020-04-29 18:44:27 +03:00
|
|
|
let lastLogin;
|
|
|
|
let createdAt;
|
|
|
|
let updatedAt;
|
2014-07-05 18:57:56 +04:00
|
|
|
|
|
|
|
should.exist(user);
|
|
|
|
|
2017-04-05 22:45:55 +03:00
|
|
|
lastLogin = user.get('last_seen');
|
2014-09-10 08:06:24 +04:00
|
|
|
createdAt = user.get('created_at');
|
|
|
|
updatedAt = user.get('updated_at');
|
2014-07-05 18:57:56 +04:00
|
|
|
|
2014-09-10 08:06:24 +04:00
|
|
|
lastLogin.should.be.an.instanceof(Date);
|
|
|
|
createdAt.should.be.an.instanceof(Date);
|
|
|
|
updatedAt.should.be.an.instanceof(Date);
|
2014-07-05 18:57:56 +04:00
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2016-08-11 10:51:19 +03:00
|
|
|
it('can findPage with limit all', function () {
|
2014-12-03 21:43:31 +03:00
|
|
|
return testUtils.fixtures.createExtraUsers().then(function () {
|
|
|
|
return UserModel.findPage({limit: 'all'});
|
|
|
|
}).then(function (results) {
|
|
|
|
results.meta.pagination.page.should.equal(1);
|
|
|
|
results.meta.pagination.limit.should.equal('all');
|
|
|
|
results.meta.pagination.pages.should.equal(1);
|
2018-10-06 03:09:07 +03:00
|
|
|
results.data.length.should.equal(9);
|
2016-08-11 10:51:19 +03:00
|
|
|
});
|
2014-12-03 21:43:31 +03:00
|
|
|
});
|
|
|
|
|
2016-08-11 10:51:19 +03:00
|
|
|
it('can findOne by role name', function () {
|
2015-01-20 20:40:25 +03:00
|
|
|
return testUtils.fixtures.createExtraUsers().then(function () {
|
|
|
|
return Promise.join(UserModel.findOne({role: 'Owner'}), UserModel.findOne({role: 'Editor'}));
|
|
|
|
}).then(function (results) {
|
2020-04-29 18:44:27 +03:00
|
|
|
let owner = results[0];
|
|
|
|
let editor = results[1];
|
2015-01-20 20:40:25 +03:00
|
|
|
|
|
|
|
should.exist(owner);
|
|
|
|
should.exist(editor);
|
|
|
|
|
|
|
|
owner = owner.toJSON();
|
|
|
|
editor = editor.toJSON();
|
|
|
|
|
|
|
|
should.exist(owner.roles);
|
|
|
|
should.exist(editor.roles);
|
|
|
|
|
|
|
|
owner.roles[0].name.should.equal('Owner');
|
|
|
|
editor.roles[0].name.should.equal('Editor');
|
2016-08-11 10:51:19 +03:00
|
|
|
});
|
2015-01-20 20:40:25 +03:00
|
|
|
});
|
|
|
|
|
2015-03-24 23:23:23 +03:00
|
|
|
it('can invite user', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const userData = testUtils.DataGenerator.forModel.users[4];
|
2015-03-24 23:23:23 +03:00
|
|
|
|
|
|
|
UserModel.add(_.extend({}, userData, {status: 'invited'}), context).then(function (createdUser) {
|
|
|
|
should.exist(createdUser);
|
|
|
|
createdUser.attributes.password.should.not.equal(userData.password, 'password was hashed');
|
|
|
|
createdUser.attributes.email.should.eql(userData.email, 'email address correct');
|
|
|
|
|
2017-01-25 16:47:49 +03:00
|
|
|
Object.keys(eventsTriggered).length.should.eql(1);
|
|
|
|
should.exist(eventsTriggered['user.added']);
|
2015-03-24 23:23:23 +03:00
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can add active user', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const userData = testUtils.DataGenerator.forModel.users[4];
|
2015-03-24 23:23:23 +03:00
|
|
|
|
|
|
|
RoleModel.findOne().then(function (role) {
|
|
|
|
userData.roles = [role.toJSON()];
|
|
|
|
|
Sorted out the mixed usages of `include` and `withRelated` (#9425)
no issue
- this commit cleans up the usages of `include` and `withRelated`.
### API layer (`include`)
- as request parameter e.g. `?include=roles,tags`
- as theme API parameter e.g. `{{get .... include="author"}}`
- as internal API access e.g. `api.posts.browse({include: 'author,tags'})`
- the `include` notation is more readable than `withRelated`
- and it allows us to use a different easier format (comma separated list)
- the API utility transforms these more readable properties into model style (or into Ghost style)
### Model access (`withRelated`)
- e.g. `models.Post.findPage({withRelated: ['tags']})`
- driven by bookshelf
---
Commits explained.
* Reorder the usage of `convertOptions`
- 1. validation
- 2. options convertion
- 3. permissions
- the reason is simple, the permission layer access the model layer
- we have to prepare the options before talking to the model layer
- added `convertOptions` where it was missed (not required, but for consistency reasons)
* Use `withRelated` when accessing the model layer and use `include` when accessing the API layer
* Change `convertOptions` API utiliy
- API Usage
- ghost.api(..., {include: 'tags,authors'})
- `include` should only be used when calling the API (either via request or via manual usage)
- `include` is only for readability and easier format
- Ghost (Model Layer Usage)
- models.Post.findOne(..., {withRelated: ['tags', 'authors']})
- should only use `withRelated`
- model layer cannot read 'tags,authors`
- model layer has no idea what `include` means, speaks a different language
- `withRelated` is bookshelf
- internal usage
* include-count plugin: use `withRelated` instead of `include`
- imagine you outsource this plugin to git and publish it to npm
- `include` is an unknown option in bookshelf
* Updated `permittedOptions` in base model
- `include` is no longer a known option
* Remove all occurances of `include` in the model layer
* Extend `filterOptions` base function
- this function should be called as first action
- we clone the unfiltered options
- check if you are using `include` (this is a protection which could help us in the beginning)
- check for permitted and (later on default `withRelated`) options
- the usage is coming in next commit
* Ensure we call `filterOptions` as first action
- use `ghostBookshelf.Model.filterOptions` as first action
- consistent naming pattern for incoming options: `unfilteredOptions`
- re-added allowed options for `toJSON`
- one unsolved architecture problem:
- if you override a function e.g. `edit`
- then you should call `filterOptions` as first action
- the base implementation of e.g. `edit` will call it again
- future improvement
* Removed `findOne` from Invite model
- no longer needed, the base implementation is the same
2018-02-15 12:53:53 +03:00
|
|
|
return UserModel.add(userData, _.extend({}, context, {withRelated: ['roles']}));
|
2015-03-24 23:23:23 +03:00
|
|
|
}).then(function (createdUser) {
|
|
|
|
should.exist(createdUser);
|
|
|
|
createdUser.get('password').should.not.equal(userData.password, 'password was hashed');
|
|
|
|
createdUser.get('email').should.eql(userData.email, 'email address correct');
|
|
|
|
createdUser.related('roles').toJSON()[0].name.should.eql('Administrator', 'role set correctly');
|
|
|
|
|
2017-01-25 16:47:49 +03:00
|
|
|
Object.keys(eventsTriggered).length.should.eql(2);
|
|
|
|
should.exist(eventsTriggered['user.added']);
|
|
|
|
should.exist(eventsTriggered['user.activated']);
|
2015-03-24 23:23:23 +03:00
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2016-02-18 02:02:16 +03:00
|
|
|
it('can NOT add active user with invalid email address', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const userData = _.clone(testUtils.DataGenerator.forModel.users[4]);
|
2016-02-18 02:02:16 +03:00
|
|
|
|
|
|
|
userData.email = 'invalidemailaddress';
|
|
|
|
|
|
|
|
RoleModel.findOne().then(function (role) {
|
|
|
|
userData.roles = [role.toJSON()];
|
|
|
|
|
Sorted out the mixed usages of `include` and `withRelated` (#9425)
no issue
- this commit cleans up the usages of `include` and `withRelated`.
### API layer (`include`)
- as request parameter e.g. `?include=roles,tags`
- as theme API parameter e.g. `{{get .... include="author"}}`
- as internal API access e.g. `api.posts.browse({include: 'author,tags'})`
- the `include` notation is more readable than `withRelated`
- and it allows us to use a different easier format (comma separated list)
- the API utility transforms these more readable properties into model style (or into Ghost style)
### Model access (`withRelated`)
- e.g. `models.Post.findPage({withRelated: ['tags']})`
- driven by bookshelf
---
Commits explained.
* Reorder the usage of `convertOptions`
- 1. validation
- 2. options convertion
- 3. permissions
- the reason is simple, the permission layer access the model layer
- we have to prepare the options before talking to the model layer
- added `convertOptions` where it was missed (not required, but for consistency reasons)
* Use `withRelated` when accessing the model layer and use `include` when accessing the API layer
* Change `convertOptions` API utiliy
- API Usage
- ghost.api(..., {include: 'tags,authors'})
- `include` should only be used when calling the API (either via request or via manual usage)
- `include` is only for readability and easier format
- Ghost (Model Layer Usage)
- models.Post.findOne(..., {withRelated: ['tags', 'authors']})
- should only use `withRelated`
- model layer cannot read 'tags,authors`
- model layer has no idea what `include` means, speaks a different language
- `withRelated` is bookshelf
- internal usage
* include-count plugin: use `withRelated` instead of `include`
- imagine you outsource this plugin to git and publish it to npm
- `include` is an unknown option in bookshelf
* Updated `permittedOptions` in base model
- `include` is no longer a known option
* Remove all occurances of `include` in the model layer
* Extend `filterOptions` base function
- this function should be called as first action
- we clone the unfiltered options
- check if you are using `include` (this is a protection which could help us in the beginning)
- check for permitted and (later on default `withRelated`) options
- the usage is coming in next commit
* Ensure we call `filterOptions` as first action
- use `ghostBookshelf.Model.filterOptions` as first action
- consistent naming pattern for incoming options: `unfilteredOptions`
- re-added allowed options for `toJSON`
- one unsolved architecture problem:
- if you override a function e.g. `edit`
- then you should call `filterOptions` as first action
- the base implementation of e.g. `edit` will call it again
- future improvement
* Removed `findOne` from Invite model
- no longer needed, the base implementation is the same
2018-02-15 12:53:53 +03:00
|
|
|
return UserModel.add(userData, _.extend({}, context, {withRelated: ['roles']}));
|
2016-02-18 02:02:16 +03:00
|
|
|
}).then(function () {
|
|
|
|
done(new Error('User was created with an invalid email address'));
|
|
|
|
}).catch(function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-03-24 23:23:23 +03:00
|
|
|
it('can edit active user', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const firstUser = testUtils.DataGenerator.Content.users[0].id;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
UserModel.findOne({id: firstUser}).then(function (results) {
|
2020-04-29 18:44:27 +03:00
|
|
|
let user;
|
2013-08-25 00:51:58 +04:00
|
|
|
should.exist(results);
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
user = results.toJSON();
|
|
|
|
user.id.should.equal(firstUser);
|
|
|
|
should.equal(user.website, null);
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2014-07-14 20:32:55 +04:00
|
|
|
return UserModel.edit({website: 'http://some.newurl.com'}, {id: firstUser});
|
2013-08-25 00:51:58 +04:00
|
|
|
}).then(function (edited) {
|
|
|
|
should.exist(edited);
|
2014-07-14 20:32:55 +04:00
|
|
|
edited.attributes.website.should.equal('http://some.newurl.com');
|
2013-05-27 05:39:38 +04:00
|
|
|
|
2017-01-25 16:47:49 +03:00
|
|
|
Object.keys(eventsTriggered).length.should.eql(2);
|
|
|
|
should.exist(eventsTriggered['user.activated.edited']);
|
|
|
|
should.exist(eventsTriggered['user.edited']);
|
2015-03-24 23:23:23 +03:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
done();
|
2014-05-06 00:58:58 +04:00
|
|
|
}).catch(done);
|
2013-08-25 00:51:58 +04:00
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2016-02-18 02:02:16 +03:00
|
|
|
it('can NOT set an invalid email address', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const firstUser = testUtils.DataGenerator.Content.users[0].id;
|
2016-02-18 02:02:16 +03:00
|
|
|
|
|
|
|
UserModel.findOne({id: firstUser}).then(function (user) {
|
|
|
|
return user.edit({email: 'notanemailaddress'});
|
|
|
|
}).then(function () {
|
|
|
|
done(new Error('Invalid email address was accepted'));
|
|
|
|
}).catch(function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-02-08 12:50:43 +03:00
|
|
|
it('can NOT set an already existing email address', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const firstUser = testUtils.DataGenerator.Content.users[0];
|
|
|
|
const secondUser = testUtils.DataGenerator.Content.users[1];
|
2017-02-13 18:36:21 +03:00
|
|
|
|
|
|
|
UserModel.edit({email: secondUser.email}, {id: firstUser.id})
|
|
|
|
.then(function () {
|
|
|
|
done(new Error('Already existing email address was accepted'));
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
2020-05-25 11:49:38 +03:00
|
|
|
(err instanceof errors.ValidationError).should.eql(true);
|
2017-02-13 18:36:21 +03:00
|
|
|
done();
|
|
|
|
});
|
2017-02-08 12:50:43 +03:00
|
|
|
});
|
|
|
|
|
2015-03-24 23:23:23 +03:00
|
|
|
it('can edit invited user', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const userData = testUtils.DataGenerator.forModel.users[4];
|
|
|
|
let userId;
|
2014-07-29 09:05:13 +04:00
|
|
|
|
2015-03-24 23:23:23 +03:00
|
|
|
UserModel.add(_.extend({}, userData, {status: 'invited'}), context).then(function (createdUser) {
|
|
|
|
should.exist(createdUser);
|
|
|
|
createdUser.attributes.password.should.not.equal(userData.password, 'password was hashed');
|
|
|
|
createdUser.attributes.email.should.eql(userData.email, 'email address correct');
|
|
|
|
createdUser.attributes.status.should.equal('invited');
|
2014-07-29 09:05:13 +04:00
|
|
|
|
2015-03-24 23:23:23 +03:00
|
|
|
userId = createdUser.attributes.id;
|
|
|
|
|
2017-01-25 16:47:49 +03:00
|
|
|
Object.keys(eventsTriggered).length.should.eql(1);
|
|
|
|
should.exist(eventsTriggered['user.added']);
|
2015-03-24 23:23:23 +03:00
|
|
|
|
|
|
|
return UserModel.edit({website: 'http://some.newurl.com'}, {id: userId});
|
2014-07-29 09:05:13 +04:00
|
|
|
}).then(function (createdUser) {
|
2015-03-24 23:23:23 +03:00
|
|
|
createdUser.attributes.status.should.equal('invited');
|
|
|
|
|
2017-01-25 16:47:49 +03:00
|
|
|
Object.keys(eventsTriggered).length.should.eql(2);
|
|
|
|
should.exist(eventsTriggered['user.edited']);
|
|
|
|
|
2015-03-24 23:23:23 +03:00
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can activate invited user', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const userData = testUtils.DataGenerator.forModel.users[4];
|
|
|
|
let userId;
|
2015-03-24 23:23:23 +03:00
|
|
|
|
|
|
|
UserModel.add(_.extend({}, userData, {status: 'invited'}), context).then(function (createdUser) {
|
2014-07-29 09:05:13 +04:00
|
|
|
should.exist(createdUser);
|
2015-03-24 23:23:23 +03:00
|
|
|
createdUser.attributes.password.should.not.equal(userData.password, 'password was hashed');
|
|
|
|
createdUser.attributes.email.should.eql(userData.email, 'email address correct');
|
|
|
|
createdUser.attributes.status.should.equal('invited');
|
|
|
|
|
|
|
|
userId = createdUser.attributes.id;
|
|
|
|
|
2017-01-25 16:47:49 +03:00
|
|
|
Object.keys(eventsTriggered).length.should.eql(1);
|
|
|
|
should.exist(eventsTriggered['user.added']);
|
2014-07-29 09:05:13 +04:00
|
|
|
|
2015-03-24 23:23:23 +03:00
|
|
|
return UserModel.edit({status: 'active'}, {id: userId});
|
|
|
|
}).then(function (createdUser) {
|
|
|
|
createdUser.attributes.status.should.equal('active');
|
|
|
|
|
2017-01-25 16:47:49 +03:00
|
|
|
Object.keys(eventsTriggered).length.should.eql(3);
|
|
|
|
should.exist(eventsTriggered['user.activated']);
|
|
|
|
should.exist(eventsTriggered['user.edited']);
|
|
|
|
|
2014-07-29 09:05:13 +04:00
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2015-03-24 23:23:23 +03:00
|
|
|
it('can destroy invited user', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const userData = testUtils.DataGenerator.forModel.users[4];
|
|
|
|
let userId;
|
2015-03-24 23:23:23 +03:00
|
|
|
|
|
|
|
UserModel.add(_.extend({}, userData, {status: 'invited'}), context).then(function (createdUser) {
|
|
|
|
should.exist(createdUser);
|
|
|
|
createdUser.attributes.password.should.not.equal(userData.password, 'password was hashed');
|
|
|
|
createdUser.attributes.email.should.eql(userData.email, 'email address correct');
|
|
|
|
createdUser.attributes.status.should.equal('invited');
|
|
|
|
|
|
|
|
userId = {id: createdUser.attributes.id};
|
|
|
|
|
2017-01-25 16:47:49 +03:00
|
|
|
Object.keys(eventsTriggered).length.should.eql(1);
|
|
|
|
should.exist(eventsTriggered['user.added']);
|
2015-03-24 23:23:23 +03:00
|
|
|
|
|
|
|
// Destroy the user
|
|
|
|
return UserModel.destroy(userId);
|
|
|
|
}).then(function (response) {
|
2016-02-08 00:27:01 +03:00
|
|
|
response.toJSON().should.be.empty();
|
2015-03-24 23:23:23 +03:00
|
|
|
|
2017-01-25 16:47:49 +03:00
|
|
|
Object.keys(eventsTriggered).length.should.eql(2);
|
|
|
|
should.exist(eventsTriggered['user.deleted']);
|
2015-03-24 23:23:23 +03:00
|
|
|
|
|
|
|
// Double check we can't find the user again
|
|
|
|
return UserModel.findOne(userId);
|
2013-08-25 00:51:58 +04:00
|
|
|
}).then(function (newResults) {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
should.equal(newResults, null);
|
2013-08-25 00:51:58 +04:00
|
|
|
|
|
|
|
done();
|
2014-05-06 00:58:58 +04:00
|
|
|
}).catch(done);
|
2013-08-25 00:51:58 +04:00
|
|
|
});
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
});
|
|
|
|
|
2016-10-14 20:24:38 +03:00
|
|
|
describe('Password change', function () {
|
|
|
|
beforeEach(testUtils.setup('users:roles'));
|
|
|
|
|
|
|
|
describe('error', function () {
|
|
|
|
it('wrong old password', function (done) {
|
|
|
|
UserModel.changePassword({
|
2017-10-18 19:45:41 +03:00
|
|
|
newPassword: '1234567890',
|
|
|
|
ne2Password: '1234567890',
|
2016-10-21 12:19:09 +03:00
|
|
|
oldPassword: '123456789',
|
✨ replace auto increment id's by object id (#7495)
* 🛠 bookshelf tarball, bson-objectid
* 🎨 schema changes
- change increment type to string
- add a default fallback for string length 191 (to avoid adding this logic to every single column which uses an ID)
- remove uuid, because ID now represents a global resource identifier
- keep uuid for post, because we are using this as preview id
- keep uuid for clients for now - we are using this param for Ghost-Auth
* ✨ base model: generate ObjectId on creating event
- each new resource get's a auto generate ObjectId
- this logic won't work for attached models, this commit comes later
* 🎨 centralised attach method
When attaching models there are two things important two know
1. To be able to attach an ObjectId, we need to register the `onCreating` event the fetched model!This is caused by the Bookshelf design in general. On this target model we are attaching the new model.
2. We need to manually fetch the target model, because Bookshelf has a weird behaviour (which is known as a bug, see see https://github.com/tgriesser/bookshelf/issues/629). The most important property when attaching a model is `parentFk`, which is the foreign key. This can be null when fetching the model with the option `withRelated`. To ensure quality and consistency, the custom attach wrapper always fetches the target model manual. By fetching the target model (again) is a little performance decrease, but it also has advantages: we can register the event, and directly unregister the event again. So very clean code.
Important: please only use the custom attach wrapper in the future.
* 🎨 token model had overriden the onCreating function because of the created_at field
- we need to ensure that the base onCreating hook get's triggered for ALL models
- if not, they don't get an ObjectId assigned
- in this case: be smart and check if the target model has a created_at field
* 🎨 we don't have a uuid field anymore, remove the usages
- no default uuid creation in models
- i am pretty sure we have some more definitions in our tests (for example in the export json files), but that is too much work to delete them all
* 🎨 do not parse ID to Number
- we had various occurances of parsing all ID's to numbers
- we don't need this behaviour anymore
- ID is string
- i will adapt the ID validation in the next commit
* 🎨 change ID regex for validation
- we only allow: ID as ObjectId, ID as 1 and ID as me
- we need to keep ID 1, because our whole software relies on ID 1 (permissions etc)
* 🎨 owner fixture
- roles: [4] does not work anymore
- 4 means -> static id 4
- this worked in an auto increment system (not even in a system with distributed writes)
- with ObjectId we generate each ID automatically (for static and dynamic resources)
- it is possible to define all id's for static resources still, but that means we need to know which ID is already used and for consistency we have to define ObjectId's for these static resources
- so no static id's anymore, except of: id 1 for owner and id 0 for external usage (because this is required from our permission system)
- NOTE: please read through the comment in the user model
* 🎨 tests: DataGenerator and test utils
First of all: we need to ensure using ObjectId's in the tests. When don't, we can't ensure that ObjectId's work properly.
This commit brings lot's of dynamic into all the static defined id's.
In one of the next commits, i will adapt all the tests.
* 🚨 remove counter in Notification API
- no need to add a counter
- we simply generate ObjectId's (they are auto incremental as well)
- our id validator does only allow ObjectId as id,1 and me
* 🎨 extend contextUser in Base Model
- remove isNumber check, because id's are no longer numbers, except of id 0/1
- use existing isExternalUser
- support id 0/1 as string or number
* ✨ Ghost Owner has id 1
- ensure we define this id in the fixtures.json
- doesn't matter if number or string
* 🎨 functional tests adaptions
- use dynamic id's
* 🎨 fix unit tests
* 🎨 integration tests adaptions
* 🎨 change importer utils
- all our export examples (test/fixtures/exports) contain id's as numbers
- fact: but we ignore them anyway when inserting into the database, see https://github.com/TryGhost/Ghost/blob/master/core/server/data/import/utils.js#L249
- in https://github.com/TryGhost/Ghost/pull/7495/commits/0e6ed957cd54dc02a25cf6fb1ab7d7e723295e2c#diff-70f514a06347c048648be464819503c4L67 i removed parsing id's to integers
- i realised that this ^ check just existed, because the userIdToMap was an object key and object keys are always strings!
- i think this logic is a little bit complicated, but i don't want to refactor this now
- this commit ensures when trying to find the user, the id comparison works again
- i've added more documentation to understand this logic ;)
- plus i renamed an attribute to improve readability
* 🎨 Data-Generator: add more defaults to createUser
- if i use the function DataGenerator.forKnex.createUser i would like to get a full set of defaults
* 🎨 test utils: change/extend function set for functional tests
- functional tests work a bit different
- they boot Ghost and seed the database
- some functional tests have mis-used the test setup
- the test setup needs two sections: integration/unit and functional tests
- any functional test is allowed to either add more data or change data in the existing Ghost db
- but what it should not do is: add test fixtures like roles or users from our DataGenerator and cross fingers it will work
- this commit adds a clean method for functional tests to add extra users
* 🎨 functional tests adaptions
- use last commit to insert users for functional tests clean
- tidy up usage of testUtils.setup or testUtils.doAuth
* 🐛 test utils: reset database before init
- ensure we don't have any left data from other tests in the database when starting ghost
* 🐛 fix test (unrelated to this PR)
- fixes a random failure
- return statement was missing
* 🎨 make changes for invites
2016-11-17 12:09:11 +03:00
|
|
|
user_id: testUtils.DataGenerator.Content.users[0].id
|
2016-10-21 12:19:09 +03:00
|
|
|
}, testUtils.context.owner).then(function () {
|
|
|
|
done(new Error('expected error!'));
|
|
|
|
}).catch(function (err) {
|
2020-05-25 11:49:38 +03:00
|
|
|
(err instanceof errors.ValidationError).should.eql(true);
|
2016-10-21 12:19:09 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-10-26 13:01:24 +03:00
|
|
|
|
|
|
|
it('too short password', function (done) {
|
|
|
|
UserModel.changePassword({
|
|
|
|
newPassword: '12345678',
|
|
|
|
ne2Password: '12345678',
|
|
|
|
oldPassword: 'Sl1m3rson99',
|
|
|
|
user_id: testUtils.DataGenerator.Content.users[0].id
|
|
|
|
}, testUtils.context.owner).then(function () {
|
|
|
|
done(new Error('expected error!'));
|
|
|
|
}).catch(function (err) {
|
2020-05-25 11:49:38 +03:00
|
|
|
(err instanceof errors.ValidationError).should.eql(true);
|
2017-10-26 13:01:24 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('very bad password', function (done) {
|
|
|
|
UserModel.changePassword({
|
|
|
|
newPassword: '1234567890',
|
|
|
|
ne2Password: '1234567890',
|
|
|
|
oldPassword: 'Sl1m3rson99',
|
|
|
|
user_id: testUtils.DataGenerator.Content.users[0].id
|
|
|
|
}, testUtils.context.owner).then(function () {
|
|
|
|
done(new Error('expected error!'));
|
|
|
|
}).catch(function (err) {
|
2020-05-25 11:49:38 +03:00
|
|
|
(err instanceof errors.ValidationError).should.eql(true);
|
2017-10-26 13:01:24 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('password matches users email adress', function (done) {
|
|
|
|
UserModel.changePassword({
|
|
|
|
newPassword: 'jbloggs@example.com',
|
|
|
|
ne2Password: 'jbloggs@example.com',
|
|
|
|
oldPassword: 'Sl1m3rson99',
|
|
|
|
user_id: testUtils.DataGenerator.Content.users[0].id
|
|
|
|
}, testUtils.context.owner).then(function () {
|
|
|
|
done(new Error('expected error!'));
|
|
|
|
}).catch(function (err) {
|
2020-05-25 11:49:38 +03:00
|
|
|
(err instanceof errors.ValidationError).should.eql(true);
|
2017-10-26 13:01:24 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('password contains words "ghost" or "password"', function (done) {
|
|
|
|
UserModel.changePassword({
|
|
|
|
newPassword: 'onepassword',
|
|
|
|
ne2Password: 'onepassword',
|
|
|
|
oldPassword: 'Sl1m3rson99',
|
|
|
|
user_id: testUtils.DataGenerator.Content.users[0].id
|
|
|
|
}, testUtils.context.owner).then(function () {
|
|
|
|
done(new Error('expected error!'));
|
|
|
|
}).catch(function (err) {
|
2020-05-25 11:49:38 +03:00
|
|
|
(err instanceof errors.ValidationError).should.eql(true);
|
2017-10-26 13:01:24 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('password matches blog URL', function (done) {
|
|
|
|
UserModel.changePassword({
|
|
|
|
newPassword: '127.0.0.1:2369',
|
|
|
|
ne2Password: '127.0.0.1:2369',
|
|
|
|
oldPassword: 'Sl1m3rson99',
|
|
|
|
user_id: testUtils.DataGenerator.Content.users[0].id
|
|
|
|
}, testUtils.context.owner).then(function () {
|
|
|
|
done(new Error('expected error!'));
|
|
|
|
}).catch(function (err) {
|
2020-05-25 11:49:38 +03:00
|
|
|
(err instanceof errors.ValidationError).should.eql(true);
|
2017-10-26 13:01:24 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('password contains repeating chars', function (done) {
|
|
|
|
UserModel.changePassword({
|
|
|
|
newPassword: 'cdcdcdcdcd',
|
|
|
|
ne2Password: 'cdcdcdcdcd',
|
|
|
|
oldPassword: 'Sl1m3rson99',
|
|
|
|
user_id: testUtils.DataGenerator.Content.users[0].id
|
|
|
|
}, testUtils.context.owner).then(function () {
|
|
|
|
done(new Error('expected error!'));
|
|
|
|
}).catch(function (err) {
|
2020-05-25 11:49:38 +03:00
|
|
|
(err instanceof errors.ValidationError).should.eql(true);
|
2017-10-26 13:01:24 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('password contains repeating numbers', function (done) {
|
|
|
|
UserModel.changePassword({
|
|
|
|
newPassword: '1231111111',
|
|
|
|
ne2Password: '1231111111',
|
|
|
|
oldPassword: 'Sl1m3rson99',
|
|
|
|
user_id: testUtils.DataGenerator.Content.users[0].id
|
|
|
|
}, testUtils.context.owner).then(function () {
|
|
|
|
done(new Error('expected error!'));
|
|
|
|
}).catch(function (err) {
|
2020-05-25 11:49:38 +03:00
|
|
|
(err instanceof errors.ValidationError).should.eql(true);
|
2017-10-26 13:01:24 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2016-10-14 20:24:38 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('User setup', function () {
|
|
|
|
beforeEach(testUtils.setup('owner'));
|
|
|
|
|
|
|
|
it('setup user', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const userData = {
|
2016-10-14 20:24:38 +03:00
|
|
|
name: 'Max Mustermann',
|
|
|
|
email: 'test@ghost.org',
|
2017-10-26 13:01:24 +03:00
|
|
|
password: 'thisissupersafe'
|
2016-10-14 20:24:38 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
UserModel.setup(userData, {id: 1})
|
|
|
|
.then(function (user) {
|
|
|
|
user.get('name').should.eql(userData.name);
|
|
|
|
user.get('email').should.eql(userData.email);
|
|
|
|
user.get('slug').should.eql('max');
|
|
|
|
|
|
|
|
// naive check that password was hashed
|
|
|
|
user.get('password').should.not.eql(userData.password);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
});
|
2013-10-30 01:34:47 +04:00
|
|
|
});
|