forked from TryGhost/Ghost
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request TryGhost#3841 from halfdan/3619-configuration-api
Implements new Configuration API
- Loading branch information
Showing
6 changed files
with
173 additions
and
0 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,78 @@ | ||
// # Configuration API | ||
// RESTful API for browsing the configuration | ||
var _ = require('lodash'), | ||
canThis = require('../permissions').canThis, | ||
config = require('../config'), | ||
errors = require('../errors'), | ||
parsePackageJson = require('../require-tree').parsePackageJson, | ||
Promise = require('bluebird'), | ||
|
||
configuration; | ||
|
||
function getValidKeys() { | ||
var validKeys = { | ||
'fileStorage': config.fileStorage === false ? false : true, | ||
'apps': config.apps || false, | ||
'version': false, | ||
'environment': process.env.NODE_ENV, | ||
'database': config.database.client, | ||
'mail': _.isObject(config.mail) ? config.mail.transport : '', | ||
'blogUrl': config.url | ||
}; | ||
|
||
return parsePackageJson('package.json').then(function (json) { | ||
validKeys.version = json.version; | ||
return validKeys; | ||
}); | ||
} | ||
|
||
/** | ||
* ## Configuration API Methods | ||
* | ||
* **See:** [API Methods](index.js.html#api%20methods) | ||
*/ | ||
configuration = { | ||
|
||
/** | ||
* ### Browse | ||
* Fetch all configuration keys | ||
* @returns {Promise(Configurations)} | ||
*/ | ||
browse: function browse(options) { | ||
return canThis(options.context).browse.configuration().then(function () { | ||
return getValidKeys().then(function (result) { | ||
return { 'configuration': _.map(result, function (value, key) { | ||
return { | ||
key: key, | ||
value: value | ||
}; | ||
})}; | ||
}); | ||
}, function () { | ||
return Promise.reject(new errors.NoPermissionError('You do not have permission to browse the configuration.')); | ||
}); | ||
}, | ||
|
||
/** | ||
* ### Read | ||
* | ||
*/ | ||
read: function read(options) { | ||
return canThis(options.context).read.configuration().then(function () { | ||
return getValidKeys().then(function (result) { | ||
if (_.has(result, options.key)) { | ||
return { 'configuration': [{ | ||
key: options.key, | ||
value: result[options.key] | ||
}]}; | ||
} else { | ||
return Promise.reject(new errors.NotFoundError('Invalid key')); | ||
} | ||
}); | ||
}, function () { | ||
return Promise.reject(new errors.NoPermissionError('You do not have permission to read the configuration.')); | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = configuration; |
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
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,75 @@ | ||
/*globals describe, before, beforeEach, afterEach, it */ | ||
/*jshint expr:true*/ | ||
var testUtils = require('../../utils'), | ||
should = require('should'), | ||
sinon = require('sinon'), | ||
|
||
|
||
rewire = require('rewire'), | ||
_ = require('lodash'), | ||
config = rewire('../../../server/config'), | ||
|
||
// Stuff we are testing | ||
ConfigurationAPI = rewire('../../../server/api/configuration'); | ||
|
||
describe('Configuration API', function () { | ||
var newConfig = { | ||
'fileStorage': true, | ||
'apps': true, | ||
'version': '0.5.0', | ||
'environment': process.env.NODE_ENV, | ||
'database': { | ||
'client': 'mysql' | ||
}, | ||
'mail': { | ||
'transport': 'SMTP' | ||
}, | ||
'blogUrl': 'http://local.tryghost.org' | ||
}; | ||
|
||
// Keep the DB clean | ||
before(testUtils.teardown); | ||
afterEach(testUtils.teardown); | ||
|
||
beforeEach(testUtils.setup('users','users:roles', 'perms:user', 'perms:role', 'perms:configuration', 'perms:init')); | ||
|
||
should.exist(ConfigurationAPI); | ||
|
||
it('can browse config', function (done) { | ||
var existingConfig = ConfigurationAPI.__get__('config'), | ||
updatedConfig = _.extend(config, newConfig); | ||
config.set(updatedConfig); | ||
ConfigurationAPI.__set__('config', updatedConfig); | ||
|
||
ConfigurationAPI.browse(testUtils.context.owner).then(function (response) { | ||
should.exist(response); | ||
should.exist(response.configuration); | ||
testUtils.API.checkResponse(response.configuration[0], 'configuration'); | ||
/*jshint unused:false */ | ||
done(); | ||
}).catch(function (error) { | ||
console.log(JSON.stringify(error)); | ||
done(); | ||
}).catch(done); | ||
}); | ||
|
||
it('can read config', function (done) { | ||
var existingConfig = ConfigurationAPI.__get__('config'), | ||
updatedConfig = _.extend(config, newConfig); | ||
config.set(updatedConfig); | ||
ConfigurationAPI.__set__('config', updatedConfig); | ||
|
||
ConfigurationAPI.read(_.extend(testUtils.context.owner, { key: 'database' })).then(function (response) { | ||
should.exist(response); | ||
should.exist(response.configuration); | ||
testUtils.API.checkResponse(response.configuration[0], 'configuration'); | ||
response.configuration[0].key.should.equal('database'); | ||
response.configuration[0].value.should.equal('mysql'); | ||
/*jshint unused:false */ | ||
done(); | ||
}).catch(function (error) { | ||
console.log(JSON.stringify(error)); | ||
done(); | ||
}).catch(done); | ||
}); | ||
}); |
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