2014-07-21 21:50:04 +04:00
|
|
|
/*globals describe, before, beforeEach, afterEach, it*/
|
2014-06-05 01:26:03 +04:00
|
|
|
/*jshint expr:true*/
|
2014-07-21 21:50:04 +04:00
|
|
|
var testUtils = require('../utils/index'),
|
|
|
|
should = require('should'),
|
|
|
|
sinon = require('sinon'),
|
|
|
|
when = require('when'),
|
|
|
|
assert = require('assert'),
|
|
|
|
_ = require('lodash'),
|
|
|
|
rewire = require('rewire'),
|
2013-09-25 14:30:59 +04:00
|
|
|
|
|
|
|
// Stuff we are testing
|
2014-07-21 21:50:04 +04:00
|
|
|
config = rewire('../../server/config'),
|
|
|
|
configUpdate = config.__get__('updateConfig'),
|
|
|
|
defaultConfig = rewire('../../../config.example')[process.env.NODE_ENV],
|
|
|
|
migration = rewire('../../server/data/migration'),
|
|
|
|
versioning = require('../../server/data/versioning'),
|
|
|
|
exporter = require('../../server/data/export'),
|
|
|
|
importer = require('../../server/data/import'),
|
|
|
|
Importer000 = require('../../server/data/import/000'),
|
|
|
|
Importer001 = require('../../server/data/import/001'),
|
|
|
|
Importer002 = require('../../server/data/import/002'),
|
|
|
|
Importer003 = require('../../server/data/import/003'),
|
|
|
|
|
|
|
|
knex,
|
|
|
|
sandbox = sinon.sandbox.create();
|
2013-09-25 14:30:59 +04:00
|
|
|
|
2014-06-05 01:26:03 +04:00
|
|
|
describe('Import', function () {
|
2014-07-21 21:50:04 +04:00
|
|
|
before(testUtils.teardown);
|
|
|
|
afterEach(testUtils.teardown);
|
|
|
|
afterEach(function () {
|
|
|
|
sandbox.restore();
|
|
|
|
});
|
2013-09-25 14:30:59 +04:00
|
|
|
|
|
|
|
should.exist(exporter);
|
|
|
|
should.exist(importer);
|
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
describe('Resolves', function () {
|
2014-07-17 18:33:21 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
beforeEach(testUtils.setup());
|
|
|
|
beforeEach(function () {
|
|
|
|
var newConfig = _.extend(config, defaultConfig);
|
2014-07-28 02:22:00 +04:00
|
|
|
|
|
|
|
migration.__get__('config', newConfig);
|
|
|
|
configUpdate(newConfig);
|
|
|
|
knex = config.database.knex;
|
2014-07-21 21:50:04 +04:00
|
|
|
});
|
2014-02-12 07:40:39 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
it('resolves 000', function (done) {
|
|
|
|
var importStub = sandbox.stub(Importer000, 'importData', function () {
|
|
|
|
return when.resolve();
|
|
|
|
}),
|
|
|
|
fakeData = { test: true };
|
2013-09-25 14:30:59 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
importer('000', fakeData).then(function () {
|
|
|
|
importStub.calledWith(fakeData).should.equal(true);
|
2013-09-25 14:30:59 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
importStub.restore();
|
2013-09-25 14:30:59 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
2013-09-25 14:30:59 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
it('resolves 001', function (done) {
|
|
|
|
var importStub = sandbox.stub(Importer001, 'importData', function () {
|
|
|
|
return when.resolve();
|
|
|
|
}),
|
|
|
|
fakeData = { test: true };
|
2013-11-24 18:29:36 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
importer('001', fakeData).then(function () {
|
|
|
|
importStub.calledWith(fakeData).should.equal(true);
|
2013-11-24 18:29:36 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
importStub.restore();
|
2013-11-24 18:29:36 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
2013-11-24 18:29:36 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
it('resolves 002', function (done) {
|
|
|
|
var importStub = sandbox.stub(Importer002, 'importData', function () {
|
|
|
|
return when.resolve();
|
|
|
|
}),
|
|
|
|
fakeData = { test: true };
|
2014-01-15 17:29:23 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
importer('002', fakeData).then(function () {
|
|
|
|
importStub.calledWith(fakeData).should.equal(true);
|
2014-01-15 17:29:23 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
importStub.restore();
|
2014-01-15 17:29:23 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
2014-01-15 17:29:23 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
it('resolves 003', function (done) {
|
|
|
|
var importStub = sandbox.stub(Importer003, 'importData', function () {
|
|
|
|
return when.resolve();
|
|
|
|
}),
|
|
|
|
fakeData = { test: true };
|
2014-02-25 00:28:18 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
importer('003', fakeData).then(function () {
|
|
|
|
importStub.calledWith(fakeData).should.equal(true);
|
2014-02-25 00:28:18 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
importStub.restore();
|
2014-02-25 00:28:18 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
2014-02-25 00:28:18 +04:00
|
|
|
});
|
|
|
|
|
2014-06-05 01:26:03 +04:00
|
|
|
describe('000', function () {
|
2013-09-25 14:30:59 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
beforeEach(testUtils.setup('owner', 'settings'));
|
2013-12-29 00:13:47 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
should.exist(Importer000);
|
2013-12-29 00:13:47 +04:00
|
|
|
|
2014-06-05 01:26:03 +04:00
|
|
|
it('imports data from 000', function (done) {
|
2013-12-29 00:13:47 +04:00
|
|
|
var exportData,
|
2014-06-05 01:26:03 +04:00
|
|
|
versioningStub = sandbox.stub(versioning, 'getDatabaseVersion', function () {
|
|
|
|
return when.resolve('000');
|
2013-12-29 00:13:47 +04:00
|
|
|
});
|
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
testUtils.fixtures.loadExportFixture('export-000').then(function (exported) {
|
2013-09-25 14:30:59 +04:00
|
|
|
exportData = exported;
|
|
|
|
|
2014-06-05 01:26:03 +04:00
|
|
|
return importer('000', exportData);
|
2013-09-25 14:30:59 +04:00
|
|
|
}).then(function () {
|
|
|
|
// Grab the data from tables
|
|
|
|
return when.all([
|
2014-06-05 01:26:03 +04:00
|
|
|
knex('users').select(),
|
|
|
|
knex('posts').select(),
|
|
|
|
knex('settings').select(),
|
|
|
|
knex('tags').select()
|
2013-09-25 14:30:59 +04:00
|
|
|
]);
|
|
|
|
}).then(function (importedData) {
|
|
|
|
should.exist(importedData);
|
2013-12-29 00:13:47 +04:00
|
|
|
|
2013-09-25 14:30:59 +04:00
|
|
|
importedData.length.should.equal(4, 'Did not get data successfully');
|
|
|
|
|
2013-12-29 00:13:47 +04:00
|
|
|
var users = importedData[0],
|
|
|
|
posts = importedData[1],
|
|
|
|
settings = importedData[2],
|
|
|
|
tags = importedData[3];
|
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
// we always have 1 user, the owner user we added
|
2013-12-29 00:13:47 +04:00
|
|
|
users.length.should.equal(1, 'There should only be one user');
|
2013-09-25 14:30:59 +04:00
|
|
|
// import no longer requires all data to be dropped, and adds posts
|
2014-07-21 21:50:04 +04:00
|
|
|
posts.length.should.equal(exportData.data.posts.length, 'Wrong number of posts');
|
2013-09-25 14:30:59 +04:00
|
|
|
|
|
|
|
// test settings
|
2013-12-29 00:13:47 +04:00
|
|
|
settings.length.should.be.above(0, 'Wrong number of settings');
|
2014-06-05 01:26:03 +04:00
|
|
|
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('003', 'Wrong database version');
|
2013-09-25 14:30:59 +04:00
|
|
|
|
|
|
|
// test tags
|
2013-12-29 00:13:47 +04:00
|
|
|
tags.length.should.equal(exportData.data.tags.length, 'no new tags');
|
|
|
|
|
2014-06-01 03:53:21 +04:00
|
|
|
versioningStub.restore();
|
2013-09-25 14:30:59 +04:00
|
|
|
|
|
|
|
done();
|
2014-05-06 00:58:58 +04:00
|
|
|
}).catch(done);
|
2013-09-25 14:30:59 +04:00
|
|
|
});
|
2013-11-24 18:29:36 +04:00
|
|
|
});
|
|
|
|
|
2014-06-05 01:26:03 +04:00
|
|
|
describe('001', function () {
|
2013-11-21 00:36:02 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
beforeEach(testUtils.setup('owner', 'settings'));
|
|
|
|
|
|
|
|
should.exist(Importer001);
|
2013-12-29 00:13:47 +04:00
|
|
|
|
2014-06-05 01:26:03 +04:00
|
|
|
it('safely imports data from 001', function (done) {
|
2013-12-29 00:13:47 +04:00
|
|
|
var exportData,
|
|
|
|
timestamp = 1349928000000;
|
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
testUtils.fixtures.loadExportFixture('export-001').then(function (exported) {
|
2013-11-24 18:29:36 +04:00
|
|
|
exportData = exported;
|
|
|
|
|
2013-12-26 07:48:16 +04:00
|
|
|
// 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;
|
|
|
|
|
2014-06-05 01:26:03 +04:00
|
|
|
return importer('001', exportData);
|
2013-11-24 18:29:36 +04:00
|
|
|
}).then(function () {
|
|
|
|
// Grab the data from tables
|
|
|
|
return when.all([
|
2014-06-05 01:26:03 +04:00
|
|
|
knex('users').select(),
|
|
|
|
knex('posts').select(),
|
|
|
|
knex('settings').select(),
|
|
|
|
knex('tags').select()
|
2013-11-24 18:29:36 +04:00
|
|
|
]);
|
|
|
|
}).then(function (importedData) {
|
|
|
|
should.exist(importedData);
|
2013-12-26 07:48:16 +04:00
|
|
|
|
2013-11-24 18:29:36 +04:00
|
|
|
importedData.length.should.equal(4, 'Did not get data successfully');
|
|
|
|
|
2013-12-26 07:48:16 +04:00
|
|
|
var users = importedData[0],
|
|
|
|
posts = importedData[1],
|
|
|
|
settings = importedData[2],
|
2013-12-29 00:13:47 +04:00
|
|
|
tags = importedData[3],
|
|
|
|
exportEmail;
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
users[0].bio.should.equal(exportData.data.users[0].bio);
|
2013-12-26 07:48:16 +04:00
|
|
|
|
2013-11-24 18:29:36 +04:00
|
|
|
// import no longer requires all data to be dropped, and adds posts
|
2014-07-21 21:50:04 +04:00
|
|
|
posts.length.should.equal(exportData.data.posts.length, 'Wrong number of posts');
|
2013-11-24 18:29:36 +04:00
|
|
|
|
|
|
|
// test settings
|
2013-12-26 07:48:16 +04:00
|
|
|
settings.length.should.be.above(0, 'Wrong number of settings');
|
2014-06-05 01:26:03 +04:00
|
|
|
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('003', 'Wrong database version');
|
2013-11-24 18:29:36 +04:00
|
|
|
|
2013-12-29 00:13:47 +04:00
|
|
|
// activeTheme should NOT have been overridden
|
2014-06-05 01:26:03 +04:00
|
|
|
_.findWhere(settings, {key: 'activeTheme'}).value.should.equal('casper', 'Wrong theme');
|
2013-12-29 00:13:47 +04:00
|
|
|
|
|
|
|
// email address should have been overridden
|
2014-06-05 01:26:03 +04:00
|
|
|
exportEmail = _.findWhere(exportData.data.settings, {key: 'email'}).value;
|
|
|
|
_.findWhere(settings, {key: 'email'}).value.should.equal(exportEmail, 'Wrong email in settings');
|
2013-12-29 00:13:47 +04:00
|
|
|
|
2013-11-24 18:29:36 +04:00
|
|
|
// test tags
|
2013-12-26 07:48:16 +04:00
|
|
|
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
|
2014-07-21 21:50:04 +04:00
|
|
|
// to ensure the return is consistent for all DBs.
|
|
|
|
assert.equal(new Date(posts[0].created_at).getTime(), timestamp);
|
|
|
|
assert.equal(new Date(posts[0].updated_at).getTime(), timestamp);
|
|
|
|
assert.equal(new Date(posts[0].published_at).getTime(), timestamp);
|
2013-11-24 18:29:36 +04:00
|
|
|
|
|
|
|
done();
|
2014-05-06 00:58:58 +04:00
|
|
|
}).catch(done);
|
2013-11-24 18:29:36 +04:00
|
|
|
});
|
|
|
|
|
2014-06-05 01:26:03 +04:00
|
|
|
it('doesn\'t import invalid post data from 001', function (done) {
|
2013-11-24 18:29:36 +04:00
|
|
|
var exportData;
|
2013-12-29 00:13:47 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
testUtils.fixtures.loadExportFixture('export-001').then(function (exported) {
|
2013-11-21 00:36:02 +04:00
|
|
|
exportData = exported;
|
2013-12-29 00:13:47 +04:00
|
|
|
|
2013-11-21 00:36:02 +04:00
|
|
|
//change title to 151 characters
|
|
|
|
exportData.data.posts[0].title = new Array(152).join('a');
|
|
|
|
exportData.data.posts[0].tags = 'Tag';
|
2014-06-05 01:26:03 +04:00
|
|
|
return importer('001', exportData);
|
2013-11-21 00:36:02 +04:00
|
|
|
}).then(function () {
|
|
|
|
(1).should.eql(0, 'Data import should not resolve promise.');
|
|
|
|
}, function (error) {
|
2014-05-05 17:51:21 +04:00
|
|
|
|
|
|
|
error[0].message.should.eql('Value in [posts.title] exceeds maximum length of 150 characters.');
|
|
|
|
error[0].type.should.eql('ValidationError');
|
2013-11-24 18:29:36 +04:00
|
|
|
|
2013-11-21 00:36:02 +04:00
|
|
|
when.all([
|
2014-06-05 01:26:03 +04:00
|
|
|
knex('users').select(),
|
|
|
|
knex('posts').select(),
|
|
|
|
knex('settings').select(),
|
|
|
|
knex('tags').select()
|
2013-11-21 00:36:02 +04:00
|
|
|
]).then(function (importedData) {
|
|
|
|
should.exist(importedData);
|
2013-12-29 00:13:47 +04:00
|
|
|
|
2013-11-21 00:36:02 +04:00
|
|
|
importedData.length.should.equal(4, 'Did not get data successfully');
|
|
|
|
|
2013-12-29 00:13:47 +04:00
|
|
|
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');
|
2014-07-21 21:50:04 +04:00
|
|
|
|
|
|
|
// Nothing should have been imported
|
|
|
|
posts.length.should.equal(0, 'Wrong number of posts');
|
|
|
|
tags.length.should.equal(0, 'no new tags');
|
2013-11-21 00:36:02 +04:00
|
|
|
|
|
|
|
// test settings
|
2013-12-29 00:13:47 +04:00
|
|
|
settings.length.should.be.above(0, 'Wrong number of settings');
|
2014-06-05 01:26:03 +04:00
|
|
|
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('003', 'Wrong database version');
|
2013-11-21 00:36:02 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
|
2013-11-21 00:36:02 +04:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2013-11-25 23:20:01 +04:00
|
|
|
|
2014-05-06 00:58:58 +04:00
|
|
|
}).catch(done);
|
2013-11-21 00:36:02 +04:00
|
|
|
});
|
2013-12-29 00:13:47 +04:00
|
|
|
|
2014-06-05 01:26:03 +04:00
|
|
|
it('doesn\'t import invalid settings data from 001', function (done) {
|
2013-11-21 00:36:02 +04:00
|
|
|
var exportData;
|
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
testUtils.fixtures.loadExportFixture('export-001').then(function (exported) {
|
2013-11-21 00:36:02 +04:00
|
|
|
exportData = exported;
|
|
|
|
//change to blank settings key
|
|
|
|
exportData.data.settings[3].key = null;
|
2014-06-05 01:26:03 +04:00
|
|
|
return importer('001', exportData);
|
2013-11-21 00:36:02 +04:00
|
|
|
}).then(function () {
|
|
|
|
(1).should.eql(0, 'Data import should not resolve promise.');
|
|
|
|
}, function (error) {
|
2014-05-05 17:51:21 +04:00
|
|
|
|
2014-07-29 01:41:45 +04:00
|
|
|
error[0].message.should.eql('Value in [settings.key] cannot be blank.');
|
2014-05-05 17:51:21 +04:00
|
|
|
error[0].type.should.eql('ValidationError');
|
2013-12-29 00:13:47 +04:00
|
|
|
|
2013-11-21 00:36:02 +04:00
|
|
|
when.all([
|
2014-06-05 01:26:03 +04:00
|
|
|
knex('users').select(),
|
|
|
|
knex('posts').select(),
|
|
|
|
knex('settings').select(),
|
|
|
|
knex('tags').select()
|
2013-11-21 00:36:02 +04:00
|
|
|
]).then(function (importedData) {
|
|
|
|
should.exist(importedData);
|
2013-12-29 00:13:47 +04:00
|
|
|
|
2013-11-21 00:36:02 +04:00
|
|
|
importedData.length.should.equal(4, 'Did not get data successfully');
|
|
|
|
|
2013-12-29 00:13:47 +04:00
|
|
|
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');
|
2014-07-21 21:50:04 +04:00
|
|
|
|
|
|
|
// Nothing should have been imported
|
|
|
|
posts.length.should.equal(0, 'Wrong number of posts');
|
|
|
|
tags.length.should.equal(0, 'no new tags');
|
2013-11-21 00:36:02 +04:00
|
|
|
|
|
|
|
// test settings
|
2013-12-29 00:13:47 +04:00
|
|
|
settings.length.should.be.above(0, 'Wrong number of settings');
|
2014-06-05 01:26:03 +04:00
|
|
|
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('003', 'Wrong database version');
|
2013-11-21 00:36:02 +04:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2013-11-25 23:20:01 +04:00
|
|
|
|
2014-05-06 00:58:58 +04:00
|
|
|
}).catch(done);
|
2013-11-21 00:36:02 +04:00
|
|
|
});
|
2013-09-25 14:30:59 +04:00
|
|
|
});
|
2013-11-24 18:29:36 +04:00
|
|
|
|
2014-06-05 01:26:03 +04:00
|
|
|
describe('002', function () {
|
2014-01-15 17:29:23 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
beforeEach(testUtils.setup('owner', 'settings'));
|
|
|
|
|
|
|
|
should.exist(Importer002);
|
2014-01-15 17:29:23 +04:00
|
|
|
|
2014-06-05 01:26:03 +04:00
|
|
|
it('safely imports data from 002', function (done) {
|
2014-01-15 17:29:23 +04:00
|
|
|
var exportData,
|
|
|
|
timestamp = 1349928000000;
|
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
testUtils.fixtures.loadExportFixture('export-002').then(function (exported) {
|
2014-01-15 17:29:23 +04:00
|
|
|
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;
|
|
|
|
|
2014-06-05 01:26:03 +04:00
|
|
|
return importer('002', exportData);
|
2014-01-15 17:29:23 +04:00
|
|
|
}).then(function () {
|
|
|
|
// Grab the data from tables
|
|
|
|
return when.all([
|
2014-06-05 01:26:03 +04:00
|
|
|
knex('users').select(),
|
|
|
|
knex('posts').select(),
|
|
|
|
knex('settings').select(),
|
|
|
|
knex('tags').select()
|
2014-01-15 17:29:23 +04:00
|
|
|
]);
|
|
|
|
}).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],
|
|
|
|
exportEmail;
|
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
// we always have 1 user, the owner user we added
|
2014-01-15 17:29:23 +04:00
|
|
|
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);
|
|
|
|
users[0].bio.should.equal(exportData.data.users[0].bio);
|
|
|
|
|
|
|
|
// import no longer requires all data to be dropped, and adds posts
|
2014-07-21 21:50:04 +04:00
|
|
|
posts.length.should.equal(exportData.data.posts.length, 'Wrong number of posts');
|
2014-01-15 17:29:23 +04:00
|
|
|
|
|
|
|
// test settings
|
|
|
|
settings.length.should.be.above(0, 'Wrong number of settings');
|
2014-06-05 01:26:03 +04:00
|
|
|
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('003', 'Wrong database version');
|
2014-01-15 17:29:23 +04:00
|
|
|
|
|
|
|
// activeTheme should NOT have been overridden
|
2014-06-05 01:26:03 +04:00
|
|
|
_.findWhere(settings, {key: 'activeTheme'}).value.should.equal('casper', 'Wrong theme');
|
2014-01-15 17:29:23 +04:00
|
|
|
|
|
|
|
// email address should have been overridden
|
2014-06-05 01:26:03 +04:00
|
|
|
exportEmail = _.findWhere(exportData.data.settings, {key: 'email'}).value;
|
|
|
|
_.findWhere(settings, {key: 'email'}).value.should.equal(exportEmail, 'Wrong email in settings');
|
2014-01-15 17:29:23 +04:00
|
|
|
|
|
|
|
// 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.
|
2014-07-21 21:50:04 +04:00
|
|
|
assert.equal(new Date(posts[0].created_at).getTime(), timestamp);
|
|
|
|
assert.equal(new Date(posts[0].updated_at).getTime(), timestamp);
|
|
|
|
assert.equal(new Date(posts[0].published_at).getTime(), timestamp);
|
2014-01-15 17:29:23 +04:00
|
|
|
|
|
|
|
done();
|
2014-05-06 00:58:58 +04:00
|
|
|
}).catch(function (error) {
|
2014-04-08 17:40:33 +04:00
|
|
|
done(new Error(error));
|
2014-04-28 03:28:50 +04:00
|
|
|
});
|
2014-01-15 17:29:23 +04:00
|
|
|
});
|
|
|
|
|
2014-06-05 01:26:03 +04:00
|
|
|
it('doesn\'t import invalid post data from 002', function (done) {
|
2014-01-15 17:29:23 +04:00
|
|
|
var exportData;
|
|
|
|
|
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
testUtils.fixtures.loadExportFixture('export-002').then(function (exported) {
|
2014-01-15 17:29:23 +04:00
|
|
|
exportData = exported;
|
|
|
|
|
|
|
|
//change title to 151 characters
|
|
|
|
exportData.data.posts[0].title = new Array(152).join('a');
|
|
|
|
exportData.data.posts[0].tags = 'Tag';
|
2014-06-05 01:26:03 +04:00
|
|
|
return importer('002', exportData);
|
2014-01-15 17:29:23 +04:00
|
|
|
}).then(function () {
|
|
|
|
(1).should.eql(0, 'Data import should not resolve promise.');
|
|
|
|
}, function (error) {
|
2014-05-05 17:51:21 +04:00
|
|
|
|
|
|
|
error[0].message.should.eql('Value in [posts.title] exceeds maximum length of 150 characters.');
|
|
|
|
error[0].type.should.eql('ValidationError');
|
2014-01-15 17:29:23 +04:00
|
|
|
|
|
|
|
when.all([
|
2014-06-05 01:26:03 +04:00
|
|
|
knex('users').select(),
|
|
|
|
knex('posts').select(),
|
|
|
|
knex('settings').select(),
|
|
|
|
knex('tags').select()
|
2014-01-15 17:29:23 +04:00
|
|
|
]).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];
|
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
// we always have 1 user, the owner user we added
|
2014-01-15 17:29:23 +04:00
|
|
|
users.length.should.equal(1, 'There should only be one user');
|
2014-07-21 21:50:04 +04:00
|
|
|
// Nothing should have been imported
|
|
|
|
posts.length.should.equal(0, 'Wrong number of posts');
|
|
|
|
tags.length.should.equal(0, 'no new tags');
|
2014-01-15 17:29:23 +04:00
|
|
|
|
|
|
|
// test settings
|
|
|
|
settings.length.should.be.above(0, 'Wrong number of settings');
|
2014-06-05 01:26:03 +04:00
|
|
|
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('003', 'Wrong database version');
|
2014-01-15 17:29:23 +04:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2014-05-06 00:58:58 +04:00
|
|
|
}).catch(done);
|
2014-01-15 17:29:23 +04:00
|
|
|
});
|
|
|
|
|
2014-06-05 01:26:03 +04:00
|
|
|
it('doesn\'t import invalid settings data from 002', function (done) {
|
2014-01-15 17:29:23 +04:00
|
|
|
var exportData;
|
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
testUtils.fixtures.loadExportFixture('export-002').then(function (exported) {
|
2014-01-15 17:29:23 +04:00
|
|
|
exportData = exported;
|
|
|
|
//change to blank settings key
|
|
|
|
exportData.data.settings[3].key = null;
|
2014-06-05 01:26:03 +04:00
|
|
|
return importer('002', exportData);
|
2014-01-15 17:29:23 +04:00
|
|
|
}).then(function () {
|
|
|
|
(1).should.eql(0, 'Data import should not resolve promise.');
|
|
|
|
}, function (error) {
|
2014-05-05 17:51:21 +04:00
|
|
|
|
2014-07-29 01:41:45 +04:00
|
|
|
error[0].message.should.eql('Value in [settings.key] cannot be blank.');
|
2014-05-05 17:51:21 +04:00
|
|
|
error[0].type.should.eql('ValidationError');
|
2014-01-15 17:29:23 +04:00
|
|
|
|
|
|
|
when.all([
|
2014-06-05 01:26:03 +04:00
|
|
|
knex('users').select(),
|
|
|
|
knex('posts').select(),
|
|
|
|
knex('settings').select(),
|
|
|
|
knex('tags').select()
|
2014-01-15 17:29:23 +04:00
|
|
|
]).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];
|
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
// we always have 1 user, the owner user we added
|
2014-01-15 17:29:23 +04:00
|
|
|
users.length.should.equal(1, 'There should only be one user');
|
2014-07-21 21:50:04 +04:00
|
|
|
// Nothing should have been imported
|
|
|
|
posts.length.should.equal(0, 'Wrong number of posts');
|
|
|
|
tags.length.should.equal(0, 'no new tags');
|
2014-01-15 17:29:23 +04:00
|
|
|
|
|
|
|
// test settings
|
|
|
|
settings.length.should.be.above(0, 'Wrong number of settings');
|
2014-06-05 01:26:03 +04:00
|
|
|
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('003', 'Wrong database version');
|
2014-01-15 17:29:23 +04:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2014-05-06 00:58:58 +04:00
|
|
|
}).catch(done);
|
2014-01-15 17:29:23 +04:00
|
|
|
});
|
|
|
|
});
|
2014-02-25 00:28:18 +04:00
|
|
|
|
2014-06-05 01:26:03 +04:00
|
|
|
describe('003', function () {
|
2014-02-25 00:28:18 +04:00
|
|
|
|
2014-07-21 21:50:04 +04:00
|
|
|
beforeEach(testUtils.setup('owner', 'settings'));
|
|
|
|
|
|
|
|
should.exist(Importer003);
|
2014-02-25 00:28:18 +04:00
|
|
|
|
2014-07-29 01:41:45 +04:00
|
|
|
it('handles validation errors nicely', function (done) {
|
2014-02-25 00:28:18 +04:00
|
|
|
var exportData;
|
|
|
|
|
2014-07-29 01:41:45 +04:00
|
|
|
testUtils.fixtures.loadExportFixture('broken-validation').then(function (exported) {
|
2014-02-25 00:28:18 +04:00
|
|
|
exportData = exported;
|
2014-06-05 01:26:03 +04:00
|
|
|
return importer('003', exportData);
|
2014-07-29 01:41:45 +04:00
|
|
|
}).catch(function (response) {
|
|
|
|
response.length.should.equal(5);
|
|
|
|
response[0].type.should.equal('ValidationError');
|
|
|
|
response[0].message.should.eql('Value in [posts.title] cannot be blank.');
|
|
|
|
response[1].type.should.equal('ValidationError');
|
|
|
|
response[1].message.should.eql('Value in [posts.slug] cannot be blank.');
|
|
|
|
response[2].type.should.equal('ValidationError');
|
|
|
|
response[2].message.should.eql('Value in [settings.key] cannot be blank.');
|
|
|
|
response[3].type.should.equal('ValidationError');
|
|
|
|
response[3].message.should.eql('Value in [tags.slug] cannot be blank.');
|
|
|
|
response[4].type.should.equal('ValidationError');
|
|
|
|
response[4].message.should.eql('Value in [tags.name] cannot be blank.');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-02-25 00:28:18 +04:00
|
|
|
|
2014-07-29 01:41:45 +04:00
|
|
|
it('handles database errors nicely', function (done) {
|
|
|
|
var exportData;
|
|
|
|
testUtils.fixtures.loadExportFixture('broken-db-errors').then(function (exported) {
|
|
|
|
exportData = exported;
|
|
|
|
return importer('003', exportData);
|
|
|
|
}).catch(function (response) {
|
|
|
|
response.length.should.equal(3);
|
|
|
|
response[0].type.should.equal('DataImportError');
|
|
|
|
response[0].message.should.eql(
|
|
|
|
'Duplicate entry found. Multiple values of "tagging-things" found for tags.slug.'
|
|
|
|
);
|
|
|
|
response[1].type.should.equal('DataImportError');
|
|
|
|
response[1].message.should.eql(
|
|
|
|
'Duplicate entry found. Multiple values of "tagging-things" found for tags.slug.'
|
|
|
|
);
|
|
|
|
response[2].type.should.equal('DataImportError');
|
|
|
|
response[2].message.should.eql(
|
|
|
|
'Duplicate entry found. Multiple values of "test-ghost-post" found for posts.slug.'
|
|
|
|
);
|
2014-02-25 00:28:18 +04:00
|
|
|
done();
|
2014-07-29 01:41:45 +04:00
|
|
|
});
|
2014-02-25 00:28:18 +04:00
|
|
|
});
|
2014-07-28 02:22:00 +04:00
|
|
|
|
|
|
|
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('003', exportData);
|
|
|
|
}).then(function () {
|
|
|
|
done(new Error('Allowed import of invalid tags data'));
|
|
|
|
}).catch(function (response) {
|
|
|
|
response.length.should.equal(4);
|
|
|
|
response[0].type.should.equal('ValidationError');
|
|
|
|
response[0].message.should.eql('Value in [tags.uuid] cannot be blank.');
|
|
|
|
response[1].type.should.equal('ValidationError');
|
|
|
|
response[1].message.should.eql('Validation (isUUID) failed for uuid');
|
|
|
|
response[2].type.should.equal('ValidationError');
|
|
|
|
response[2].message.should.eql('Value in [tags.name] cannot be blank.');
|
|
|
|
response[3].type.should.equal('ValidationError');
|
|
|
|
response[3].message.should.eql('Value in [tags.slug] cannot be blank.');
|
|
|
|
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('003', exportData);
|
|
|
|
}).then(function () {
|
|
|
|
done(new Error('Allowed import of invalid tags data'));
|
|
|
|
}).catch(function (response) {
|
|
|
|
response.length.should.equal(6);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-02-25 00:28:18 +04:00
|
|
|
});
|
2013-09-25 14:30:59 +04:00
|
|
|
});
|