2018-12-17 16:45:09 +03:00
|
|
|
const path = require('path');
|
|
|
|
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');
|
2020-05-28 19:54:18 +03:00
|
|
|
const {events} = require('../../../core/server/lib/common');
|
2019-09-20 18:02:45 +03:00
|
|
|
const testUtils = require('../../utils');
|
2021-03-12 09:42:33 +03:00
|
|
|
const {exportedBodyLatest} = require('../../utils/fixtures/export/body-generator');
|
2018-12-17 16:45:09 +03:00
|
|
|
const localUtils = require('./utils');
|
|
|
|
|
2019-08-19 14:41:09 +03:00
|
|
|
describe('DB API', function () {
|
2020-11-30 17:25:22 +03:00
|
|
|
let request;
|
|
|
|
let eventsTriggered;
|
|
|
|
|
2021-03-24 05:44:56 +03:00
|
|
|
// NOTE: has to be beforeEach otherwise imports overlap and cause duplicate entry errors
|
|
|
|
beforeEach(async function () {
|
2020-11-30 17:25:22 +03:00
|
|
|
await testUtils.startGhost();
|
|
|
|
request = supertest.agent(config.get('url'));
|
|
|
|
await localUtils.doAuth(request);
|
2018-12-17 16:45:09 +03:00
|
|
|
});
|
|
|
|
|
2019-08-19 14:41:09 +03:00
|
|
|
beforeEach(function () {
|
2018-12-17 16:45:09 +03:00
|
|
|
eventsTriggered = {};
|
|
|
|
|
2020-05-28 19:54:18 +03:00
|
|
|
sinon.stub(events, 'emit').callsFake((eventName, eventObj) => {
|
2018-12-17 16:45:09 +03:00
|
|
|
if (!eventsTriggered[eventName]) {
|
|
|
|
eventsTriggered[eventName] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
eventsTriggered[eventName].push(eventObj);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-08-19 14:41:09 +03:00
|
|
|
afterEach(function () {
|
2021-03-24 05:44:56 +03:00
|
|
|
testUtils.stopGhost();
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2018-12-17 16:45:09 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can export a JSON database', async function () {
|
|
|
|
const res = await request.get(localUtils.API.getApiQuery(`db/`))
|
2018-12-17 16:45:09 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect('Content-Disposition', /Attachment; filename="[A-Za-z0-9._-]+\.json"/);
|
|
|
|
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
should.exist(res.headers['content-disposition']);
|
|
|
|
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse.db);
|
|
|
|
jsonResponse.db.should.have.length(1);
|
2021-03-12 09:42:33 +03:00
|
|
|
|
|
|
|
const dataKeys = Object.keys(exportedBodyLatest().db[0].data);
|
|
|
|
|
2021-03-05 14:38:45 +03:00
|
|
|
Object.keys(jsonResponse.db[0].data).length.should.eql(28);
|
2021-03-12 09:42:33 +03:00
|
|
|
Object.keys(jsonResponse.db[0].data).length.should.eql(dataKeys.length);
|
|
|
|
jsonResponse.db[0].data.should.have.only.keys(...dataKeys);
|
2018-12-17 16:45:09 +03:00
|
|
|
});
|
|
|
|
|
2021-03-24 05:44:56 +03:00
|
|
|
it('Can import a JSON database exported from Ghost 2.0', async function () {
|
2020-11-30 17:25:22 +03:00
|
|
|
await request.delete(localUtils.API.getApiQuery('db/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect(204);
|
|
|
|
|
|
|
|
const res = await request.post(localUtils.API.getApiQuery('db/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect('Content-Type', /json/)
|
2020-07-14 11:10:59 +03:00
|
|
|
.attach('importfile', path.join(__dirname, '/../../utils/fixtures/export/v2_export.json'))
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse.db);
|
|
|
|
should.exist(jsonResponse.problems);
|
|
|
|
jsonResponse.problems.should.have.length(3);
|
|
|
|
|
|
|
|
const res2 = await request.get(localUtils.API.getApiQuery('posts/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
res2.body.posts.should.have.length(7);
|
|
|
|
});
|
|
|
|
|
2021-03-24 05:44:56 +03:00
|
|
|
it('Can import a JSON database exported from Ghost 3.0', async function () {
|
2020-07-14 11:10:59 +03:00
|
|
|
await request.delete(localUtils.API.getApiQuery('db/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect(204);
|
|
|
|
|
|
|
|
const res = await request.post(localUtils.API.getApiQuery('db/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.attach('importfile', path.join(__dirname, '/../../utils/fixtures/export/v3_export.json'))
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse.db);
|
|
|
|
should.exist(jsonResponse.problems);
|
2021-03-24 05:44:56 +03:00
|
|
|
jsonResponse.problems.should.have.length(2);
|
2020-07-14 11:10:59 +03:00
|
|
|
|
|
|
|
const res2 = await request.get(localUtils.API.getApiQuery('posts/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
res2.body.posts.should.have.length(7);
|
|
|
|
});
|
|
|
|
|
2021-03-24 05:44:56 +03:00
|
|
|
it('Can import a JSON database exported from Ghost 4.0', async function () {
|
2020-07-14 11:10:59 +03:00
|
|
|
await request.delete(localUtils.API.getApiQuery('db/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect(204);
|
|
|
|
|
|
|
|
const res = await request.post(localUtils.API.getApiQuery('db/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.attach('importfile', path.join(__dirname, '/../../utils/fixtures/export/v4_export.json'))
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse.db);
|
|
|
|
should.exist(jsonResponse.problems);
|
|
|
|
jsonResponse.problems.should.have.length(3);
|
|
|
|
|
|
|
|
const res2 = await request.get(localUtils.API.getApiQuery('posts/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
res2.body.posts.should.have.length(7);
|
2019-01-02 15:35:23 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can delete all content', async function () {
|
|
|
|
const res = await request
|
2019-02-04 17:16:24 +03:00
|
|
|
.get(localUtils.API.getApiQuery('posts/'))
|
2018-12-17 16:45:09 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
let jsonResponse = res.body;
|
|
|
|
jsonResponse.posts.should.have.length(7);
|
|
|
|
|
|
|
|
await request.delete(localUtils.API.getApiQuery('db/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect(204);
|
|
|
|
|
|
|
|
const res2 = await request.get(localUtils.API.getApiQuery('posts/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
res2.body.posts.should.have.length(0);
|
|
|
|
eventsTriggered['post.unpublished'].length.should.eql(7);
|
|
|
|
eventsTriggered['post.deleted'].length.should.eql(7);
|
|
|
|
eventsTriggered['tag.deleted'].length.should.eql(1);
|
2018-12-17 16:45:09 +03:00
|
|
|
});
|
|
|
|
});
|