2019-02-04 17:16:24 +03:00
|
|
|
const url = require('url');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const testUtils = require('../../../../utils');
|
2020-03-30 18:26:47 +03:00
|
|
|
const schema = require('../../../../../core/server/data/schema').tables;
|
2019-02-04 17:16:24 +03:00
|
|
|
const API_URL = '/ghost/api/v2/admin/';
|
|
|
|
|
|
|
|
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'],
|
|
|
|
invites: ['invites', 'meta'],
|
|
|
|
themes: ['themes'],
|
|
|
|
|
|
|
|
post: _(schema.posts)
|
|
|
|
.keys()
|
2019-02-25 12:44:19 +03:00
|
|
|
// by default we only return mobiledoc
|
|
|
|
.without('html', 'plaintext')
|
2019-02-13 13:41:14 +03:00
|
|
|
.without('visibility')
|
|
|
|
.without('locale')
|
2019-02-22 06:17:14 +03:00
|
|
|
.without('page')
|
2019-09-17 18:26:23 +03:00
|
|
|
.without('author_id', 'author')
|
2019-12-16 15:19:08 +03:00
|
|
|
// emails are not supported in API v2
|
2020-11-06 20:32:23 +03:00
|
|
|
.without('email_recipient_filter')
|
2019-02-25 13:08:28 +03:00
|
|
|
// always returns computed properties
|
|
|
|
.concat('url', 'primary_tag', 'primary_author', 'excerpt')
|
|
|
|
.concat('authors', 'tags')
|
2019-09-16 11:45:55 +03:00
|
|
|
// returns meta fields from `posts_meta` schema
|
|
|
|
.concat(
|
2019-12-16 15:19:08 +03:00
|
|
|
..._(schema.posts_meta).keys()
|
|
|
|
.without('post_id', 'id')
|
|
|
|
// emails are not supported in API v2
|
|
|
|
.without('email_subject')
|
2019-09-16 11:45:55 +03:00
|
|
|
)
|
2019-02-04 17:16:24 +03:00
|
|
|
,
|
|
|
|
user: _(schema.users)
|
|
|
|
.keys()
|
2019-02-13 13:41:14 +03:00
|
|
|
.without('visibility')
|
2019-02-04 17:16:24 +03:00
|
|
|
.without('password')
|
2019-02-13 13:41:14 +03:00
|
|
|
.without('locale')
|
2019-02-13 01:36:42 +03:00
|
|
|
.without('ghost_auth_id')
|
2019-07-24 21:22:08 +03:00
|
|
|
.concat('url')
|
2019-02-04 17:16:24 +03:00
|
|
|
,
|
|
|
|
tag: _(schema.tags)
|
|
|
|
.keys()
|
2019-02-12 21:26:31 +03:00
|
|
|
// unused field
|
|
|
|
.without('parent_id')
|
2019-02-04 17:16:24 +03:00
|
|
|
,
|
|
|
|
setting: _(schema.settings)
|
|
|
|
.keys()
|
|
|
|
,
|
|
|
|
subscriber: _(schema.subscribers)
|
|
|
|
.keys()
|
|
|
|
,
|
2019-10-03 12:15:50 +03:00
|
|
|
member: _(schema.members)
|
|
|
|
.keys()
|
|
|
|
,
|
2019-02-04 17:16:24 +03:00
|
|
|
role: _(schema.roles)
|
|
|
|
.keys()
|
|
|
|
,
|
|
|
|
permission: _(schema.permissions)
|
|
|
|
.keys()
|
|
|
|
,
|
|
|
|
notification: ['type', 'message', 'status', 'id', 'dismissible', 'location', 'custom'],
|
|
|
|
theme: ['name', 'package', 'active'],
|
|
|
|
invite: _(schema.invites)
|
|
|
|
.keys()
|
|
|
|
.without('token')
|
|
|
|
,
|
|
|
|
webhook: _(schema.webhooks)
|
2019-08-19 14:41:09 +03:00
|
|
|
.keys()
|
2019-02-04 17:16:24 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
_.each(expectedProperties, (value, key) => {
|
|
|
|
if (!value.__wrapped__) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated: x_by
|
|
|
|
*/
|
|
|
|
expectedProperties[key] = value
|
|
|
|
.without(
|
|
|
|
'created_by',
|
|
|
|
'updated_by',
|
|
|
|
'published_by'
|
|
|
|
)
|
|
|
|
.value();
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
API: {
|
|
|
|
getApiQuery(route) {
|
|
|
|
return url.resolve(API_URL, route);
|
|
|
|
},
|
|
|
|
|
|
|
|
checkResponse(...args) {
|
|
|
|
this.expectedProperties = expectedProperties;
|
|
|
|
return testUtils.API.checkResponse.call(this, ...args);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
doAuth(...args) {
|
|
|
|
return testUtils.API.doAuth(`${API_URL}session/`, ...args);
|
|
|
|
},
|
|
|
|
|
2019-07-31 23:34:49 +03:00
|
|
|
getValidAdminToken(endpoint, key) {
|
2019-02-04 17:16:24 +03:00
|
|
|
const jwt = require('jsonwebtoken');
|
2019-07-31 23:34:49 +03:00
|
|
|
key = key || testUtils.DataGenerator.Content.api_keys[0];
|
|
|
|
|
2019-02-04 17:16:24 +03:00
|
|
|
const JWT_OPTIONS = {
|
2019-07-31 23:34:49 +03:00
|
|
|
keyid: key.id,
|
2019-02-04 17:16:24 +03:00
|
|
|
algorithm: 'HS256',
|
|
|
|
expiresIn: '5m',
|
|
|
|
audience: endpoint
|
|
|
|
};
|
|
|
|
|
|
|
|
return jwt.sign(
|
2019-02-26 07:03:47 +03:00
|
|
|
{},
|
2019-07-31 23:34:49 +03:00
|
|
|
Buffer.from(key.secret, 'hex'),
|
2019-02-04 17:16:24 +03:00
|
|
|
JWT_OPTIONS
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|