Ghost/test/regression/models/model_users.test.js

563 lines
23 KiB
JavaScript
Raw Normal View History

const errors = require('@tryghost/errors');
const should = require('should');
const sinon = require('sinon');
const testUtils = require('../../utils');
const Promise = require('bluebird');
const _ = require('lodash');
// Stuff we are testing
const events = require('../../../core/server/lib/common/events');
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;
describe('User Model', function run() {
let eventsTriggered = {};
before(testUtils.teardownDb);
afterEach(testUtils.teardownDb);
afterEach(function () {
sinon.restore();
});
before(function () {
should.exist(UserModel);
});
describe('Registration', function runRegistration() {
beforeEach(testUtils.setup('roles'));
it('can add first', function (done) {
const userData = testUtils.DataGenerator.forModel.users[0];
UserModel.add(userData, 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');
done();
}).catch(done);
});
it('shortens slug if possible', function (done) {
const userData = testUtils.DataGenerator.forModel.users[2];
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) {
const userData = testUtils.DataGenerator.forModel.users[2];
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);
});
it('does NOT lowercase email', function (done) {
const userData = testUtils.DataGenerator.forModel.users[2];
UserModel.add(userData, context).then(function (createdUser) {
2013-11-18 04:34:02 +04:00
should.exist(createdUser);
createdUser.attributes.email.should.eql(userData.email, 'email address correct');
2013-11-18 04:34:02 +04:00
done();
}).catch(done);
2013-11-18 04:34:02 +04:00
});
it('can find gravatar', function (done) {
const userData = testUtils.DataGenerator.forModel.users[4];
sinon.stub(imageLib.gravatar, 'lookup').callsFake(function (data) {
data.image = 'http://www.gravatar.com/avatar/2fab21a4c4ed88e76add10650c73bae1?d=404';
return Promise.resolve(data);
});
UserModel.add(userData, context).then(function (createdUser) {
should.exist(createdUser);
createdUser.attributes.profile_image.should.eql(
'http://www.gravatar.com/avatar/2fab21a4c4ed88e76add10650c73bae1?d=404', 'Gravatar found'
);
done();
}).catch(done);
});
it('can handle no gravatar', function (done) {
const userData = testUtils.DataGenerator.forModel.users[0];
sinon.stub(imageLib.gravatar, 'lookup').callsFake(function (data) {
return Promise.resolve(data);
});
UserModel.add(userData, context).then(function (createdUser) {
should.exist(createdUser);
should.not.exist(createdUser.image);
done();
}).catch(done);
});
it('can find by email and is case insensitive', function (done) {
const userData = testUtils.DataGenerator.forModel.users[2];
const email = testUtils.DataGenerator.forModel.users[2].email;
UserModel.add(userData, context).then(function () {
// 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();
}).catch(done);
});
});
describe('Basic Operations', function () {
beforeEach(testUtils.setup('users:roles'));
beforeEach(function () {
eventsTriggered = {};
sinon.stub(events, 'emit').callsFake(function (eventName, eventObj) {
if (!eventsTriggered[eventName]) {
eventsTriggered[eventName] = [];
}
eventsTriggered[eventName].push(eventObj);
});
});
it('sets last login time on successful login', function (done) {
const userData = testUtils.DataGenerator.forModel.users[0];
UserModel.check({email: userData.email, password: userData.password}).then(function (activeUser) {
should.exist(activeUser.get('last_seen'));
done();
}).catch(done);
});
it('converts fetched dateTime fields to Date objects', function (done) {
const userData = testUtils.DataGenerator.forModel.users[0];
UserModel.check({email: userData.email, password: userData.password}).then(function (user) {
return UserModel.findOne({id: user.id});
}).then(function (user) {
let lastLogin;
let createdAt;
let updatedAt;
should.exist(user);
lastLogin = user.get('last_seen');
createdAt = user.get('created_at');
updatedAt = user.get('updated_at');
lastLogin.should.be.an.instanceof(Date);
createdAt.should.be.an.instanceof(Date);
updatedAt.should.be.an.instanceof(Date);
done();
}).catch(done);
});
it('can findPage with limit all', function () {
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);
results.data.length.should.equal(9);
});
});
it('can findOne by role name', function () {
return testUtils.fixtures.createExtraUsers().then(function () {
return Promise.join(UserModel.findOne({role: 'Owner'}), UserModel.findOne({role: 'Editor'}));
}).then(function (results) {
let owner = results[0];
let editor = results[1];
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');
});
});
it('can invite user', function (done) {
const userData = testUtils.DataGenerator.forModel.users[4];
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');
Object.keys(eventsTriggered).length.should.eql(1);
should.exist(eventsTriggered['user.added']);
done();
}).catch(done);
});
it('can add active user', function (done) {
const userData = testUtils.DataGenerator.forModel.users[4];
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']}));
}).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');
Object.keys(eventsTriggered).length.should.eql(2);
should.exist(eventsTriggered['user.added']);
should.exist(eventsTriggered['user.activated']);
done();
}).catch(done);
});
it('can NOT add active user with invalid email address', function (done) {
const userData = _.clone(testUtils.DataGenerator.forModel.users[4]);
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']}));
}).then(function () {
done(new Error('User was created with an invalid email address'));
}).catch(function () {
done();
});
});
it('can edit active user', function (done) {
const firstUser = testUtils.DataGenerator.Content.users[0].id;
UserModel.findOne({id: firstUser}).then(function (results) {
let user;
should.exist(results);
user = results.toJSON();
user.id.should.equal(firstUser);
should.equal(user.website, null);
return UserModel.edit({website: 'http://some.newurl.com'}, {id: firstUser});
}).then(function (edited) {
should.exist(edited);
edited.attributes.website.should.equal('http://some.newurl.com');
Object.keys(eventsTriggered).length.should.eql(2);
should.exist(eventsTriggered['user.activated.edited']);
should.exist(eventsTriggered['user.edited']);
done();
}).catch(done);
});
it('can NOT set an invalid email address', function (done) {
const firstUser = testUtils.DataGenerator.Content.users[0].id;
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();
});
});
it('can NOT set an already existing email address', function (done) {
const firstUser = testUtils.DataGenerator.Content.users[0];
const secondUser = testUtils.DataGenerator.Content.users[1];
UserModel.edit({email: secondUser.email}, {id: firstUser.id})
.then(function () {
done(new Error('Already existing email address was accepted'));
})
.catch(function (err) {
(err instanceof errors.ValidationError).should.eql(true);
done();
});
});
it('can edit invited user', function (done) {
const userData = testUtils.DataGenerator.forModel.users[4];
let userId;
2014-07-29 09:05:13 +04: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
userId = createdUser.attributes.id;
Object.keys(eventsTriggered).length.should.eql(1);
should.exist(eventsTriggered['user.added']);
return UserModel.edit({website: 'http://some.newurl.com'}, {id: userId});
2014-07-29 09:05:13 +04:00
}).then(function (createdUser) {
createdUser.attributes.status.should.equal('invited');
Object.keys(eventsTriggered).length.should.eql(2);
should.exist(eventsTriggered['user.edited']);
done();
}).catch(done);
});
it('can activate invited user', function (done) {
const userData = testUtils.DataGenerator.forModel.users[4];
let userId;
UserModel.add(_.extend({}, userData, {status: 'invited'}), context).then(function (createdUser) {
2014-07-29 09:05:13 +04:00
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 = createdUser.attributes.id;
Object.keys(eventsTriggered).length.should.eql(1);
should.exist(eventsTriggered['user.added']);
2014-07-29 09:05:13 +04:00
return UserModel.edit({status: 'active'}, {id: userId});
}).then(function (createdUser) {
createdUser.attributes.status.should.equal('active');
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);
});
it('can destroy invited user', function (done) {
const userData = testUtils.DataGenerator.forModel.users[4];
let userId;
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};
Object.keys(eventsTriggered).length.should.eql(1);
should.exist(eventsTriggered['user.added']);
// Destroy the user
return UserModel.destroy(userId);
}).then(function (response) {
response.toJSON().should.be.empty();
Object.keys(eventsTriggered).length.should.eql(2);
should.exist(eventsTriggered['user.deleted']);
// Double check we can't find the user again
return UserModel.findOne(userId);
}).then(function (newResults) {
should.equal(newResults, null);
done();
}).catch(done);
});
});
describe('Password change', function () {
beforeEach(testUtils.setup('users:roles'));
describe('error', function () {
it('wrong old password', function (done) {
UserModel.changePassword({
newPassword: '1234567890',
ne2Password: '1234567890',
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
}, testUtils.context.owner).then(function () {
done(new Error('expected error!'));
}).catch(function (err) {
(err instanceof errors.ValidationError).should.eql(true);
done();
});
});
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) {
(err instanceof errors.ValidationError).should.eql(true);
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) {
(err instanceof errors.ValidationError).should.eql(true);
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) {
(err instanceof errors.ValidationError).should.eql(true);
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) {
(err instanceof errors.ValidationError).should.eql(true);
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) {
(err instanceof errors.ValidationError).should.eql(true);
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) {
(err instanceof errors.ValidationError).should.eql(true);
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) {
(err instanceof errors.ValidationError).should.eql(true);
done();
});
});
});
});
describe('User setup', function () {
beforeEach(testUtils.setup('owner'));
it('setup user', function (done) {
const userData = {
name: 'Max Mustermann',
email: 'test@ghost.org',
password: 'thisissupersafe'
};
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);
});
});
});