Ghost/test/unit/data/schema/fixtures/utils_spec.js

293 lines
12 KiB
JavaScript
Raw Normal View History

const should = require('should');
const sinon = require('sinon');
const Promise = require('bluebird');
const rewire = require('rewire');
const models = require('../../../../../core/server/models');
const baseUtils = require('../../../../../core/server/models/base/utils');
const fixtureUtils = rewire('../../../../../core/server/data/schema/fixtures/utils');
const fixtures = require('../../../../../core/server/data/schema/fixtures/fixtures');
✨ 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
describe('Migration Fixture Utils', function () {
let loggerStub;
beforeEach(function () {
loggerStub = {
info: sinon.stub(),
warn: sinon.stub()
};
models.init();
});
afterEach(function () {
sinon.restore();
});
describe('Match Func', function () {
const matchFunc = fixtureUtils.__get__('matchFunc');
let getStub;
beforeEach(function () {
getStub = sinon.stub();
getStub.withArgs('foo').returns('bar');
getStub.withArgs('fun').returns('baz');
});
it('should match undefined with no args', function () {
matchFunc()({get: getStub}).should.be.true();
getStub.calledOnce.should.be.true();
getStub.calledWith(undefined).should.be.true();
});
it('should match key with match string', function () {
matchFunc('foo', 'bar')({get: getStub}).should.be.true();
getStub.calledOnce.should.be.true();
getStub.calledWith('foo').should.be.true();
matchFunc('foo', 'buz')({get: getStub}).should.be.false();
getStub.calledTwice.should.be.true();
getStub.secondCall.calledWith('foo').should.be.true();
});
it('should match value when key is 0', function () {
matchFunc('foo', 0, 'bar')({get: getStub}).should.be.true();
getStub.calledOnce.should.be.true();
getStub.calledWith('foo').should.be.true();
matchFunc('foo', 0, 'buz')({get: getStub}).should.be.false();
getStub.calledTwice.should.be.true();
getStub.secondCall.calledWith('foo').should.be.true();
});
it('should match key & value when match is array', function () {
matchFunc(['foo', 'fun'], 'bar', 'baz')({get: getStub}).should.be.true();
getStub.calledTwice.should.be.true();
getStub.getCall(0).calledWith('fun').should.be.true();
getStub.getCall(1).calledWith('foo').should.be.true();
matchFunc(['foo', 'fun'], 'baz', 'bar')({get: getStub}).should.be.false();
getStub.callCount.should.eql(4);
getStub.getCall(2).calledWith('fun').should.be.true();
getStub.getCall(3).calledWith('foo').should.be.true();
});
it('should match key only when match is array, but value is all', function () {
matchFunc(['foo', 'fun'], 'bar', 'all')({get: getStub}).should.be.true();
getStub.calledOnce.should.be.true();
getStub.calledWith('foo').should.be.true();
matchFunc(['foo', 'fun'], 'all', 'bar')({get: getStub}).should.be.false();
getStub.callCount.should.eql(3);
getStub.getCall(1).calledWith('fun').should.be.true();
getStub.getCall(2).calledWith('foo').should.be.true();
});
it('should match key & value when match and value are arrays', function () {
matchFunc(['foo', 'fun'], 'bar', ['baz', 'buz'])({get: getStub}).should.be.true();
getStub.calledTwice.should.be.true();
getStub.getCall(0).calledWith('fun').should.be.true();
getStub.getCall(1).calledWith('foo').should.be.true();
matchFunc(['foo', 'fun'], 'bar', ['biz', 'buz'])({get: getStub}).should.be.false();
getStub.callCount.should.eql(4);
getStub.getCall(2).calledWith('fun').should.be.true();
getStub.getCall(3).calledWith('foo').should.be.true();
});
});
describe('Add Fixtures For Model', function () {
it('should call add for main post fixture', function (done) {
const postOneStub = sinon.stub(models.Post, 'findOne').returns(Promise.resolve());
const postAddStub = sinon.stub(models.Post, 'add').returns(Promise.resolve({}));
fixtureUtils.addFixturesForModel(fixtures.models[4]).then(function (result) {
should.exist(result);
result.should.be.an.Object();
result.should.have.property('expected', 7);
result.should.have.property('done', 7);
postOneStub.callCount.should.eql(7);
postAddStub.callCount.should.eql(7);
done();
✨ Themes API activation permissions & validation (#8104) refs #8093 ✨ Add activate theme permission - add permission to activate themes - update tests - also: update tests for invites TODO: change how the active theme setting is updated to reduce extra permissions ✨ Move theme validation to gscan - add a new gscan validation method and use it for upload - update activate endpoint to do validation also using gscan - change to using SettingsModel instead of API so that we don't call validation or permissions on the settings API - remove validation from the settings model - remove the old validation function - add new invalid theme message to translations & remove a bunch of theme validation related unused keys 📖 Planned changes 🚨 Tests for theme activation API endpoint 🐛 Don't allow deleting the active theme 🚫 Prevent activeTheme being set via settings API - We want to control how this happens in future. - We still want to store the information in settings, via the model. - We just don't want to be able to change this info via the settings edit endpoint 🐛 ✨ Fix warnings for uploads & add for activations - warnings for uploads were broken in f8b498d - fix the response + adds tests to cover that warnings are correctly returned - add the same response to activations + more tests - activations now return a single theme object - the theme that was activated + any warnings 🎨 Improve how we generate theme API responses - remove the requirement to pass in the active theme! - move this to a specialist function, away from the list 🎨 Do not load gscan on boot
2017-03-13 14:44:44 +03:00
}).catch(done);
});
it('should not call add for main post fixture if it is already found', function (done) {
const postOneStub = sinon.stub(models.Post, 'findOne').returns(Promise.resolve({}));
const postAddStub = sinon.stub(models.Post, 'add').returns(Promise.resolve({}));
✨ Multiple authors (#9426) no issue This PR adds the server side logic for multiple authors. This adds the ability to add multiple authors per post. We keep and support single authors (maybe till the next major - this is still in discussion) ### key notes - `authors` are not fetched by default, only if we need them - the migration script iterates over all posts and figures out if an author_id is valid and exists (in master we can add invalid author_id's) and then adds the relation (falls back to owner if invalid) - ~~i had to push a fork of bookshelf to npm because we currently can't bump bookshelf + the two bugs i discovered are anyway not yet merged (https://github.com/kirrg001/bookshelf/commits/master)~~ replaced by new bookshelf release - the implementation of single & multiple authors lives in a single place (introduction of a new concept: model relation) - if you destroy an author, we keep the behaviour for now -> remove all posts where the primary author id matches. furthermore, remove all relations in posts_authors (e.g. secondary author) - we make re-use of the `excludeAttrs` concept which was invented in the contributors PR (to protect editing authors as author/contributor role) -> i've added a clear todo that we need a logic to make a diff of the target relation -> both for tags and authors - `authors` helper available (same as `tags` helper) - `primary_author` computed field available - `primary_author` functionality available (same as `primary_tag` e.g. permalinks, prev/next helper etc)
2018-03-27 17:16:15 +03:00
fixtureUtils.addFixturesForModel(fixtures.models[4]).then(function (result) {
should.exist(result);
result.should.be.an.Object();
result.should.have.property('expected', 7);
result.should.have.property('done', 0);
postOneStub.callCount.should.eql(7);
postAddStub.callCount.should.eql(0);
done();
✨ Themes API activation permissions & validation (#8104) refs #8093 ✨ Add activate theme permission - add permission to activate themes - update tests - also: update tests for invites TODO: change how the active theme setting is updated to reduce extra permissions ✨ Move theme validation to gscan - add a new gscan validation method and use it for upload - update activate endpoint to do validation also using gscan - change to using SettingsModel instead of API so that we don't call validation or permissions on the settings API - remove validation from the settings model - remove the old validation function - add new invalid theme message to translations & remove a bunch of theme validation related unused keys 📖 Planned changes 🚨 Tests for theme activation API endpoint 🐛 Don't allow deleting the active theme 🚫 Prevent activeTheme being set via settings API - We want to control how this happens in future. - We still want to store the information in settings, via the model. - We just don't want to be able to change this info via the settings edit endpoint 🐛 ✨ Fix warnings for uploads & add for activations - warnings for uploads were broken in f8b498d - fix the response + adds tests to cover that warnings are correctly returned - add the same response to activations + more tests - activations now return a single theme object - the theme that was activated + any warnings 🎨 Improve how we generate theme API responses - remove the requirement to pass in the active theme! - move this to a specialist function, away from the list 🎨 Do not load gscan on boot
2017-03-13 14:44:44 +03:00
}).catch(done);
});
});
describe('Add Fixtures For Relation', function () {
it('should call attach for permissions-roles', function (done) {
const fromItem = {
related: sinon.stub().returnsThis(),
findWhere: sinon.stub().returns()
};
const toItem = [{get: sinon.stub()}];
const dataMethodStub = {
filter: sinon.stub().returns(toItem),
find: sinon.stub().returns(fromItem)
};
const baseUtilAttachStub = sinon.stub(baseUtils, 'attach').returns(Promise.resolve([{}]));
const permsAllStub = sinon.stub(models.Permission, 'findAll').returns(Promise.resolve(dataMethodStub));
const rolesAllStub = sinon.stub(models.Role, 'findAll').returns(Promise.resolve(dataMethodStub));
fixtureUtils.addFixturesForRelation(fixtures.relations[0]).then(function (result) {
should.exist(result);
result.should.be.an.Object();
result.should.have.property('expected', 69);
result.should.have.property('done', 69);
// Permissions & Roles
permsAllStub.calledOnce.should.be.true();
rolesAllStub.calledOnce.should.be.true();
dataMethodStub.filter.callCount.should.eql(69);
dataMethodStub.find.callCount.should.eql(7);
baseUtilAttachStub.callCount.should.eql(69);
fromItem.related.callCount.should.eql(69);
fromItem.findWhere.callCount.should.eql(69);
toItem[0].get.callCount.should.eql(138);
done();
}).catch(done);
});
it('should call attach for posts-tags', function (done) {
const fromItem = {
related: sinon.stub().returnsThis(),
findWhere: sinon.stub().returns()
};
const toItem = [{get: sinon.stub()}];
const dataMethodStub = {
filter: sinon.stub().returns(toItem),
find: sinon.stub().returns(fromItem)
};
const baseUtilAttachStub = sinon.stub(baseUtils, 'attach').returns(Promise.resolve([{}]));
const postsAllStub = sinon.stub(models.Post, 'findAll').returns(Promise.resolve(dataMethodStub));
const tagsAllStub = sinon.stub(models.Tag, 'findAll').returns(Promise.resolve(dataMethodStub));
fixtureUtils.addFixturesForRelation(fixtures.relations[1]).then(function (result) {
should.exist(result);
result.should.be.an.Object();
result.should.have.property('expected', 7);
result.should.have.property('done', 7);
// Posts & Tags
postsAllStub.calledOnce.should.be.true();
tagsAllStub.calledOnce.should.be.true();
dataMethodStub.filter.callCount.should.eql(7);
dataMethodStub.find.callCount.should.eql(7);
fromItem.related.callCount.should.eql(7);
fromItem.findWhere.callCount.should.eql(7);
toItem[0].get.callCount.should.eql(7);
baseUtilAttachStub.callCount.should.eql(7);
done();
}).catch(done);
});
it('will not call attach for posts-tags if already present', function (done) {
const fromItem = {
related: sinon.stub().returnsThis(),
findWhere: sinon.stub().returns({}),
tags: sinon.stub().returnsThis(),
attach: sinon.stub().returns(Promise.resolve({}))
};
const toItem = [{get: sinon.stub()}];
const dataMethodStub = {
filter: sinon.stub().returns(toItem),
find: sinon.stub().returns(fromItem)
};
const postsAllStub = sinon.stub(models.Post, 'findAll').returns(Promise.resolve(dataMethodStub));
const tagsAllStub = sinon.stub(models.Tag, 'findAll').returns(Promise.resolve(dataMethodStub));
fixtureUtils.addFixturesForRelation(fixtures.relations[1]).then(function (result) {
should.exist(result);
result.should.be.an.Object();
result.should.have.property('expected', 7);
result.should.have.property('done', 0);
// Posts & Tags
postsAllStub.calledOnce.should.be.true();
tagsAllStub.calledOnce.should.be.true();
dataMethodStub.filter.callCount.should.eql(7);
dataMethodStub.find.callCount.should.eql(7);
fromItem.related.callCount.should.eql(7);
fromItem.findWhere.callCount.should.eql(7);
toItem[0].get.callCount.should.eql(7);
fromItem.tags.called.should.be.false();
fromItem.attach.called.should.be.false();
done();
}).catch(done);
});
});
describe('findModelFixtureEntry', function () {
it('should fetch a single fixture entry', function () {
const foundFixture = fixtureUtils.findModelFixtureEntry('Integration', {slug: 'zapier'});
foundFixture.should.be.an.Object();
foundFixture.should.eql({
slug: 'zapier',
name: 'Zapier',
description: 'Built-in Zapier integration',
type: 'builtin',
api_keys: [{type: 'admin'}]
});
});
});
describe('findModelFixtures', function () {
it('should fetch a fixture with multiple entries', function () {
const foundFixture = fixtureUtils.findModelFixtures('Permission', {object_type: 'db'});
foundFixture.should.be.an.Object();
foundFixture.entries.should.be.an.Array().with.lengthOf(4);
foundFixture.entries[0].should.eql({
name: 'Export database',
action_type: 'exportContent',
object_type: 'db'
});
foundFixture.entries[3].should.eql({
name: 'Backup database',
action_type: 'backupContent',
object_type: 'db'
});
});
});
describe('findPermissionRelationsForObject', function () {
it('should fetch a fixture with multiple entries', function () {
const foundFixture = fixtureUtils.findPermissionRelationsForObject('db');
foundFixture.should.be.an.Object();
foundFixture.entries.should.be.an.Object();
foundFixture.entries.should.have.property('Administrator', {db: 'all'});
});
});
});