2017-03-21 11:24:11 +03:00
|
|
|
var should = require('should'), // jshint ignore:line
|
|
|
|
sinon = require('sinon'),
|
2016-07-15 19:22:41 +03:00
|
|
|
rewire = require('rewire'),
|
|
|
|
_ = require('lodash'),
|
|
|
|
Promise = require('bluebird'),
|
|
|
|
crypto = require('crypto'),
|
|
|
|
fs = require('fs'),
|
2016-07-14 13:59:42 +03:00
|
|
|
models = require('../../server/models'),
|
|
|
|
exporter = require('../../server/data/export'),
|
|
|
|
schema = require('../../server/data/schema'),
|
2017-01-25 16:47:49 +03:00
|
|
|
backupDatabase = rewire('../../server/data/db/backup'),
|
|
|
|
fixtures = require('../../server/data/schema/fixtures'),
|
2014-12-20 13:16:29 +03:00
|
|
|
|
2017-03-21 11:24:11 +03:00
|
|
|
sandbox = sinon.sandbox.create();
|
2017-02-14 15:41:28 +03:00
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
// Check version integrity
|
|
|
|
// These tests exist to ensure that developers are not able to modify the database schema, or permissions fixtures
|
|
|
|
// without knowing that they also need to update the default database version,
|
|
|
|
// both of which are required for migrations to work properly.
|
2017-01-25 16:47:49 +03:00
|
|
|
describe('DB version integrity', function () {
|
2016-03-21 15:44:23 +03:00
|
|
|
// Only these variables should need updating
|
2017-10-10 15:36:35 +03:00
|
|
|
var currentSchemaHash = '0de1eaa8bc79046a9f43927917c294c3',
|
2017-11-14 13:23:38 +03:00
|
|
|
currentFixturesHash = 'e2c71e808c3d33660c498d164639dc8c';
|
2016-03-21 15:44:23 +03:00
|
|
|
|
|
|
|
// If this test is failing, then it is likely a change has been made that requires a DB version bump,
|
|
|
|
// and the values above will need updating as confirmation
|
|
|
|
it('should not change without fixing this test', function () {
|
|
|
|
var tablesNoValidation = _.cloneDeep(schema.tables),
|
|
|
|
schemaHash,
|
|
|
|
fixturesHash;
|
|
|
|
|
|
|
|
_.each(tablesNoValidation, function (table) {
|
|
|
|
return _.each(table, function (column, name) {
|
|
|
|
table[name] = _.omit(column, 'validations');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-04-05 22:16:28 +03:00
|
|
|
schemaHash = crypto.createHash('md5').update(JSON.stringify(tablesNoValidation), 'binary').digest('hex');
|
|
|
|
fixturesHash = crypto.createHash('md5').update(JSON.stringify(fixtures), 'binary').digest('hex');
|
2016-03-21 15:44:23 +03:00
|
|
|
|
|
|
|
schemaHash.should.eql(currentSchemaHash);
|
|
|
|
fixturesHash.should.eql(currentFixturesHash);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-01-25 16:47:49 +03:00
|
|
|
describe('Migrations', function () {
|
2016-07-14 13:59:42 +03:00
|
|
|
before(function () {
|
|
|
|
models.init();
|
|
|
|
});
|
|
|
|
|
2016-02-25 10:10:36 +03:00
|
|
|
afterEach(function () {
|
|
|
|
sandbox.restore();
|
2014-12-20 13:16:29 +03:00
|
|
|
});
|
2016-02-25 10:10:36 +03:00
|
|
|
|
2016-03-13 23:49:30 +03:00
|
|
|
describe('Backup', function () {
|
2016-03-23 18:02:40 +03:00
|
|
|
var exportStub, filenameStub, fsStub;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
exportStub = sandbox.stub(exporter, 'doExport').returns(new Promise.resolve());
|
|
|
|
filenameStub = sandbox.stub(exporter, 'fileName').returns(new Promise.resolve('test'));
|
|
|
|
fsStub = sandbox.stub(fs, 'writeFile').yields();
|
|
|
|
});
|
2016-03-13 23:49:30 +03:00
|
|
|
|
2016-03-23 18:02:40 +03:00
|
|
|
it('should create a backup JSON file', function (done) {
|
2017-01-25 16:47:49 +03:00
|
|
|
backupDatabase().then(function () {
|
2016-03-13 23:49:30 +03:00
|
|
|
exportStub.calledOnce.should.be.true();
|
|
|
|
filenameStub.calledOnce.should.be.true();
|
|
|
|
fsStub.calledOnce.should.be.true();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|
2014-12-20 13:16:29 +03:00
|
|
|
});
|