2015-04-18 00:27:04 +03:00
|
|
|
var _ = require('lodash'),
|
|
|
|
url = require('url'),
|
2014-08-22 11:58:21 +04:00
|
|
|
moment = require('moment'),
|
|
|
|
config = require('../../server/config'),
|
2016-02-14 15:46:09 +03:00
|
|
|
schema = require('../../server/data/schema').tables,
|
2014-08-22 11:58:21 +04:00
|
|
|
ApiRouteBase = '/ghost/api/v0.1/',
|
|
|
|
host = config.server.host,
|
|
|
|
port = config.server.port,
|
2016-02-14 15:46:09 +03:00
|
|
|
protocol = 'http://',
|
2013-11-07 13:34:18 +04:00
|
|
|
expectedProperties = {
|
2016-02-14 15:46:09 +03:00
|
|
|
// API top level
|
|
|
|
posts: ['posts', 'meta'],
|
|
|
|
tags: ['tags', 'meta'],
|
|
|
|
users: ['users', 'meta'],
|
|
|
|
settings: ['settings', 'meta'],
|
|
|
|
roles: ['roles'],
|
|
|
|
pagination: ['page', 'limit', 'pages', 'total', 'next', 'prev'],
|
|
|
|
slugs: ['slugs'],
|
|
|
|
slug: ['slug'],
|
|
|
|
// object / model level
|
|
|
|
// Post API swaps author_id to author, and always returns a computed 'url' property
|
|
|
|
post: _(schema.posts).keys().without('author_id').concat('author', 'url').value(),
|
|
|
|
// User API always removes the password field
|
|
|
|
user: _(schema.users).keys().without('password').value(),
|
|
|
|
// Tag API swaps parent_id to parent
|
|
|
|
tag: _(schema.tags).keys().without('parent_id').concat('parent').value(),
|
|
|
|
setting: _.keys(schema.settings),
|
|
|
|
accesstoken: _.keys(schema.accesstokens),
|
|
|
|
role: _.keys(schema.roles),
|
|
|
|
permission: _.keys(schema.permissions),
|
2014-06-30 01:45:03 +04:00
|
|
|
notification: ['type', 'message', 'status', 'id', 'dismissible', 'location'],
|
2016-02-14 15:46:09 +03:00
|
|
|
theme: ['uuid', 'name', 'version', 'active']
|
2013-11-07 13:34:18 +04:00
|
|
|
};
|
|
|
|
|
2014-04-29 00:42:38 +04:00
|
|
|
function getApiQuery(route) {
|
2014-04-07 17:50:18 +04:00
|
|
|
return url.resolve(ApiRouteBase, route);
|
|
|
|
}
|
2013-11-03 21:13:19 +04:00
|
|
|
|
2014-04-29 00:42:38 +04:00
|
|
|
function getApiURL(route) {
|
2016-02-14 15:46:09 +03:00
|
|
|
var baseURL = url.resolve(protocol + host + ':' + port, ApiRouteBase);
|
2013-11-03 21:13:19 +04:00
|
|
|
return url.resolve(baseURL, route);
|
2013-10-08 05:39:33 +04:00
|
|
|
}
|
2016-02-14 15:46:09 +03:00
|
|
|
|
|
|
|
function getURL() {
|
|
|
|
return protocol + host;
|
|
|
|
}
|
|
|
|
|
2014-04-29 00:42:38 +04:00
|
|
|
function getSigninURL() {
|
2016-02-14 15:46:09 +03:00
|
|
|
return url.resolve(protocol + host + ':' + port, 'ghost/signin/');
|
2013-10-08 05:39:33 +04:00
|
|
|
}
|
2016-02-14 15:46:09 +03:00
|
|
|
|
2014-04-29 00:42:38 +04:00
|
|
|
function getAdminURL() {
|
2016-02-14 15:46:09 +03:00
|
|
|
return url.resolve(protocol + host + ':' + port, 'ghost/');
|
|
|
|
}
|
|
|
|
|
|
|
|
function isISO8601(date) {
|
|
|
|
return moment(date).parsingFlags().iso;
|
2013-11-24 18:29:36 +04:00
|
|
|
}
|
2013-10-08 05:39:33 +04:00
|
|
|
|
2013-11-03 21:13:19 +04:00
|
|
|
// make sure the API only returns expected properties only
|
2016-02-14 15:46:09 +03:00
|
|
|
function checkResponseValue(jsonResponse, expectedProperties) {
|
|
|
|
var providedProperties = _.keys(jsonResponse),
|
|
|
|
missing = _.difference(expectedProperties, providedProperties),
|
|
|
|
unexpected = _.difference(providedProperties, expectedProperties);
|
|
|
|
|
|
|
|
_.each(missing, function (prop) {
|
|
|
|
jsonResponse.should.have.property(prop);
|
|
|
|
});
|
|
|
|
|
|
|
|
_.each(unexpected, function (prop) {
|
|
|
|
jsonResponse.should.not.have.property(prop);
|
|
|
|
});
|
|
|
|
|
|
|
|
providedProperties.length.should.eql(expectedProperties.length);
|
2013-10-08 05:39:33 +04:00
|
|
|
}
|
|
|
|
|
2015-04-18 00:27:04 +03:00
|
|
|
function checkResponse(jsonResponse, objectType, additionalProperties, missingProperties) {
|
2014-07-08 20:00:59 +04:00
|
|
|
var checkProperties = expectedProperties[objectType];
|
|
|
|
checkProperties = additionalProperties ? checkProperties.concat(additionalProperties) : checkProperties;
|
2015-04-18 00:27:04 +03:00
|
|
|
checkProperties = missingProperties ? _.xor(checkProperties, missingProperties) : checkProperties;
|
2014-07-08 20:00:59 +04:00
|
|
|
|
|
|
|
checkResponseValue(jsonResponse, checkProperties);
|
2014-04-29 00:42:38 +04:00
|
|
|
}
|
|
|
|
|
2013-11-07 13:34:18 +04:00
|
|
|
module.exports = {
|
|
|
|
getApiURL: getApiURL,
|
2014-04-07 17:50:18 +04:00
|
|
|
getApiQuery: getApiQuery,
|
2013-11-07 13:34:18 +04:00
|
|
|
getSigninURL: getSigninURL,
|
2013-11-24 18:29:36 +04:00
|
|
|
getAdminURL: getAdminURL,
|
2015-10-22 16:28:47 +03:00
|
|
|
getURL: getURL,
|
2013-11-07 13:34:18 +04:00
|
|
|
checkResponse: checkResponse,
|
2014-07-05 18:57:56 +04:00
|
|
|
checkResponseValue: checkResponseValue,
|
|
|
|
isISO8601: isISO8601
|
2013-11-07 13:34:18 +04:00
|
|
|
};
|