2022-08-22 19:35:37 +03:00
|
|
|
const assert = require('assert');
|
|
|
|
const {agentProvider, fixtureManager, mockManager, matchers} = require('../../utils/e2e-framework');
|
2022-09-13 19:29:37 +03:00
|
|
|
const {anyArray, anyEtag, anyErrorId, anyLocationFor, anyObject, anyObjectId, anyISODateTime, anyString, anyUuid} = matchers;
|
2022-09-13 16:05:03 +03:00
|
|
|
|
|
|
|
const matchPostShallowIncludes = {
|
|
|
|
id: anyObjectId,
|
|
|
|
uuid: anyUuid,
|
2022-09-13 19:29:37 +03:00
|
|
|
comment_id: anyString,
|
2022-09-13 16:05:03 +03:00
|
|
|
url: anyString,
|
|
|
|
authors: anyArray,
|
|
|
|
primary_author: anyObject,
|
|
|
|
tags: anyArray,
|
|
|
|
primary_tag: anyObject,
|
|
|
|
tiers: anyArray,
|
|
|
|
created_at: anyISODateTime,
|
2022-09-13 16:09:45 +03:00
|
|
|
updated_at: anyISODateTime,
|
|
|
|
published_at: anyISODateTime
|
2022-09-13 16:05:03 +03:00
|
|
|
};
|
2022-08-22 19:35:37 +03:00
|
|
|
|
|
|
|
describe('Posts API', function () {
|
|
|
|
let agent;
|
|
|
|
|
|
|
|
before(async function () {
|
|
|
|
agent = await agentProvider.getAdminAPIAgent();
|
|
|
|
await fixtureManager.init('posts');
|
|
|
|
await agent.loginAsOwner();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
mockManager.restore();
|
|
|
|
});
|
|
|
|
|
2022-09-13 15:30:17 +03:00
|
|
|
it('Can browse', async function () {
|
|
|
|
const res = await agent.get('posts/?limit=2')
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
2022-09-13 16:05:03 +03:00
|
|
|
posts: new Array(2).fill(matchPostShallowIncludes)
|
2022-09-13 15:30:17 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can browse with formats', async function () {
|
|
|
|
const res = await agent.get('posts/?formats=mobiledoc,lexical,html,plaintext&limit=2')
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
2022-09-13 16:05:03 +03:00
|
|
|
posts: new Array(2).fill(matchPostShallowIncludes)
|
2022-09-13 15:30:17 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-13 19:29:37 +03:00
|
|
|
describe('Create', function () {
|
|
|
|
it('Can create a post with mobiledoc', async function () {
|
|
|
|
const post = {
|
|
|
|
title: 'Mobiledoc test',
|
|
|
|
mobiledoc: JSON.stringify({
|
|
|
|
version: '0.3.1',
|
|
|
|
ghostVersion: '4.0',
|
|
|
|
markups: [],
|
|
|
|
atoms: [],
|
|
|
|
cards: [],
|
|
|
|
sections: [
|
|
|
|
[1, 'p', [
|
|
|
|
[0, [], 0, 'Testing post creation with mobiledoc']
|
|
|
|
]]
|
|
|
|
]
|
2022-09-13 23:01:53 +03:00
|
|
|
}),
|
|
|
|
lexical: null
|
2022-09-13 19:29:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
await agent
|
2022-09-15 18:49:14 +03:00
|
|
|
.post('/posts/?formats=mobiledoc,lexical,html')
|
2022-09-13 19:29:37 +03:00
|
|
|
.body({posts: [post]})
|
|
|
|
.expectStatus(201)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
posts: [Object.assign(matchPostShallowIncludes, {published_at: null})]
|
|
|
|
})
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag,
|
|
|
|
location: anyLocationFor('posts')
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can create a post with lexical', async function () {
|
|
|
|
const post = {
|
|
|
|
title: 'Lexical test',
|
2022-09-13 23:01:53 +03:00
|
|
|
mobiledoc: null,
|
2022-09-13 19:29:37 +03:00
|
|
|
lexical: JSON.stringify({
|
2022-09-15 18:49:14 +03:00
|
|
|
root: {
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
detail: 0,
|
|
|
|
format: 0,
|
|
|
|
mode: 'normal',
|
|
|
|
style: '',
|
|
|
|
text: 'Testing post creation with lexical',
|
|
|
|
type: 'text',
|
|
|
|
version: 1
|
|
|
|
}
|
|
|
|
],
|
|
|
|
direction: 'ltr',
|
|
|
|
format: '',
|
|
|
|
indent: 0,
|
|
|
|
type: 'paragraph',
|
|
|
|
version: 1
|
|
|
|
}
|
|
|
|
],
|
|
|
|
direction: 'ltr',
|
|
|
|
format: '',
|
|
|
|
indent: 0,
|
|
|
|
type: 'root',
|
|
|
|
version: 1
|
|
|
|
}
|
2022-09-13 19:29:37 +03:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
await agent
|
2022-09-15 18:49:14 +03:00
|
|
|
.post('/posts/?formats=mobiledoc,lexical,html')
|
2022-09-13 19:29:37 +03:00
|
|
|
.body({posts: [post]})
|
|
|
|
.expectStatus(201)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
posts: [Object.assign(matchPostShallowIncludes, {published_at: null})]
|
|
|
|
})
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag,
|
|
|
|
location: anyLocationFor('posts')
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Errors if both mobiledoc and lexical are present', async function () {
|
|
|
|
const post = {
|
|
|
|
title: 'Mobiledoc+lexical test',
|
|
|
|
mobiledoc: JSON.stringify({
|
|
|
|
version: '0.3.1',
|
|
|
|
ghostVersion: '4.0',
|
|
|
|
markups: [],
|
|
|
|
atoms: [],
|
|
|
|
cards: [],
|
|
|
|
sections: [
|
|
|
|
[1, 'p', [
|
|
|
|
[0, [], 0, 'Testing post creation with mobiledoc']
|
|
|
|
]]
|
|
|
|
]
|
|
|
|
}),
|
|
|
|
lexical: JSON.stringify({
|
2022-09-15 18:49:14 +03:00
|
|
|
root: {
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
detail: 0,
|
|
|
|
format: 0,
|
|
|
|
mode: 'normal',
|
|
|
|
style: '',
|
|
|
|
text: 'Testing post creation with lexical',
|
|
|
|
type: 'text',
|
|
|
|
version: 1
|
|
|
|
}
|
|
|
|
],
|
|
|
|
direction: 'ltr',
|
|
|
|
format: '',
|
|
|
|
indent: 0,
|
|
|
|
type: 'paragraph',
|
|
|
|
version: 1
|
|
|
|
}
|
|
|
|
],
|
|
|
|
direction: 'ltr',
|
|
|
|
format: '',
|
|
|
|
indent: 0,
|
|
|
|
type: 'root',
|
|
|
|
version: 1
|
|
|
|
}
|
2022-09-13 19:29:37 +03:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
await agent
|
|
|
|
.post('/posts/?formats=mobiledoc,lexical')
|
|
|
|
.body({posts: [post]})
|
|
|
|
.expectStatus(422)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
errors: [{
|
|
|
|
id: anyErrorId
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
});
|
2022-09-15 18:49:14 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Errors with an invalid lexical state object', async function () {
|
|
|
|
const post = {
|
|
|
|
title: 'Invalid lexical state',
|
|
|
|
lexical: JSON.stringify({
|
|
|
|
notLexical: true
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
await agent
|
|
|
|
.post('/posts/?formats=mobiledoc,lexical,html')
|
|
|
|
.body({posts: [post]})
|
|
|
|
.expectStatus(422)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
errors: [{
|
|
|
|
id: anyErrorId
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
});
|
2022-09-13 19:29:37 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-08-22 19:35:37 +03:00
|
|
|
describe('Delete', function () {
|
|
|
|
it('Can destroy a post', async function () {
|
|
|
|
await agent
|
|
|
|
.delete(`posts/${fixtureManager.get('posts', 0).id}/`)
|
|
|
|
.expectStatus(204)
|
|
|
|
.expectEmptyBody()
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Cannot delete a non-existent posts', async function () {
|
|
|
|
// This error message from the API is not really what I would expect
|
|
|
|
// Adding this as a guard to demonstrate how future refactoring improves the output
|
|
|
|
await agent
|
|
|
|
.delete('/posts/abcd1234abcd1234abcd1234/')
|
|
|
|
.expectStatus(404)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
errors: [{
|
|
|
|
id: anyErrorId
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|