mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-02 08:13:34 +03:00
7eb316b786
* 🛠 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 0e6ed957cd (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
1405 lines
60 KiB
JavaScript
1405 lines
60 KiB
JavaScript
var testUtils = require('../utils/index'),
|
|
should = require('should'),
|
|
sinon = require('sinon'),
|
|
Promise = require('bluebird'),
|
|
moment = require('moment'),
|
|
assert = require('assert'),
|
|
_ = require('lodash'),
|
|
validator = require('validator'),
|
|
|
|
// Stuff we are testing
|
|
db = require('../../server/data/db'),
|
|
versioning = require('../../server/data/schema').versioning,
|
|
exporter = require('../../server/data/export'),
|
|
importer = require('../../server/data/import'),
|
|
DataImporter = require('../../server/data/import/data-importer'),
|
|
|
|
DEF_DB_VERSION = versioning.getNewestDatabaseVersion(),
|
|
knex = db.knex,
|
|
sandbox = sinon.sandbox.create();
|
|
|
|
// Tests in here do an import for each test
|
|
describe('Import', function () {
|
|
before(testUtils.teardown);
|
|
afterEach(testUtils.teardown);
|
|
afterEach(function () {
|
|
sandbox.restore();
|
|
});
|
|
|
|
should.exist(exporter);
|
|
should.exist(importer);
|
|
|
|
describe('Resolves', function () {
|
|
beforeEach(testUtils.setup());
|
|
|
|
it('resolves DataImporter', function (done) {
|
|
var importStub = sandbox.stub(DataImporter, 'importData', function () {
|
|
return Promise.resolve();
|
|
}),
|
|
fakeData = {test: true};
|
|
|
|
importer.doImport(fakeData).then(function () {
|
|
importStub.calledWith(fakeData).should.equal(true);
|
|
|
|
importStub.restore();
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
});
|
|
|
|
describe('Sanitizes', function () {
|
|
beforeEach(testUtils.setup('roles', 'owner', 'settings'));
|
|
|
|
it('import results have data and problems', function (done) {
|
|
var exportData;
|
|
|
|
testUtils.fixtures.loadExportFixture('export-003').then(function (exported) {
|
|
exportData = exported;
|
|
return importer.doImport(exportData);
|
|
}).then(function (importResult) {
|
|
should.exist(importResult);
|
|
should.exist(importResult.data);
|
|
should.exist(importResult.problems);
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
|
|
it('removes duplicate posts', function (done) {
|
|
var exportData;
|
|
|
|
testUtils.fixtures.loadExportFixture('export-003-duplicate-posts').then(function (exported) {
|
|
exportData = exported;
|
|
return importer.doImport(exportData);
|
|
}).then(function (importResult) {
|
|
should.exist(importResult.data.data.posts);
|
|
|
|
importResult.data.data.posts.length.should.equal(1);
|
|
|
|
importResult.problems.posts.length.should.equal(1);
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
|
|
it('removes duplicate tags and updates associations', function (done) {
|
|
var exportData;
|
|
|
|
testUtils.fixtures.loadExportFixture('export-003-duplicate-tags').then(function (exported) {
|
|
exportData = exported;
|
|
return importer.doImport(exportData);
|
|
}).then(function (importResult) {
|
|
should.exist(importResult.data.data.tags);
|
|
should.exist(importResult.data.data.posts_tags);
|
|
|
|
importResult.data.data.tags.length.should.equal(1);
|
|
|
|
// Check we imported all posts_tags associations
|
|
importResult.data.data.posts_tags.length.should.equal(2);
|
|
// Check the post_tag.tag_id was updated when we removed duplicate tag
|
|
_.every(importResult.data.data.posts_tags, function (postTag) {
|
|
return postTag.tag_id !== 2;
|
|
});
|
|
|
|
importResult.problems.tags.length.should.equal(1);
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
});
|
|
|
|
describe('DataImporter', function () {
|
|
beforeEach(testUtils.setup('roles', 'owner', 'settings'));
|
|
|
|
should.exist(DataImporter);
|
|
|
|
it('imports data from 000', function (done) {
|
|
var exportData;
|
|
|
|
testUtils.fixtures.loadExportFixture('export-000').then(function (exported) {
|
|
exportData = exported;
|
|
|
|
return importer.doImport(exportData);
|
|
}).then(function () {
|
|
// Grab the data from tables
|
|
return Promise.all([
|
|
knex('users').select(),
|
|
knex('posts').select(),
|
|
knex('settings').select(),
|
|
knex('tags').select()
|
|
]);
|
|
}).then(function (importedData) {
|
|
should.exist(importedData);
|
|
|
|
importedData.length.should.equal(4, 'Did not get data successfully');
|
|
|
|
var users = importedData[0],
|
|
posts = importedData[1],
|
|
settings = importedData[2],
|
|
tags = importedData[3];
|
|
|
|
// we always have 1 user, the owner user we added
|
|
users.length.should.equal(1, 'There should only be one user');
|
|
// import no longer requires all data to be dropped, and adds posts
|
|
posts.length.should.equal(exportData.data.posts.length, 'Wrong number of posts');
|
|
|
|
// test settings
|
|
settings.length.should.be.above(0, 'Wrong number of settings');
|
|
_.find(settings, {key: 'databaseVersion'}).value.should.equal(DEF_DB_VERSION, 'Wrong database version');
|
|
|
|
// test tags
|
|
tags.length.should.equal(exportData.data.tags.length, 'no new tags');
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
|
|
it('safely imports data, from 001', function (done) {
|
|
var exportData,
|
|
timestamp = moment().startOf('day').valueOf(); // no ms
|
|
|
|
testUtils.fixtures.loadExportFixture('export-001').then(function (exported) {
|
|
exportData = exported;
|
|
|
|
// Modify timestamp data for testing
|
|
exportData.data.posts[0].created_at = timestamp;
|
|
exportData.data.posts[0].updated_at = timestamp;
|
|
exportData.data.posts[0].published_at = timestamp;
|
|
|
|
return importer.doImport(exportData);
|
|
}).then(function () {
|
|
// Grab the data from tables
|
|
return Promise.all([
|
|
knex('users').select(),
|
|
knex('posts').select(),
|
|
knex('settings').select(),
|
|
knex('tags').select()
|
|
]);
|
|
}).then(function (importedData) {
|
|
should.exist(importedData);
|
|
|
|
importedData.length.should.equal(4, 'Did not get data successfully');
|
|
|
|
var users = importedData[0],
|
|
posts = importedData[1],
|
|
settings = importedData[2],
|
|
tags = importedData[3];
|
|
|
|
// we always have 1 user, the default user we added
|
|
users.length.should.equal(1, 'There should only be one user');
|
|
|
|
// user should still have the credentials from the original insert, not the import
|
|
users[0].email.should.equal(testUtils.DataGenerator.Content.users[0].email);
|
|
users[0].password.should.equal(testUtils.DataGenerator.Content.users[0].password);
|
|
// but the name, slug, and bio should have been overridden
|
|
users[0].name.should.equal(exportData.data.users[0].name);
|
|
users[0].slug.should.equal(exportData.data.users[0].slug);
|
|
should.not.exist(users[0].bio, 'bio is not imported');
|
|
|
|
// import no longer requires all data to be dropped, and adds posts
|
|
posts.length.should.equal(exportData.data.posts.length, 'Wrong number of posts');
|
|
|
|
// test settings
|
|
settings.length.should.be.above(0, 'Wrong number of settings');
|
|
_.find(settings, {key: 'databaseVersion'}).value.should.equal(DEF_DB_VERSION, 'Wrong database version');
|
|
|
|
// activeTheme should NOT have been overridden
|
|
_.find(settings, {key: 'activeTheme'}).value.should.equal('casper', 'Wrong theme');
|
|
|
|
// test tags
|
|
tags.length.should.equal(exportData.data.tags.length, 'no new tags');
|
|
|
|
// Ensure imported post retains set timestamp
|
|
// When in sqlite we are returned a unix timestamp number,
|
|
// in MySQL we're returned a date object.
|
|
// We pass the returned post always through the date object
|
|
// to ensure the return is consistent for all DBs.
|
|
assert.equal(moment(posts[0].created_at).valueOf(), timestamp);
|
|
assert.equal(moment(posts[0].updated_at).valueOf(), timestamp);
|
|
assert.equal(moment(posts[0].published_at).valueOf(), timestamp);
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
|
|
it('doesn\'t import invalid post data from 001', function (done) {
|
|
var exportData;
|
|
|
|
testUtils.fixtures.loadExportFixture('export-001').then(function (exported) {
|
|
exportData = exported;
|
|
|
|
// change title to 151 characters
|
|
exportData.data.posts[0].title = new Array(152).join('a');
|
|
exportData.data.posts[0].tags = 'Tag';
|
|
return importer.doImport(exportData);
|
|
}).then(function () {
|
|
(1).should.eql(0, 'Data import should not resolve promise.');
|
|
}, function (error) {
|
|
error[0].message.should.eql('Value in [posts.title] exceeds maximum length of 150 characters.');
|
|
error[0].errorType.should.eql('ValidationError');
|
|
|
|
Promise.all([
|
|
knex('users').select(),
|
|
knex('posts').select(),
|
|
knex('settings').select(),
|
|
knex('tags').select()
|
|
]).then(function (importedData) {
|
|
should.exist(importedData);
|
|
|
|
importedData.length.should.equal(4, 'Did not get data successfully');
|
|
|
|
var users = importedData[0],
|
|
posts = importedData[1],
|
|
settings = importedData[2],
|
|
tags = importedData[3];
|
|
|
|
// we always have 1 user, the default user we added
|
|
users.length.should.equal(1, 'There should only be one user');
|
|
|
|
// Nothing should have been imported
|
|
posts.length.should.equal(0, 'Wrong number of posts');
|
|
tags.length.should.equal(0, 'no new tags');
|
|
|
|
// test settings
|
|
settings.length.should.be.above(0, 'Wrong number of settings');
|
|
_.find(settings, {key: 'databaseVersion'}).value.should.equal(DEF_DB_VERSION, 'Wrong database version');
|
|
|
|
done();
|
|
});
|
|
}).catch(done);
|
|
});
|
|
|
|
it('doesn\'t import invalid settings data from 001', function (done) {
|
|
var exportData;
|
|
|
|
testUtils.fixtures.loadExportFixture('export-001').then(function (exported) {
|
|
exportData = exported;
|
|
// change to blank settings key
|
|
exportData.data.settings[3].key = null;
|
|
return importer.doImport(exportData);
|
|
}).then(function () {
|
|
(1).should.eql(0, 'Data import should not resolve promise.');
|
|
}, function (error) {
|
|
error[0].message.should.eql('Value in [settings.key] cannot be blank.');
|
|
error[0].errorType.should.eql('ValidationError');
|
|
|
|
Promise.all([
|
|
knex('users').select(),
|
|
knex('posts').select(),
|
|
knex('settings').select(),
|
|
knex('tags').select()
|
|
]).then(function (importedData) {
|
|
should.exist(importedData);
|
|
|
|
importedData.length.should.equal(4, 'Did not get data successfully');
|
|
|
|
var users = importedData[0],
|
|
posts = importedData[1],
|
|
settings = importedData[2],
|
|
tags = importedData[3];
|
|
|
|
// we always have 1 user, the default user we added
|
|
users.length.should.equal(1, 'There should only be one user');
|
|
|
|
// Nothing should have been imported
|
|
posts.length.should.equal(0, 'Wrong number of posts');
|
|
tags.length.should.equal(0, 'no new tags');
|
|
|
|
// test settings
|
|
settings.length.should.be.above(0, 'Wrong number of settings');
|
|
_.find(settings, {key: 'databaseVersion'}).value.should.equal(DEF_DB_VERSION, 'Wrong database version');
|
|
|
|
done();
|
|
});
|
|
}).catch(done);
|
|
});
|
|
});
|
|
|
|
describe('002', function () {
|
|
beforeEach(testUtils.setup('roles', 'owner', 'settings'));
|
|
|
|
it('safely imports data from 002', function (done) {
|
|
var exportData,
|
|
timestamp = moment().startOf('day').valueOf(); // no ms
|
|
|
|
testUtils.fixtures.loadExportFixture('export-002').then(function (exported) {
|
|
exportData = exported;
|
|
|
|
// Modify timestamp data for testing
|
|
exportData.data.posts[0].created_at = timestamp;
|
|
exportData.data.posts[0].updated_at = timestamp;
|
|
exportData.data.posts[0].published_at = timestamp;
|
|
|
|
return importer.doImport(exportData);
|
|
}).then(function () {
|
|
// Grab the data from tables
|
|
return Promise.all([
|
|
knex('users').select(),
|
|
knex('posts').select(),
|
|
knex('settings').select(),
|
|
knex('tags').select()
|
|
]);
|
|
}).then(function (importedData) {
|
|
should.exist(importedData);
|
|
|
|
importedData.length.should.equal(4, 'Did not get data successfully');
|
|
|
|
var users = importedData[0],
|
|
posts = importedData[1],
|
|
settings = importedData[2],
|
|
tags = importedData[3];
|
|
|
|
// we always have 1 user, the owner user we added
|
|
users.length.should.equal(1, 'There should only be one user');
|
|
|
|
// user should still have the credentials from the original insert, not the import
|
|
users[0].email.should.equal(testUtils.DataGenerator.Content.users[0].email);
|
|
users[0].password.should.equal(testUtils.DataGenerator.Content.users[0].password);
|
|
// but the name, slug, and bio should have been overridden
|
|
users[0].name.should.equal(exportData.data.users[0].name);
|
|
users[0].slug.should.equal(exportData.data.users[0].slug);
|
|
should.not.exist(users[0].bio, 'bio is not imported');
|
|
|
|
// import no longer requires all data to be dropped, and adds posts
|
|
posts.length.should.equal(exportData.data.posts.length, 'Wrong number of posts');
|
|
|
|
// test settings
|
|
settings.length.should.be.above(0, 'Wrong number of settings');
|
|
_.find(settings, {key: 'databaseVersion'}).value.should.equal(DEF_DB_VERSION, 'Wrong database version');
|
|
|
|
// activeTheme should NOT have been overridden
|
|
_.find(settings, {key: 'activeTheme'}).value.should.equal('casper', 'Wrong theme');
|
|
|
|
// test tags
|
|
tags.length.should.equal(exportData.data.tags.length, 'no new tags');
|
|
|
|
// Ensure imported post retains set timestamp
|
|
// When in sqlite we are returned a unix timestamp number,
|
|
// in MySQL we're returned a date object.
|
|
// We pass the returned post always through the date object
|
|
// to ensure the return is consistant for all DBs.
|
|
assert.equal(moment(posts[0].created_at).valueOf(), timestamp);
|
|
assert.equal(moment(posts[0].updated_at).valueOf(), timestamp);
|
|
assert.equal(moment(posts[0].published_at).valueOf(), timestamp);
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
|
|
it('doesn\'t import invalid post data from 002', function (done) {
|
|
var exportData;
|
|
|
|
testUtils.fixtures.loadExportFixture('export-002').then(function (exported) {
|
|
exportData = exported;
|
|
|
|
// change title to 151 characters
|
|
exportData.data.posts[0].title = new Array(152).join('a');
|
|
exportData.data.posts[0].tags = 'Tag';
|
|
return importer.doImport(exportData);
|
|
}).then(function () {
|
|
(1).should.eql(0, 'Data import should not resolve promise.');
|
|
}, function (error) {
|
|
error[0].message.should.eql('Value in [posts.title] exceeds maximum length of 150 characters.');
|
|
error[0].errorType.should.eql('ValidationError');
|
|
|
|
Promise.all([
|
|
knex('users').select(),
|
|
knex('posts').select(),
|
|
knex('settings').select(),
|
|
knex('tags').select()
|
|
]).then(function (importedData) {
|
|
should.exist(importedData);
|
|
|
|
importedData.length.should.equal(4, 'Did not get data successfully');
|
|
|
|
var users = importedData[0],
|
|
posts = importedData[1],
|
|
settings = importedData[2],
|
|
tags = importedData[3];
|
|
|
|
// we always have 1 user, the owner user we added
|
|
users.length.should.equal(1, 'There should only be one user');
|
|
// Nothing should have been imported
|
|
posts.length.should.equal(0, 'Wrong number of posts');
|
|
tags.length.should.equal(0, 'no new tags');
|
|
|
|
// test settings
|
|
settings.length.should.be.above(0, 'Wrong number of settings');
|
|
_.find(settings, {key: 'databaseVersion'}).value.should.equal(DEF_DB_VERSION, 'Wrong database version');
|
|
|
|
done();
|
|
});
|
|
}).catch(done);
|
|
});
|
|
|
|
it('doesn\'t import invalid settings data from 002', function (done) {
|
|
var exportData;
|
|
|
|
testUtils.fixtures.loadExportFixture('export-002').then(function (exported) {
|
|
exportData = exported;
|
|
// change to blank settings key
|
|
exportData.data.settings[3].key = null;
|
|
return importer.doImport(exportData);
|
|
}).then(function () {
|
|
(1).should.eql(0, 'Data import should not resolve promise.');
|
|
}, function (error) {
|
|
error[0].message.should.eql('Value in [settings.key] cannot be blank.');
|
|
error[0].errorType.should.eql('ValidationError');
|
|
|
|
Promise.all([
|
|
knex('users').select(),
|
|
knex('posts').select(),
|
|
knex('settings').select(),
|
|
knex('tags').select()
|
|
]).then(function (importedData) {
|
|
should.exist(importedData);
|
|
|
|
importedData.length.should.equal(4, 'Did not get data successfully');
|
|
|
|
var users = importedData[0],
|
|
posts = importedData[1],
|
|
settings = importedData[2],
|
|
tags = importedData[3];
|
|
|
|
// we always have 1 user, the owner user we added
|
|
users.length.should.equal(1, 'There should only be one user');
|
|
// Nothing should have been imported
|
|
posts.length.should.equal(0, 'Wrong number of posts');
|
|
tags.length.should.equal(0, 'no new tags');
|
|
|
|
// test settings
|
|
settings.length.should.be.above(0, 'Wrong number of settings');
|
|
_.find(settings, {key: 'databaseVersion'}).value.should.equal(DEF_DB_VERSION, 'Wrong database version');
|
|
|
|
done();
|
|
});
|
|
}).catch(done);
|
|
});
|
|
});
|
|
|
|
describe('003', function () {
|
|
beforeEach(testUtils.setup('roles', 'owner', 'settings'));
|
|
|
|
it('safely imports data from 003 (single user)', function (done) {
|
|
var exportData;
|
|
|
|
testUtils.fixtures.loadExportFixture('export-003').then(function (exported) {
|
|
exportData = exported;
|
|
return importer.doImport(exportData);
|
|
}).then(function () {
|
|
// Grab the data from tables
|
|
return Promise.all([
|
|
knex('users').select(),
|
|
knex('posts').select(),
|
|
knex('settings').select(),
|
|
knex('tags').select()
|
|
]);
|
|
}).then(function (importedData) {
|
|
should.exist(importedData);
|
|
|
|
importedData.length.should.equal(4, 'Did not get data successfully');
|
|
|
|
var users = importedData[0],
|
|
posts = importedData[1],
|
|
settings = importedData[2],
|
|
tags = importedData[3];
|
|
|
|
// user should still have the credentials from the original insert, not the import
|
|
users[0].email.should.equal(testUtils.DataGenerator.Content.users[0].email);
|
|
users[0].password.should.equal(testUtils.DataGenerator.Content.users[0].password);
|
|
// but the name, slug, and bio should have been overridden
|
|
users[0].name.should.equal(exportData.data.users[0].name);
|
|
users[0].slug.should.equal(exportData.data.users[0].slug);
|
|
should.not.exist(users[0].bio, 'bio is not imported');
|
|
|
|
// test posts
|
|
posts.length.should.equal(1, 'Wrong number of posts');
|
|
// test tags
|
|
tags.length.should.equal(1, 'no new tags');
|
|
|
|
// test settings
|
|
settings.length.should.be.above(0, 'Wrong number of settings');
|
|
_.find(settings, {key: 'databaseVersion'}).value.should.equal(DEF_DB_VERSION, 'Wrong database version');
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
|
|
it('handles validation errors nicely', function (done) {
|
|
var exportData;
|
|
|
|
testUtils.fixtures.loadExportFixture('export-003-badValidation').then(function (exported) {
|
|
exportData = exported;
|
|
return importer.doImport(exportData);
|
|
}).then(function () {
|
|
done(new Error('Allowed import of duplicate data'));
|
|
}).catch(function (response) {
|
|
response.length.should.equal(5);
|
|
response[0].errorType.should.equal('ValidationError');
|
|
response[0].message.should.eql('Value in [posts.title] cannot be blank.');
|
|
response[1].errorType.should.equal('ValidationError');
|
|
response[1].message.should.eql('Value in [posts.slug] cannot be blank.');
|
|
response[2].errorType.should.equal('ValidationError');
|
|
response[2].message.should.eql('Value in [settings.key] cannot be blank.');
|
|
response[3].errorType.should.equal('ValidationError');
|
|
response[3].message.should.eql('Value in [tags.slug] cannot be blank.');
|
|
response[4].errorType.should.equal('ValidationError');
|
|
response[4].message.should.eql('Value in [tags.name] cannot be blank.');
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
|
|
it('handles database errors nicely', function (done) {
|
|
var exportData;
|
|
testUtils.fixtures.loadExportFixture('export-003-dbErrors').then(function (exported) {
|
|
exportData = exported;
|
|
return importer.doImport(exportData);
|
|
}).then(function () {
|
|
done(new Error('Allowed import of duplicate data'));
|
|
}).catch(function (response) {
|
|
response.length.should.be.above(0);
|
|
response[0].errorType.should.equal('DataImportError');
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
|
|
it('doesn\'t import posts with an invalid author', function (done) {
|
|
var exportData;
|
|
|
|
testUtils.fixtures.loadExportFixture('export-003-mu-unknownAuthor').then(function (exported) {
|
|
exportData = exported;
|
|
|
|
return importer.doImport(exportData);
|
|
}).then(function () {
|
|
done(new Error('Allowed import of unknown author'));
|
|
}).catch(function (response) {
|
|
response.length.should.equal(1);
|
|
response[0].message.should.eql('Attempting to import data linked to unknown user id 2');
|
|
response[0].errorType.should.equal('DataImportError');
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
|
|
it('doesn\'t import invalid tags data from 003', function (done) {
|
|
var exportData;
|
|
|
|
testUtils.fixtures.loadExportFixture('export-003-nullTags').then(function (exported) {
|
|
exportData = exported;
|
|
|
|
exportData.data.tags.length.should.be.above(1);
|
|
exportData.data.posts_tags.length.should.be.above(1);
|
|
|
|
return importer.doImport(exportData);
|
|
}).then(function () {
|
|
done(new Error('Allowed import of invalid tags data'));
|
|
}).catch(function (response) {
|
|
response.length.should.equal(2);
|
|
response[0].errorType.should.equal('ValidationError');
|
|
response[0].message.should.eql('Value in [tags.name] cannot be blank.');
|
|
response[1].errorType.should.equal('ValidationError');
|
|
response[1].message.should.eql('Value in [tags.slug] cannot be blank.');
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
|
|
it('doesn\'t import invalid posts data from 003', function (done) {
|
|
var exportData;
|
|
|
|
testUtils.fixtures.loadExportFixture('export-003-nullPosts').then(function (exported) {
|
|
exportData = exported;
|
|
|
|
exportData.data.posts.length.should.be.above(1);
|
|
|
|
return importer.doImport(exportData);
|
|
}).then(function () {
|
|
done(new Error('Allowed import of invalid post data'));
|
|
}).catch(function (response) {
|
|
response.length.should.equal(5, response);
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
|
|
it('correctly sanitizes incorrect UUIDs', function (done) {
|
|
var exportData;
|
|
|
|
testUtils.fixtures.loadExportFixture('export-003-wrongUUID').then(function (exported) {
|
|
exportData = exported;
|
|
|
|
exportData.data.posts.length.should.be.above(0);
|
|
|
|
return importer.doImport(exportData);
|
|
}).then(function () {
|
|
// Grab the data from tables
|
|
return knex('posts').select();
|
|
}).then(function (importedData) {
|
|
should.exist(importedData);
|
|
|
|
assert.equal(validator.isUUID(importedData[0].uuid), true, 'Old Ghost UUID NOT fixed');
|
|
assert.equal(validator.isUUID(importedData[1].uuid), true, 'Empty UUID NOT fixed');
|
|
assert.equal(validator.isUUID(importedData[2].uuid), true, 'Missing UUID NOT fixed');
|
|
assert.equal(validator.isUUID(importedData[3].uuid), true, 'Malformed UUID NOT fixed');
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
});
|
|
});
|
|
|
|
// Tests in here do an import-per-describe, and then have several tests to check various bits of data
|
|
describe('Import (new test structure)', function () {
|
|
before(testUtils.teardown);
|
|
|
|
describe('imports multi user data onto blank ghost install', function () {
|
|
var exportData;
|
|
|
|
before(function doImport(done) {
|
|
testUtils.initFixtures('roles', 'owner', 'settings').then(function () {
|
|
return testUtils.fixtures.loadExportFixture('export-003-mu');
|
|
}).then(function (exported) {
|
|
exportData = exported;
|
|
return importer.doImport(exportData);
|
|
}).then(function () {
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
after(testUtils.teardown);
|
|
|
|
it('gets the right data', function (done) {
|
|
var fetchImported = Promise.join(
|
|
knex('posts').select(),
|
|
knex('settings').select(),
|
|
knex('tags').select()
|
|
);
|
|
|
|
fetchImported.then(function (importedData) {
|
|
var posts,
|
|
settings,
|
|
tags,
|
|
post1,
|
|
post2,
|
|
post3;
|
|
|
|
// General data checks
|
|
should.exist(importedData);
|
|
importedData.length.should.equal(3, 'Did not get data successfully');
|
|
|
|
// Test posts, settings and tags
|
|
posts = importedData[0];
|
|
settings = importedData[1];
|
|
tags = importedData[2];
|
|
|
|
post1 = _.find(posts, function (post) {
|
|
return post.slug === exportData.data.posts[0].slug;
|
|
});
|
|
post2 = _.find(posts, function (post) {
|
|
return post.slug === exportData.data.posts[1].slug;
|
|
});
|
|
post3 = _.find(posts, function (post) {
|
|
return post.slug === exportData.data.posts[2].slug;
|
|
});
|
|
|
|
// test posts
|
|
posts.length.should.equal(3, 'Wrong number of posts');
|
|
post1.title.should.equal(exportData.data.posts[0].title);
|
|
post2.title.should.equal(exportData.data.posts[1].title);
|
|
post3.title.should.equal(exportData.data.posts[2].title);
|
|
|
|
// test tags
|
|
tags.length.should.equal(3, 'should be 3 tags');
|
|
|
|
// test settings
|
|
settings.length.should.be.above(0, 'Wrong number of settings');
|
|
_.find(settings, {key: 'databaseVersion'}).value.should.equal(DEF_DB_VERSION, 'Wrong database version');
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
|
|
it('imports users with correct roles and status', function (done) {
|
|
var fetchImported = Promise.join(
|
|
knex('users').select(),
|
|
knex('roles_users').select()
|
|
);
|
|
|
|
fetchImported.then(function (importedData) {
|
|
var user1,
|
|
user2,
|
|
user3,
|
|
users,
|
|
rolesUsers;
|
|
|
|
// General data checks
|
|
should.exist(importedData);
|
|
importedData.length.should.equal(2, 'Did not get data successfully');
|
|
|
|
// Test the users and roles
|
|
users = importedData[0];
|
|
rolesUsers = importedData[1];
|
|
|
|
// we imported 3 users
|
|
// the original user should be untouched
|
|
// the two news users should have been created
|
|
users.length.should.equal(3, 'There should only be three users');
|
|
|
|
// the owner user is first
|
|
user1 = users[0];
|
|
// the other two users should have the imported data, but they get inserted in different orders
|
|
user2 = _.find(users, function (user) {
|
|
return user.name === exportData.data.users[1].name;
|
|
});
|
|
user3 = _.find(users, function (user) {
|
|
return user.name === exportData.data.users[2].name;
|
|
});
|
|
|
|
user1.email.should.equal(testUtils.DataGenerator.Content.users[0].email);
|
|
user1.password.should.equal(testUtils.DataGenerator.Content.users[0].password);
|
|
user1.status.should.equal('active');
|
|
user2.email.should.equal(exportData.data.users[1].email);
|
|
user3.email.should.equal(exportData.data.users[2].email);
|
|
|
|
// Newly created users should have a status of locked
|
|
user2.status.should.equal('locked');
|
|
user3.status.should.equal('locked');
|
|
|
|
// Newly created users should have created_at/_by and updated_at/_by set to when they were imported
|
|
user2.created_by.should.equal(user1.id);
|
|
user2.created_at.should.not.equal(exportData.data.users[1].created_at);
|
|
user2.updated_by.should.equal(user1.id);
|
|
user2.updated_at.should.not.equal(exportData.data.users[1].updated_at);
|
|
user3.created_by.should.equal(user1.id);
|
|
user3.created_at.should.not.equal(exportData.data.users[2].created_at);
|
|
user3.updated_by.should.equal(user1.id);
|
|
user3.updated_at.should.not.equal(exportData.data.users[2].updated_at);
|
|
|
|
rolesUsers.length.should.equal(3, 'There should be 3 role relations');
|
|
|
|
_.each(rolesUsers, function (roleUser) {
|
|
if (roleUser.user_id === user1.id) {
|
|
roleUser.role_id.should.equal(testUtils.DataGenerator.Content.roles[3].id, 'Original user should be an owner');
|
|
}
|
|
if (roleUser.user_id === user2.id) {
|
|
roleUser.role_id.should.equal(testUtils.DataGenerator.Content.roles[0].id, 'Josephine should be an admin');
|
|
}
|
|
if (roleUser.user_id === user3.id) {
|
|
roleUser.role_id.should.equal(testUtils.DataGenerator.Content.roles[2].id, 'Smith should be an author by default');
|
|
}
|
|
});
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
|
|
it('imports posts & tags with correct authors, owners etc', function (done) {
|
|
var fetchImported = Promise.join(
|
|
knex('users').select(),
|
|
knex('posts').select(),
|
|
knex('tags').select()
|
|
);
|
|
|
|
fetchImported.then(function (importedData) {
|
|
var users, user1, user2, user3,
|
|
posts, post1, post2, post3,
|
|
tags, tag1, tag2, tag3;
|
|
|
|
// General data checks
|
|
should.exist(importedData);
|
|
importedData.length.should.equal(3, 'Did not get data successfully');
|
|
|
|
// Test the users and roles
|
|
users = importedData[0];
|
|
posts = importedData[1];
|
|
tags = importedData[2];
|
|
|
|
// Grab the users
|
|
// the owner user is first
|
|
user1 = users[0];
|
|
// the other two users should have the imported data, but they get inserted in different orders
|
|
user2 = _.find(users, function (user) {
|
|
return user.name === exportData.data.users[1].name;
|
|
});
|
|
user3 = _.find(users, function (user) {
|
|
return user.name === exportData.data.users[2].name;
|
|
});
|
|
post1 = _.find(posts, function (post) {
|
|
return post.slug === exportData.data.posts[0].slug;
|
|
});
|
|
post2 = _.find(posts, function (post) {
|
|
return post.slug === exportData.data.posts[1].slug;
|
|
});
|
|
post3 = _.find(posts, function (post) {
|
|
return post.slug === exportData.data.posts[2].slug;
|
|
});
|
|
tag1 = _.find(tags, function (tag) {
|
|
return tag.slug === exportData.data.tags[0].slug;
|
|
});
|
|
tag2 = _.find(tags, function (tag) {
|
|
return tag.slug === exportData.data.tags[1].slug;
|
|
});
|
|
tag3 = _.find(tags, function (tag) {
|
|
return tag.slug === exportData.data.tags[2].slug;
|
|
});
|
|
|
|
// Check the authors are correct
|
|
post1.author_id.should.equal(user2.id);
|
|
post2.author_id.should.equal(user3.id);
|
|
post3.author_id.should.equal(user1.id);
|
|
|
|
// Created by should be what was in the import file
|
|
post1.created_by.should.equal(user1.id);
|
|
post2.created_by.should.equal(user3.id);
|
|
post3.created_by.should.equal(user1.id);
|
|
|
|
// Updated by gets set to the current user
|
|
post1.updated_by.should.equal(user1.id);
|
|
post2.updated_by.should.equal(user1.id);
|
|
post3.updated_by.should.equal(user1.id);
|
|
|
|
// Published by should be what was in the import file
|
|
post1.published_by.should.equal(user2.id);
|
|
post2.published_by.should.equal(user3.id);
|
|
post3.published_by.should.equal(user1.id);
|
|
|
|
// Created by should be what was in the import file
|
|
tag1.created_by.should.equal(user1.id);
|
|
tag2.created_by.should.equal(user2.id);
|
|
tag3.created_by.should.equal(user3.id);
|
|
|
|
// Updated by gets set to the current user
|
|
tag1.updated_by.should.equal(user1.id);
|
|
tag2.updated_by.should.equal(user1.id);
|
|
tag3.updated_by.should.equal(user1.id);
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
});
|
|
|
|
describe('imports multi user data with no owner onto blank ghost install', function () {
|
|
var exportData;
|
|
|
|
before(function doImport(done) {
|
|
testUtils.initFixtures('roles', 'owner', 'settings').then(function () {
|
|
return testUtils.fixtures.loadExportFixture('export-003-mu-noOwner');
|
|
}).then(function (exported) {
|
|
exportData = exported;
|
|
return importer.doImport(exportData);
|
|
}).then(function () {
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
after(testUtils.teardown);
|
|
|
|
it('gets the right data', function (done) {
|
|
var fetchImported = Promise.join(
|
|
knex('posts').select(),
|
|
knex('settings').select(),
|
|
knex('tags').select()
|
|
);
|
|
|
|
fetchImported.then(function (importedData) {
|
|
var posts,
|
|
settings,
|
|
tags,
|
|
post1,
|
|
post2,
|
|
post3;
|
|
|
|
// General data checks
|
|
should.exist(importedData);
|
|
importedData.length.should.equal(3, 'Did not get data successfully');
|
|
|
|
// Test posts, settings and tags
|
|
posts = importedData[0];
|
|
settings = importedData[1];
|
|
tags = importedData[2];
|
|
|
|
post1 = _.find(posts, function (post) {
|
|
return post.slug === exportData.data.posts[0].slug;
|
|
});
|
|
post2 = _.find(posts, function (post) {
|
|
return post.slug === exportData.data.posts[1].slug;
|
|
});
|
|
post3 = _.find(posts, function (post) {
|
|
return post.slug === exportData.data.posts[2].slug;
|
|
});
|
|
|
|
// test posts
|
|
posts.length.should.equal(3, 'Wrong number of posts');
|
|
post1.title.should.equal(exportData.data.posts[0].title);
|
|
post2.title.should.equal(exportData.data.posts[1].title);
|
|
post3.title.should.equal(exportData.data.posts[2].title);
|
|
|
|
// test tags
|
|
tags.length.should.equal(3, 'should be 3 tags');
|
|
|
|
// test settings
|
|
settings.length.should.be.above(0, 'Wrong number of settings');
|
|
_.find(settings, {key: 'databaseVersion'}).value.should.equal(DEF_DB_VERSION, 'Wrong database version');
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
|
|
it('imports users with correct roles and status', function (done) {
|
|
var fetchImported = Promise.join(
|
|
knex('users').select(),
|
|
knex('roles_users').select()
|
|
);
|
|
|
|
fetchImported.then(function (importedData) {
|
|
var user1,
|
|
user2,
|
|
user3,
|
|
users,
|
|
rolesUsers;
|
|
|
|
// General data checks
|
|
should.exist(importedData);
|
|
importedData.length.should.equal(2, 'Did not get data successfully');
|
|
|
|
// Test the users and roles
|
|
users = importedData[0];
|
|
rolesUsers = importedData[1];
|
|
|
|
// we imported 3 users
|
|
// the original user should be untouched
|
|
// the two news users should have been created
|
|
users.length.should.equal(3, 'There should only be three users');
|
|
|
|
// the owner user is first
|
|
user1 = users[0];
|
|
// the other two users should have the imported data, but they get inserted in different orders
|
|
user2 = _.find(users, function (user) {
|
|
return user.name === exportData.data.users[0].name;
|
|
});
|
|
user3 = _.find(users, function (user) {
|
|
return user.name === exportData.data.users[1].name;
|
|
});
|
|
|
|
user1.email.should.equal(testUtils.DataGenerator.Content.users[0].email);
|
|
user1.password.should.equal(testUtils.DataGenerator.Content.users[0].password);
|
|
user1.status.should.equal('active');
|
|
user2.email.should.equal(exportData.data.users[0].email);
|
|
user3.email.should.equal(exportData.data.users[1].email);
|
|
|
|
// Newly created users should have a status of locked
|
|
user2.status.should.equal('locked');
|
|
user3.status.should.equal('locked');
|
|
|
|
// Newly created users should have created_at/_by and updated_at/_by set to when they were imported
|
|
user2.created_by.should.equal(user1.id);
|
|
user2.created_at.should.not.equal(exportData.data.users[0].created_at);
|
|
user2.updated_by.should.equal(user1.id);
|
|
user2.updated_at.should.not.equal(exportData.data.users[0].updated_at);
|
|
user3.created_by.should.equal(user1.id);
|
|
user3.created_at.should.not.equal(exportData.data.users[1].created_at);
|
|
user3.updated_by.should.equal(user1.id);
|
|
user3.updated_at.should.not.equal(exportData.data.users[1].updated_at);
|
|
|
|
rolesUsers.length.should.equal(3, 'There should be 3 role relations');
|
|
|
|
_.each(rolesUsers, function (roleUser) {
|
|
if (roleUser.user_id === user1.id) {
|
|
roleUser.role_id.should.equal(testUtils.DataGenerator.Content.roles[3].id, 'Original user should be an owner');
|
|
}
|
|
if (roleUser.user_id === user2.id) {
|
|
roleUser.role_id.should.equal(testUtils.DataGenerator.Content.roles[0].id, 'Josephine should be an admin');
|
|
}
|
|
if (roleUser.user_id === user3.id) {
|
|
roleUser.role_id.should.equal(testUtils.DataGenerator.Content.roles[2].id, 'Smith should be an author by default');
|
|
}
|
|
});
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
|
|
it('imports posts & tags with correct authors, owners etc', function (done) {
|
|
var fetchImported = Promise.join(
|
|
knex('users').select(),
|
|
knex('posts').select(),
|
|
knex('tags').select()
|
|
);
|
|
|
|
fetchImported.then(function (importedData) {
|
|
var users, user1, user2, user3,
|
|
posts, post1, post2, post3,
|
|
tags, tag1, tag2, tag3;
|
|
|
|
// General data checks
|
|
should.exist(importedData);
|
|
importedData.length.should.equal(3, 'Did not get data successfully');
|
|
|
|
// Test the users and roles
|
|
users = importedData[0];
|
|
posts = importedData[1];
|
|
tags = importedData[2];
|
|
|
|
// Grab the users
|
|
// the owner user is first
|
|
user1 = users[0];
|
|
// the other two users should have the imported data, but they get inserted in different orders
|
|
user2 = _.find(users, function (user) {
|
|
return user.name === exportData.data.users[0].name;
|
|
});
|
|
user3 = _.find(users, function (user) {
|
|
return user.name === exportData.data.users[1].name;
|
|
});
|
|
post1 = _.find(posts, function (post) {
|
|
return post.slug === exportData.data.posts[0].slug;
|
|
});
|
|
post2 = _.find(posts, function (post) {
|
|
return post.slug === exportData.data.posts[1].slug;
|
|
});
|
|
post3 = _.find(posts, function (post) {
|
|
return post.slug === exportData.data.posts[2].slug;
|
|
});
|
|
tag1 = _.find(tags, function (tag) {
|
|
return tag.slug === exportData.data.tags[0].slug;
|
|
});
|
|
tag2 = _.find(tags, function (tag) {
|
|
return tag.slug === exportData.data.tags[1].slug;
|
|
});
|
|
tag3 = _.find(tags, function (tag) {
|
|
return tag.slug === exportData.data.tags[2].slug;
|
|
});
|
|
|
|
// Check the authors are correct
|
|
post1.author_id.should.equal(user2.id);
|
|
post2.author_id.should.equal(user3.id);
|
|
post3.author_id.should.equal(user1.id);
|
|
|
|
// Created by should be what was in the import file
|
|
post1.created_by.should.equal(user1.id);
|
|
post2.created_by.should.equal(user3.id);
|
|
post3.created_by.should.equal(user1.id);
|
|
|
|
// Updated by gets set to the current user
|
|
post1.updated_by.should.equal(user1.id);
|
|
post2.updated_by.should.equal(user1.id);
|
|
post3.updated_by.should.equal(user1.id);
|
|
|
|
// Published by should be what was in the import file
|
|
post1.published_by.should.equal(user2.id);
|
|
post2.published_by.should.equal(user3.id);
|
|
post3.published_by.should.equal(user1.id);
|
|
|
|
// Created by should be what was in the import file
|
|
tag1.created_by.should.equal(user1.id);
|
|
tag2.created_by.should.equal(user2.id);
|
|
tag3.created_by.should.equal(user3.id);
|
|
|
|
// Updated by gets set to the current user
|
|
tag1.updated_by.should.equal(user1.id);
|
|
tag2.updated_by.should.equal(user1.id);
|
|
tag3.updated_by.should.equal(user1.id);
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
});
|
|
|
|
describe('imports multi user data onto existing data', function () {
|
|
var exportData;
|
|
|
|
before(function doImport(done) {
|
|
// initialise the blog with some data
|
|
testUtils.initFixtures('users:roles', 'posts', 'settings').then(function () {
|
|
return testUtils.fixtures.loadExportFixture('export-003-mu');
|
|
}).then(function (exported) {
|
|
exportData = exported;
|
|
return importer.doImport(exportData);
|
|
}).then(function () {
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
after(testUtils.teardown);
|
|
|
|
it('gets the right data', function (done) {
|
|
var fetchImported = Promise.join(
|
|
knex('posts').select(),
|
|
knex('settings').select(),
|
|
knex('tags').select()
|
|
);
|
|
|
|
fetchImported.then(function (importedData) {
|
|
var posts,
|
|
settings,
|
|
tags,
|
|
post1,
|
|
post2,
|
|
post3;
|
|
|
|
// General data checks
|
|
should.exist(importedData);
|
|
importedData.length.should.equal(3, 'Did not get data successfully');
|
|
|
|
// Test posts, settings and tags
|
|
posts = importedData[0];
|
|
settings = importedData[1];
|
|
tags = importedData[2];
|
|
|
|
post1 = _.find(posts, function (post) {
|
|
return post.slug === exportData.data.posts[0].slug;
|
|
});
|
|
post2 = _.find(posts, function (post) {
|
|
return post.slug === exportData.data.posts[1].slug;
|
|
});
|
|
post3 = _.find(posts, function (post) {
|
|
return post.slug === exportData.data.posts[2].slug;
|
|
});
|
|
|
|
// test posts
|
|
posts.length.should.equal(
|
|
(exportData.data.posts.length + testUtils.DataGenerator.Content.posts.length),
|
|
'Wrong number of posts'
|
|
);
|
|
|
|
posts[0].title.should.equal(testUtils.DataGenerator.Content.posts[0].title);
|
|
|
|
post1.title.should.equal(exportData.data.posts[0].title);
|
|
post2.title.should.equal(exportData.data.posts[1].title);
|
|
post3.title.should.equal(exportData.data.posts[2].title);
|
|
|
|
// test tags
|
|
tags.length.should.equal(
|
|
(exportData.data.tags.length + testUtils.DataGenerator.Content.tags.length),
|
|
'Wrong number of tags'
|
|
);
|
|
|
|
tags[0].name.should.equal(testUtils.DataGenerator.Content.tags[0].name);
|
|
|
|
// test settings
|
|
settings.length.should.be.above(0, 'Wrong number of settings');
|
|
_.find(settings, {key: 'databaseVersion'}).value.should.equal(DEF_DB_VERSION, 'Wrong database version');
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
|
|
it('imports users with correct roles and status', function (done) {
|
|
var fetchImported = Promise.join(
|
|
knex('users').select(),
|
|
knex('roles_users').select()
|
|
);
|
|
|
|
fetchImported.then(function (importedData) {
|
|
var ownerUser,
|
|
newUser,
|
|
existingUser,
|
|
users,
|
|
rolesUsers;
|
|
|
|
// General data checks
|
|
should.exist(importedData);
|
|
importedData.length.should.equal(2, 'Did not get data successfully');
|
|
|
|
// Test the users and roles
|
|
users = importedData[0];
|
|
rolesUsers = importedData[1];
|
|
|
|
// we imported 3 users, there were already 4 users, only one of the imported users is new
|
|
users.length.should.equal(5, 'There should only be three users');
|
|
|
|
// the owner user is first
|
|
ownerUser = users[0];
|
|
// the other two users should have the imported data, but they get inserted in different orders
|
|
newUser = _.find(users, function (user) {
|
|
return user.name === exportData.data.users[1].name;
|
|
});
|
|
existingUser = _.find(users, function (user) {
|
|
return user.name === exportData.data.users[2].name;
|
|
});
|
|
|
|
ownerUser.email.should.equal(testUtils.DataGenerator.Content.users[0].email);
|
|
ownerUser.password.should.equal(testUtils.DataGenerator.Content.users[0].password);
|
|
ownerUser.status.should.equal('active');
|
|
newUser.email.should.equal(exportData.data.users[1].email);
|
|
existingUser.email.should.equal(exportData.data.users[2].email);
|
|
|
|
// Newly created users should have a status of locked
|
|
newUser.status.should.equal('locked');
|
|
// The already existing user should still have a status of active
|
|
existingUser.status.should.equal('active');
|
|
|
|
// Newly created users should have created_at/_by and updated_at/_by set to when they were imported
|
|
newUser.created_by.should.equal(ownerUser.id);
|
|
newUser.created_at.should.not.equal(exportData.data.users[1].created_at);
|
|
newUser.updated_by.should.equal(ownerUser.id);
|
|
newUser.updated_at.should.not.equal(exportData.data.users[1].updated_at);
|
|
|
|
rolesUsers.length.should.equal(5, 'There should be 5 role relations');
|
|
|
|
_.each(rolesUsers, function (roleUser) {
|
|
if (roleUser.user_id === ownerUser.id) {
|
|
roleUser.role_id.should.equal(testUtils.DataGenerator.Content.roles[3].id, 'Original user should be an owner');
|
|
}
|
|
if (roleUser.user_id === newUser.id) {
|
|
roleUser.role_id.should.equal(testUtils.DataGenerator.Content.roles[0].id, 'New user should be an admin');
|
|
}
|
|
if (roleUser.user_id === existingUser.id) {
|
|
roleUser.role_id.should.equal(testUtils.DataGenerator.Content.roles[0].id, 'Existing user was an admin');
|
|
}
|
|
});
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
|
|
it('imports posts & tags with correct authors, owners etc', function (done) {
|
|
var fetchImported = Promise.join(
|
|
knex('users').select(),
|
|
knex('posts').select(),
|
|
knex('tags').select()
|
|
);
|
|
|
|
fetchImported.then(function (importedData) {
|
|
var users, ownerUser, newUser, existingUser,
|
|
posts, post1, post2, post3,
|
|
tags, tag1, tag2, tag3;
|
|
|
|
// General data checks
|
|
should.exist(importedData);
|
|
importedData.length.should.equal(3, 'Did not get data successfully');
|
|
|
|
// Test the users and roles
|
|
users = importedData[0];
|
|
posts = importedData[1];
|
|
tags = importedData[2];
|
|
|
|
// Grab the users
|
|
// the owner user is first
|
|
ownerUser = users[0];
|
|
// the other two users should have the imported data, but they get inserted in different orders
|
|
newUser = _.find(users, function (user) {
|
|
return user.name === exportData.data.users[1].name;
|
|
});
|
|
existingUser = _.find(users, function (user) {
|
|
return user.name === exportData.data.users[2].name;
|
|
});
|
|
post1 = _.find(posts, function (post) {
|
|
return post.slug === exportData.data.posts[0].slug;
|
|
});
|
|
post2 = _.find(posts, function (post) {
|
|
return post.slug === exportData.data.posts[1].slug;
|
|
});
|
|
post3 = _.find(posts, function (post) {
|
|
return post.slug === exportData.data.posts[2].slug;
|
|
});
|
|
tag1 = _.find(tags, function (tag) {
|
|
return tag.slug === exportData.data.tags[0].slug;
|
|
});
|
|
tag2 = _.find(tags, function (tag) {
|
|
return tag.slug === exportData.data.tags[1].slug;
|
|
});
|
|
tag3 = _.find(tags, function (tag) {
|
|
return tag.slug === exportData.data.tags[2].slug;
|
|
});
|
|
|
|
// Check the authors are correct
|
|
post1.author_id.should.equal(newUser.id);
|
|
post2.author_id.should.equal(existingUser.id);
|
|
post3.author_id.should.equal(ownerUser.id);
|
|
|
|
// Created by should be what was in the import file
|
|
post1.created_by.should.equal(ownerUser.id);
|
|
post2.created_by.should.equal(existingUser.id);
|
|
post3.created_by.should.equal(ownerUser.id);
|
|
|
|
// Updated by gets set to the current user
|
|
post1.updated_by.should.equal(ownerUser.id);
|
|
post2.updated_by.should.equal(ownerUser.id);
|
|
post3.updated_by.should.equal(ownerUser.id);
|
|
|
|
// Published by should be what was in the import file
|
|
post1.published_by.should.equal(newUser.id);
|
|
post2.published_by.should.equal(existingUser.id);
|
|
post3.published_by.should.equal(ownerUser.id);
|
|
|
|
// Created by should be what was in the import file
|
|
tag1.created_by.should.equal(ownerUser.id);
|
|
tag2.created_by.should.equal(newUser.id);
|
|
tag3.created_by.should.equal(existingUser.id);
|
|
|
|
// Updated by gets set to the current user
|
|
tag1.updated_by.should.equal(ownerUser.id);
|
|
tag2.updated_by.should.equal(ownerUser.id);
|
|
tag3.updated_by.should.equal(ownerUser.id);
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
});
|
|
|
|
describe('imports multi user data onto existing data without duplicate owners', function () {
|
|
var exportData;
|
|
|
|
before(function doImport(done) {
|
|
// initialise the blog with some data
|
|
testUtils.initFixtures('users:roles', 'posts', 'settings').then(function () {
|
|
return testUtils.fixtures.loadExportFixture('export-003-mu-multipleOwner');
|
|
}).then(function (exported) {
|
|
exportData = exported;
|
|
return importer.doImport(exportData);
|
|
}).then(function () {
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
after(testUtils.teardown);
|
|
|
|
it('imports users with correct roles and status', function (done) {
|
|
var fetchImported = Promise.join(
|
|
knex('users').select(),
|
|
knex('roles_users').select()
|
|
);
|
|
|
|
fetchImported.then(function (importedData) {
|
|
var ownerUser,
|
|
newUser,
|
|
existingUser,
|
|
users,
|
|
rolesUsers;
|
|
|
|
// General data checks
|
|
should.exist(importedData);
|
|
importedData.length.should.equal(2, 'Did not get data successfully');
|
|
|
|
// Test the users and roles
|
|
users = importedData[0];
|
|
rolesUsers = importedData[1];
|
|
|
|
// the owner user is first
|
|
ownerUser = users[0];
|
|
|
|
// the other two users should have the imported data, but they get inserted in different orders
|
|
newUser = _.find(users, function (user) {
|
|
return user.name === exportData.data.users[1].name;
|
|
});
|
|
existingUser = _.find(users, function (user) {
|
|
return user.name === exportData.data.users[2].name;
|
|
});
|
|
|
|
// we imported 3 users, there were already 4 users, only one of the imported users is new
|
|
users.length.should.equal(5, 'There should only be three users');
|
|
|
|
rolesUsers.length.should.equal(5, 'There should be 5 role relations');
|
|
|
|
_.each(rolesUsers, function (roleUser) {
|
|
if (roleUser.user_id === ownerUser.id) {
|
|
roleUser.role_id.should.equal(testUtils.DataGenerator.Content.roles[3].id, 'Original user should be an owner');
|
|
}
|
|
if (roleUser.user_id === newUser.id) {
|
|
roleUser.role_id.should.equal(testUtils.DataGenerator.Content.roles[0].id, 'New user should be downgraded from owner to admin');
|
|
}
|
|
if (roleUser.user_id === existingUser.id) {
|
|
roleUser.role_id.should.equal(testUtils.DataGenerator.Content.roles[0].id, 'Existing user was an admin');
|
|
}
|
|
});
|
|
|
|
done();
|
|
}).catch(done);
|
|
});
|
|
});
|
|
});
|