Ghost/ghost/core/test/e2e-api/admin/utils.js

250 lines
5.9 KiB
JavaScript
Raw Normal View History

const url = require('url');
const _ = require('lodash');
const testUtils = require('../../utils');
// NOTE: the dependance on the schema here is wrong! It is a design flaw which is causing problems for API maintenance and compatibility
// whenever you need to modify any of the below property lists using schema - rework them into an "allowlist" array like it's done in
// the commit introducing this comment.
const schema = require('../../../core/server/data/schema').tables;
const API_URL = '/ghost/api/admin/';
const expectedProperties = {
// API top level
posts: ['posts', 'meta'],
pages: ['pages', 'meta'],
tags: ['tags', 'meta'],
users: ['users', 'meta'],
settings: ['settings', 'meta'],
roles: ['roles'],
pagination: ['page', 'limit', 'pages', 'total', 'next', 'prev'],
slugs: ['slugs'],
slug: ['slug'],
invites: ['invites', 'meta'],
themes: ['themes'],
actions: ['actions', 'meta'],
members: ['members', 'meta'],
snippets: ['snippets', 'meta'],
action: ['id', 'resource_type', 'actor_type', 'event', 'created_at', 'actor', 'context', 'resource_id', 'actor_id'],
config: [
'version',
'environment',
'database',
'mail',
'labs',
'clientExtensions',
'enableDeveloperExperiments',
'useGravatar',
'stripeDirect',
'emailAnalytics',
'tenor',
'mailgunIsConfigured',
'editor'
],
post: [
'id',
'uuid',
'title',
'slug',
'mobiledoc',
'comment_id',
'feature_image',
'feature_image_alt',
'feature_image_caption',
'featured',
'status',
'visibility',
Renamed newsletter_id and email_recipient_filter options (#14798) refs https://github.com/TryGhost/Team/issues/1596 - Renamed `newsletter_id` to `newsletter` option, the `newsletter` option expects a slug instead of an id - Renamed `email_recipient_filter` to `email_segment` option - Default `email_segment` to `all`. Ignored if no newsletter is set - `email_segment` is ignored if no newsletter is set - When reverting a post to a draft, both `newsletter` and `email_segment` are reset to their default values (null, all) - Removed legacy mapping from old email_recipient_filter values 'paid' and 'free' (already a migration in place) - Dropped legacy throwing errors when email_recipient_filter is paid or free in transformEmailRecipientFilter - Reorganized transformEmailRecipientFilter parameters for the now required newsletter parameter - Fixed an issue where the newsletter filter wasn't working because it wasn't in permittedoptions - Fixed an issue where you could send to an archived newsletter - Added an extra protection when scheduling to an active, and later archiving the newsletter - Dropped support for `send_email_when_published` in API - When importing posts we currently don't have a system in place to set the newsletter_id to map the `send_email_when_published` behaviour. Since this was already the case, I won't include a fix in this PR. - Stripped `email_recipient_filter`/`email_segment` from Content API (https://ghost.slack.com/archives/C02G9E68C/p1652363211841359?thread_ts=1650623650.233229&cid=C02G9E68C) - Updated `admin-api-schema` to 3.2.0, which includes the new email_segment property - Contains a temporary fix for https://github.com/TryGhost/Team/issues/1626, where the `.related('newsletter').fetch` call fails when the newsletter relation is already loaded, because of the overridden `formatOnWrite` method. Since the `email_recipient_filter` is no longer used without a newsletter, the `none` value is no longer used. A migration transforms all those values to `all`. This should be safe, because we only send an email now when newsletter_id is not null (scheduled posts should already have a newsletter_id, even if at the time of scheduling they didn't add the newsletter_id option, because at that time, we defaulted to the default newsletter). Admin changes to make this work: https://github.com/TryGhost/Admin/pull/2380
2022-05-16 11:18:04 +03:00
'email_segment',
'created_at',
'updated_at',
'published_at',
'custom_excerpt',
'codeinjection_head',
'codeinjection_foot',
'custom_template',
'canonical_url',
'url',
'primary_tag',
'primary_author',
'excerpt',
'tags',
'authors',
'email',
'og_image',
'og_title',
'og_description',
'twitter_image',
'twitter_title',
'twitter_description',
'meta_title',
'meta_description',
'email_subject',
'frontmatter',
'email_only',
'tiers',
'newsletter',
'count',
'post_revisions'
],
page: [
'id',
'uuid',
'title',
'slug',
'mobiledoc',
'comment_id',
'feature_image',
'feature_image_alt',
'feature_image_caption',
'featured',
'status',
'visibility',
'created_at',
'updated_at',
'published_at',
'custom_excerpt',
'codeinjection_head',
'codeinjection_foot',
'custom_template',
'canonical_url',
'url',
'primary_tag',
'primary_author',
'excerpt',
'tags',
'authors',
'og_image',
'og_title',
'og_description',
'twitter_image',
'twitter_title',
'twitter_description',
'meta_title',
'meta_description',
'frontmatter',
'tiers',
'count'
],
user: _(schema.users)
.keys()
.without('visibility')
.without('password')
.without('locale')
.concat('url')
,
tag: _(schema.tags)
.keys()
// unused field
.without('parent_id')
,
setting: _(schema.settings)
.keys()
,
member: [
'id',
'uuid',
'email',
'status',
'name',
'note',
'geolocation',
'subscribed',
'email_count',
'email_opened_count',
'email_open_rate',
'created_at',
'updated_at',
'avatar_image',
'comped',
'last_seen_at',
'labels'
],
member_signin_url: ['member_id', 'url'],
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)
.keys()
2019-11-06 09:11:06 +03:00
,
email: _(schema.emails)
.keys(),
email_previews: ['html', 'subject', 'plaintext'],
email_recipient: _(schema.email_recipients)
.keys()
.filter(key => key.indexOf('@@') === -1),
snippet: _(schema.snippets).keys()
};
_.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);
},
getValidAdminToken(audience, keyid = 0) {
const jwt = require('jsonwebtoken');
const JWT_OPTIONS = {
keyid: testUtils.DataGenerator.Content.api_keys[keyid].id,
algorithm: 'HS256',
expiresIn: '5m',
audience: audience
};
return jwt.sign(
{},
Buffer.from(testUtils.DataGenerator.Content.api_keys[keyid].secret, 'hex'),
JWT_OPTIONS
);
},
async startGhost(overrides = {}) {
const defaults = {
backend: true,
frontend: false
};
return await testUtils.startGhost(Object.assign(defaults, overrides));
}
};