Ghost/test/e2e-api/admin/custom_theme_settings.test.js
Kevin Ansfield c33b596e9c
Added API tests for custom theme settings (#13519)
refs https://github.com/TryGhost/Team/issues/1104

- bumped `@tryghost/custom-theme-settings-service` so it throws a more appropriate `ValidationError` when setting keys don't exist or a select value is not known
- changed the custom theme settings service to have a `.init()` method which creates an instance of the service under `.api` so that we're able to create the instance at a particular point in the boot process when we know the models have been initialised
  - there were problems in tests because the service was being initialised through the require chain before models were initialised through the boot process
- fixed incorrect `camelCase` of resource name in API responses
2021-10-08 16:18:49 +01:00

212 lines
7.6 KiB
JavaScript

const should = require('should');
const supertest = require('supertest');
const testUtils = require('../../utils');
const localUtils = require('./utils');
const config = require('../../../core/shared/config');
describe('Custom Theme Settings API', function () {
let request;
before(async function () {
await testUtils.startGhost();
request = supertest.agent(config.get('url'));
await localUtils.doAuth(request, 'users:extra', 'custom_theme_settings');
// require here so we know it's already been set up with models
const customThemeSettingsService = require('../../../core/server/services/custom-theme-settings');
// fake a theme activation with custom settings - settings match fixtures
await customThemeSettingsService.api.activateTheme({
name: 'casper',
customSettings: {
header_typography: {
type: 'select',
options: ['Serif', 'Sans-serif'],
default: 'Sans-serif'
},
footer_type: {
type: 'select',
options: ['Full', 'Minimal', 'CTA'],
default: 'Full',
group: 'homepage'
}
}
});
});
describe('Browse', function () {
it('can fetch settings for current theme', async function () {
const res = await request
.get(localUtils.API.getApiQuery(`custom_theme_settings/`))
.set('Origin', config.get('url'))
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(200);
should.not.exist(res.headers['x-cache-invalidate']);
const jsonResponse = res.body;
should.exist(jsonResponse);
should.exist(jsonResponse.custom_theme_settings);
jsonResponse.custom_theme_settings.length.should.equal(2);
jsonResponse.custom_theme_settings[0].should.match({
id: /.+/,
key: 'header_typography',
type: 'select',
options: ['Serif', 'Sans-serif'],
default: 'Sans-serif',
value: 'Serif'
});
jsonResponse.custom_theme_settings[1].should.match({
id: /.+/,
key: 'footer_type',
type: 'select',
options: ['Full', 'Minimal', 'CTA'],
default: 'Full',
value: 'Full',
group: 'homepage'
});
});
});
describe('Edit', function () {
it('can update all settings for current theme', async function () {
// `.updateSettings()` only cares about `key` and `value`, everything else is set by the theme
const custom_theme_settings = [{
id: 'id',
type: 'type',
options: ['option'],
default: 'default',
key: 'header_typography',
value: 'Sans-serif'
}, {
key: 'footer_type',
value: 'Minimal'
}];
const res = await request
.put(localUtils.API.getApiQuery(`custom_theme_settings/`))
.set('Origin', config.get('url'))
.send({custom_theme_settings})
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(200);
should.exist(res.headers['x-cache-invalidate']);
const jsonResponse = res.body;
should.exist(jsonResponse);
should.exist(jsonResponse.custom_theme_settings);
jsonResponse.custom_theme_settings.length.should.equal(2);
jsonResponse.custom_theme_settings[0].should.match({
id: /.+/,
key: 'header_typography',
type: 'select',
options: ['Serif', 'Sans-serif'],
default: 'Sans-serif',
value: 'Sans-serif'
});
jsonResponse.custom_theme_settings[1].should.match({
id: /.+/,
key: 'footer_type',
type: 'select',
options: ['Full', 'Minimal', 'CTA'],
default: 'Full',
value: 'Minimal',
group: 'homepage'
});
});
it('can update some settings', async function () {
// `.updateSettings()` only cares about `key` and `value`, everything else is set by the theme
const custom_theme_settings = [{
key: 'footer_type',
value: 'Minimal'
}];
const res = await request
.put(localUtils.API.getApiQuery(`custom_theme_settings/`))
.set('Origin', config.get('url'))
.send({custom_theme_settings})
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(200);
should.exist(res.headers['x-cache-invalidate']);
const jsonResponse = res.body;
should.exist(jsonResponse);
should.exist(jsonResponse.custom_theme_settings);
jsonResponse.custom_theme_settings.length.should.equal(2);
jsonResponse.custom_theme_settings[0].should.match({
id: /.+/,
key: 'header_typography',
type: 'select',
options: ['Serif', 'Sans-serif'],
default: 'Sans-serif',
value: 'Sans-serif' // set in previous test
});
jsonResponse.custom_theme_settings[1].should.match({
id: /.+/,
key: 'footer_type',
type: 'select',
options: ['Full', 'Minimal', 'CTA'],
default: 'Full',
value: 'Minimal',
group: 'homepage'
});
});
it('errors for unknown key', async function () {
const custom_theme_settings = [{
key: 'unknown',
value: 'Not gonna work'
}];
const res = await request
.put(localUtils.API.getApiQuery(`custom_theme_settings/`))
.set('Origin', config.get('url'))
.send({custom_theme_settings})
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(422);
should.not.exist(res.headers['x-cache-invalidate']);
const jsonResponse = res.body;
should.exist(jsonResponse);
should.exist(jsonResponse.errors);
});
it('errors for invalid select value', async function () {
const custom_theme_settings = [{
key: 'header_typography',
value: 'Not gonna work'
}];
const res = await request
.put(localUtils.API.getApiQuery(`custom_theme_settings/`))
.set('Origin', config.get('url'))
.send({custom_theme_settings})
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(422);
should.not.exist(res.headers['x-cache-invalidate']);
const jsonResponse = res.body;
should.exist(jsonResponse);
should.exist(jsonResponse.errors);
});
});
});