2017-03-08 13:26:57 +03:00
|
|
|
var should = require('should'),
|
|
|
|
sinon = require('sinon'),
|
|
|
|
rewire = require('rewire'),
|
|
|
|
Promise = require('bluebird'),
|
|
|
|
db = require('../../server/data/db'),
|
|
|
|
errors = require('../../server/errors'),
|
|
|
|
exporter = rewire('../../server/data/export'),
|
|
|
|
schema = require('../../server/data/schema'),
|
|
|
|
models = require('../../server/models'),
|
2016-03-12 21:54:06 +03:00
|
|
|
schemaTables = Object.keys(schema.tables),
|
2017-03-21 11:24:11 +03:00
|
|
|
|
2016-03-12 21:54:06 +03:00
|
|
|
sandbox = sinon.sandbox.create();
|
|
|
|
|
|
|
|
describe('Exporter', function () {
|
2017-11-28 20:19:23 +03:00
|
|
|
var tablesStub, queryMock, 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 () {
|
|
|
|
sandbox.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('doExport', function () {
|
|
|
|
beforeEach(function () {
|
2017-01-26 15:12:00 +03:00
|
|
|
exporter.__set__('ghostVersion', {
|
|
|
|
full: '1.0.0'
|
|
|
|
});
|
|
|
|
|
2016-03-12 21:54:06 +03:00
|
|
|
tablesStub = sandbox.stub(schema.commands, 'getTables').returns(schemaTables);
|
|
|
|
|
|
|
|
queryMock = {
|
|
|
|
select: sandbox.stub()
|
|
|
|
};
|
|
|
|
|
|
|
|
knexMock = sandbox.stub().returns(queryMock);
|
|
|
|
|
2017-11-28 20:19:23 +03:00
|
|
|
sandbox.stub(db, 'knex').get(function () {
|
|
|
|
return knexMock;
|
2017-03-08 13:26:57 +03:00
|
|
|
});
|
2016-03-12 21:54:06 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should try to export all the correct tables', function (done) {
|
|
|
|
// Setup for success
|
|
|
|
queryMock.select.returns(new Promise.resolve({}));
|
|
|
|
|
|
|
|
// Execute
|
|
|
|
exporter.doExport().then(function (exportData) {
|
|
|
|
// No tables, less the number of excluded tables
|
|
|
|
var expectedCallCount = schemaTables.length - 4;
|
|
|
|
|
|
|
|
should.exist(exportData);
|
|
|
|
|
2017-01-26 15:12:00 +03:00
|
|
|
exportData.meta.version.should.eql('1.0.0');
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
knexMock.callCount.should.eql(expectedCallCount);
|
|
|
|
queryMock.select.callCount.should.have.eql(expectedCallCount);
|
|
|
|
|
|
|
|
knexMock.getCall(0).args[0].should.eql('posts');
|
|
|
|
knexMock.getCall(1).args[0].should.eql('users');
|
|
|
|
knexMock.getCall(2).args[0].should.eql('roles');
|
|
|
|
knexMock.getCall(3).args[0].should.eql('roles_users');
|
|
|
|
knexMock.getCall(4).args[0].should.eql('permissions');
|
|
|
|
knexMock.getCall(5).args[0].should.eql('permissions_users');
|
|
|
|
knexMock.getCall(6).args[0].should.eql('permissions_roles');
|
|
|
|
knexMock.getCall(7).args[0].should.eql('permissions_apps');
|
|
|
|
knexMock.getCall(8).args[0].should.eql('settings');
|
|
|
|
knexMock.getCall(9).args[0].should.eql('tags');
|
|
|
|
knexMock.getCall(10).args[0].should.eql('posts_tags');
|
|
|
|
knexMock.getCall(11).args[0].should.eql('apps');
|
|
|
|
knexMock.getCall(12).args[0].should.eql('app_settings');
|
|
|
|
knexMock.getCall(13).args[0].should.eql('app_fields');
|
|
|
|
|
|
|
|
knexMock.calledWith('clients').should.be.false();
|
|
|
|
knexMock.calledWith('client_trusted_domains').should.be.false();
|
|
|
|
knexMock.calledWith('refreshtokens').should.be.false();
|
|
|
|
knexMock.calledWith('accesstokens').should.be.false();
|
2016-03-12 21:54:06 +03:00
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
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) {
|
|
|
|
(err instanceof errors.DataExportError).should.eql(true);
|
|
|
|
done();
|
|
|
|
});
|
2016-03-12 21:54:06 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('exportFileName', function () {
|
|
|
|
it('should return a correctly structured filename', function (done) {
|
2017-03-08 13:26:57 +03:00
|
|
|
var settingsStub = sandbox.stub(models.Settings, 'findOne').returns(
|
|
|
|
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();
|
2016-03-12 21:54:06 +03:00
|
|
|
result.should.match(/^testblog\.ghost\.[0-9]{4}-[0-9]{2}-[0-9]{2}\.json$/);
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a correctly structured filename if settings is empty', function (done) {
|
2017-03-08 13:26:57 +03:00
|
|
|
var settingsStub = sandbox.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();
|
2016-03-12 21:54:06 +03:00
|
|
|
result.should.match(/^ghost\.[0-9]{4}-[0-9]{2}-[0-9]{2}\.json$/);
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a correctly structured filename if settings errors', function (done) {
|
2017-03-08 13:26:57 +03:00
|
|
|
var settingsStub = sandbox.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();
|
2016-03-12 21:54:06 +03:00
|
|
|
result.should.match(/^ghost\.[0-9]{4}-[0-9]{2}-[0-9]{2}\.json$/);
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|