diff --git a/core/server/data/import/001.js b/core/server/data/import/001.js deleted file mode 100644 index 5def4a9871..0000000000 --- a/core/server/data/import/001.js +++ /dev/null @@ -1,8 +0,0 @@ -var Importer000 = require('./000'); - -module.exports = { - Importer001: Importer000, - importData: function (data) { - return new Importer000.importData(data); - } -}; diff --git a/core/server/data/import/002.js b/core/server/data/import/002.js deleted file mode 100644 index b0a113d256..0000000000 --- a/core/server/data/import/002.js +++ /dev/null @@ -1,8 +0,0 @@ -var Importer000 = require('./000'); - -module.exports = { - Importer002: Importer000, - importData: function (data) { - return new Importer000.importData(data); - } -}; diff --git a/core/server/data/import/003.js b/core/server/data/import/003.js deleted file mode 100644 index 349df223e7..0000000000 --- a/core/server/data/import/003.js +++ /dev/null @@ -1,8 +0,0 @@ -var Importer000 = require('./000'); - -module.exports = { - Importer003: Importer000, - importData: function (data) { - return new Importer000.importData(data); - } -}; diff --git a/core/server/data/import/000.js b/core/server/data/import/data-importer.js similarity index 81% rename from core/server/data/import/000.js rename to core/server/data/import/data-importer.js index 571aeac9fb..4b692fb4b7 100644 --- a/core/server/data/import/000.js +++ b/core/server/data/import/data-importer.js @@ -3,37 +3,15 @@ var Promise = require('bluebird'), models = require('../../models'), utils = require('./utils'), - Importer000; + DataImporter; -Importer000 = function () { - _.bindAll(this, 'doImport'); +DataImporter = function () {}; - this.version = '000'; - - this.importFrom = { - '000': this.doImport, - '001': this.doImport, - '002': this.doImport, - '003': this.doImport - }; +DataImporter.prototype.importData = function (data) { + return this.doImport(data); }; -Importer000.prototype.importData = function (data) { - return this.canImport(data) - .then(function (importerFunc) { - return importerFunc(data); - }); -}; - -Importer000.prototype.canImport = function (data) { - if (data.meta && data.meta.version && this.importFrom[data.meta.version]) { - return Promise.resolve(this.importFrom[data.meta.version]); - } - - return Promise.reject('Unsupported version of data: ' + data.meta.version); -}; - -Importer000.prototype.loadUsers = function () { +DataImporter.prototype.loadUsers = function () { var users = {all: {}}; return models.User.findAll({include: ['roles']}).then(function (_users) { @@ -52,11 +30,7 @@ Importer000.prototype.loadUsers = function () { }); }; -// Importer000.prototype.importerFunction = function (t) { -// -// }; - -Importer000.prototype.doUserImport = function (t, tableData, users, errors) { +DataImporter.prototype.doUserImport = function (t, tableData, users, errors) { var userOps = [], imported = []; @@ -89,7 +63,7 @@ Importer000.prototype.doUserImport = function (t, tableData, users, errors) { return Promise.resolve({}); }; -Importer000.prototype.doImport = function (data) { +DataImporter.prototype.doImport = function (data) { var self = this, tableData = data.data, imported = {}, @@ -171,8 +145,8 @@ Importer000.prototype.doImport = function (data) { }; module.exports = { - Importer000: Importer000, + DataImporter: DataImporter, importData: function (data) { - return new Importer000().importData(data); + return new DataImporter().importData(data); } }; diff --git a/core/server/data/import/index.js b/core/server/data/import/index.js index 0deaa54e9c..7aba95f375 100644 --- a/core/server/data/import/index.js +++ b/core/server/data/import/index.js @@ -3,7 +3,7 @@ var Promise = require('bluebird'), validation = require('../validation'), errors = require('../../errors'), uuid = require('node-uuid'), - validator = require('validator'), + importer = require('./data-importer'), tables = require('../schema').tables, validate, handleErrors, @@ -89,14 +89,14 @@ sanitize = function sanitize(data) { }); _.each(tableNames, function (tableName) { - // Sanitize the table data for duplicates and valid uuid values + // Sanitize the table data for duplicates and valid uuid and created_at values var sanitizedTableData = _.transform(data.data[tableName], function (memo, importValues) { var uuidMissing = (!importValues.uuid && tables[tableName].uuid) ? true : false, - uuidMalformed = (importValues.uuid && !validator.isUUID(importValues.uuid)) ? true : false, + uuidMalformed = (importValues.uuid && !validation.validator.isUUID(importValues.uuid)) ? true : false, isDuplicate, problemTag; - // Check for correct UUID and fix if neccessary + // Check for correct UUID and fix if necessary if (uuidMissing || uuidMalformed) { importValues.uuid = uuid.v4(); } @@ -184,25 +184,12 @@ validate = function validate(data) { }); }; -module.exports = function (version, data) { - var importer, - sanitizeResults; - - sanitizeResults = sanitize(data); +module.exports = function (data) { + var sanitizeResults = sanitize(data); data = sanitizeResults.data; return validate(data).then(function () { - try { - importer = require('./' + version); - } catch (ignore) { - // Zero effs given - } - - if (!importer) { - return Promise.reject('No importer found'); - } - return importer.importData(data); }).then(function () { return sanitizeResults; diff --git a/core/server/data/import/utils.js b/core/server/data/import/utils.js index 82d4b2d498..ebe8c615eb 100644 --- a/core/server/data/import/utils.js +++ b/core/server/data/import/utils.js @@ -185,34 +185,25 @@ utils = { var ops = []; tableData = stripProperties(['id'], tableData); - _.each(tableData, function (post) { // Validate minimum post fields if (areEmpty(post, 'title', 'slug', 'markdown')) { return; } - ops.push(function () { - return models.Post.add(post, _.extend(internal, {transacting: transaction, importing: true})) + // The post importer has auto-timestamping disabled + if (!post.created_at) { + post.created_at = Date.now(); + } + + ops.push(models.Post.add(post, _.extend(internal, {transacting: transaction, importing: true})) .catch(function (error) { return Promise.reject({raw: error, model: 'post', data: post}); - }); - }); + }) + ); }); - return Promise.reduce(ops, function (results, op) { - return op().then(function (result) { - results.push(result); - - return results; - }).catch(function (error) { - if (error) { - results.push(error); - } - - return results; - }); - }, []).settle(); + return Promise.settle(ops); }, importUsers: function importUsers(tableData, existingUsers, transaction) { diff --git a/core/server/data/importer/handlers/json.js b/core/server/data/importer/handlers/json.js index ec1b48dd95..7af0fef842 100644 --- a/core/server/data/importer/handlers/json.js +++ b/core/server/data/importer/handlers/json.js @@ -20,7 +20,11 @@ JSONHandler = { importData = JSON.parse(fileData); // if importData follows JSON-API format `{ db: [exportedData] }` - if (_.keys(importData).length === 1 && Array.isArray(importData.db)) { + if (_.keys(importData).length === 1) { + if (!importData.db || !Array.isArray(importData.db)) { + throw new Error('Invalid JSON format, expected `{ db: [exportedData] }`'); + } + importData = importData.db[0]; } diff --git a/core/server/data/importer/importers/data.js b/core/server/data/importer/importers/data.js index 455164e9ff..b5f9ef3947 100644 --- a/core/server/data/importer/importers/data.js +++ b/core/server/data/importer/importers/data.js @@ -8,7 +8,7 @@ DataImporter = { return importData; }, doImport: function (importData) { - return importer('003', importData); + return importer(importData); } }; diff --git a/core/server/data/validation/index.js b/core/server/data/validation/index.js index dd18ddc1b1..9ecd8502c8 100644 --- a/core/server/data/validation/index.js +++ b/core/server/data/validation/index.js @@ -27,9 +27,8 @@ validator.extend('isEmptyOrURL', function (str) { return (_.isEmpty(str) || validator.isURL(str, {require_protocol: false})); }); -// Validation validation against schema attributes -// values are checked against the validation objects -// form schema.js +// Validation against schema attributes +// values are checked against the validation objects from schema.js validateSchema = function (tableName, model) { var columns = _.keys(schema[tableName]), validationErrors = []; @@ -163,6 +162,7 @@ validate = function (value, key, validations) { }; module.exports = { + validator: validator, validateSchema: validateSchema, validateSettings: validateSettings, validateActiveTheme: validateActiveTheme diff --git a/core/test/integration/import_spec.js b/core/test/integration/import_spec.js index 250850d4f0..6514f3ac3b 100644 --- a/core/test/integration/import_spec.js +++ b/core/test/integration/import_spec.js @@ -13,13 +13,9 @@ var testUtils = require('../utils/index'), config = rewire('../../server/config'), 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'), + DataImporter = require('../../server/data/import/data-importer'), knex, sandbox = sinon.sandbox.create(); @@ -45,58 +41,13 @@ describe('Import', function () { knex = config.database.knex; }); - it('resolves 000', function (done) { - var importStub = sandbox.stub(Importer000, 'importData', function () { + it('resolves DataImporter', function (done) { + var importStub = sandbox.stub(DataImporter, 'importData', function () { return Promise.resolve(); }), fakeData = {test: true}; - importer('000', fakeData).then(function () { - importStub.calledWith(fakeData).should.equal(true); - - importStub.restore(); - - done(); - }).catch(done); - }); - - it('resolves 001', function (done) { - var importStub = sandbox.stub(Importer001, 'importData', function () { - return Promise.resolve(); - }), - fakeData = {test: true}; - - importer('001', fakeData).then(function () { - importStub.calledWith(fakeData).should.equal(true); - - importStub.restore(); - - done(); - }).catch(done); - }); - - it('resolves 002', function (done) { - var importStub = sandbox.stub(Importer002, 'importData', function () { - return Promise.resolve(); - }), - fakeData = {test: true}; - - importer('002', fakeData).then(function () { - importStub.calledWith(fakeData).should.equal(true); - - importStub.restore(); - - done(); - }).catch(done); - }); - - it('resolves 003', function (done) { - var importStub = sandbox.stub(Importer003, 'importData', function () { - return Promise.resolve(); - }), - fakeData = {test: true}; - - importer('003', fakeData).then(function () { + importer(fakeData).then(function () { importStub.calledWith(fakeData).should.equal(true); importStub.restore(); @@ -117,7 +68,7 @@ describe('Import', function () { testUtils.fixtures.loadExportFixture('export-003').then(function (exported) { exportData = exported; - return importer('003', exportData); + return importer(exportData); }).then(function (importResult) { should.exist(importResult); should.exist(importResult.data); @@ -132,7 +83,7 @@ describe('Import', function () { testUtils.fixtures.loadExportFixture('export-003-duplicate-posts').then(function (exported) { exportData = exported; - return importer('003', exportData); + return importer(exportData); }).then(function (importResult) { should.exist(importResult.data.data.posts); @@ -149,7 +100,7 @@ describe('Import', function () { testUtils.fixtures.loadExportFixture('export-003-duplicate-tags').then(function (exported) { exportData = exported; - return importer('003', exportData); + return importer(exportData); }).then(function (importResult) { should.exist(importResult.data.data.tags); should.exist(importResult.data.data.posts_tags); @@ -170,24 +121,21 @@ describe('Import', function () { }); }); - describe('000', function () { + describe('DataImporter', function () { before(function () { knex = config.database.knex; }); beforeEach(testUtils.setup('roles', 'owner', 'settings')); - should.exist(Importer000); + should.exist(DataImporter); it('imports data from 000', function (done) { - var exportData, - versioningStub = sandbox.stub(versioning, 'getDatabaseVersion', function () { - return Promise.resolve('000'); - }); + var exportData; testUtils.fixtures.loadExportFixture('export-000').then(function (exported) { exportData = exported; - return importer('000', exportData); + return importer(exportData); }).then(function () { // Grab the data from tables return Promise.all([ @@ -218,22 +166,11 @@ describe('Import', function () { // test tags tags.length.should.equal(exportData.data.tags.length, 'no new tags'); - versioningStub.restore(); - done(); }).catch(done); }); - }); - describe('001', function () { - before(function () { - knex = config.database.knex; - }); - beforeEach(testUtils.setup('roles', 'owner', 'settings')); - - should.exist(Importer001); - - it('safely imports data from 001', function (done) { + it('safely imports data, from 001', function (done) { var exportData, timestamp = 1349928000000; @@ -245,7 +182,7 @@ describe('Import', function () { exportData.data.posts[0].updated_at = timestamp; exportData.data.posts[0].published_at = timestamp; - return importer('001', exportData); + return importer(exportData); }).then(function () { // Grab the data from tables return Promise.all([ @@ -315,7 +252,7 @@ describe('Import', function () { // change title to 151 characters exportData.data.posts[0].title = new Array(152).join('a'); exportData.data.posts[0].tags = 'Tag'; - return importer('001', exportData); + return importer(exportData); }).then(function () { (1).should.eql(0, 'Data import should not resolve promise.'); }, function (error) { @@ -360,7 +297,7 @@ describe('Import', function () { exportData = exported; // change to blank settings key exportData.data.settings[3].key = null; - return importer('001', exportData); + return importer(exportData); }).then(function () { (1).should.eql(0, 'Data import should not resolve promise.'); }, function (error) { @@ -405,8 +342,6 @@ describe('Import', function () { }); beforeEach(testUtils.setup('roles', 'owner', 'settings')); - should.exist(Importer002); - it('safely imports data from 002', function (done) { var exportData, timestamp = 1349928000000; @@ -419,7 +354,7 @@ describe('Import', function () { exportData.data.posts[0].updated_at = timestamp; exportData.data.posts[0].published_at = timestamp; - return importer('002', exportData); + return importer(exportData); }).then(function () { // Grab the data from tables return Promise.all([ @@ -489,7 +424,7 @@ describe('Import', function () { // change title to 151 characters exportData.data.posts[0].title = new Array(152).join('a'); exportData.data.posts[0].tags = 'Tag'; - return importer('002', exportData); + return importer(exportData); }).then(function () { (1).should.eql(0, 'Data import should not resolve promise.'); }, function (error) { @@ -533,7 +468,7 @@ describe('Import', function () { exportData = exported; // change to blank settings key exportData.data.settings[3].key = null; - return importer('002', exportData); + return importer(exportData); }).then(function () { (1).should.eql(0, 'Data import should not resolve promise.'); }, function (error) { @@ -577,14 +512,12 @@ describe('Import', function () { }); beforeEach(testUtils.setup('roles', 'owner', 'settings')); - should.exist(Importer003); - it('safely imports data from 003 (single user)', function (done) { var exportData; testUtils.fixtures.loadExportFixture('export-003').then(function (exported) { exportData = exported; - return importer('003', exportData); + return importer(exportData); }).then(function () { // Grab the data from tables return Promise.all([ @@ -629,7 +562,7 @@ describe('Import', function () { testUtils.fixtures.loadExportFixture('export-003-badValidation').then(function (exported) { exportData = exported; - return importer('003', exportData); + return importer(exportData); }).then(function () { done(new Error('Allowed import of duplicate data')); }).catch(function (response) { @@ -652,7 +585,7 @@ describe('Import', function () { var exportData; testUtils.fixtures.loadExportFixture('export-003-dbErrors').then(function (exported) { exportData = exported; - return importer('003', exportData); + return importer(exportData); }).then(function () { done(new Error('Allowed import of duplicate data')); }).catch(function (response) { @@ -668,7 +601,7 @@ describe('Import', function () { testUtils.fixtures.loadExportFixture('export-003-mu-unknownAuthor').then(function (exported) { exportData = exported; - return importer('003', exportData); + return importer(exportData); }).then(function () { done(new Error('Allowed import of unknown author')); }).catch(function (response) { @@ -689,7 +622,7 @@ describe('Import', function () { exportData.data.tags.length.should.be.above(1); exportData.data.posts_tags.length.should.be.above(1); - return importer('003', exportData); + return importer(exportData); }).then(function () { done(new Error('Allowed import of invalid tags data')); }).catch(function (response) { @@ -710,7 +643,7 @@ describe('Import', function () { exportData.data.posts.length.should.be.above(1); - return importer('003', exportData); + return importer(exportData); }).then(function () { done(new Error('Allowed import of invalid tags data')); }).catch(function (response) { @@ -727,7 +660,7 @@ describe('Import', function () { exportData.data.posts.length.should.be.above(0); - return importer('003', exportData); + return importer(exportData); }).then(function () { // Grab the data from tables return knex('posts').select(); @@ -748,697 +681,693 @@ describe('Import', function () { describe('Import (new test structure)', function () { before(testUtils.teardown); - describe('003', function () { + after(testUtils.teardown); + + describe('imports multi user data onto blank ghost install', function () { + var exportData; + + before(function doImport(done) { + knex = config.database.knex; + + testUtils.initFixtures('roles', 'owner', 'settings').then(function () { + return testUtils.fixtures.loadExportFixture('export-003-mu'); + }).then(function (exported) { + exportData = exported; + return importer(exportData); + }).then(function () { + done(); + }).catch(done); + }); after(testUtils.teardown); - should.exist(Importer003); + it('gets the right data', function (done) { + var fetchImported = Promise.join( + knex('posts').select(), + knex('settings').select(), + knex('tags').select() + ); - describe('imports multi user data onto blank ghost install', function () { - var exportData; + fetchImported.then(function (importedData) { + var posts, + settings, + tags, + post1, + post2, + post3; - before(function doImport(done) { - knex = config.database.knex; + // General data checks + should.exist(importedData); + importedData.length.should.equal(3, 'Did not get data successfully'); - testUtils.initFixtures('roles', 'owner', 'settings').then(function () { - return testUtils.fixtures.loadExportFixture('export-003-mu'); - }).then(function (exported) { - exportData = exported; - return importer('003', exportData); - }).then(function () { - done(); - }).catch(done); - }); - after(testUtils.teardown); + // Test posts, settings and tags + posts = importedData[0]; + settings = importedData[1]; + tags = importedData[2]; - it('gets the right data', function (done) { - var fetchImported = Promise.join( - knex('posts').select(), - knex('settings').select(), - knex('tags').select() - ); + 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; + }); - fetchImported.then(function (importedData) { - var posts, - settings, - tags, - post1, - post2, - post3; + // 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); - // General data checks - should.exist(importedData); - importedData.length.should.equal(3, 'Did not get data successfully'); + // test tags + tags.length.should.equal(3, 'should be 3 tags'); - // Test posts, settings and tags - posts = importedData[0]; - settings = importedData[1]; - tags = importedData[2]; + // test settings + settings.length.should.be.above(0, 'Wrong number of settings'); + _.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('003', 'Wrong database version'); - 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'); - _.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('003', '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(4, 'Original user should be an owner'); - } - if (roleUser.user_id === user2.id) { - roleUser.role_id.should.equal(1, 'Josephine should be an admin'); - } - if (roleUser.user_id === user3.id) { - roleUser.role_id.should.equal(3, '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); - }); + done(); + }).catch(done); }); - describe('imports multi user data with no owner onto blank ghost install', function () { - var exportData; + it('imports users with correct roles and status', function (done) { + var fetchImported = Promise.join( + knex('users').select(), + knex('roles_users').select() + ); - before(function doImport(done) { - knex = config.database.knex; + fetchImported.then(function (importedData) { + var user1, + user2, + user3, + users, + rolesUsers; - testUtils.initFixtures('roles', 'owner', 'settings').then(function () { - return testUtils.fixtures.loadExportFixture('export-003-mu-noOwner'); - }).then(function (exported) { - exportData = exported; - return importer('003', exportData); - }).then(function () { - done(); - }).catch(done); - }); - after(testUtils.teardown); + // General data checks + should.exist(importedData); + importedData.length.should.equal(2, 'Did not get data successfully'); - it('gets the right data', function (done) { - var fetchImported = Promise.join( - knex('posts').select(), - knex('settings').select(), - knex('tags').select() - ); + // Test the users and roles + users = importedData[0]; + rolesUsers = importedData[1]; - fetchImported.then(function (importedData) { - var posts, - settings, - tags, - post1, - post2, - post3; + // 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'); - // General data checks - should.exist(importedData); - importedData.length.should.equal(3, 'Did not get data successfully'); + // 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; + }); - // Test posts, settings and tags - posts = importedData[0]; - settings = importedData[1]; - tags = importedData[2]; + 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); - 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; - }); + // Newly created users should have a status of locked + user2.status.should.equal('locked'); + user3.status.should.equal('locked'); - // 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); + // 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); - // test tags - tags.length.should.equal(3, 'should be 3 tags'); + rolesUsers.length.should.equal(3, 'There should be 3 role relations'); - // test settings - settings.length.should.be.above(0, 'Wrong number of settings'); - _.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('003', 'Wrong database version'); + _.each(rolesUsers, function (roleUser) { + if (roleUser.user_id === user1.id) { + roleUser.role_id.should.equal(4, 'Original user should be an owner'); + } + if (roleUser.user_id === user2.id) { + roleUser.role_id.should.equal(1, 'Josephine should be an admin'); + } + if (roleUser.user_id === user3.id) { + roleUser.role_id.should.equal(3, 'Smith should be an author by default'); + } + }); - 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(4, 'Original user should be an owner'); - } - if (roleUser.user_id === user2.id) { - roleUser.role_id.should.equal(1, 'Josephine should be an admin'); - } - if (roleUser.user_id === user3.id) { - roleUser.role_id.should.equal(3, '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); - }); + done(); + }).catch(done); }); - describe('imports multi user data onto existing data', function () { - var exportData; + it('imports posts & tags with correct authors, owners etc', function (done) { + var fetchImported = Promise.join( + knex('users').select(), + knex('posts').select(), + knex('tags').select() + ); - before(function doImport(done) { - knex = config.database.knex; + fetchImported.then(function (importedData) { + var users, user1, user2, user3, + posts, post1, post2, post3, + tags, tag1, tag2, tag3; - // 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('003', exportData); - }).then(function () { - done(); - }).catch(done); - }); - after(testUtils.teardown); + // General data checks + should.exist(importedData); + importedData.length.should.equal(3, 'Did not get data successfully'); - it('gets the right data', function (done) { - var fetchImported = Promise.join( - knex('posts').select(), - knex('settings').select(), - knex('tags').select() + // 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) { + knex = config.database.knex; + + testUtils.initFixtures('roles', 'owner', 'settings').then(function () { + return testUtils.fixtures.loadExportFixture('export-003-mu-noOwner'); + }).then(function (exported) { + exportData = exported; + return importer(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'); + _.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('003', '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(4, 'Original user should be an owner'); + } + if (roleUser.user_id === user2.id) { + roleUser.role_id.should.equal(1, 'Josephine should be an admin'); + } + if (roleUser.user_id === user3.id) { + roleUser.role_id.should.equal(3, '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) { + knex = config.database.knex; + + // 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(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' ); - fetchImported.then(function (importedData) { - var posts, - settings, - tags, - post1, - post2, - post3; + posts[0].title.should.equal(testUtils.DataGenerator.Content.posts[0].title); - // General data checks - should.exist(importedData); - importedData.length.should.equal(3, 'Did not get data successfully'); + 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 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'); - _.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('003', '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() + // test tags + tags.length.should.equal( + (exportData.data.tags.length + testUtils.DataGenerator.Content.tags.length), + 'Wrong number of tags' ); - fetchImported.then(function (importedData) { - var ownerUser, - newUser, - existingUser, - users, - rolesUsers; + tags[0].name.should.equal(testUtils.DataGenerator.Content.tags[0].name); - // General data checks - should.exist(importedData); - importedData.length.should.equal(2, 'Did not get data successfully'); + // test settings + settings.length.should.be.above(0, 'Wrong number of settings'); + _.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('003', 'Wrong database version'); - // Test the users and roles - users = importedData[0]; - rolesUsers = importedData[1]; + done(); + }).catch(done); + }); - // 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'); + it('imports users with correct roles and status', function (done) { + var fetchImported = Promise.join( + knex('users').select(), + knex('roles_users').select() + ); - // 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; - }); + fetchImported.then(function (importedData) { + var ownerUser, + newUser, + existingUser, + users, + rolesUsers; - 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); + // General data checks + should.exist(importedData); + importedData.length.should.equal(2, 'Did not get data successfully'); - // 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'); + // Test the users and roles + users = importedData[0]; + rolesUsers = importedData[1]; - // 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); + // 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'); + // 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; + }); - _.each(rolesUsers, function (roleUser) { - if (roleUser.user_id === ownerUser.id) { - roleUser.role_id.should.equal(4, 'Original user should be an owner'); - } - if (roleUser.user_id === newUser.id) { - roleUser.role_id.should.equal(1, 'New user should be an admin'); - } - if (roleUser.user_id === existingUser.id) { - roleUser.role_id.should.equal(1, 'Existing user was an admin'); - } - }); + 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); - done(); - }).catch(done); - }); + // 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'); - it('imports posts & tags with correct authors, owners etc', function (done) { - var fetchImported = Promise.join( - knex('users').select(), - knex('posts').select(), - knex('tags').select() - ); + // 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); - fetchImported.then(function (importedData) { - var users, ownerUser, newUser, existingUser, - posts, post1, post2, post3, - tags, tag1, tag2, tag3; + rolesUsers.length.should.equal(5, 'There should be 5 role relations'); - // General data checks - should.exist(importedData); - importedData.length.should.equal(3, 'Did not get data successfully'); + _.each(rolesUsers, function (roleUser) { + if (roleUser.user_id === ownerUser.id) { + roleUser.role_id.should.equal(4, 'Original user should be an owner'); + } + if (roleUser.user_id === newUser.id) { + roleUser.role_id.should.equal(1, 'New user should be an admin'); + } + if (roleUser.user_id === existingUser.id) { + roleUser.role_id.should.equal(1, 'Existing user was an admin'); + } + }); - // Test the users and roles - users = importedData[0]; - posts = importedData[1]; - tags = importedData[2]; + done(); + }).catch(done); + }); - // 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; - }); + it('imports posts & tags with correct authors, owners etc', function (done) { + var fetchImported = Promise.join( + knex('users').select(), + knex('posts').select(), + knex('tags').select() + ); - // 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); + fetchImported.then(function (importedData) { + var users, ownerUser, newUser, existingUser, + posts, post1, post2, post3, + tags, tag1, tag2, tag3; - // 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); + // General data checks + should.exist(importedData); + importedData.length.should.equal(3, 'Did not get data successfully'); - // 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); + // Test the users and roles + users = importedData[0]; + posts = importedData[1]; + tags = importedData[2]; - // 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); + // 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; + }); - // 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); + // 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); - // 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); + // 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); - done(); - }).catch(done); - }); + // 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); }); }); }); diff --git a/core/test/unit/importer_spec.js b/core/test/unit/importer_spec.js index 5339903c37..ff7978b88e 100644 --- a/core/test/unit/importer_spec.js +++ b/core/test/unit/importer_spec.js @@ -1,9 +1,10 @@ /*globals describe, afterEach, it*/ /*jshint expr:true*/ -var should = require('should'), - sinon = require('sinon'), - Promise = require('bluebird'), - _ = require('lodash'), +var should = require('should'), + sinon = require('sinon'), + Promise = require('bluebird'), + _ = require('lodash'), + testUtils = require('../utils'), // Stuff we are testing ImportManager = require('../../server/data/importer'), @@ -179,6 +180,32 @@ describe('Importer', function () { JSONHandler.types.should.containEql('application/json'); JSONHandler.loadFile.should.be.instanceof(Function); }); + + it('correctly handles a valid db api wrapper', function (done) { + var file = [{ + path: testUtils.fixtures.getExportFixturePath('export-003-api-wrapper'), + name: 'export-003-api-wrapper.json' + }]; + JSONHandler.loadFile(file).then(function (result) { + _.keys(result).should.containEql('meta'); + _.keys(result).should.containEql('data'); + done(); + }); + }); + + it('correctly errors when given a bad db api wrapper', function (done) { + var file = [{ + path: testUtils.fixtures.getExportFixturePath('export-003-api-wrapper-bad'), + name: 'export-003-api-wrapper-bad.json' + }]; + + JSONHandler.loadFile(file).then(function () { + done(new Error('Didn\'t error for bad db api wrapper')); + }).catch(function (response) { + response.type.should.equal('BadRequestError'); + done(); + }); + }); }); describe('DataImporter', function () { diff --git a/core/test/utils/fixtures/export-003-api-wrapper-bad.json b/core/test/utils/fixtures/export-003-api-wrapper-bad.json new file mode 100644 index 0000000000..23db3e559c --- /dev/null +++ b/core/test/utils/fixtures/export-003-api-wrapper-bad.json @@ -0,0 +1,31 @@ +{"asdadas":[{ + "meta": { + "exported_on": 1388318311015, + "version": "003" + }, + "data": { + "posts": [ + { + "id": 1, + "title": "Welcome to Ghost", + "slug": "welcome-to-ghost", + "markdown": "You're live! Nice.", + "html": "
You're live! Nice.
", + "image": null, + "featured": 0, + "page": 0, + "status": "published", + "language": "en_US", + "meta_title": null, + "meta_description": null, + "author_id": 2, + "created_at": 1388318310782, + "created_by": 1, + "updated_at": 1388318310782, + "updated_by": 1, + "published_at": 1388318310783, + "published_by": 2 + } + ] + } +}]} \ No newline at end of file diff --git a/core/test/utils/fixtures/export-003-api-wrapper.json b/core/test/utils/fixtures/export-003-api-wrapper.json new file mode 100644 index 0000000000..d39a34e691 --- /dev/null +++ b/core/test/utils/fixtures/export-003-api-wrapper.json @@ -0,0 +1,31 @@ +{"db":[{ + "meta": { + "exported_on": 1388318311015, + "version": "003" + }, + "data": { + "posts": [ + { + "id": 1, + "title": "Welcome to Ghost", + "slug": "welcome-to-ghost", + "markdown": "You're live! Nice.", + "html": "You're live! Nice.
", + "image": null, + "featured": 0, + "page": 0, + "status": "published", + "language": "en_US", + "meta_title": null, + "meta_description": null, + "author_id": 2, + "created_at": 1388318310782, + "created_by": 1, + "updated_at": 1388318310782, + "updated_by": 1, + "published_at": 1388318310783, + "published_by": 2 + } + ] + } +}]} \ No newline at end of file diff --git a/core/test/utils/fixtures/export-003-minimal.json b/core/test/utils/fixtures/export-003-minimal.json new file mode 100644 index 0000000000..c0a796fe69 --- /dev/null +++ b/core/test/utils/fixtures/export-003-minimal.json @@ -0,0 +1,15 @@ +{ + "meta": { + "exported_on": 1388318311015, + "version": "003" + }, + "data": { + "posts": [ + { + "title": "Welcome to Ghost", + "slug": "welcome-to-ghost", + "markdown": "You're live! Nice." + } + ] + } +} \ No newline at end of file diff --git a/core/test/utils/index.js b/core/test/utils/index.js index 94a8798081..332a1640a5 100644 --- a/core/test/utils/index.js +++ b/core/test/utils/index.js @@ -280,11 +280,15 @@ fixtures = { }); }, + getExportFixturePath: function (filename) { + return path.resolve(__dirname + '/fixtures/' + filename + '.json'); + }, + loadExportFixture: function loadExportFixture(filename) { - var filepath = path.resolve(__dirname + '/fixtures/' + filename + '.json'), + var filePath = this.getExportFixturePath(filename), readFile = Promise.promisify(fs.readFile); - return readFile(filepath).then(function (fileContents) { + return readFile(filePath).then(function (fileContents) { var data; // Parse the json data