-
-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added configuration controller to v2 API (#10056)
refs #10060
- Loading branch information
1 parent
033ddf1
commit bf295a9
Showing
6 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
core/server/api/v2/utils/serializers/output/configuration.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); | ||
}); | ||
}); |