mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
parent
033ddf1b43
commit
bf295a96a8
6 changed files with 167 additions and 2 deletions
67
core/server/api/v2/configuration.js
Normal file
67
core/server/api/v2/configuration.js
Normal file
|
@ -0,0 +1,67 @@
|
|||
const Promise = require('bluebird');
|
||||
const {isPlainObject} = require('lodash');
|
||||
const urlService = require('../../services/url');
|
||||
const models = require('../../models');
|
||||
const config = require('../../config');
|
||||
const labs = require('../../services/labs');
|
||||
const settingsCache = require('../../services/settings/cache');
|
||||
const ghostVersion = require('../../lib/ghost-version');
|
||||
|
||||
function fetchAvailableTimezones() {
|
||||
const timezones = require('../../data/timezones.json');
|
||||
return timezones;
|
||||
}
|
||||
|
||||
function getAboutConfig() {
|
||||
return {
|
||||
version: ghostVersion.full,
|
||||
environment: config.get('env'),
|
||||
database: config.get('database').client,
|
||||
mail: isPlainObject(config.get('mail')) ? config.get('mail').transport : ''
|
||||
};
|
||||
}
|
||||
|
||||
function getBaseConfig() {
|
||||
return {
|
||||
useGravatar: !config.isPrivacyDisabled('useGravatar'),
|
||||
publicAPI: labs.isSet('publicAPI'),
|
||||
blogUrl: urlService.utils.urlFor('home', true),
|
||||
blogTitle: settingsCache.get('title'),
|
||||
clientExtensions: config.get('clientExtensions'),
|
||||
enableDeveloperExperiments: config.get('enableDeveloperExperiments')
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
docName: 'configuration',
|
||||
read: {
|
||||
permissions: false,
|
||||
data: [
|
||||
'key'
|
||||
],
|
||||
query({data}) {
|
||||
if (!data.key) {
|
||||
return models.Client.findOne({slug: 'ghost-admin'})
|
||||
.then((ghostAdmin) => {
|
||||
const configuration = getBaseConfig();
|
||||
|
||||
configuration.clientId = ghostAdmin.get('slug');
|
||||
configuration.clientSecret = ghostAdmin.get('secret');
|
||||
|
||||
return configuration;
|
||||
});
|
||||
}
|
||||
|
||||
if (data.key === 'about') {
|
||||
return Promise.resolve(getAboutConfig());
|
||||
}
|
||||
|
||||
// Timezone endpoint
|
||||
if (data.key === 'timezones') {
|
||||
return Promise.resolve(fetchAvailableTimezones());
|
||||
}
|
||||
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
};
|
|
@ -81,5 +81,9 @@ module.exports = {
|
|||
|
||||
get authors() {
|
||||
return shared.pipeline(require('./authors'), localUtils);
|
||||
},
|
||||
|
||||
get configuration() {
|
||||
return shared.pipeline(require('./configuration'), localUtils);
|
||||
}
|
||||
};
|
||||
|
|
11
core/server/api/v2/utils/serializers/output/configuration.js
Normal file
11
core/server/api/v2/utils/serializers/output/configuration.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
const debug = require('ghost-ignition').debug('api:v2:utils:serializers:output:configuration');
|
||||
|
||||
module.exports = {
|
||||
all(configuration, apiConfig, frame) {
|
||||
frame.response = {
|
||||
configuration: configuration ? [configuration] : []
|
||||
};
|
||||
|
||||
debug(frame.response);
|
||||
}
|
||||
};
|
|
@ -65,5 +65,9 @@ module.exports = {
|
|||
|
||||
get authors() {
|
||||
return require('./authors');
|
||||
},
|
||||
|
||||
get configuration() {
|
||||
return require('./configuration');
|
||||
}
|
||||
};
|
||||
|
|
|
@ -22,8 +22,8 @@ module.exports = function apiRoutes() {
|
|||
router.options('*', shared.middlewares.api.cors);
|
||||
|
||||
// ## Configuration
|
||||
router.get('/configuration', api.http(api.configuration.read));
|
||||
router.get('/configuration/:key', mw.authAdminApi, api.http(api.configuration.read));
|
||||
router.get('/configuration', apiv2.http(apiv2.configuration.read));
|
||||
router.get('/configuration/:key', mw.authAdminApi, apiv2.http(apiv2.configuration.read));
|
||||
|
||||
// ## Posts
|
||||
router.get('/posts', mw.authAdminApi, apiv2.http(apiv2.posts.browse));
|
||||
|
|
79
core/test/functional/api/v2/admin/configuration_spec.js
Normal file
79
core/test/functional/api/v2/admin/configuration_spec.js
Normal file
|
@ -0,0 +1,79 @@
|
|||
const should = require('should');
|
||||
const supertest = require('supertest');
|
||||
const testUtils = require('../../../../utils');
|
||||
const localUtils = require('./utils');
|
||||
const config = require('../../../../../../core/server/config');
|
||||
const ghost = testUtils.startGhost;
|
||||
|
||||
let request;
|
||||
describe('Configuration API', function () {
|
||||
before(function () {
|
||||
return ghost()
|
||||
.then(function () {
|
||||
request = supertest.agent(config.get('url'));
|
||||
})
|
||||
.then(function () {
|
||||
return localUtils.doAuth(request);
|
||||
});
|
||||
});
|
||||
|
||||
describe('success', function () {
|
||||
it('can retrieve public configuration and all expected properties', function (done) {
|
||||
request.get(localUtils.API.getApiQuery('configuration/'))
|
||||
.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.exist(res.body.configuration);
|
||||
|
||||
res.body.configuration.should.be.an.Array().with.lengthOf(1);
|
||||
const props = res.body.configuration[0];
|
||||
|
||||
props.blogUrl.should.eql('http://127.0.0.1:2369/');
|
||||
|
||||
props.useGravatar.should.eql(false);
|
||||
props.publicAPI.should.eql(true);
|
||||
props.clientId.should.eql('ghost-admin');
|
||||
props.clientSecret.should.eql('not_available');
|
||||
|
||||
// value not available, because settings API was not called yet
|
||||
props.hasOwnProperty('blogTitle').should.eql(true);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can read about config and get all expected properties', function (done) {
|
||||
request.get(localUtils.API.getApiQuery('configuration/about/'))
|
||||
.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.exist(res.body.configuration);
|
||||
|
||||
res.body.configuration.should.be.an.Array().with.lengthOf(1);
|
||||
const props = res.body.configuration[0];
|
||||
|
||||
// Check the structure
|
||||
props.should.have.property('version').which.is.a.String();
|
||||
props.should.have.property('environment').which.is.a.String();
|
||||
props.should.have.property('database').which.is.a.String();
|
||||
props.should.have.property('mail').which.is.a.String();
|
||||
|
||||
// Check a few values
|
||||
props.environment.should.match(/^testing/);
|
||||
props.version.should.eql(require('../../../../../../package.json').version);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue