2018-10-07 17:36:02 +03:00
|
|
|
const url = require('url');
|
|
|
|
const _ = require('lodash');
|
2019-01-22 19:54:50 +03:00
|
|
|
const testUtils = require('../../../utils');
|
|
|
|
const schema = require('../../../../server/data/schema').tables;
|
2018-10-07 17:36:02 +03:00
|
|
|
const API_URL = '/ghost/api/v2/admin/';
|
|
|
|
|
2018-12-17 18:14:36 +03:00
|
|
|
const expectedProperties = {
|
|
|
|
// API top level
|
|
|
|
posts: ['posts', 'meta'],
|
|
|
|
tags: ['tags', 'meta'],
|
|
|
|
users: ['users', 'meta'],
|
|
|
|
settings: ['settings', 'meta'],
|
|
|
|
subscribers: ['subscribers', 'meta'],
|
|
|
|
roles: ['roles'],
|
|
|
|
pagination: ['page', 'limit', 'pages', 'total', 'next', 'prev'],
|
|
|
|
slugs: ['slugs'],
|
|
|
|
slug: ['slug'],
|
2018-12-17 19:45:07 +03:00
|
|
|
invites: ['invites', 'meta'],
|
|
|
|
themes: ['themes'],
|
|
|
|
|
2018-12-17 18:14:36 +03:00
|
|
|
post: _(schema.posts)
|
|
|
|
.keys()
|
|
|
|
// by default we only return html
|
|
|
|
.without('mobiledoc', 'plaintext')
|
2019-01-31 17:36:39 +03:00
|
|
|
// always returns computed properties: url, comment_id, primary_tag, primary_author
|
|
|
|
.without('author_id').concat('url', 'primary_tag', 'primary_author')
|
2018-12-17 19:45:07 +03:00
|
|
|
,
|
|
|
|
user: _(schema.users)
|
|
|
|
.keys()
|
|
|
|
.without('password')
|
|
|
|
.without('ghost_auth_access_token')
|
|
|
|
,
|
|
|
|
tag: _(schema.tags)
|
|
|
|
.keys()
|
|
|
|
// Tag API swaps parent_id to parent
|
|
|
|
.without('parent_id').concat('parent')
|
|
|
|
,
|
|
|
|
setting: _(schema.settings)
|
|
|
|
.keys()
|
|
|
|
,
|
|
|
|
subscriber: _(schema.subscribers)
|
|
|
|
.keys()
|
|
|
|
,
|
|
|
|
accesstoken: _(schema.accesstokens)
|
|
|
|
.keys()
|
|
|
|
,
|
|
|
|
role: _(schema.roles)
|
|
|
|
.keys()
|
|
|
|
,
|
|
|
|
permission: _(schema.permissions)
|
|
|
|
.keys()
|
|
|
|
,
|
2018-12-17 18:14:36 +03:00
|
|
|
notification: ['type', 'message', 'status', 'id', 'dismissible', 'location', 'custom'],
|
|
|
|
theme: ['name', 'package', 'active'],
|
2018-12-17 19:45:07 +03:00
|
|
|
invite: _(schema.invites)
|
|
|
|
.keys()
|
|
|
|
.without('token')
|
|
|
|
,
|
2018-12-17 18:14:36 +03:00
|
|
|
webhook: _(schema.webhooks)
|
|
|
|
.keys()
|
|
|
|
.without(
|
|
|
|
'name',
|
|
|
|
'last_triggered_at',
|
|
|
|
'last_triggered_error',
|
|
|
|
'last_triggered_status',
|
|
|
|
'secret',
|
|
|
|
'integration_id'
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2018-12-17 19:45:07 +03:00
|
|
|
_.each(expectedProperties, (value, key) => {
|
|
|
|
if (!value.__wrapped__) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated: x_by
|
|
|
|
*/
|
|
|
|
expectedProperties[key] = value
|
|
|
|
.without(
|
|
|
|
'created_by',
|
|
|
|
'updated_by',
|
|
|
|
'published_by'
|
|
|
|
)
|
|
|
|
.value();
|
|
|
|
});
|
|
|
|
|
2018-10-07 17:36:02 +03:00
|
|
|
module.exports = {
|
|
|
|
API: {
|
|
|
|
getApiQuery(route) {
|
|
|
|
return url.resolve(API_URL, route);
|
2018-12-17 18:14:36 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
checkResponse(...args) {
|
|
|
|
this.expectedProperties = expectedProperties;
|
|
|
|
return testUtils.API.checkResponse.call(this, ...args);
|
2018-10-07 17:36:02 +03:00
|
|
|
}
|
2018-10-09 06:24:41 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
doAuth(...args) {
|
|
|
|
return testUtils.API.doAuth(`${API_URL}session/`, ...args);
|
2019-01-18 19:36:11 +03:00
|
|
|
},
|
|
|
|
|
2019-01-18 21:11:51 +03:00
|
|
|
getValidAdminToken(endpoint) {
|
2019-01-18 19:36:11 +03:00
|
|
|
const jwt = require('jsonwebtoken');
|
|
|
|
const JWT_OPTIONS = {
|
2019-01-23 15:22:47 +03:00
|
|
|
algorithm: 'HS256',
|
2019-01-23 20:08:57 +03:00
|
|
|
expiresIn: '5m',
|
2019-01-23 15:22:47 +03:00
|
|
|
audience: endpoint
|
2019-01-18 19:36:11 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return jwt.sign(
|
|
|
|
{
|
|
|
|
kid: testUtils.DataGenerator.Content.api_keys[0].id
|
|
|
|
},
|
|
|
|
Buffer.from(testUtils.DataGenerator.Content.api_keys[0].secret, 'hex'),
|
2019-01-23 15:22:47 +03:00
|
|
|
JWT_OPTIONS
|
2019-01-18 19:36:11 +03:00
|
|
|
);
|
2018-10-07 17:36:02 +03:00
|
|
|
}
|
|
|
|
};
|