2019-02-22 06:17:14 +03:00
|
|
|
const should = require('should');
|
|
|
|
const supertest = require('supertest');
|
|
|
|
const _ = require('lodash');
|
2019-09-20 18:02:45 +03:00
|
|
|
const testUtils = require('../../utils');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../../core/shared/config');
|
2020-03-30 18:26:47 +03:00
|
|
|
const models = require('../../../core/server/models');
|
2019-02-22 06:17:14 +03:00
|
|
|
const localUtils = require('./utils');
|
2019-09-20 18:02:45 +03:00
|
|
|
|
2019-02-22 06:17:14 +03:00
|
|
|
const ghost = testUtils.startGhost;
|
|
|
|
let request;
|
|
|
|
|
|
|
|
describe('Pages API', function () {
|
|
|
|
let ghostServer;
|
|
|
|
let ownerCookie;
|
|
|
|
|
|
|
|
before(function () {
|
|
|
|
return ghost()
|
|
|
|
.then(function (_ghostServer) {
|
|
|
|
ghostServer = _ghostServer;
|
|
|
|
request = supertest.agent(config.get('url'));
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
return localUtils.doAuth(request, 'users:extra', 'posts');
|
|
|
|
})
|
|
|
|
.then(function (cookie) {
|
|
|
|
ownerCookie = cookie;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can retrieve all pages', function (done) {
|
|
|
|
request.get(localUtils.API.getApiQuery('pages/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200)
|
|
|
|
.end(function (err, res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse.pages);
|
|
|
|
localUtils.API.checkResponse(jsonResponse, 'pages');
|
|
|
|
jsonResponse.pages.should.have.length(2);
|
|
|
|
|
2019-02-25 13:08:28 +03:00
|
|
|
localUtils.API.checkResponse(jsonResponse.pages[0], 'page');
|
2019-02-22 06:17:14 +03:00
|
|
|
localUtils.API.checkResponse(jsonResponse.meta.pagination, 'pagination');
|
|
|
|
_.isBoolean(jsonResponse.pages[0].featured).should.eql(true);
|
|
|
|
|
|
|
|
// Absolute urls by default
|
2019-03-21 21:08:38 +03:00
|
|
|
jsonResponse.pages[0].url.should.match(new RegExp(`${config.get('url')}/p/[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}`));
|
2019-02-22 06:17:14 +03:00
|
|
|
jsonResponse.pages[1].url.should.eql(`${config.get('url')}/static-page-test/`);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can add a page', function () {
|
|
|
|
const page = {
|
|
|
|
title: 'My Page',
|
|
|
|
page: false,
|
|
|
|
status: 'published'
|
|
|
|
};
|
|
|
|
|
|
|
|
return request.post(localUtils.API.getApiQuery('pages/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({pages: [page]})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(201)
|
|
|
|
.then((res) => {
|
|
|
|
res.body.pages.length.should.eql(1);
|
2019-02-25 12:55:24 +03:00
|
|
|
|
2019-03-04 17:53:15 +03:00
|
|
|
localUtils.API.checkResponse(res.body.pages[0], 'page');
|
2019-02-22 06:17:14 +03:00
|
|
|
should.exist(res.headers['x-cache-invalidate']);
|
|
|
|
|
2020-09-14 13:33:37 +03:00
|
|
|
should.exist(res.headers.location);
|
|
|
|
res.headers.location.should.equal(`http://127.0.0.1:2369${localUtils.API.getApiQuery('pages/')}${res.body.pages[0].id}/`);
|
|
|
|
|
2019-02-22 06:17:14 +03:00
|
|
|
return models.Post.findOne({
|
|
|
|
id: res.body.pages[0].id
|
|
|
|
}, testUtils.context.internal);
|
|
|
|
})
|
|
|
|
.then((model) => {
|
|
|
|
model.get('title').should.eql(page.title);
|
|
|
|
model.get('status').should.eql(page.status);
|
2019-09-16 13:51:54 +03:00
|
|
|
model.get('type').should.eql('page');
|
2019-02-22 06:17:14 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can update a page', function () {
|
|
|
|
const page = {
|
|
|
|
title: 'updated page',
|
|
|
|
page: false
|
|
|
|
};
|
|
|
|
|
|
|
|
return request
|
|
|
|
.get(localUtils.API.getApiQuery(`pages/${testUtils.DataGenerator.Content.posts[5].id}/`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect(200)
|
|
|
|
.then((res) => {
|
|
|
|
page.updated_at = res.body.pages[0].updated_at;
|
|
|
|
|
|
|
|
return request.put(localUtils.API.getApiQuery('pages/' + testUtils.DataGenerator.Content.posts[5].id))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({pages: [page]})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
should.exist(res.headers['x-cache-invalidate']);
|
2019-02-25 13:08:28 +03:00
|
|
|
localUtils.API.checkResponse(res.body.pages[0], 'page');
|
2019-02-22 06:17:14 +03:00
|
|
|
|
|
|
|
return models.Post.findOne({
|
|
|
|
id: res.body.pages[0].id
|
|
|
|
}, testUtils.context.internal);
|
|
|
|
})
|
|
|
|
.then((model) => {
|
2019-09-16 13:51:54 +03:00
|
|
|
model.get('type').should.eql('page');
|
2019-02-22 06:17:14 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Cannot get page via posts endpoint', function () {
|
|
|
|
return request.get(localUtils.API.getApiQuery(`posts/${testUtils.DataGenerator.Content.posts[5].id}/`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Cannot update page via posts endpoint', function () {
|
|
|
|
const page = {
|
|
|
|
title: 'fails',
|
|
|
|
updated_at: new Date().toISOString()
|
|
|
|
};
|
|
|
|
|
|
|
|
return request.put(localUtils.API.getApiQuery('posts/' + testUtils.DataGenerator.Content.posts[5].id))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({posts: [page]})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can delete a page', function () {
|
|
|
|
return request.del(localUtils.API.getApiQuery('pages/' + testUtils.DataGenerator.Content.posts[5].id))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(204)
|
|
|
|
.then((res) => {
|
|
|
|
res.body.should.be.empty();
|
|
|
|
res.headers['x-cache-invalidate'].should.eql('/*');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|