mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
Updated settings to use new test framework
- split out the two tests that use files, as the new framework doesn't support this yet - convert the 3 existing tests for the settings endpoint to use the new framework - introduced a new pattern of using a function to generate a matching array, so that we can do extra stuff - in this case, just one of the items needed a matcher for the value, which results in slightly weird code - wrapping in a function gives us somewhere to do this and leave a comment
This commit is contained in:
parent
c11187d770
commit
c66eeb5879
3 changed files with 1313 additions and 250 deletions
1124
test/e2e-api/admin/__snapshots__/settings.test.js.snap
Normal file
1124
test/e2e-api/admin/__snapshots__/settings.test.js.snap
Normal file
File diff suppressed because it is too large
Load diff
50
test/e2e-api/admin/settings-files.test.js
Normal file
50
test/e2e-api/admin/settings-files.test.js
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
const should = require('should');
|
||||||
|
const supertest = require('supertest');
|
||||||
|
const os = require('os');
|
||||||
|
const fs = require('fs-extra');
|
||||||
|
const config = require('../../../core/shared/config');
|
||||||
|
const testUtils = require('../../utils');
|
||||||
|
const localUtils = require('./utils');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The new test framework doesn't yet support files
|
||||||
|
*/
|
||||||
|
describe('Settings File API', function () {
|
||||||
|
let request;
|
||||||
|
|
||||||
|
before(async function () {
|
||||||
|
await localUtils.startGhost();
|
||||||
|
request = supertest.agent(config.get('url'));
|
||||||
|
await localUtils.doAuth(request);
|
||||||
|
});
|
||||||
|
|
||||||
|
after(function () {
|
||||||
|
return testUtils.stopGhost();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Can download routes.yaml', async function () {
|
||||||
|
const res = await request.get(localUtils.API.getApiQuery('settings/routes/yaml/'))
|
||||||
|
.set('Origin', config.get('url'))
|
||||||
|
.set('Accept', 'application/yaml')
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
res.headers['content-disposition'].should.eql('Attachment; filename="routes.yaml"');
|
||||||
|
res.headers['content-type'].should.eql('application/yaml; charset=utf-8');
|
||||||
|
res.headers['content-length'].should.eql('138');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Can upload routes.yaml', async function () {
|
||||||
|
const newRoutesYamlPath = `${os.tmpdir()}/routes.yaml`;
|
||||||
|
|
||||||
|
await fs.writeFile(newRoutesYamlPath, 'routes:\ncollections:\ntaxonomies:\n');
|
||||||
|
const res = await request
|
||||||
|
.post(localUtils.API.getApiQuery('settings/routes/yaml/'))
|
||||||
|
.set('Origin', config.get('url'))
|
||||||
|
.attach('routes', newRoutesYamlPath)
|
||||||
|
.expect('Content-Type', /application\/json/)
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
res.headers['x-cache-invalidate'].should.eql('/*');
|
||||||
|
await testUtils.stopGhost();
|
||||||
|
});
|
||||||
|
});
|
|
@ -1,87 +1,69 @@
|
||||||
const should = require('should');
|
const {agentProvider, fixtureManager, matchers} = require('../../utils/e2e-framework');
|
||||||
const _ = require('lodash');
|
const {stringMatching, anyEtag, anyObjectId, anyISODateTime} = matchers;
|
||||||
const supertest = require('supertest');
|
|
||||||
const os = require('os');
|
const CURRENT_SETTINGS_COUNT = 86;
|
||||||
const fs = require('fs-extra');
|
|
||||||
const config = require('../../../core/shared/config');
|
const settingsMatcher = {
|
||||||
const testUtils = require('../../utils');
|
id: anyObjectId,
|
||||||
const localUtils = require('./utils');
|
created_at: anyISODateTime,
|
||||||
const labs = require('../../../core/shared/labs');
|
updated_at: anyISODateTime
|
||||||
|
};
|
||||||
|
|
||||||
|
const publicHashSettingMatcher = {
|
||||||
|
id: anyObjectId,
|
||||||
|
value: stringMatching(/[a-z0-9]{30}/),
|
||||||
|
created_at: anyISODateTime,
|
||||||
|
updated_at: anyISODateTime
|
||||||
|
};
|
||||||
|
|
||||||
|
const matchSettingsArray = (length) => {
|
||||||
|
const settingsArray = new Array(length).fill(settingsMatcher);
|
||||||
|
|
||||||
|
if (length > 25) {
|
||||||
|
// Item at index 25 is the public hash, which is always different
|
||||||
|
settingsArray[25] = publicHashSettingMatcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
return settingsArray;
|
||||||
|
};
|
||||||
|
|
||||||
describe('Settings API', function () {
|
describe('Settings API', function () {
|
||||||
let request;
|
let agent;
|
||||||
|
|
||||||
before(async function () {
|
before(async function () {
|
||||||
await localUtils.startGhost();
|
agent = await agentProvider.getAdminAPIAgent();
|
||||||
request = supertest.agent(config.get('url'));
|
await fixtureManager.init();
|
||||||
await localUtils.doAuth(request);
|
await agent.loginAsOwner();
|
||||||
});
|
|
||||||
|
|
||||||
after(function () {
|
|
||||||
return testUtils.stopGhost();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Can request all settings', async function () {
|
it('Can request all settings', async function () {
|
||||||
const res = await request.get(localUtils.API.getApiQuery('settings/'))
|
await agent
|
||||||
.set('Origin', config.get('url'))
|
.get('settings/')
|
||||||
.expect('Content-Type', /json/)
|
.expectStatus(200)
|
||||||
.expect('Cache-Control', testUtils.cacheRules.private)
|
.matchBodySnapshot({
|
||||||
.expect(200);
|
settings: matchSettingsArray(CURRENT_SETTINGS_COUNT)
|
||||||
|
})
|
||||||
should.not.exist(res.headers['x-cache-invalidate']);
|
.matchHeaderSnapshot({
|
||||||
const jsonResponse = res.body;
|
etag: anyEtag
|
||||||
should.exist(jsonResponse);
|
});
|
||||||
|
|
||||||
localUtils.API.checkResponse(jsonResponse, 'settings');
|
|
||||||
|
|
||||||
JSON.parse(_.find(jsonResponse.settings, {key: 'unsplash'}).value).should.eql(true);
|
|
||||||
JSON.parse(_.find(jsonResponse.settings, {key: 'amp'}).value).should.eql(false);
|
|
||||||
should.not.exist(_.find(jsonResponse.settings, {key: 'permalinks'}));
|
|
||||||
should.not.exist(_.find(jsonResponse.settings, {key: 'ghost_head'}));
|
|
||||||
should.not.exist(_.find(jsonResponse.settings, {key: 'ghost_foot'}));
|
|
||||||
|
|
||||||
testUtils.API.isISO8601(jsonResponse.settings[0].created_at).should.be.true();
|
|
||||||
jsonResponse.settings[0].created_at.should.be.an.instanceof(String);
|
|
||||||
|
|
||||||
should.not.exist(_.find(jsonResponse.settings, function (setting) {
|
|
||||||
return setting.type === 'core';
|
|
||||||
}));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Can read a setting', async function () {
|
it('Can read a setting', async function () {
|
||||||
const res = await request.get(localUtils.API.getApiQuery('settings/codeinjection_head/'))
|
await agent.get('settings/codeinjection_head/')
|
||||||
.set('Origin', config.get('url'))
|
.expectStatus(200)
|
||||||
.expect('Content-Type', /json/)
|
.matchBodySnapshot({
|
||||||
.expect('Cache-Control', testUtils.cacheRules.private)
|
settings: [settingsMatcher]
|
||||||
.expect(200);
|
})
|
||||||
|
.matchHeaderSnapshot({
|
||||||
should.not.exist(res.headers['x-cache-invalidate']);
|
etag: anyEtag
|
||||||
const jsonResponse = res.body;
|
});
|
||||||
|
|
||||||
should.exist(jsonResponse);
|
|
||||||
should.exist(jsonResponse.settings);
|
|
||||||
|
|
||||||
jsonResponse.settings.length.should.eql(1);
|
|
||||||
|
|
||||||
testUtils.API.checkResponseValue(jsonResponse.settings[0], ['id', 'group', 'key', 'value', 'type', 'flags', 'created_at', 'updated_at']);
|
|
||||||
jsonResponse.settings[0].key.should.eql('codeinjection_head');
|
|
||||||
testUtils.API.isISO8601(jsonResponse.settings[0].created_at).should.be.true();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Can edit a setting', async function () {
|
it('Can edit a setting', async function () {
|
||||||
const res = await request.get(localUtils.API.getApiQuery('settings/'))
|
const settingsToChange = [
|
||||||
.set('Origin', config.get('url'))
|
|
||||||
.expect('Content-Type', /json/)
|
|
||||||
.expect('Cache-Control', testUtils.cacheRules.private);
|
|
||||||
|
|
||||||
const jsonResponse = res.body;
|
|
||||||
const changedValue = [];
|
|
||||||
|
|
||||||
const settingToChange = {
|
|
||||||
settings: [
|
|
||||||
{
|
{
|
||||||
key: 'title',
|
key: 'title',
|
||||||
value: changedValue
|
value: []
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'codeinjection_head',
|
key: 'codeinjection_head',
|
||||||
|
@ -156,111 +138,18 @@ describe('Settings API', function () {
|
||||||
key: 'unsplash',
|
key: 'unsplash',
|
||||||
value: false
|
value: false
|
||||||
}
|
}
|
||||||
]
|
];
|
||||||
};
|
|
||||||
|
|
||||||
should.exist(jsonResponse);
|
await agent.put('settings/')
|
||||||
should.exist(jsonResponse.settings);
|
.body({
|
||||||
|
settings: settingsToChange
|
||||||
const {body, headers} = await request.put(localUtils.API.getApiQuery('settings/'))
|
})
|
||||||
.set('Origin', config.get('url'))
|
.expectStatus(200)
|
||||||
.send(settingToChange)
|
.matchBodySnapshot({
|
||||||
.expect('Content-Type', /json/)
|
settings: matchSettingsArray(settingsToChange.length)
|
||||||
.expect('Cache-Control', testUtils.cacheRules.private)
|
})
|
||||||
.expect(200);
|
.matchHeaderSnapshot({
|
||||||
|
etag: anyEtag
|
||||||
const putBody = body;
|
|
||||||
headers['x-cache-invalidate'].should.eql('/*');
|
|
||||||
should.exist(putBody);
|
|
||||||
|
|
||||||
putBody.settings.length.should.equal(settingToChange.settings.length);
|
|
||||||
|
|
||||||
putBody.settings[0].key.should.eql('title');
|
|
||||||
putBody.settings[0].value.should.eql(JSON.stringify(changedValue));
|
|
||||||
|
|
||||||
putBody.settings[1].key.should.eql('codeinjection_head');
|
|
||||||
should.equal(putBody.settings[1].value, null);
|
|
||||||
|
|
||||||
putBody.settings[2].key.should.eql('navigation');
|
|
||||||
should.equal(putBody.settings[2].value, JSON.stringify([{label: 'label1'}]));
|
|
||||||
|
|
||||||
putBody.settings[3].key.should.eql('slack_username');
|
|
||||||
should.equal(putBody.settings[3].value, 'New Slack Username');
|
|
||||||
|
|
||||||
putBody.settings[4].key.should.eql('is_private');
|
|
||||||
should.equal(putBody.settings[4].value, false);
|
|
||||||
|
|
||||||
putBody.settings[5].key.should.eql('meta_title');
|
|
||||||
should.equal(putBody.settings[5].value, 'SEO title');
|
|
||||||
|
|
||||||
putBody.settings[6].key.should.eql('meta_description');
|
|
||||||
should.equal(putBody.settings[6].value, 'SEO description');
|
|
||||||
|
|
||||||
putBody.settings[6].key.should.eql('meta_description');
|
|
||||||
should.equal(putBody.settings[6].value, 'SEO description');
|
|
||||||
|
|
||||||
putBody.settings[7].key.should.eql('og_image');
|
|
||||||
should.equal(putBody.settings[7].value, `${config.get('url')}/content/images/2019/07/facebook.png`);
|
|
||||||
|
|
||||||
putBody.settings[8].key.should.eql('og_title');
|
|
||||||
should.equal(putBody.settings[8].value, 'facebook title');
|
|
||||||
|
|
||||||
putBody.settings[9].key.should.eql('og_description');
|
|
||||||
should.equal(putBody.settings[9].value, 'facebook description');
|
|
||||||
|
|
||||||
putBody.settings[10].key.should.eql('twitter_image');
|
|
||||||
should.equal(putBody.settings[10].value, `${config.get('url')}/content/images/2019/07/twitter.png`);
|
|
||||||
|
|
||||||
putBody.settings[11].key.should.eql('twitter_title');
|
|
||||||
should.equal(putBody.settings[11].value, 'twitter title');
|
|
||||||
|
|
||||||
putBody.settings[12].key.should.eql('twitter_description');
|
|
||||||
should.equal(putBody.settings[12].value, 'twitter description');
|
|
||||||
|
|
||||||
putBody.settings[13].key.should.eql('lang');
|
|
||||||
should.equal(putBody.settings[13].value, 'ua');
|
|
||||||
|
|
||||||
putBody.settings[14].key.should.eql('labs');
|
|
||||||
should.equal(putBody.settings[14].value, JSON.stringify(labs.getAll()));
|
|
||||||
|
|
||||||
putBody.settings[15].key.should.eql('timezone');
|
|
||||||
should.equal(putBody.settings[15].value, 'Pacific/Auckland');
|
|
||||||
|
|
||||||
putBody.settings[16].key.should.eql('unsplash');
|
|
||||||
should.equal(putBody.settings[16].value, false);
|
|
||||||
|
|
||||||
putBody.settings[17].key.should.eql('slack');
|
|
||||||
should.equal(putBody.settings[17].value, JSON.stringify([{
|
|
||||||
url: 'https://overrides.tld',
|
|
||||||
username: 'New Slack Username'
|
|
||||||
}]));
|
|
||||||
|
|
||||||
localUtils.API.checkResponse(putBody, 'settings');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Can download routes.yaml', async function () {
|
|
||||||
const res = await request.get(localUtils.API.getApiQuery('settings/routes/yaml/'))
|
|
||||||
.set('Origin', config.get('url'))
|
|
||||||
.set('Accept', 'application/yaml')
|
|
||||||
.expect(200);
|
|
||||||
|
|
||||||
res.headers['content-disposition'].should.eql('Attachment; filename="routes.yaml"');
|
|
||||||
res.headers['content-type'].should.eql('application/yaml; charset=utf-8');
|
|
||||||
res.headers['content-length'].should.eql('138');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('Can upload routes.yaml', async function () {
|
|
||||||
const newRoutesYamlPath = `${os.tmpdir()}/routes.yaml`;
|
|
||||||
|
|
||||||
await fs.writeFile(newRoutesYamlPath, 'routes:\ncollections:\ntaxonomies:\n');
|
|
||||||
const res = await request
|
|
||||||
.post(localUtils.API.getApiQuery('settings/routes/yaml/'))
|
|
||||||
.set('Origin', config.get('url'))
|
|
||||||
.attach('routes', newRoutesYamlPath)
|
|
||||||
.expect('Content-Type', /application\/json/)
|
|
||||||
.expect(200);
|
|
||||||
|
|
||||||
res.headers['x-cache-invalidate'].should.eql('/*');
|
|
||||||
await testUtils.stopGhost();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue