2018-12-17 16:45:09 +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-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 07:12:10 +03:00
|
|
|
before(async function () {
|
2021-11-18 11:55:35 +03:00
|
|
|
await localUtils.startGhost();
|
2020-11-30 17:25:22 +03:00
|
|
|
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 () {
|
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
|
|
|
|
2021-03-25 05:16:24 +03:00
|
|
|
const dataKeys = Object.keys(exportedBodyLatest().db[0].data).sort();
|
2021-03-12 09:42:33 +03:00
|
|
|
|
2021-03-25 03:22:34 +03:00
|
|
|
// NOTE: using `Object.keys` here instead of `should.have.only.keys` assertion
|
|
|
|
// because when `have.only.keys` fails there's no useful diff
|
2021-03-25 05:16:24 +03:00
|
|
|
Object.keys(jsonResponse.db[0].data).sort().should.be.eql(dataKeys);
|
2018-12-17 16:45:09 +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);
|
|
|
|
|
2021-07-07 19:12:51 +03:00
|
|
|
const deleteRequest = await request.delete(localUtils.API.getApiQuery('db/'))
|
2020-11-30 17:25:22 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect(204);
|
2021-07-07 19:12:51 +03:00
|
|
|
should.exist(deleteRequest.headers['x-cache-invalidate']);
|
2020-11-30 17:25:22 +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(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
|
|
|
});
|
2023-03-06 12:39:22 +03:00
|
|
|
|
|
|
|
it('Can trigger external media inliner', async function () {
|
|
|
|
const response = await request
|
|
|
|
.post(localUtils.API.getApiQuery('db/media/inline'))
|
|
|
|
.send({
|
|
|
|
domains: ['https://example.com']
|
|
|
|
})
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
// @NOTE: the response format is temporary for test purposes
|
|
|
|
// before feature graduates to GA, it should become
|
|
|
|
// a more consistent format
|
|
|
|
response.body.should.eql({
|
|
|
|
db: [{
|
|
|
|
status: 'success'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
2018-12-17 16:45:09 +03:00
|
|
|
});
|