2020-04-29 18:44:27 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const rewire = require('rewire');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const db = require('../../../../core/server/data/db');
|
|
|
|
const common = require('../../../../core/server/lib/common');
|
|
|
|
const exporter = rewire('../../../../core/server/data/exporter');
|
|
|
|
const schema = require('../../../../core/server/data/schema');
|
|
|
|
const models = require('../../../../core/server/models');
|
|
|
|
const schemaTables = Object.keys(schema.tables);
|
2016-03-12 21:54:06 +03:00
|
|
|
|
|
|
|
describe('Exporter', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
let tablesStub;
|
|
|
|
let queryMock;
|
|
|
|
let knexMock;
|
2016-03-12 21:54:06 +03:00
|
|
|
|
2017-03-08 13:26:57 +03:00
|
|
|
before(function () {
|
|
|
|
models.init();
|
|
|
|
});
|
|
|
|
|
2016-03-12 21:54:06 +03:00
|
|
|
afterEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2016-03-12 21:54:06 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('doExport', function () {
|
|
|
|
beforeEach(function () {
|
2017-01-26 15:12:00 +03:00
|
|
|
exporter.__set__('ghostVersion', {
|
2018-08-03 16:14:38 +03:00
|
|
|
full: '2.0.0'
|
2017-01-26 15:12:00 +03:00
|
|
|
});
|
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
tablesStub = sinon.stub(schema.commands, 'getTables').returns(schemaTables);
|
2016-03-12 21:54:06 +03:00
|
|
|
|
|
|
|
queryMock = {
|
2019-01-21 19:53:44 +03:00
|
|
|
whereNot: sinon.stub(),
|
|
|
|
select: sinon.stub()
|
2016-03-12 21:54:06 +03:00
|
|
|
};
|
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
knexMock = sinon.stub().returns(queryMock);
|
2016-03-12 21:54:06 +03:00
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(db, 'knex').get(function () {
|
2017-11-28 20:19:23 +03:00
|
|
|
return knexMock;
|
2017-03-08 13:26:57 +03:00
|
|
|
});
|
2016-03-12 21:54:06 +03:00
|
|
|
});
|
|
|
|
|
2018-07-30 18:21:52 +03:00
|
|
|
it('should try to export all the correct tables (without excluded)', function (done) {
|
2016-03-12 21:54:06 +03:00
|
|
|
// Setup for success
|
|
|
|
queryMock.select.returns(new Promise.resolve({}));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
exporter.doExport().then(function (exportData) {
|
|
|
|
// No tables, less the number of excluded tables
|
2020-04-29 18:44:27 +03:00
|
|
|
const expectedCallCount = schemaTables.length - exporter.EXCLUDED_TABLES.length;
|
2016-03-12 21:54:06 +03:00
|
|
|
|
|
|
|
should.exist(exportData);
|
|
|
|
|
2019-09-16 11:45:55 +03:00
|
|
|
//TODO: Update when 3.0.0 is released
|
2018-08-03 16:14:38 +03:00
|
|
|
exportData.meta.version.should.eql('2.0.0');
|
2017-01-26 15:12:00 +03:00
|
|
|
|
2016-03-14 19:52:22 +03:00
|
|
|
tablesStub.calledOnce.should.be.true();
|
2017-11-28 20:19:23 +03:00
|
|
|
db.knex.called.should.be.true();
|
2016-03-14 19:52:22 +03:00
|
|
|
queryMock.select.called.should.be.true();
|
2018-08-06 18:18:59 +03:00
|
|
|
queryMock.whereNot.calledOnce.should.be.true();
|
2016-03-14 19:52:22 +03:00
|
|
|
|
|
|
|
knexMock.callCount.should.eql(expectedCallCount);
|
|
|
|
queryMock.select.callCount.should.have.eql(expectedCallCount);
|
|
|
|
|
|
|
|
knexMock.getCall(0).args[0].should.eql('posts');
|
2019-09-16 11:45:55 +03:00
|
|
|
knexMock.getCall(1).args[0].should.eql('posts_meta');
|
|
|
|
knexMock.getCall(2).args[0].should.eql('users');
|
|
|
|
knexMock.getCall(3).args[0].should.eql('posts_authors');
|
|
|
|
knexMock.getCall(4).args[0].should.eql('roles');
|
|
|
|
knexMock.getCall(5).args[0].should.eql('roles_users');
|
|
|
|
knexMock.getCall(6).args[0].should.eql('permissions');
|
|
|
|
knexMock.getCall(7).args[0].should.eql('permissions_users');
|
|
|
|
knexMock.getCall(8).args[0].should.eql('permissions_roles');
|
|
|
|
knexMock.getCall(9).args[0].should.eql('permissions_apps');
|
|
|
|
knexMock.getCall(10).args[0].should.eql('settings');
|
|
|
|
knexMock.getCall(11).args[0].should.eql('tags');
|
|
|
|
knexMock.getCall(12).args[0].should.eql('posts_tags');
|
|
|
|
knexMock.getCall(13).args[0].should.eql('apps');
|
|
|
|
knexMock.getCall(14).args[0].should.eql('app_settings');
|
|
|
|
knexMock.getCall(15).args[0].should.eql('app_fields');
|
2016-03-14 19:52:22 +03:00
|
|
|
|
2016-03-12 21:54:06 +03:00
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2019-09-12 16:54:39 +03:00
|
|
|
// SKIPPED: the "extra" clients and client_trusted_domains tables no longer exist
|
|
|
|
it.skip('should try to export all the correct tables with extra tables', function (done) {
|
2018-07-30 18:21:52 +03:00
|
|
|
// Setup for success
|
|
|
|
queryMock.select.returns(new Promise.resolve({}));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
exporter.doExport({include: ['clients', 'client_trusted_domains']}).then(function (exportData) {
|
2018-10-17 14:29:24 +03:00
|
|
|
// all tables, except of the tokes and sessions
|
|
|
|
const expectedCallCount = schemaTables.length - (exporter.EXCLUDED_TABLES.length - 2);
|
2018-07-30 18:21:52 +03:00
|
|
|
|
|
|
|
should.exist(exportData);
|
|
|
|
|
2018-08-03 16:14:38 +03:00
|
|
|
exportData.meta.version.should.eql('2.0.0');
|
2018-07-30 18:21:52 +03:00
|
|
|
|
|
|
|
tablesStub.calledOnce.should.be.true();
|
|
|
|
db.knex.called.should.be.true();
|
|
|
|
queryMock.select.called.should.be.true();
|
|
|
|
|
|
|
|
knexMock.callCount.should.eql(expectedCallCount);
|
|
|
|
queryMock.select.callCount.should.have.eql(expectedCallCount);
|
|
|
|
|
|
|
|
knexMock.getCall(0).args[0].should.eql('posts');
|
2019-09-16 11:45:55 +03:00
|
|
|
knexMock.getCall(1).args[0].should.eql('posts_meta');
|
|
|
|
knexMock.getCall(2).args[0].should.eql('users');
|
|
|
|
knexMock.getCall(3).args[0].should.eql('posts_authors');
|
|
|
|
knexMock.getCall(4).args[0].should.eql('roles');
|
|
|
|
knexMock.getCall(5).args[0].should.eql('roles_users');
|
|
|
|
knexMock.getCall(6).args[0].should.eql('permissions');
|
|
|
|
knexMock.getCall(7).args[0].should.eql('permissions_users');
|
|
|
|
knexMock.getCall(8).args[0].should.eql('permissions_roles');
|
|
|
|
knexMock.getCall(9).args[0].should.eql('permissions_apps');
|
|
|
|
knexMock.getCall(10).args[0].should.eql('settings');
|
|
|
|
knexMock.getCall(11).args[0].should.eql('tags');
|
|
|
|
knexMock.getCall(12).args[0].should.eql('posts_tags');
|
|
|
|
knexMock.getCall(13).args[0].should.eql('apps');
|
|
|
|
knexMock.getCall(14).args[0].should.eql('app_settings');
|
|
|
|
knexMock.getCall(15).args[0].should.eql('app_fields');
|
|
|
|
knexMock.getCall(16).args[0].should.eql('clients');
|
|
|
|
knexMock.getCall(17).args[0].should.eql('client_trusted_domains');
|
2018-07-30 18:21:52 +03:00
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2016-03-12 21:54:06 +03:00
|
|
|
it('should catch and log any errors', function (done) {
|
|
|
|
// Setup for failure
|
2017-11-28 20:19:23 +03:00
|
|
|
queryMock.select.returns(Promise.reject({}));
|
2016-03-12 21:54:06 +03:00
|
|
|
|
|
|
|
// Execute
|
2016-10-06 15:27:35 +03:00
|
|
|
exporter.doExport()
|
|
|
|
.then(function () {
|
|
|
|
done(new Error('expected error for export'));
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
2017-12-12 00:47:46 +03:00
|
|
|
(err instanceof common.errors.DataExportError).should.eql(true);
|
2016-10-06 15:27:35 +03:00
|
|
|
done();
|
|
|
|
});
|
2016-03-12 21:54:06 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('exportFileName', function () {
|
|
|
|
it('should return a correctly structured filename', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const settingsStub = sinon.stub(models.Settings, 'findOne').returns(
|
2017-03-08 13:26:57 +03:00
|
|
|
new Promise.resolve({
|
|
|
|
get: function () {
|
|
|
|
return 'testblog';
|
|
|
|
}
|
|
|
|
})
|
2016-03-12 21:54:06 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
exporter.fileName().then(function (result) {
|
|
|
|
should.exist(result);
|
2016-03-14 19:52:22 +03:00
|
|
|
settingsStub.calledOnce.should.be.true();
|
2019-03-13 23:06:05 +03:00
|
|
|
result.should.match(/^testblog\.ghost\.[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}\.json$/);
|
2016-03-12 21:54:06 +03:00
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a correctly structured filename if settings is empty', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const settingsStub = sinon.stub(models.Settings, 'findOne').returns(
|
2016-03-12 21:54:06 +03:00
|
|
|
new Promise.resolve()
|
|
|
|
);
|
|
|
|
|
|
|
|
exporter.fileName().then(function (result) {
|
|
|
|
should.exist(result);
|
2016-03-14 19:52:22 +03:00
|
|
|
settingsStub.calledOnce.should.be.true();
|
2019-03-13 23:06:05 +03:00
|
|
|
result.should.match(/^ghost\.[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}\.json$/);
|
2016-03-12 21:54:06 +03:00
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a correctly structured filename if settings errors', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const settingsStub = sinon.stub(models.Settings, 'findOne').returns(
|
2016-03-12 21:54:06 +03:00
|
|
|
new Promise.reject()
|
|
|
|
);
|
|
|
|
|
|
|
|
exporter.fileName().then(function (result) {
|
|
|
|
should.exist(result);
|
2016-03-14 19:52:22 +03:00
|
|
|
settingsStub.calledOnce.should.be.true();
|
2019-03-13 23:06:05 +03:00
|
|
|
result.should.match(/^ghost\.[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}\.json$/);
|
2016-03-12 21:54:06 +03:00
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|