2014-08-17 10:17:23 +04:00
|
|
|
var Promise = require('bluebird'),
|
2014-02-13 07:26:56 +04:00
|
|
|
_ = require('lodash'),
|
2013-12-29 00:13:47 +04:00
|
|
|
fs = require('fs-extra'),
|
|
|
|
path = require('path'),
|
2016-05-19 14:49:22 +03:00
|
|
|
Module = require('module'),
|
2015-07-29 21:07:58 +03:00
|
|
|
uuid = require('node-uuid'),
|
2016-02-12 14:56:27 +03:00
|
|
|
db = require('../../server/data/db'),
|
2014-07-16 01:43:22 +04:00
|
|
|
migration = require('../../server/data/migration/'),
|
2016-03-29 16:14:44 +03:00
|
|
|
fixtureUtils = require('../../server/data/migration/fixtures/utils'),
|
2016-07-15 19:22:41 +03:00
|
|
|
models = require('../../server/models'),
|
2014-07-21 21:50:04 +04:00
|
|
|
SettingsAPI = require('../../server/api/settings'),
|
|
|
|
permissions = require('../../server/permissions'),
|
2016-01-25 20:50:04 +03:00
|
|
|
sequence = require('../../server/utils/sequence'),
|
2013-11-07 17:26:47 +04:00
|
|
|
DataGenerator = require('./fixtures/data-generator'),
|
2015-07-28 03:27:54 +03:00
|
|
|
filterData = require('./fixtures/filter-param'),
|
2014-02-22 05:25:31 +04:00
|
|
|
API = require('./api'),
|
2014-07-21 13:29:03 +04:00
|
|
|
fork = require('./fork'),
|
2016-06-10 08:20:13 +03:00
|
|
|
mocks = require('./mocks'),
|
2014-07-17 18:33:21 +04:00
|
|
|
config = require('../../server/config'),
|
2014-07-21 13:29:03 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
fixtures,
|
|
|
|
getFixtureOps,
|
|
|
|
toDoList,
|
2016-05-19 14:49:22 +03:00
|
|
|
originalRequireFn,
|
2014-07-21 21:50:04 +04:00
|
|
|
postsInserted = 0,
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2016-05-19 14:49:22 +03:00
|
|
|
mockNotExistingModule,
|
|
|
|
unmockNotExistingModule,
|
2014-07-21 21:50:04 +04:00
|
|
|
teardown,
|
|
|
|
setup,
|
|
|
|
doAuth,
|
2014-10-19 23:10:13 +04:00
|
|
|
login,
|
|
|
|
togglePermalinks,
|
2013-06-15 02:12:04 +04:00
|
|
|
|
2014-08-09 23:16:54 +04:00
|
|
|
initFixtures,
|
2014-07-21 21:50:04 +04:00
|
|
|
initData,
|
|
|
|
clearData;
|
2013-06-15 02:12:04 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
/** TEST FIXTURES **/
|
|
|
|
fixtures = {
|
2016-06-10 08:12:46 +03:00
|
|
|
insertPosts: function insertPosts(posts) {
|
|
|
|
return Promise.resolve(db.knex('posts').insert(posts));
|
|
|
|
},
|
|
|
|
|
2016-05-19 14:49:22 +03:00
|
|
|
insertPostsAndTags: function insertPostsAndTags() {
|
2016-02-12 14:56:27 +03:00
|
|
|
return Promise.resolve(db.knex('posts').insert(DataGenerator.forKnex.posts)).then(function () {
|
|
|
|
return db.knex('tags').insert(DataGenerator.forKnex.tags);
|
2014-07-23 12:32:27 +04:00
|
|
|
}).then(function () {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('posts_tags').insert(DataGenerator.forKnex.posts_tags);
|
2014-07-23 12:32:27 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
insertMultiAuthorPosts: function insertMultiAuthorPosts(max) {
|
2014-09-03 19:42:55 +04:00
|
|
|
/*jshint unused:false*/
|
2016-02-12 14:56:27 +03:00
|
|
|
var author,
|
2014-07-23 12:32:27 +04:00
|
|
|
authors,
|
|
|
|
i, j, k = postsInserted,
|
|
|
|
posts = [];
|
|
|
|
|
|
|
|
max = max || 50;
|
|
|
|
// insert users of different roles
|
2014-09-03 19:42:55 +04:00
|
|
|
return Promise.resolve(fixtures.createUsersWithRoles()).then(function () {
|
2014-07-23 12:32:27 +04:00
|
|
|
// create the tags
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('tags').insert(DataGenerator.forKnex.tags);
|
2014-09-03 19:42:55 +04:00
|
|
|
}).then(function () {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('users').select('id');
|
2014-07-23 12:32:27 +04:00
|
|
|
}).then(function (results) {
|
2016-06-11 21:23:27 +03:00
|
|
|
authors = _.map(results, 'id');
|
2014-07-23 12:32:27 +04:00
|
|
|
|
|
|
|
// Let's insert posts with random authors
|
|
|
|
for (i = 0; i < max; i += 1) {
|
|
|
|
author = authors[i % authors.length];
|
2014-09-03 19:42:55 +04:00
|
|
|
posts.push(DataGenerator.forKnex.createGenericPost(k, null, null, author));
|
|
|
|
k = k + 1;
|
2014-07-23 12:32:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Keep track so we can run this function again safely
|
|
|
|
postsInserted = k;
|
|
|
|
|
|
|
|
return sequence(_.times(posts.length, function (index) {
|
|
|
|
return function () {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('posts').insert(posts[index]);
|
2014-07-23 12:32:27 +04:00
|
|
|
};
|
|
|
|
}));
|
|
|
|
}).then(function () {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.all([
|
2016-02-12 14:56:27 +03:00
|
|
|
db.knex('posts').orderBy('id', 'asc').select('id'),
|
|
|
|
db.knex('tags').select('id')
|
2014-07-23 12:32:27 +04:00
|
|
|
]);
|
|
|
|
}).then(function (results) {
|
2016-06-11 21:23:27 +03:00
|
|
|
var posts = _.map(results[0], 'id'),
|
|
|
|
tags = _.map(results[1], 'id'),
|
2014-07-23 12:32:27 +04:00
|
|
|
promises = [],
|
|
|
|
i;
|
|
|
|
|
|
|
|
if (max > posts.length) {
|
2014-08-17 10:17:23 +04:00
|
|
|
throw new Error('Trying to add more posts_tags than the number of posts. ' + max + ' ' + posts.length);
|
2014-07-23 12:32:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < max; i += 1) {
|
|
|
|
promises.push(DataGenerator.forKnex.createPostsTags(posts[i], tags[i % tags.length]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return sequence(_.times(promises.length, function (index) {
|
|
|
|
return function () {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('posts_tags').insert(promises[index]);
|
2014-07-23 12:32:27 +04:00
|
|
|
};
|
|
|
|
}));
|
|
|
|
});
|
2014-07-21 21:50:04 +04:00
|
|
|
},
|
2013-12-31 21:09:49 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
insertMorePosts: function insertMorePosts(max) {
|
|
|
|
var lang,
|
|
|
|
status,
|
|
|
|
posts = [],
|
2016-02-12 14:56:27 +03:00
|
|
|
i, j, k = postsInserted;
|
2013-06-15 02:12:04 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
max = max || 50;
|
2013-08-25 00:51:58 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
for (i = 0; i < 2; i += 1) {
|
|
|
|
lang = i % 2 ? 'en' : 'fr';
|
2014-09-03 19:42:55 +04:00
|
|
|
posts.push(DataGenerator.forKnex.createGenericPost(k, null, lang));
|
|
|
|
k = k + 1;
|
2014-02-13 07:26:56 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
for (j = 0; j < max; j += 1) {
|
|
|
|
status = j % 2 ? 'draft' : 'published';
|
2014-09-03 19:42:55 +04:00
|
|
|
posts.push(DataGenerator.forKnex.createGenericPost(k, status, lang));
|
|
|
|
k = k + 1;
|
2014-07-21 21:50:04 +04:00
|
|
|
}
|
2014-02-13 07:26:56 +04:00
|
|
|
}
|
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
// Keep track so we can run this function again safely
|
|
|
|
postsInserted = k;
|
|
|
|
|
|
|
|
return sequence(_.times(posts.length, function (index) {
|
2014-07-17 18:33:21 +04:00
|
|
|
return function () {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('posts').insert(posts[index]);
|
2014-03-16 20:21:02 +04:00
|
|
|
};
|
|
|
|
}));
|
2014-07-21 21:50:04 +04:00
|
|
|
},
|
|
|
|
|
2015-07-29 21:07:58 +03:00
|
|
|
insertMoreTags: function insertMoreTags(max) {
|
|
|
|
max = max || 50;
|
|
|
|
var tags = [],
|
|
|
|
tagName,
|
2016-02-12 14:56:27 +03:00
|
|
|
i;
|
2015-07-29 21:07:58 +03:00
|
|
|
|
|
|
|
for (i = 0; i < max; i += 1) {
|
|
|
|
tagName = uuid.v4().split('-')[0];
|
|
|
|
tags.push(DataGenerator.forKnex.createBasic({name: tagName, slug: tagName}));
|
|
|
|
}
|
|
|
|
|
|
|
|
return sequence(_.times(tags.length, function (index) {
|
|
|
|
return function () {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('tags').insert(tags[index]);
|
2015-07-29 21:07:58 +03:00
|
|
|
};
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
insertMorePostsTags: function insertMorePostsTags(max) {
|
|
|
|
max = max || 50;
|
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.all([
|
2016-02-12 14:56:27 +03:00
|
|
|
db.knex('posts').orderBy('id', 'asc').select('id'),
|
|
|
|
db.knex('tags').select('id', 'name')
|
2014-07-21 21:50:04 +04:00
|
|
|
]).then(function (results) {
|
2016-06-11 21:23:27 +03:00
|
|
|
var posts = _.map(results[0], 'id'),
|
2014-07-21 21:50:04 +04:00
|
|
|
injectionTagId = _.chain(results[1])
|
2016-06-11 21:23:27 +03:00
|
|
|
.filter({name: 'injection'})
|
|
|
|
.map('id')
|
2014-07-21 21:50:04 +04:00
|
|
|
.value()[0],
|
|
|
|
promises = [],
|
|
|
|
i;
|
|
|
|
|
|
|
|
if (max > posts.length) {
|
|
|
|
throw new Error('Trying to add more posts_tags than the number of posts.');
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < max; i += 1) {
|
|
|
|
promises.push(DataGenerator.forKnex.createPostsTags(posts[i], injectionTagId));
|
2014-07-16 01:43:22 +04:00
|
|
|
}
|
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
return sequence(_.times(promises.length, function (index) {
|
|
|
|
return function () {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('posts_tags').insert(promises[index]);
|
2014-07-21 21:50:04 +04:00
|
|
|
};
|
|
|
|
}));
|
2014-02-12 07:40:39 +04:00
|
|
|
});
|
2014-07-21 21:50:04 +04:00
|
|
|
},
|
2016-09-21 17:48:14 +03:00
|
|
|
|
2014-07-30 19:40:30 +04:00
|
|
|
insertRoles: function insertRoles() {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('roles').insert(DataGenerator.forKnex.roles);
|
2014-07-30 19:40:30 +04:00
|
|
|
},
|
2013-12-29 00:13:47 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
initOwnerUser: function initOwnerUser() {
|
2016-02-12 14:56:27 +03:00
|
|
|
var user = DataGenerator.Content.users[0];
|
2014-07-21 21:50:04 +04:00
|
|
|
|
|
|
|
user = DataGenerator.forKnex.createBasic(user);
|
2014-09-10 08:06:24 +04:00
|
|
|
user = _.extend({}, user, {status: 'inactive'});
|
2014-07-21 21:50:04 +04:00
|
|
|
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('roles').insert(DataGenerator.forKnex.roles).then(function () {
|
|
|
|
return db.knex('users').insert(user);
|
2014-07-30 05:25:53 +04:00
|
|
|
}).then(function () {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('roles_users').insert(DataGenerator.forKnex.roles_users[0]);
|
2014-07-30 05:25:53 +04:00
|
|
|
});
|
2014-07-21 21:50:04 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
insertOwnerUser: function insertOwnerUser() {
|
2016-02-12 14:56:27 +03:00
|
|
|
var user;
|
2014-07-21 21:50:04 +04:00
|
|
|
|
|
|
|
user = DataGenerator.forKnex.createUser(DataGenerator.Content.users[0]);
|
2014-02-25 00:28:18 +04:00
|
|
|
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('users').insert(user).then(function () {
|
|
|
|
return db.knex('roles_users').insert(DataGenerator.forKnex.roles_users[0]);
|
2014-08-09 23:16:54 +04:00
|
|
|
});
|
2014-07-21 21:50:04 +04:00
|
|
|
},
|
2014-02-25 00:28:18 +04:00
|
|
|
|
2015-10-24 22:04:02 +03:00
|
|
|
overrideOwnerUser: function overrideOwnerUser(slug) {
|
2016-02-12 14:56:27 +03:00
|
|
|
var user;
|
2014-07-21 21:50:04 +04:00
|
|
|
user = DataGenerator.forKnex.createUser(DataGenerator.Content.users[0]);
|
2016-09-13 19:23:02 +03:00
|
|
|
|
2015-10-24 22:04:02 +03:00
|
|
|
if (slug) {
|
|
|
|
user.slug = slug;
|
|
|
|
}
|
2014-02-25 00:28:18 +04:00
|
|
|
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('users')
|
2014-07-21 21:50:04 +04:00
|
|
|
.where('id', '=', '1')
|
|
|
|
.update(user);
|
|
|
|
},
|
|
|
|
|
|
|
|
createUsersWithRoles: function createUsersWithRoles() {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('roles').insert(DataGenerator.forKnex.roles).then(function () {
|
|
|
|
return db.knex('users').insert(DataGenerator.forKnex.users);
|
2014-07-21 21:50:04 +04:00
|
|
|
}).then(function () {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('roles_users').insert(DataGenerator.forKnex.roles_users);
|
2014-07-21 21:50:04 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-08-19 14:02:07 +03:00
|
|
|
createUsersWithRolesWithoutOwner: function createUsersWithRolesWithoutOwner() {
|
|
|
|
var usersWithoutOwner = DataGenerator.forKnex.users.slice(1);
|
|
|
|
|
|
|
|
return db.knex('roles').insert(DataGenerator.forKnex.roles).then(function () {
|
|
|
|
return db.knex('users').insert(usersWithoutOwner);
|
|
|
|
}).then(function () {
|
|
|
|
return db.knex('roles_users').insert(DataGenerator.forKnex.roles_users);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-07-24 13:46:05 +04:00
|
|
|
createExtraUsers: function createExtraUsers() {
|
2016-02-12 14:56:27 +03:00
|
|
|
// grab 3 more users
|
|
|
|
var extraUsers = DataGenerator.Content.users.slice(2, 5);
|
2014-07-24 13:46:05 +04:00
|
|
|
|
|
|
|
extraUsers = _.map(extraUsers, function (user) {
|
|
|
|
return DataGenerator.forKnex.createUser(_.extend({}, user, {
|
|
|
|
email: 'a' + user.email,
|
|
|
|
slug: 'a' + user.slug
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('users').insert(extraUsers).then(function () {
|
|
|
|
return db.knex('roles_users').insert([
|
2014-09-10 08:06:24 +04:00
|
|
|
{user_id: 5, role_id: 1},
|
|
|
|
{user_id: 6, role_id: 2},
|
|
|
|
{user_id: 7, role_id: 3}
|
2014-07-24 13:46:05 +04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-08-23 21:59:36 +04:00
|
|
|
// Creates a client, and access and refresh tokens for user 3 (author)
|
|
|
|
createTokensForUser: function createTokensForUser() {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('clients').insert(DataGenerator.forKnex.clients).then(function () {
|
|
|
|
return db.knex('accesstokens').insert(DataGenerator.forKnex.createToken({user_id: 3}));
|
2014-08-23 21:59:36 +04:00
|
|
|
}).then(function () {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('refreshtokens').insert(DataGenerator.forKnex.createToken({user_id: 3}));
|
2014-08-23 21:59:36 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-08-05 22:11:17 +04:00
|
|
|
createInvitedUsers: function createInvitedUser() {
|
2016-02-12 14:56:27 +03:00
|
|
|
// grab 3 more users
|
|
|
|
var extraUsers = DataGenerator.Content.users.slice(2, 5);
|
2014-08-05 22:11:17 +04:00
|
|
|
|
|
|
|
extraUsers = _.map(extraUsers, function (user) {
|
|
|
|
return DataGenerator.forKnex.createUser(_.extend({}, user, {
|
|
|
|
email: 'inv' + user.email,
|
|
|
|
slug: 'inv' + user.slug,
|
|
|
|
status: 'invited-pending'
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('users').insert(extraUsers).then(function () {
|
|
|
|
return db.knex('roles_users').insert([
|
2014-09-10 08:06:24 +04:00
|
|
|
{user_id: 8, role_id: 1},
|
|
|
|
{user_id: 9, role_id: 2},
|
|
|
|
{user_id: 10, role_id: 3}
|
2014-08-05 22:11:17 +04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-06-10 08:12:46 +03:00
|
|
|
insertOne: function insertOne(obj, fn, index) {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex(obj)
|
2016-06-10 08:12:46 +03:00
|
|
|
.insert(DataGenerator.forKnex[fn](DataGenerator.Content[obj][index || 0]));
|
2014-07-21 21:50:04 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
insertApps: function insertApps() {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('apps').insert(DataGenerator.forKnex.apps).then(function () {
|
|
|
|
return db.knex('app_fields').insert(DataGenerator.forKnex.app_fields);
|
2014-07-21 21:50:04 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-12-14 14:31:00 +03:00
|
|
|
getImportFixturePath: function (filename) {
|
|
|
|
return path.resolve(__dirname + '/fixtures/import/' + filename);
|
|
|
|
},
|
|
|
|
|
2014-12-11 18:50:10 +03:00
|
|
|
getExportFixturePath: function (filename) {
|
2014-12-14 14:31:00 +03:00
|
|
|
return path.resolve(__dirname + '/fixtures/export/' + filename + '.json');
|
2014-12-11 18:50:10 +03:00
|
|
|
},
|
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
loadExportFixture: function loadExportFixture(filename) {
|
2014-12-11 18:50:10 +03:00
|
|
|
var filePath = this.getExportFixturePath(filename),
|
2014-08-17 10:17:23 +04:00
|
|
|
readFile = Promise.promisify(fs.readFile);
|
2014-07-21 21:50:04 +04:00
|
|
|
|
2014-12-11 18:50:10 +03:00
|
|
|
return readFile(filePath).then(function (fileContents) {
|
2014-07-21 21:50:04 +04:00
|
|
|
var data;
|
|
|
|
|
|
|
|
// Parse the json data
|
|
|
|
try {
|
|
|
|
data = JSON.parse(fileContents);
|
|
|
|
} catch (e) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return new Error('Failed to parse the file');
|
2014-02-25 00:28:18 +04:00
|
|
|
}
|
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
return data;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
permissionsFor: function permissionsFor(obj) {
|
2016-04-08 12:09:26 +03:00
|
|
|
var permsToInsert = fixtureUtils.findModelFixtures('Permission', {object_type: obj}).entries,
|
2016-03-29 16:14:44 +03:00
|
|
|
permsRolesToInsert = fixtureUtils.findPermissionRelationsForObject(obj).entries,
|
2014-07-21 21:50:04 +04:00
|
|
|
actions = [],
|
2014-09-10 08:06:24 +04:00
|
|
|
permissionsRoles = [],
|
2014-07-21 21:50:04 +04:00
|
|
|
roles = {
|
2014-09-10 08:06:24 +04:00
|
|
|
Administrator: 1,
|
|
|
|
Editor: 2,
|
|
|
|
Author: 3,
|
|
|
|
Owner: 4
|
2014-07-21 21:50:04 +04:00
|
|
|
};
|
|
|
|
|
2016-05-25 10:34:46 +03:00
|
|
|
// CASE: if empty db will throw SQLITE_MISUSE, hard to debug
|
|
|
|
if (_.isEmpty(permsToInsert)) {
|
|
|
|
return Promise.reject(new Error('no permission found:' + obj));
|
|
|
|
}
|
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
permsToInsert = _.map(permsToInsert, function (perms) {
|
|
|
|
actions.push(perms.action_type);
|
|
|
|
return DataGenerator.forKnex.createBasic(perms);
|
2014-02-25 00:28:18 +04:00
|
|
|
});
|
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
_.each(permsRolesToInsert, function (perms, role) {
|
|
|
|
if (perms[obj]) {
|
|
|
|
if (perms[obj] === 'all') {
|
|
|
|
_.each(actions, function (action, i) {
|
2014-09-10 08:06:24 +04:00
|
|
|
permissionsRoles.push({permission_id: (i + 1), role_id: roles[role]});
|
2014-07-21 21:50:04 +04:00
|
|
|
});
|
2014-09-10 08:06:24 +04:00
|
|
|
} else {
|
2014-07-21 21:50:04 +04:00
|
|
|
_.each(perms[obj], function (action) {
|
2014-09-10 08:06:24 +04:00
|
|
|
permissionsRoles.push({permission_id: (_.indexOf(actions, action) + 1), role_id: roles[role]});
|
2014-07-21 21:50:04 +04:00
|
|
|
});
|
|
|
|
}
|
2014-02-25 00:28:18 +04:00
|
|
|
}
|
2014-07-21 21:50:04 +04:00
|
|
|
});
|
2014-02-25 00:28:18 +04:00
|
|
|
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('permissions').insert(permsToInsert).then(function () {
|
2016-05-25 10:34:46 +03:00
|
|
|
if (_.isEmpty(permissionsRoles)) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('permissions_roles').insert(permissionsRoles);
|
2014-02-25 00:28:18 +04:00
|
|
|
});
|
2015-08-22 00:46:42 +03:00
|
|
|
},
|
2016-05-25 10:34:46 +03:00
|
|
|
|
2015-08-22 00:46:42 +03:00
|
|
|
insertClients: function insertClients() {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('clients').insert(DataGenerator.forKnex.clients);
|
2015-08-22 00:46:42 +03:00
|
|
|
},
|
2016-05-25 10:34:46 +03:00
|
|
|
|
2015-08-22 00:46:42 +03:00
|
|
|
insertAccessToken: function insertAccessToken(override) {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex('accesstokens').insert(DataGenerator.forKnex.createToken(override));
|
2016-09-21 17:48:14 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
insertInvites: function insertInvites() {
|
|
|
|
return db.knex('invites').insert(DataGenerator.forKnex.invites)
|
|
|
|
.then(function () {
|
|
|
|
return db.knex('invites_roles').insert(DataGenerator.forKnex.invites_roles);
|
|
|
|
});
|
2014-07-21 21:50:04 +04:00
|
|
|
}
|
|
|
|
};
|
2014-02-25 00:28:18 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
/** Test Utility Functions **/
|
|
|
|
initData = function initData() {
|
2016-07-15 19:22:41 +03:00
|
|
|
return migration.populate();
|
2014-07-21 21:50:04 +04:00
|
|
|
};
|
2014-02-25 00:28:18 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
clearData = function clearData() {
|
|
|
|
// we must always try to delete all tables
|
|
|
|
return migration.reset();
|
|
|
|
};
|
2013-12-29 00:13:47 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
toDoList = {
|
2014-09-10 08:06:24 +04:00
|
|
|
app: function insertApp() { return fixtures.insertOne('apps', 'createApp'); },
|
|
|
|
app_field: function insertAppField() {
|
2014-07-21 21:50:04 +04:00
|
|
|
// TODO: use the actual app ID to create the field
|
|
|
|
return fixtures.insertOne('apps', 'createApp').then(function () {
|
|
|
|
return fixtures.insertOne('app_fields', 'createAppField');
|
|
|
|
});
|
|
|
|
},
|
2014-09-10 08:06:24 +04:00
|
|
|
app_setting: function insertAppSetting() {
|
2014-07-21 21:50:04 +04:00
|
|
|
// TODO: use the actual app ID to create the field
|
|
|
|
return fixtures.insertOne('apps', 'createApp').then(function () {
|
|
|
|
return fixtures.insertOne('app_settings', 'createAppSetting');
|
|
|
|
});
|
|
|
|
},
|
2014-09-10 08:06:24 +04:00
|
|
|
permission: function insertPermission() { return fixtures.insertOne('permissions', 'createPermission'); },
|
|
|
|
role: function insertRole() { return fixtures.insertOne('roles', 'createRole'); },
|
|
|
|
roles: function insertRoles() { return fixtures.insertRoles(); },
|
|
|
|
tag: function insertTag() { return fixtures.insertOne('tags', 'createTag'); },
|
2016-04-14 23:44:05 +03:00
|
|
|
subscriber: function insertSubscriber() { return fixtures.insertOne('subscribers', 'createSubscriber'); },
|
2016-05-19 14:49:22 +03:00
|
|
|
posts: function insertPostsAndTags() { return fixtures.insertPostsAndTags(); },
|
2014-07-23 12:32:27 +04:00
|
|
|
'posts:mu': function insertMultiAuthorPosts() { return fixtures.insertMultiAuthorPosts(); },
|
2015-07-29 21:07:58 +03:00
|
|
|
tags: function insertMoreTags() { return fixtures.insertMoreTags(); },
|
2014-09-10 08:06:24 +04:00
|
|
|
apps: function insertApps() { return fixtures.insertApps(); },
|
|
|
|
settings: function populateSettings() {
|
2016-07-15 19:22:41 +03:00
|
|
|
return models.Settings.populateDefaults().then(function () { return SettingsAPI.updateSettingsCache(); });
|
2014-07-21 21:50:04 +04:00
|
|
|
},
|
|
|
|
'users:roles': function createUsersWithRoles() { return fixtures.createUsersWithRoles(); },
|
2016-08-19 14:02:07 +03:00
|
|
|
'users:roles:no-owner': function createUsersWithRoles() { return fixtures.createUsersWithRolesWithoutOwner(); },
|
2014-09-10 08:06:24 +04:00
|
|
|
users: function createExtraUsers() { return fixtures.createExtraUsers(); },
|
2014-08-23 21:59:36 +04:00
|
|
|
'user:token': function createTokensForUser() { return fixtures.createTokensForUser(); },
|
2014-09-10 08:06:24 +04:00
|
|
|
owner: function insertOwnerUser() { return fixtures.insertOwnerUser(); },
|
2014-07-21 21:50:04 +04:00
|
|
|
'owner:pre': function initOwnerUser() { return fixtures.initOwnerUser(); },
|
|
|
|
'owner:post': function overrideOwnerUser() { return fixtures.overrideOwnerUser(); },
|
|
|
|
'perms:init': function initPermissions() { return permissions.init(); },
|
2014-09-10 08:06:24 +04:00
|
|
|
perms: function permissionsFor(obj) {
|
2014-07-21 21:50:04 +04:00
|
|
|
return function permissionsForObj() { return fixtures.permissionsFor(obj); };
|
2015-08-22 00:46:42 +03:00
|
|
|
},
|
2015-07-28 03:27:54 +03:00
|
|
|
clients: function insertClients() { return fixtures.insertClients(); },
|
2016-09-21 17:48:14 +03:00
|
|
|
filter: function createFilterParamFixtures() { return filterData(DataGenerator); },
|
|
|
|
invites: function insertInvites() { return fixtures.insertInvites(); }
|
2014-07-21 21:50:04 +04:00
|
|
|
};
|
2013-12-29 00:13:47 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
/**
|
|
|
|
* ## getFixtureOps
|
|
|
|
*
|
|
|
|
* Takes the arguments from a setup function and turns them into an array of promises to fullfil
|
|
|
|
*
|
|
|
|
* This is effectively a list of instructions with regard to which fixtures should be setup for this test.
|
|
|
|
* * `default` - a special option which will cause the full suite of normal fixtures to be initialised
|
|
|
|
* * `perms:init` - initialise the permissions object after having added permissions
|
|
|
|
* * `perms:obj` - initialise permissions for a particular object type
|
|
|
|
* * `users:roles` - create a full suite of users, one per role
|
2014-09-10 08:06:24 +04:00
|
|
|
* @param {Object} toDos
|
2014-07-21 21:50:04 +04:00
|
|
|
*/
|
|
|
|
getFixtureOps = function getFixtureOps(toDos) {
|
|
|
|
// default = default fixtures, if it isn't present, init with tables only
|
|
|
|
var tablesOnly = !toDos.default,
|
|
|
|
fixtureOps = [];
|
|
|
|
|
|
|
|
// Database initialisation
|
|
|
|
if (toDos.init || toDos.default) {
|
|
|
|
fixtureOps.push(function initDB() {
|
2016-09-30 16:05:17 +03:00
|
|
|
return new Promise(function initDb(resolve, reject) {
|
|
|
|
migration.populate({tablesOnly: tablesOnly})
|
|
|
|
.then(function () {
|
|
|
|
resolve();
|
|
|
|
})
|
|
|
|
.catch(reject);
|
|
|
|
});
|
2014-07-21 21:50:04 +04:00
|
|
|
});
|
2016-05-19 14:49:22 +03:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
delete toDos.default;
|
|
|
|
delete toDos.init;
|
|
|
|
}
|
2013-12-29 00:13:47 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
// Go through our list of things to do, and add them to an array
|
|
|
|
_.each(toDos, function (value, toDo) {
|
|
|
|
var tmp;
|
2016-05-25 10:34:46 +03:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
if (toDo !== 'perms:init' && toDo.indexOf('perms:') !== -1) {
|
|
|
|
tmp = toDo.split(':');
|
|
|
|
fixtureOps.push(toDoList[tmp[0]](tmp[1]));
|
|
|
|
} else {
|
2016-05-25 10:34:46 +03:00
|
|
|
if (!toDoList[toDo]) {
|
|
|
|
throw new Error('setup todo does not exist - spell mistake?');
|
|
|
|
}
|
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
fixtureOps.push(toDoList[toDo]);
|
2013-12-29 00:13:47 +04:00
|
|
|
}
|
2014-07-21 21:50:04 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
return fixtureOps;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ## Test Setup and Teardown
|
|
|
|
|
2014-08-09 23:16:54 +04:00
|
|
|
initFixtures = function initFixtures() {
|
2014-09-10 08:06:24 +04:00
|
|
|
var options = _.merge({init: true}, _.transform(arguments, function (result, val) {
|
|
|
|
result[val] = true;
|
|
|
|
})),
|
2014-08-09 23:16:54 +04:00
|
|
|
fixtureOps = getFixtureOps(options);
|
|
|
|
|
|
|
|
return sequence(fixtureOps);
|
|
|
|
};
|
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
/**
|
|
|
|
* ## Setup Integration Tests
|
|
|
|
* Setup takes a list of arguments like: 'default', 'tag', 'perms:tag', 'perms:init'
|
|
|
|
* Setup does 'init' (DB) by default
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
setup = function setup() {
|
2014-08-09 23:16:54 +04:00
|
|
|
var self = this,
|
|
|
|
args = arguments;
|
2014-07-21 21:50:04 +04:00
|
|
|
|
2016-05-25 10:34:46 +03:00
|
|
|
return function setup(done) {
|
2016-07-15 19:22:41 +03:00
|
|
|
models.init();
|
2016-03-02 07:42:01 +03:00
|
|
|
|
2016-05-25 10:34:46 +03:00
|
|
|
if (done) {
|
2016-05-19 14:49:22 +03:00
|
|
|
initFixtures.apply(self, args).then(function () {
|
2016-05-25 10:34:46 +03:00
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
} else {
|
|
|
|
return initFixtures.apply(self, args);
|
|
|
|
}
|
2014-07-21 21:50:04 +04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ## DoAuth For Route Tests
|
|
|
|
*
|
2014-08-09 23:16:54 +04:00
|
|
|
* This function manages the work of ensuring we have an overridden owner user, and grabbing an access token
|
2014-07-21 21:50:04 +04:00
|
|
|
* @returns {deferred.promise<AccessToken>}
|
|
|
|
*/
|
|
|
|
// TODO make this do the DB init as well
|
|
|
|
doAuth = function doAuth() {
|
|
|
|
var options = arguments,
|
|
|
|
request = arguments[0],
|
|
|
|
fixtureOps;
|
|
|
|
|
|
|
|
// Remove request from this list
|
|
|
|
delete options[0];
|
2016-08-19 09:32:01 +03:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
// No DB setup, but override the owner
|
|
|
|
options = _.merge({'owner:post': true}, _.transform(options, function (result, val) {
|
2015-07-18 03:26:15 +03:00
|
|
|
if (val) {
|
|
|
|
result[val] = true;
|
|
|
|
}
|
2014-09-10 08:06:24 +04:00
|
|
|
}));
|
2014-07-21 21:50:04 +04:00
|
|
|
|
|
|
|
fixtureOps = getFixtureOps(options);
|
|
|
|
|
2014-10-19 23:10:13 +04:00
|
|
|
return sequence(fixtureOps).then(function () {
|
|
|
|
return login(request);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
login = function login(request) {
|
2016-08-19 09:32:01 +03:00
|
|
|
var user = DataGenerator.forModel.users[request.userIndex || 0];
|
2014-10-19 23:10:13 +04:00
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
request.post('/ghost/api/v0.1/authentication/token/')
|
2016-09-13 18:41:14 +03:00
|
|
|
.set('Origin', config.get('url'))
|
2015-10-22 16:28:47 +03:00
|
|
|
.send({
|
|
|
|
grant_type: 'password',
|
|
|
|
username: user.email,
|
|
|
|
password: user.password,
|
|
|
|
client_id: 'ghost-admin',
|
|
|
|
client_secret: 'not_available'
|
2016-02-07 22:10:27 +03:00
|
|
|
}).then(function then(res) {
|
2014-10-19 23:10:13 +04:00
|
|
|
resolve(res.body.access_token);
|
2016-02-07 22:10:27 +03:00
|
|
|
}, reject);
|
2014-10-19 23:10:13 +04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
togglePermalinks = function togglePermalinks(request, toggle) {
|
|
|
|
var permalinkString = toggle === 'date' ? '/:year/:month/:day/:slug/' : '/:slug/';
|
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
return new Promise(function (resolve, reject) {
|
2014-10-19 23:10:13 +04:00
|
|
|
doAuth(request).then(function (token) {
|
|
|
|
request.put('/ghost/api/v0.1/settings/')
|
|
|
|
.set('Authorization', 'Bearer ' + token)
|
|
|
|
.send({settings: [
|
|
|
|
{
|
|
|
|
uuid: '75e994ae-490e-45e6-9207-0eab409c1c04',
|
|
|
|
key: 'permalinks',
|
|
|
|
value: permalinkString,
|
|
|
|
type: 'blog',
|
|
|
|
created_at: '2014-10-16T17:39:16.005Z',
|
|
|
|
created_by: 1,
|
|
|
|
updated_at: '2014-10-20T19:44:18.077Z',
|
|
|
|
updated_by: 1
|
|
|
|
}
|
|
|
|
]})
|
2014-08-17 10:17:23 +04:00
|
|
|
.end(function (err, res) {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
2016-09-13 19:23:02 +03:00
|
|
|
if (res.statusCode !== 200) {
|
|
|
|
return reject(res.body);
|
|
|
|
}
|
|
|
|
|
2014-10-19 23:10:13 +04:00
|
|
|
resolve(res.body);
|
2014-08-17 10:17:23 +04:00
|
|
|
});
|
|
|
|
});
|
2013-12-29 00:13:47 +04:00
|
|
|
});
|
2014-07-21 21:50:04 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
teardown = function teardown(done) {
|
2016-05-25 10:34:46 +03:00
|
|
|
if (done) {
|
|
|
|
migration.reset().then(function () {
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
} else {
|
|
|
|
return migration.reset();
|
|
|
|
}
|
2014-07-21 13:29:03 +04:00
|
|
|
};
|
|
|
|
|
2016-05-19 14:49:22 +03:00
|
|
|
/**
|
|
|
|
* offer helper functions for mocking
|
|
|
|
* we start with a small function set to mock non existent modules
|
|
|
|
*/
|
|
|
|
originalRequireFn = Module.prototype.require;
|
|
|
|
mockNotExistingModule = function mockNotExistingModule(modulePath, module) {
|
|
|
|
Module.prototype.require = function (path) {
|
|
|
|
if (path.match(modulePath)) {
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
|
|
|
return originalRequireFn.apply(this, arguments);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
unmockNotExistingModule = function unmockNotExistingModule() {
|
|
|
|
Module.prototype.require = originalRequireFn;
|
|
|
|
};
|
|
|
|
|
2013-10-08 05:39:33 +04:00
|
|
|
module.exports = {
|
2014-07-21 13:29:03 +04:00
|
|
|
teardown: teardown,
|
2014-07-21 21:50:04 +04:00
|
|
|
setup: setup,
|
|
|
|
doAuth: doAuth,
|
2014-10-19 23:10:13 +04:00
|
|
|
login: login,
|
|
|
|
togglePermalinks: togglePermalinks,
|
2014-07-21 13:29:03 +04:00
|
|
|
|
2016-05-19 14:49:22 +03:00
|
|
|
mockNotExistingModule: mockNotExistingModule,
|
|
|
|
unmockNotExistingModule: unmockNotExistingModule,
|
|
|
|
|
2014-08-09 23:16:54 +04:00
|
|
|
initFixtures: initFixtures,
|
2013-10-08 05:39:33 +04:00
|
|
|
initData: initData,
|
|
|
|
clearData: clearData,
|
2014-07-21 21:50:04 +04:00
|
|
|
|
2016-06-10 08:20:13 +03:00
|
|
|
mocks: mocks,
|
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
fixtures: fixtures,
|
2013-12-29 00:13:47 +04:00
|
|
|
|
2013-10-08 05:39:33 +04:00
|
|
|
DataGenerator: DataGenerator,
|
2014-02-22 05:25:31 +04:00
|
|
|
API: API,
|
2014-05-16 06:29:42 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
fork: fork,
|
|
|
|
|
2014-07-24 13:46:05 +04:00
|
|
|
// Helpers to make it easier to write tests which are easy to read
|
2014-07-21 21:50:04 +04:00
|
|
|
context: {
|
|
|
|
internal: {context: {internal: true}},
|
2016-04-14 23:44:05 +03:00
|
|
|
external: {context: {external: true}},
|
2014-07-21 21:50:04 +04:00
|
|
|
owner: {context: {user: 1}},
|
|
|
|
admin: {context: {user: 2}},
|
|
|
|
editor: {context: {user: 3}},
|
|
|
|
author: {context: {user: 4}}
|
2014-07-24 13:46:05 +04:00
|
|
|
},
|
|
|
|
users: {
|
|
|
|
ids: {
|
|
|
|
owner: 1,
|
|
|
|
admin: 2,
|
|
|
|
editor: 3,
|
|
|
|
author: 4,
|
|
|
|
admin2: 5,
|
|
|
|
editor2: 6,
|
|
|
|
author2: 7
|
|
|
|
}
|
|
|
|
},
|
|
|
|
roles: {
|
|
|
|
ids: {
|
|
|
|
owner: 4,
|
|
|
|
admin: 1,
|
|
|
|
editor: 2,
|
|
|
|
author: 3
|
|
|
|
}
|
2014-07-28 17:19:49 +04:00
|
|
|
},
|
2014-09-25 13:35:28 +04:00
|
|
|
|
|
|
|
cacheRules: {
|
|
|
|
public: 'public, max-age=0',
|
|
|
|
hour: 'public, max-age=' + 3600,
|
|
|
|
day: 'public, max-age=' + 86400,
|
|
|
|
year: 'public, max-age=' + 31536000,
|
|
|
|
private: 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
|
|
|
|
}
|
2013-10-08 05:39:33 +04:00
|
|
|
};
|