mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-27 22:49:56 -05:00
3240d4adf0
refs https://github.com/TryGhost/Team/issues/555 - This test fixure is up to date and contains real export from Ghost 3.0. Previous file was just an old 2.x export file which was updated manually on "as needed bases" - The aim is to keep these files as close to real world as possible instead of manually making up data for the needs of test suites - Fixed test suite which was using the v3 export file
169 lines
6.4 KiB
JavaScript
169 lines
6.4 KiB
JavaScript
const path = require('path');
|
|
const should = require('should');
|
|
const supertest = require('supertest');
|
|
const sinon = require('sinon');
|
|
const config = require('../../../core/shared/config');
|
|
const {events} = require('../../../core/server/lib/common');
|
|
const testUtils = require('../../utils');
|
|
const {exportedBodyLatest} = require('../../utils/fixtures/export/body-generator');
|
|
const localUtils = require('./utils');
|
|
|
|
describe('DB API', function () {
|
|
let request;
|
|
let eventsTriggered;
|
|
|
|
// NOTE: has to be beforeEach otherwise imports overlap and cause duplicate entry errors
|
|
beforeEach(async function () {
|
|
await testUtils.startGhost();
|
|
request = supertest.agent(config.get('url'));
|
|
await localUtils.doAuth(request);
|
|
});
|
|
|
|
beforeEach(function () {
|
|
eventsTriggered = {};
|
|
|
|
sinon.stub(events, 'emit').callsFake((eventName, eventObj) => {
|
|
if (!eventsTriggered[eventName]) {
|
|
eventsTriggered[eventName] = [];
|
|
}
|
|
|
|
eventsTriggered[eventName].push(eventObj);
|
|
});
|
|
});
|
|
|
|
afterEach(function () {
|
|
testUtils.stopGhost();
|
|
sinon.restore();
|
|
});
|
|
|
|
it('Can export a JSON database', async function () {
|
|
const res = await 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"/);
|
|
|
|
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);
|
|
|
|
const dataKeys = Object.keys(exportedBodyLatest().db[0].data);
|
|
|
|
Object.keys(jsonResponse.db[0].data).length.should.eql(28);
|
|
Object.keys(jsonResponse.db[0].data).length.should.eql(dataKeys.length);
|
|
jsonResponse.db[0].data.should.have.only.keys(...dataKeys);
|
|
});
|
|
|
|
it('Can import a JSON database exported from Ghost 2.0', async function () {
|
|
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/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);
|
|
});
|
|
|
|
it('Can import a JSON database exported from Ghost 3.0', async function () {
|
|
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);
|
|
jsonResponse.problems.should.have.length(2);
|
|
|
|
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);
|
|
});
|
|
|
|
it('Can import a JSON database exported from Ghost 4.0', async function () {
|
|
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'))
|
|
.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);
|
|
});
|
|
|
|
it('Can delete all content', async function () {
|
|
const res = await request
|
|
.get(localUtils.API.getApiQuery('posts/'))
|
|
.set('Origin', config.get('url'))
|
|
.expect('Content-Type', /json/)
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
.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);
|
|
});
|
|
});
|