2016-10-12 18:18:57 +03:00
|
|
|
// # Fixture Utils
|
|
|
|
// Standalone file which can be required to help with advanced operations on the fixtures.json file
|
2020-04-29 18:44:27 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
const Promise = require('bluebird');
|
2021-06-15 17:36:27 +03:00
|
|
|
const logging = require('@tryghost/logging');
|
2020-04-29 18:44:27 +03:00
|
|
|
const models = require('../../../models');
|
|
|
|
const baseUtils = require('../../../models/base/utils');
|
2020-08-11 20:44:21 +03:00
|
|
|
const {sequence} = require('@tryghost/promise');
|
2020-04-29 18:44:27 +03:00
|
|
|
const moment = require('moment');
|
|
|
|
const fixtures = require('./fixtures');
|
|
|
|
|
2016-10-12 18:18:57 +03:00
|
|
|
/**
|
|
|
|
* ### Match Func
|
|
|
|
* Figures out how to match across various combinations of keys and values.
|
|
|
|
* Match can be a string or an array containing 2 strings
|
|
|
|
* Key and Value are the values to be found
|
|
|
|
* Value can also be an array, in which case we look for a match in the array.
|
|
|
|
* @api private
|
|
|
|
* @param {String|Array} match
|
|
|
|
* @param {String|Integer} key
|
|
|
|
* @param {String|Array} [value]
|
|
|
|
* @returns {Function} matching function
|
|
|
|
*/
|
2020-10-20 01:56:46 +03:00
|
|
|
const matchFunc = function matchFunc(match, key, value) {
|
2016-10-12 18:18:57 +03:00
|
|
|
if (_.isArray(match)) {
|
|
|
|
return function (item) {
|
2020-04-29 18:44:27 +03:00
|
|
|
let valueTest = true;
|
2016-10-12 18:18:57 +03:00
|
|
|
|
|
|
|
if (_.isArray(value)) {
|
|
|
|
valueTest = value.indexOf(item.get(match[1])) > -1;
|
|
|
|
} else if (value !== 'all') {
|
|
|
|
valueTest = item.get(match[1]) === value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item.get(match[0]) === key && valueTest;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return function (item) {
|
|
|
|
key = key === 0 && value ? value : key;
|
|
|
|
return item.get(match) === key;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-10-20 01:56:46 +03:00
|
|
|
const matchObj = function matchObj(match, item) {
|
|
|
|
const matchedObj = {};
|
2016-10-12 18:18:57 +03:00
|
|
|
|
|
|
|
if (_.isArray(match)) {
|
|
|
|
_.each(match, function (matchProp) {
|
2020-10-20 01:56:46 +03:00
|
|
|
matchedObj[matchProp] = item.get(matchProp);
|
2016-10-12 18:18:57 +03:00
|
|
|
});
|
|
|
|
} else {
|
2020-10-20 01:56:46 +03:00
|
|
|
matchedObj[match] = item.get(match);
|
2016-10-12 18:18:57 +03:00
|
|
|
}
|
|
|
|
|
2020-10-20 01:56:46 +03:00
|
|
|
return matchedObj;
|
2016-10-12 18:18:57 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Fetch Relation Data
|
|
|
|
* Before we build relations we need to fetch all of the models from both sides so that we can
|
|
|
|
* use filter and find to quickly locate the correct models.
|
|
|
|
* @api private
|
|
|
|
* @param {{from, to, entries}} relation
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
2020-10-20 01:56:46 +03:00
|
|
|
const fetchRelationData = function fetchRelationData(relation, options) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const fromOptions = _.extend({}, options, {withRelated: [relation.from.relation]});
|
|
|
|
|
|
|
|
const props = {
|
|
|
|
from: models[relation.from.model].findAll(fromOptions),
|
|
|
|
to: models[relation.to.model].findAll(options)
|
|
|
|
};
|
2016-10-12 18:18:57 +03:00
|
|
|
|
|
|
|
return Promise.props(props);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Add Fixtures for Model
|
|
|
|
* Takes a model fixture, with a name and some entries and processes these
|
|
|
|
* into a sequence of promises to get each fixture added.
|
|
|
|
*
|
|
|
|
* @param {{name, entries}} modelFixture
|
|
|
|
* @returns {Promise.<*>}
|
|
|
|
*/
|
2020-10-20 01:56:46 +03:00
|
|
|
const addFixturesForModel = function addFixturesForModel(modelFixture, options = {}) {
|
2017-09-19 13:54:01 +03:00
|
|
|
// Clone the fixtures as they get changed in this function.
|
|
|
|
// The initial blog posts will be added a `published_at` property, which
|
|
|
|
// would change the fixturesHash.
|
|
|
|
modelFixture = _.cloneDeep(modelFixture);
|
|
|
|
// The Post model fixtures need a `published_at` date, where at least the seconds
|
|
|
|
// are different, otherwise `prev_post` and `next_post` helpers won't workd with
|
|
|
|
// them.
|
|
|
|
if (modelFixture.name === 'Post') {
|
|
|
|
_.forEach(modelFixture.entries, function (post, index) {
|
|
|
|
if (!post.published_at) {
|
|
|
|
post.published_at = moment().add(index, 'seconds');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-12 18:18:57 +03:00
|
|
|
return Promise.mapSeries(modelFixture.entries, function (entry) {
|
2018-07-24 15:37:17 +03:00
|
|
|
let data = {};
|
|
|
|
|
2017-01-26 00:08:09 +03:00
|
|
|
// CASE: if id is specified, only query by id
|
2018-07-24 15:37:17 +03:00
|
|
|
if (entry.id) {
|
|
|
|
data.id = entry.id;
|
|
|
|
} else if (entry.slug) {
|
|
|
|
data.slug = entry.slug;
|
|
|
|
} else {
|
|
|
|
data = _.cloneDeep(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (modelFixture.name === 'Post') {
|
|
|
|
data.status = 'all';
|
|
|
|
}
|
|
|
|
|
|
|
|
return models[modelFixture.name].findOne(data, options).then(function (found) {
|
2016-10-12 18:18:57 +03:00
|
|
|
if (!found) {
|
|
|
|
return models[modelFixture.name].add(entry, options);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).then(function (results) {
|
|
|
|
return {expected: modelFixture.entries.length, done: _.compact(results).length};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ## Add Fixtures for Relation
|
|
|
|
* Takes a relation fixtures object, with a from, to and some entries and processes these
|
|
|
|
* into a sequence of promises, to get each fixture added.
|
|
|
|
*
|
|
|
|
* @param {{from, to, entries}} relationFixture
|
|
|
|
* @returns {Promise.<*>}
|
|
|
|
*/
|
2020-10-20 01:56:46 +03:00
|
|
|
const addFixturesForRelation = function addFixturesForRelation(relationFixture, options) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const ops = [];
|
|
|
|
let max = 0;
|
2016-10-12 18:18:57 +03:00
|
|
|
|
|
|
|
return fetchRelationData(relationFixture, options).then(function getRelationOps(data) {
|
|
|
|
_.each(relationFixture.entries, function processEntries(entry, key) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const fromItem = data.from.find(matchFunc(relationFixture.from.match, key));
|
2016-10-12 18:18:57 +03:00
|
|
|
|
2018-02-07 14:31:21 +03:00
|
|
|
// CASE: You add new fixtures e.g. a new role in a new release.
|
|
|
|
// As soon as an **older** migration script wants to add permissions for any resource, it iterates over the
|
|
|
|
// permissions for each role. But if the role does not exist yet, it won't find the matching db entry and breaks.
|
|
|
|
if (!fromItem) {
|
2020-05-22 21:22:20 +03:00
|
|
|
logging.warn('Skip: Target database entry not found for key: ' + key);
|
2018-02-07 14:31:21 +03:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2020-10-20 01:56:46 +03:00
|
|
|
_.each(entry, function processEntryValues(value, entryKey) {
|
|
|
|
let toItems = data.to.filter(matchFunc(relationFixture.to.match, entryKey, value));
|
2016-10-12 18:18:57 +03:00
|
|
|
max += toItems.length;
|
|
|
|
|
|
|
|
// Remove any duplicates that already exist in the collection
|
|
|
|
toItems = _.reject(toItems, function (item) {
|
|
|
|
return fromItem
|
|
|
|
.related(relationFixture.from.relation)
|
2020-08-03 21:43:53 +03:00
|
|
|
.find((model) => {
|
|
|
|
const objectToMatch = matchObj(relationFixture.to.match, item);
|
|
|
|
return Object.keys(objectToMatch).every(function (keyToCheck) {
|
|
|
|
return model.get(keyToCheck) === objectToMatch[keyToCheck];
|
|
|
|
});
|
|
|
|
});
|
2016-10-12 18:18:57 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (toItems && toItems.length > 0) {
|
|
|
|
ops.push(function addRelationItems() {
|
✨ 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
|
|
|
return baseUtils.attach(
|
|
|
|
models[relationFixture.from.Model || relationFixture.from.model],
|
|
|
|
fromItem.id,
|
|
|
|
relationFixture.from.relation,
|
|
|
|
toItems,
|
|
|
|
options
|
|
|
|
);
|
2016-10-12 18:18:57 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return sequence(ops);
|
|
|
|
}).then(function (result) {
|
|
|
|
return {expected: max, done: _(result).map('length').sum()};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Find Model Fixture
|
|
|
|
* Finds a model fixture based on model name
|
|
|
|
* @api private
|
|
|
|
* @param {String} modelName
|
|
|
|
* @returns {Object} model fixture
|
|
|
|
*/
|
2020-10-20 01:56:46 +03:00
|
|
|
const findModelFixture = function findModelFixture(modelName) {
|
2016-10-12 18:18:57 +03:00
|
|
|
return _.find(fixtures.models, function (modelFixture) {
|
|
|
|
return modelFixture.name === modelName;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Find Model Fixture Entry
|
|
|
|
* Find a single model fixture entry by model name & a matching expression for the FIND function
|
|
|
|
* @param {String} modelName
|
|
|
|
* @param {String|Object|Function} matchExpr
|
|
|
|
* @returns {Object} model fixture entry
|
|
|
|
*/
|
2020-10-20 01:56:46 +03:00
|
|
|
const findModelFixtureEntry = function findModelFixtureEntry(modelName, matchExpr) {
|
2016-10-12 18:18:57 +03:00
|
|
|
return _.find(findModelFixture(modelName).entries, matchExpr);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Find Model Fixtures
|
|
|
|
* Find a model fixture name & a matching expression for the FILTER function
|
|
|
|
* @param {String} modelName
|
|
|
|
* @param {String|Object|Function} matchExpr
|
|
|
|
* @returns {Object} model fixture
|
|
|
|
*/
|
2020-10-20 01:56:46 +03:00
|
|
|
const findModelFixtures = function findModelFixtures(modelName, matchExpr) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const foundModel = _.cloneDeep(findModelFixture(modelName));
|
2016-10-12 18:18:57 +03:00
|
|
|
foundModel.entries = _.filter(foundModel.entries, matchExpr);
|
|
|
|
return foundModel;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Find Relation Fixture
|
|
|
|
* Find a relation fixture by from & to models
|
|
|
|
* @api private
|
|
|
|
* @param {String} from
|
|
|
|
* @param {String} to
|
|
|
|
* @returns {Object} relation fixture
|
|
|
|
*/
|
2020-10-20 01:56:46 +03:00
|
|
|
const findRelationFixture = function findRelationFixture(from, to) {
|
2016-10-12 18:18:57 +03:00
|
|
|
return _.find(fixtures.relations, function (relation) {
|
|
|
|
return relation.from.model === from && relation.to.model === to;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Find Permission Relations For Object
|
|
|
|
* Specialist function can return the permission relation fixture with only entries for a particular object.model
|
|
|
|
* @param {String} objName
|
|
|
|
* @returns {Object} fixture relation
|
|
|
|
*/
|
2020-10-20 01:56:46 +03:00
|
|
|
const findPermissionRelationsForObject = function findPermissionRelationsForObject(objName, role) {
|
2016-10-12 18:18:57 +03:00
|
|
|
// Make a copy and delete any entries we don't want
|
2020-04-29 18:44:27 +03:00
|
|
|
const foundRelation = _.cloneDeep(findRelationFixture('Role', 'Permission'));
|
2016-10-12 18:18:57 +03:00
|
|
|
|
2019-04-17 20:18:44 +03:00
|
|
|
_.each(foundRelation.entries, function (entry, key) {
|
2016-10-12 18:18:57 +03:00
|
|
|
_.each(entry, function (perm, obj) {
|
|
|
|
if (obj !== objName) {
|
|
|
|
delete entry[obj];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-04-17 20:18:44 +03:00
|
|
|
if (_.isEmpty(entry) || (role && role !== key)) {
|
|
|
|
delete foundRelation.entries[key];
|
2016-10-12 18:18:57 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return foundRelation;
|
|
|
|
};
|
|
|
|
|
2020-10-20 01:56:46 +03:00
|
|
|
const removeFixturesForModel = function removeFixturesForModel(modelFixture, options) {
|
2017-12-05 11:14:55 +03:00
|
|
|
return Promise.mapSeries(modelFixture.entries, function (entry) {
|
|
|
|
return models[modelFixture.name].findOne(entry.id ? {id: entry.id} : entry, options).then(function (found) {
|
|
|
|
if (found) {
|
|
|
|
return models[modelFixture.name].destroy(_.extend(options, {id: found.id}));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).then(function (results) {
|
|
|
|
return {expected: modelFixture.entries.length, done: results.length};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-10-20 01:56:46 +03:00
|
|
|
const removeFixturesForRelation = function removeFixturesForRelation(relationFixture, options) {
|
2019-04-17 20:18:44 +03:00
|
|
|
return fetchRelationData(relationFixture, options).then(function getRelationOps(data) {
|
|
|
|
const ops = [];
|
|
|
|
|
|
|
|
_.each(relationFixture.entries, function processEntries(entry, key) {
|
|
|
|
const fromItem = data.from.find(matchFunc(relationFixture.from.match, key));
|
|
|
|
|
2020-10-20 01:56:46 +03:00
|
|
|
_.each(entry, function processEntryValues(value, entryKey) {
|
|
|
|
const toItems = data.to.filter(matchFunc(relationFixture.to.match, entryKey, value));
|
2019-04-17 20:18:44 +03:00
|
|
|
|
|
|
|
if (toItems && toItems.length > 0) {
|
|
|
|
ops.push(function detachRelation() {
|
|
|
|
return baseUtils.detach(
|
|
|
|
models[relationFixture.from.Model || relationFixture.from.model],
|
|
|
|
fromItem.id,
|
|
|
|
relationFixture.from.relation,
|
|
|
|
toItems,
|
|
|
|
options
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return sequence(ops);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-10-12 18:18:57 +03:00
|
|
|
module.exports = {
|
|
|
|
addFixturesForModel: addFixturesForModel,
|
|
|
|
addFixturesForRelation: addFixturesForRelation,
|
|
|
|
findModelFixtureEntry: findModelFixtureEntry,
|
|
|
|
findModelFixtures: findModelFixtures,
|
2018-02-07 12:46:22 +03:00
|
|
|
findRelationFixture: findRelationFixture,
|
2017-12-05 11:14:55 +03:00
|
|
|
findPermissionRelationsForObject: findPermissionRelationsForObject,
|
2019-04-17 20:18:44 +03:00
|
|
|
removeFixturesForModel: removeFixturesForModel,
|
|
|
|
removeFixturesForRelation: removeFixturesForRelation
|
2016-10-12 18:18:57 +03:00
|
|
|
};
|