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');
|
2018-12-17 16:45:09 +03:00
|
|
|
const localUtils = require('./utils');
|
|
|
|
|
|
|
|
let ghost = testUtils.startGhost;
|
|
|
|
let request;
|
|
|
|
let eventsTriggered;
|
|
|
|
|
2019-08-19 14:41:09 +03:00
|
|
|
describe('DB API', function () {
|
2018-12-17 16:45:09 +03:00
|
|
|
let backupClient;
|
|
|
|
let schedulerClient;
|
|
|
|
|
2019-08-19 14:41:09 +03:00
|
|
|
before(function () {
|
2018-12-17 16:45:09 +03:00
|
|
|
return ghost()
|
|
|
|
.then(() => {
|
|
|
|
request = supertest.agent(config.get('url'));
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return localUtils.doAuth(request);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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 () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2018-12-17 16:45:09 +03:00
|
|
|
});
|
|
|
|
|
2019-08-19 14:41:09 +03:00
|
|
|
it('Can export a JSON database', function () {
|
2018-12-17 16:45:09 +03:00
|
|
|
return request.get(localUtils.API.getApiQuery(`db/`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200)
|
|
|
|
.expect('Content-Disposition', /Attachment; filename="[A-Za-z0-9._-]+\.json"/)
|
|
|
|
.then((res) => {
|
|
|
|
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);
|
2020-10-16 14:52:06 +03:00
|
|
|
Object.keys(jsonResponse.db[0].data).length.should.eql(34);
|
2018-12-17 16:45:09 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-08-19 14:41:09 +03:00
|
|
|
it('Can import a JSON database', function () {
|
2019-01-02 15:35:23 +03:00
|
|
|
return Promise.resolve()
|
|
|
|
.then(() => {
|
|
|
|
return request.delete(localUtils.API.getApiQuery('db/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect(204);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return request.post(localUtils.API.getApiQuery('db/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect('Content-Type', /json/)
|
2019-09-20 18:02:45 +03:00
|
|
|
.attach('importfile', path.join(__dirname, '/../../utils/fixtures/export/default_export.json'))
|
2019-01-02 15:35:23 +03:00
|
|
|
.expect(200)
|
|
|
|
.then((res) => {
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse.db);
|
|
|
|
should.exist(jsonResponse.problems);
|
2019-04-15 17:02:54 +03:00
|
|
|
jsonResponse.problems.should.have.length(3);
|
2019-01-02 15:35:23 +03:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return request.get(localUtils.API.getApiQuery('posts/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200)
|
|
|
|
.then((res) => {
|
|
|
|
let jsonResponse = res.body;
|
|
|
|
jsonResponse.posts.should.have.length(7);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-08-19 14:41:09 +03:00
|
|
|
it('Can delete all content', function () {
|
2019-02-04 17:16:24 +03:00
|
|
|
return request
|
|
|
|
.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)
|
|
|
|
.expect(200)
|
|
|
|
.then((res) => {
|
|
|
|
let jsonResponse = res.body;
|
|
|
|
jsonResponse.posts.should.have.length(7);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return request.delete(localUtils.API.getApiQuery('db/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect(204);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return request.get(localUtils.API.getApiQuery('posts/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200)
|
|
|
|
.then((res) => {
|
|
|
|
res.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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|