Added proper importer regression tests

refs #10582
This commit is contained in:
kirrg001 2019-03-07 12:59:50 +01:00 committed by Katharina Irrgang
parent 145a9456a2
commit 255b55cab5
3 changed files with 107 additions and 3 deletions

View File

@ -69,13 +69,17 @@ class SettingsImporter extends BaseImporter {
obj.value = JSON.stringify([{url: ''}]);
}
// CASE: export files might contain "0" or "1" for booleans
// CASE: export files might contain "0" or "1" for booleans. Model layer needs real booleans.
// transform "0" to false
// transform "false" to false
// transform "null" to null
if (obj.value === '0' || obj.value === '1') {
obj.value = !!+obj.value;
}
// CASE: export files might contain "false" or "true" for booleans. Model layer needs real booleans.
// transform "false" to false
if (obj.value === 'false' || obj.value === 'true') {
obj.value = obj.value === 'true';
}
});
return super.beforeImport();

View File

@ -1,6 +1,8 @@
const path = require('path');
const _ = require('lodash');
const os = require('os');
const fs = require('fs-extra');
const uuid = require('uuid');
const should = require('should');
const supertest = require('supertest');
const sinon = require('sinon');
@ -65,6 +67,47 @@ describe('DB API', () => {
});
});
it('can export & import', () => {
return request.put(localUtils.API.getApiQuery('settings/'))
.set('Origin', config.get('url'))
.send({
settings: [
{
key: 'is_private',
value: true
}
]
})
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(200)
.then(() => {
return request.get(localUtils.API.getApiQuery('db/'))
.set('Origin', config.get('url'))
.expect('Content-Type', /json/)
.expect(200);
})
.then((res) => {
const jsonResponse = res.body;
should.exist(jsonResponse.db);
const exportFolder = path.join(os.tmpdir(), uuid.v1());
const exportPath = path.join(exportFolder, 'export.json');
fs.ensureDirSync(exportFolder);
fs.writeJSONSync(exportPath, jsonResponse);
return request.post(localUtils.API.getApiQuery('db/'))
.set('Origin', config.get('url'))
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.attach('importfile', exportPath)
.expect(200);
})
.then((res) => {
res.body.problems.length.should.eql(3);
});
});
it('import should fail without file', () => {
return request.post(localUtils.API.getApiQuery('db/'))
.set('Origin', config.get('url'))

View File

@ -869,6 +869,63 @@ describe('Integration: Importer', function () {
});
});
it('does import settings with string booleans', function () {
const exportData = exportedLatestBody().db[0];
exportData.data.settings[0] = testUtils.DataGenerator.forKnex.createSetting({
key: 'amp',
value: 'true'
});
exportData.data.settings[1] = testUtils.DataGenerator.forKnex.createSetting({
key: 'is_private',
value: '0'
});
exportData.data.settings[2] = testUtils.DataGenerator.forKnex.createSetting({
key: 'force_i18n',
value: false
});
return dataImporter.doImport(exportData, importOptions)
.then(function (imported) {
imported.problems.length.should.eql(0);
return models.Settings.findOne(_.merge({key: 'amp'}, testUtils.context.internal));
})
.then(function (result) {
result.attributes.value.should.eql(true);
return models.Settings.findOne(_.merge({key: 'is_private'}, testUtils.context.internal));
})
.then((result) => {
result.attributes.value.should.eql(false);
return models.Settings.findOne(_.merge({key: 'force_i18n'}, testUtils.context.internal));
})
.then((result) => {
result.attributes.value.should.eql(false);
return db
.knex('settings')
.where('key', 'amp');
})
.then((result) => {
result[0].value.should.eql('true');
return db
.knex('settings')
.where('key', 'is_private');
})
.then((result) => {
result[0].value.should.eql('false');
return db
.knex('settings')
.where('key', 'force_i18n');
})
.then((result) => {
result[0].value.should.eql('false');
});
});
it('import comment_id', function () {
const exportData = exportedLatestBody().db[0];