2022-02-23 13:07:41 +03:00
|
|
|
const assert = require('assert');
|
2018-10-13 01:48:49 +03:00
|
|
|
const cheerio = require('cheerio');
|
|
|
|
const moment = require('moment');
|
2022-03-02 19:00:22 +03:00
|
|
|
const testUtils = require('../../utils');
|
|
|
|
const models = require('../../../core/server/models');
|
2022-02-23 13:07:41 +03:00
|
|
|
|
|
|
|
const {agentProvider, fixtureManager, matchers} = require('../../utils/e2e-framework');
|
2022-03-01 14:17:13 +03:00
|
|
|
const {anyArray, anyEtag, anyUuid, anyISODateTimeWithTZ} = matchers;
|
2022-02-23 13:07:41 +03:00
|
|
|
|
|
|
|
const postMatcher = {
|
2022-03-01 14:17:13 +03:00
|
|
|
published_at: anyISODateTimeWithTZ,
|
|
|
|
created_at: anyISODateTimeWithTZ,
|
|
|
|
updated_at: anyISODateTimeWithTZ,
|
2022-02-23 13:07:41 +03:00
|
|
|
uuid: anyUuid
|
|
|
|
};
|
|
|
|
|
|
|
|
const postMatcheShallowIncludes = Object.assign(
|
|
|
|
{},
|
|
|
|
postMatcher, {
|
|
|
|
tags: anyArray,
|
|
|
|
authors: anyArray
|
|
|
|
}
|
|
|
|
);
|
2018-10-13 01:48:49 +03:00
|
|
|
|
2019-02-04 17:49:59 +03:00
|
|
|
describe('Posts Content API', function () {
|
2022-02-23 13:07:41 +03:00
|
|
|
let agent;
|
2020-11-30 17:25:22 +03:00
|
|
|
|
|
|
|
before(async function () {
|
2022-02-23 13:07:41 +03:00
|
|
|
agent = await agentProvider.getContentAPIAgent();
|
|
|
|
await fixtureManager.init('owner:post', 'users:no-owner', 'user:inactive', 'posts', 'tags:extra', 'api_keys');
|
|
|
|
agent.authenticate();
|
2018-10-13 01:48:49 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can request posts', async function () {
|
2022-02-23 13:07:41 +03:00
|
|
|
const res = await agent.get('posts/')
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
posts: new Array(11)
|
|
|
|
.fill(postMatcher)
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(res.body.posts[0].slug, 'welcome', 'Default order "published_at desc" check');
|
|
|
|
assert.equal(res.body.posts[6].slug, 'integrations', 'Default order "published_at desc" check');
|
2020-11-30 17:25:22 +03:00
|
|
|
|
|
|
|
// kitchen sink
|
2022-02-23 13:07:41 +03:00
|
|
|
assert.equal(res.body.posts[9].slug, fixtureManager.get('posts', 1).slug);
|
2020-11-30 17:25:22 +03:00
|
|
|
|
2022-02-23 13:07:41 +03:00
|
|
|
let urlParts = new URL(res.body.posts[9].feature_image);
|
|
|
|
assert.equal(urlParts.protocol, 'http:');
|
|
|
|
assert.equal(urlParts.host, '127.0.0.1:2369');
|
2020-11-30 17:25:22 +03:00
|
|
|
|
2022-02-23 13:07:41 +03:00
|
|
|
urlParts = new URL(res.body.posts[9].url);
|
|
|
|
assert.equal(urlParts.protocol, 'http:');
|
|
|
|
assert.equal(urlParts.host, '127.0.0.1:2369');
|
2020-11-30 17:25:22 +03:00
|
|
|
|
|
|
|
const $ = cheerio.load(res.body.posts[9].html);
|
2022-02-23 13:07:41 +03:00
|
|
|
urlParts = new URL($('img').attr('src'));
|
|
|
|
assert.equal(urlParts.protocol, 'http:');
|
|
|
|
assert.equal(urlParts.host, '127.0.0.1:2369');
|
|
|
|
|
|
|
|
assert.equal(res.body.posts[7].slug, 'not-so-short-bit-complex');
|
|
|
|
assert.match(res.body.posts[7].html, /<a href="http:\/\/127.0.0.1:2369\/about#nowhere" title="Relative URL/);
|
|
|
|
assert.equal(res.body.posts[9].slug, 'ghostly-kitchen-sink');
|
|
|
|
assert.match(res.body.posts[9].html, /<img src="http:\/\/127.0.0.1:2369\/content\/images\/lol.jpg"/);
|
2018-10-13 01:48:49 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can filter posts by tag', async function () {
|
2022-02-23 13:07:41 +03:00
|
|
|
const res = await agent.get('posts/?filter=tag:kitchen-sink,featured:true&include=tags')
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
posts: new Array(4)
|
|
|
|
.fill(postMatcher)
|
|
|
|
});
|
2020-11-30 17:25:22 +03:00
|
|
|
|
|
|
|
const jsonResponse = res.body;
|
2022-02-23 13:07:41 +03:00
|
|
|
const ids = jsonResponse.posts.map(p => p.id);
|
|
|
|
|
|
|
|
assert.equal(jsonResponse.posts.length, 4);
|
|
|
|
assert.deepEqual(ids, [
|
|
|
|
fixtureManager.get('posts', 4).id,
|
|
|
|
fixtureManager.get('posts', 2).id,
|
|
|
|
fixtureManager.get('posts', 1).id,
|
|
|
|
fixtureManager.get('posts', 0).id
|
|
|
|
], 'Should have content filtered and ordered');
|
|
|
|
|
|
|
|
jsonResponse.posts.forEach((post) => {
|
2020-11-30 17:25:22 +03:00
|
|
|
if (post.featured) {
|
2022-02-23 13:07:41 +03:00
|
|
|
assert.equal(post.featured, true, `Each post must either be featured or have the tag 'kitchen-sink'`);
|
2020-11-30 17:25:22 +03:00
|
|
|
} else {
|
2022-02-23 13:07:41 +03:00
|
|
|
const tag = post.tags
|
|
|
|
.map(t => t.slug)
|
|
|
|
.filter(s => s === 'kitchen-sink');
|
|
|
|
assert.equal(tag, 'kitchen-sink', `Each post must either be featured or have the tag 'kitchen-sink'`);
|
2020-11-30 17:25:22 +03:00
|
|
|
}
|
|
|
|
});
|
2018-10-13 01:48:49 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can filter posts by authors', async function () {
|
2022-02-23 13:07:41 +03:00
|
|
|
const res = await agent
|
|
|
|
.get('posts/?filter=authors:[joe-bloggs,pat,ghost,slimer-mcectoplasm]&include=authors')
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
posts: new Array(11)
|
|
|
|
.fill(postMatcher)
|
|
|
|
});
|
2020-11-30 17:25:22 +03:00
|
|
|
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
|
2022-02-23 13:07:41 +03:00
|
|
|
assert.equal(jsonResponse.posts[0].slug, 'not-so-short-bit-complex', 'The API orders by number of matched authors');
|
2020-11-30 17:25:22 +03:00
|
|
|
|
2022-02-23 13:07:41 +03:00
|
|
|
const primaryAuthors = jsonResponse.posts.map((post) => {
|
2020-11-30 17:25:22 +03:00
|
|
|
return post.primary_author.slug;
|
|
|
|
});
|
2022-02-23 13:07:41 +03:00
|
|
|
const ghostPrimaryAuthors = primaryAuthors.filter((value) => {
|
2020-11-30 17:25:22 +03:00
|
|
|
return value === 'ghost';
|
2022-02-23 13:07:41 +03:00
|
|
|
});
|
|
|
|
const joePrimaryAuthors = primaryAuthors.filter((value) => {
|
2020-11-30 17:25:22 +03:00
|
|
|
return value === 'joe-bloggs';
|
2022-02-23 13:07:41 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(ghostPrimaryAuthors.length, 7, `Each post must either have the author 'joe-bloggs' or 'ghost', 'pat' is non existing author`);
|
|
|
|
assert.equal(joePrimaryAuthors.length, 4, `Each post must either have the author 'joe-bloggs' or 'ghost', 'pat' is non existing author`);
|
2018-10-13 01:48:49 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can request fields of posts', async function () {
|
2022-02-23 13:07:41 +03:00
|
|
|
await agent
|
|
|
|
.get('posts/?&fields=url')
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot();
|
Fixed backward compatibility for `send_email_when_published` (#12357)
no-issue
* Handled send_email_when_published in Posts API
This restores backwards compatibility of the Posts API allowing existing
clients to continue to use the `send_email_when_published` flag. This
change uses two edits, which is unfortunate. The reason being is that
this is an API compatibility issue, not a model issue, so we shouldn't
introduce code to the model layer to handle it. The visibility property
of the model is used to determine how to fall back, and because it can
be left out of the API request, and relies on a default in the settings,
we require that the model decide on the `visibility` before we run our
fallback logic (or we duplicate the `visibility` default at the cost of
maintenance in the future)
* Dropped send_email_when_published column from posts
Since this column is not used any more, we can drop it from the table.
We include an extra migration to repopulate the column in the event of
a rollback
* Updated importer to handle send_email_when_published
Because we currently export this value from Ghost, we should correctly
import it. This follows the same logic as the migrations for this value.
* Included send_email_when_published in API response
As our v3 API documentation includes `send_email_when_published` we must
retain backward compatibility by calculating the property.
* Fixed fields filter with send_email_when_published
* Added safety checks to frame properties
Some parts of the code pass a manually created "frame" which is missing
lots of properties, so we check for the existence of all of them before
using them.
* Fixed 3.1 migration to include columnDefinition
We require that migrations have all the information they need contained
within them as they run in an unknown state of the codebase, which could
be from the commit they are introduced, to any future commit. In this
case the column definition is removed from the schema in 3.38 and the
migration would fail when run in this version or later.
2020-11-11 16:03:41 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can include relations', async function () {
|
2022-02-23 13:07:41 +03:00
|
|
|
await agent
|
|
|
|
.get('posts/?include=tags,authors')
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
posts: new Array(11)
|
|
|
|
.fill(postMatcheShallowIncludes)
|
|
|
|
});
|
2018-10-13 01:48:49 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can request posts from different origin', async function () {
|
2022-02-23 13:07:41 +03:00
|
|
|
await agent
|
|
|
|
.get('posts/')
|
|
|
|
.header('Origin', 'https://example.com')
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
posts: new Array(11)
|
|
|
|
.fill(postMatcher)
|
|
|
|
});
|
2018-10-13 01:48:49 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can filter by published date', async function () {
|
2018-10-13 01:48:49 +03:00
|
|
|
function createFilter(publishedAt, op) {
|
|
|
|
// This line deliberately uses double quotes because GQL cannot handle either double quotes
|
|
|
|
// or escaped singles, see TryGhost/GQL#34
|
2019-08-19 14:41:09 +03:00
|
|
|
return encodeURIComponent('published_at:' + op + '\'' + publishedAt + '\'');
|
2018-10-13 01:48:49 +03:00
|
|
|
}
|
|
|
|
|
2022-02-23 13:07:41 +03:00
|
|
|
const res = await agent
|
|
|
|
.get('posts/?limit=1')
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
posts: new Array(1)
|
|
|
|
.fill(postMatcher)
|
|
|
|
});
|
2020-11-30 17:25:22 +03:00
|
|
|
|
|
|
|
const post = res.body.posts[0];
|
|
|
|
const publishedAt = moment(post.published_at).format('YYYY-MM-DD HH:mm:ss');
|
|
|
|
|
2022-02-23 13:07:41 +03:00
|
|
|
const res2 = await agent
|
|
|
|
.get(`posts/?limit=1&filter=${createFilter(publishedAt, `<`)}`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
posts: new Array(1)
|
|
|
|
.fill(postMatcher)
|
|
|
|
});
|
2020-11-30 17:25:22 +03:00
|
|
|
|
|
|
|
const post2 = res2.body.posts[0];
|
|
|
|
const publishedAt2 = moment(post2.published_at).format('YYYY-MM-DD HH:mm:ss');
|
|
|
|
|
2022-02-23 13:07:41 +03:00
|
|
|
assert.equal(post2.title, 'Customizing your brand and design settings');
|
2020-11-30 17:25:22 +03:00
|
|
|
|
2022-02-23 13:07:41 +03:00
|
|
|
const res3 = await agent
|
|
|
|
.get(`posts/?limit=1&filter=${createFilter(publishedAt2, `>`)}`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
posts: new Array(1)
|
|
|
|
.fill(postMatcher)
|
|
|
|
});
|
2020-11-30 17:25:22 +03:00
|
|
|
|
|
|
|
const post3 = res3.body.posts[0];
|
2022-02-23 13:07:41 +03:00
|
|
|
assert.equal(post3.title, 'Start here for a quick overview of everything you need to know');
|
2018-10-13 01:48:49 +03:00
|
|
|
});
|
2018-10-19 11:40:47 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can request a single post', async function () {
|
2022-02-23 13:07:41 +03:00
|
|
|
await agent
|
|
|
|
.get(`posts/${fixtureManager.get('posts', 0).id}/`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
posts: new Array(1)
|
|
|
|
.fill(postMatcher)
|
|
|
|
});
|
2018-10-19 11:40:47 +03:00
|
|
|
});
|
2022-03-02 19:00:22 +03:00
|
|
|
|
|
|
|
it('Can include free and paid tiers for public post', async function () {
|
|
|
|
const publicPost = testUtils.DataGenerator.forKnex.createPost({
|
|
|
|
slug: 'free-to-see',
|
|
|
|
visibility: 'public',
|
|
|
|
published_at: moment().add(15, 'seconds').toDate() // here to ensure sorting is not modified
|
|
|
|
});
|
|
|
|
await models.Post.add(publicPost, {context: {internal: true}});
|
|
|
|
|
|
|
|
const publicPostRes = await agent
|
|
|
|
.get(`posts/${publicPost.id}/?include=tiers`)
|
|
|
|
.expectStatus(200);
|
|
|
|
const publicPostData = publicPostRes.body.posts[0];
|
|
|
|
publicPostData.tiers.length.should.eql(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can include free and paid tiers for members only post', async function () {
|
|
|
|
const membersPost = testUtils.DataGenerator.forKnex.createPost({
|
|
|
|
slug: 'thou-shalt-not-be-seen',
|
|
|
|
visibility: 'members',
|
|
|
|
published_at: moment().add(45, 'seconds').toDate() // here to ensure sorting is not modified
|
|
|
|
});
|
|
|
|
await models.Post.add(membersPost, {context: {internal: true}});
|
|
|
|
|
|
|
|
const membersPostRes = await agent
|
|
|
|
.get(`posts/${membersPost.id}/?include=tiers`)
|
|
|
|
.expectStatus(200);
|
|
|
|
const membersPostData = membersPostRes.body.posts[0];
|
|
|
|
membersPostData.tiers.length.should.eql(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can include only paid tier for paid post', async function () {
|
|
|
|
const paidPost = testUtils.DataGenerator.forKnex.createPost({
|
|
|
|
slug: 'thou-shalt-be-paid-for',
|
|
|
|
visibility: 'paid',
|
|
|
|
published_at: moment().add(30, 'seconds').toDate() // here to ensure sorting is not modified
|
|
|
|
});
|
|
|
|
await models.Post.add(paidPost, {context: {internal: true}});
|
|
|
|
|
|
|
|
const paidPostRes = await agent
|
|
|
|
.get(`posts/${paidPost.id}/?include=tiers`)
|
|
|
|
.expectStatus(200);
|
|
|
|
const paidPostData = paidPostRes.body.posts[0];
|
|
|
|
paidPostData.tiers.length.should.eql(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can include specific tier for post with tiers visibility', async function () {
|
|
|
|
const res = await agent
|
|
|
|
.get(`products/`)
|
|
|
|
.expectStatus(200);
|
|
|
|
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
const paidTier = jsonResponse.products.find(p => p.type === 'paid');
|
|
|
|
|
|
|
|
const tiersPost = testUtils.DataGenerator.forKnex.createPost({
|
|
|
|
slug: 'thou-shalt-be-for-specific-tiers',
|
|
|
|
visibility: 'tiers',
|
|
|
|
published_at: moment().add(30, 'seconds').toDate() // here to ensure sorting is not modified
|
|
|
|
});
|
|
|
|
|
|
|
|
tiersPost.tiers = [paidTier];
|
|
|
|
|
|
|
|
await models.Post.add(tiersPost, {context: {internal: true}});
|
|
|
|
|
|
|
|
const tiersPostRes = await agent
|
|
|
|
.get(`posts/${tiersPost.id}/?include=tiers`)
|
|
|
|
.expectStatus(200);
|
|
|
|
|
|
|
|
const tiersPostData = tiersPostRes.body.posts[0];
|
|
|
|
|
|
|
|
tiersPostData.tiers.length.should.eql(1);
|
|
|
|
});
|
2018-10-13 01:48:49 +03:00
|
|
|
});
|