2019-02-04 17:16:24 +03:00
|
|
|
const path = require('path');
|
|
|
|
const _ = require('lodash');
|
2019-03-07 14:59:50 +03:00
|
|
|
const os = require('os');
|
2019-02-04 17:16:24 +03:00
|
|
|
const fs = require('fs-extra');
|
2019-03-07 14:59:50 +03:00
|
|
|
const uuid = require('uuid');
|
2019-02-04 17:16:24 +03:00
|
|
|
const should = require('should');
|
|
|
|
const supertest = require('supertest');
|
|
|
|
const sinon = require('sinon');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../../../../core/shared/config');
|
2021-04-27 16:18:04 +03:00
|
|
|
const events = require('../../../../../core/server/lib/common/events');
|
2019-02-04 17:16:24 +03:00
|
|
|
const testUtils = require('../../../../utils');
|
|
|
|
const localUtils = require('./utils');
|
|
|
|
|
|
|
|
let ghost = testUtils.startGhost;
|
|
|
|
let request;
|
|
|
|
let eventsTriggered;
|
|
|
|
|
2019-08-19 14:41:09 +03:00
|
|
|
describe('DB API', function () {
|
2019-08-09 01:53:59 +03:00
|
|
|
let backupKey;
|
|
|
|
let schedulerKey;
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2019-08-19 14:41:09 +03:00
|
|
|
before(function () {
|
2019-02-04 17:16:24 +03:00
|
|
|
return ghost()
|
|
|
|
.then(() => {
|
|
|
|
request = supertest.agent(config.get('url'));
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return localUtils.doAuth(request);
|
|
|
|
})
|
|
|
|
.then(() => {
|
2021-06-01 15:09:12 +03:00
|
|
|
backupKey = _.find(testUtils.getExistingData().apiKeys, {integration: {slug: 'ghost-backup'}});
|
|
|
|
schedulerKey = _.find(testUtils.getExistingData().apiKeys, {integration: {slug: 'ghost-scheduler'}});
|
2019-02-04 17:16:24 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-08-19 14:41:09 +03:00
|
|
|
beforeEach(function () {
|
2019-02-04 17:16:24 +03:00
|
|
|
eventsTriggered = {};
|
|
|
|
|
2020-05-28 19:54:18 +03:00
|
|
|
sinon.stub(events, 'emit').callsFake((eventName, eventObj) => {
|
2019-02-04 17:16:24 +03:00
|
|
|
if (!eventsTriggered[eventName]) {
|
|
|
|
eventsTriggered[eventName] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
eventsTriggered[eventName].push(eventObj);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-08-19 14:41:09 +03:00
|
|
|
afterEach(function () {
|
2019-02-04 17:16:24 +03:00
|
|
|
sinon.restore();
|
|
|
|
});
|
|
|
|
|
2021-03-24 08:39:25 +03:00
|
|
|
it('can export the database with more tables', function () {
|
|
|
|
return request.get(localUtils.API.getApiQuery('db/?include=mobiledoc_revisions'))
|
2019-02-04 17:16:24 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.then((res) => {
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse.db);
|
|
|
|
jsonResponse.db.should.have.length(1);
|
2021-03-25 09:27:49 +03:00
|
|
|
|
|
|
|
// NOTE: 9 default tables + 1 from include parameters
|
|
|
|
Object.keys(jsonResponse.db[0].data).length.should.eql(10);
|
2019-02-04 17:16:24 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-08-19 14:41:09 +03:00
|
|
|
it('can export & import', function () {
|
2019-07-15 09:01:02 +03:00
|
|
|
const exportFolder = path.join(os.tmpdir(), uuid.v4());
|
2019-03-08 08:54:28 +03:00
|
|
|
const exportPath = path.join(exportFolder, 'export.json');
|
|
|
|
|
2019-03-07 14:59:50 +03:00
|
|
|
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);
|
|
|
|
|
|
|
|
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);
|
2019-03-08 08:54:28 +03:00
|
|
|
fs.removeSync(exportFolder);
|
2019-03-07 14:59:50 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-08-19 14:41:09 +03:00
|
|
|
it('import should fail without file', function () {
|
2019-02-04 17:16:24 +03:00
|
|
|
return request.post(localUtils.API.getApiQuery('db/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect('Content-Type', /json/)
|
2019-02-18 21:22:32 +03:00
|
|
|
.expect(422);
|
2019-02-04 17:16:24 +03:00
|
|
|
});
|
|
|
|
|
2019-08-19 14:41:09 +03:00
|
|
|
it('import should fail with unsupported file', function () {
|
2019-02-04 17:16:24 +03:00
|
|
|
return request.post(localUtils.API.getApiQuery('db/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.attach('importfile', path.join(__dirname, '/../../../../utils/fixtures/csv/single-column-with-header.csv'))
|
|
|
|
.expect(415);
|
|
|
|
});
|
|
|
|
|
2019-08-19 14:41:09 +03:00
|
|
|
it('export can be triggered by backup integration', function () {
|
2019-08-09 01:53:59 +03:00
|
|
|
const backupQuery = `?filename=test`;
|
2019-02-04 17:16:24 +03:00
|
|
|
const fsStub = sinon.stub(fs, 'writeFile').resolves();
|
|
|
|
|
|
|
|
return request.post(localUtils.API.getApiQuery(`db/backup${backupQuery}`))
|
2019-08-09 01:53:59 +03:00
|
|
|
.set('Authorization', `Ghost ${localUtils.getValidAdminToken('/v2/admin/', backupKey)}`)
|
|
|
|
.set('Origin', config.get('url'))
|
2019-02-04 17:16:24 +03:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.then((res) => {
|
2019-02-04 17:49:59 +03:00
|
|
|
res.body.should.be.Object();
|
2019-02-04 17:16:24 +03:00
|
|
|
res.body.db[0].filename.should.match(/test\.json/);
|
|
|
|
fsStub.calledOnce.should.eql(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-08-19 14:41:09 +03:00
|
|
|
it('export can not be triggered by integration other than backup', function () {
|
2019-02-04 17:16:24 +03:00
|
|
|
const fsStub = sinon.stub(fs, 'writeFile').resolves();
|
|
|
|
|
2019-08-09 01:53:59 +03:00
|
|
|
return request.post(localUtils.API.getApiQuery(`db/backup`))
|
|
|
|
.set('Authorization', `Ghost ${localUtils.getValidAdminToken('/v2/admin/', schedulerKey)}`)
|
|
|
|
.set('Origin', config.get('url'))
|
2019-02-04 17:16:24 +03:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(403)
|
2019-07-05 14:40:43 +03:00
|
|
|
.then((res) => {
|
2019-02-04 17:16:24 +03:00
|
|
|
should.exist(res.body.errors);
|
2019-02-25 18:19:51 +03:00
|
|
|
res.body.errors[0].type.should.eql('NoPermissionError');
|
2019-02-04 17:16:24 +03:00
|
|
|
fsStub.called.should.eql(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-08-19 14:41:09 +03:00
|
|
|
it('export can be triggered by Admin authentication', function () {
|
2019-02-04 17:16:24 +03:00
|
|
|
const fsStub = sinon.stub(fs, 'writeFile').resolves();
|
|
|
|
|
|
|
|
return request.post(localUtils.API.getApiQuery(`db/backup`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
2019-08-09 01:53:59 +03:00
|
|
|
.expect(200);
|
2019-02-04 17:16:24 +03:00
|
|
|
});
|
|
|
|
});
|