mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-19 00:11:49 +03:00
d7bbb0b935
refs https://github.com/TryGhost/Arch/issues/16 - Using the API directly on the repository level prevented us from ensuring collection consistency through transactions. - This change migrates the PostsRepository to use Bookshelf model layer directly, which also allows to put queries into transactions. - Additional optimization here was removing the `getAllPosts` method from CollectionService. This is an attempt to reduce the API surface of the of the service before calling it a GA.
376 lines
11 KiB
JavaScript
376 lines
11 KiB
JavaScript
const assert = require('assert/strict');
|
|
const {
|
|
agentProvider,
|
|
fixtureManager,
|
|
mockManager,
|
|
matchers
|
|
} = require('../../utils/e2e-framework');
|
|
const {
|
|
anyContentVersion,
|
|
anyEtag,
|
|
anyErrorId,
|
|
anyLocationFor,
|
|
anyObjectId,
|
|
anyISODateTime,
|
|
anyNumber,
|
|
anyString,
|
|
anyUuid,
|
|
anyArray,
|
|
anyObject
|
|
} = matchers;
|
|
|
|
const matchCollection = {
|
|
id: anyObjectId,
|
|
created_at: anyISODateTime,
|
|
updated_at: anyISODateTime
|
|
};
|
|
|
|
const tierSnapshot = {
|
|
id: anyObjectId,
|
|
created_at: anyISODateTime,
|
|
updated_at: anyISODateTime
|
|
};
|
|
|
|
const matchPostShallowIncludes = {
|
|
id: anyObjectId,
|
|
uuid: anyUuid,
|
|
comment_id: anyString,
|
|
url: anyString,
|
|
authors: anyArray,
|
|
primary_author: anyObject,
|
|
tags: anyArray,
|
|
primary_tag: anyObject,
|
|
tiers: Array(2).fill(tierSnapshot),
|
|
created_at: anyISODateTime,
|
|
updated_at: anyISODateTime,
|
|
published_at: anyISODateTime,
|
|
post_revisions: anyArray
|
|
};
|
|
|
|
/**
|
|
*
|
|
* @param {number} postCount
|
|
*/
|
|
const buildMatcher = (postCount, opts = {}) => {
|
|
let obj = {
|
|
id: anyObjectId
|
|
};
|
|
if (opts.withSortOrder) {
|
|
obj.sort_order = anyNumber;
|
|
}
|
|
return {
|
|
...matchCollection,
|
|
posts: Array(postCount).fill(obj)
|
|
};
|
|
};
|
|
|
|
describe('Collections API', function () {
|
|
let agent;
|
|
|
|
before(async function () {
|
|
mockManager.mockLabsEnabled('collections');
|
|
agent = await agentProvider.getAdminAPIAgent();
|
|
await fixtureManager.init('users', 'posts');
|
|
await agent.loginAsOwner();
|
|
});
|
|
|
|
afterEach(function () {
|
|
mockManager.restore();
|
|
});
|
|
|
|
it('Can add a Collection', async function () {
|
|
const collection = {
|
|
title: 'Test Collection',
|
|
description: 'Test Collection Description'
|
|
};
|
|
|
|
await agent
|
|
.post('/collections/')
|
|
.body({
|
|
collections: [collection]
|
|
})
|
|
.expectStatus(201)
|
|
.matchHeaderSnapshot({
|
|
'content-version': anyContentVersion,
|
|
etag: anyEtag,
|
|
location: anyLocationFor('collections')
|
|
})
|
|
.matchBodySnapshot({
|
|
collections: [matchCollection]
|
|
});
|
|
});
|
|
|
|
describe('Browse', function () {
|
|
it('Can browse Collections', async function () {
|
|
await agent
|
|
.get('/collections/')
|
|
.expectStatus(200)
|
|
.matchHeaderSnapshot({
|
|
'content-version': anyContentVersion,
|
|
etag: anyEtag
|
|
})
|
|
.matchBodySnapshot({
|
|
collections: [
|
|
buildMatcher(13, {withSortOrder: true}),
|
|
buildMatcher(2, {withSortOrder: true}),
|
|
buildMatcher(0)
|
|
]
|
|
});
|
|
});
|
|
});
|
|
|
|
it('Can read a Collection', async function () {
|
|
const collection = {
|
|
title: 'Test Collection to Read'
|
|
};
|
|
|
|
const addResponse = await agent
|
|
.post('/collections/')
|
|
.body({
|
|
collections: [collection]
|
|
})
|
|
.expectStatus(201)
|
|
.matchHeaderSnapshot({
|
|
'content-version': anyContentVersion,
|
|
etag: anyEtag,
|
|
location: anyLocationFor('collections')
|
|
})
|
|
.matchBodySnapshot({
|
|
collections: [matchCollection]
|
|
});
|
|
|
|
const collectionId = addResponse.body.collections[0].id;
|
|
|
|
const readResponse = await agent
|
|
.get(`/collections/${collectionId}/`)
|
|
.expectStatus(200)
|
|
.matchHeaderSnapshot({
|
|
'content-version': anyContentVersion,
|
|
etag: anyEtag
|
|
})
|
|
.matchBodySnapshot({
|
|
collections: [matchCollection]
|
|
});
|
|
|
|
assert.equal(readResponse.body.collections[0].title, 'Test Collection to Read');
|
|
|
|
await agent
|
|
.delete(`/collections/${collectionId}/`)
|
|
.expectStatus(204);
|
|
});
|
|
|
|
describe('Edit', function () {
|
|
let collectionToEdit;
|
|
|
|
before(async function () {
|
|
const collection = {
|
|
title: 'Test Collection to Edit'
|
|
};
|
|
|
|
const addResponse = await agent
|
|
.post('/collections/')
|
|
.body({
|
|
collections: [collection]
|
|
})
|
|
.expectStatus(201);
|
|
|
|
collectionToEdit = addResponse.body.collections[0];
|
|
});
|
|
|
|
it('Can edit a Collection', async function () {
|
|
const editResponse = await agent
|
|
.put(`/collections/${collectionToEdit.id}/`)
|
|
.body({
|
|
collections: [{
|
|
title: 'Test Collection Edited'
|
|
}]
|
|
})
|
|
.expectStatus(200)
|
|
.matchHeaderSnapshot({
|
|
'content-version': anyContentVersion,
|
|
etag: anyEtag
|
|
})
|
|
.matchBodySnapshot({
|
|
collections: [matchCollection]
|
|
});
|
|
|
|
assert.equal(editResponse.body.collections[0].title, 'Test Collection Edited');
|
|
});
|
|
|
|
it('Fails to edit unexistent Collection', async function () {
|
|
const unexistentID = '5951f5fca366002ebd5dbef7';
|
|
await agent
|
|
.put(`/collections/${unexistentID}/`)
|
|
.body({
|
|
collections: [{
|
|
id: unexistentID,
|
|
title: 'Editing unexistent Collection'
|
|
}]
|
|
})
|
|
.expectStatus(404)
|
|
.matchBodySnapshot({
|
|
errors: [{
|
|
id: anyErrorId
|
|
}]
|
|
})
|
|
.matchHeaderSnapshot({
|
|
'content-version': anyContentVersion,
|
|
etag: anyEtag
|
|
});
|
|
});
|
|
});
|
|
|
|
it('Can delete a Collection', async function () {
|
|
const collection = {
|
|
title: 'Test Collection to Delete'
|
|
};
|
|
|
|
const addResponse = await agent
|
|
.post('/collections/')
|
|
.body({
|
|
collections: [collection]
|
|
})
|
|
.expectStatus(201)
|
|
.matchHeaderSnapshot({
|
|
'content-version': anyContentVersion,
|
|
etag: anyEtag,
|
|
location: anyLocationFor('collections')
|
|
})
|
|
.matchBodySnapshot({
|
|
collections: [matchCollection]
|
|
});
|
|
|
|
const collectionId = addResponse.body.collections[0].id;
|
|
|
|
await agent
|
|
.delete(`/collections/${collectionId}/`)
|
|
.expectStatus(204)
|
|
.matchHeaderSnapshot({
|
|
'content-version': anyContentVersion,
|
|
etag: anyEtag
|
|
})
|
|
.matchBodySnapshot();
|
|
|
|
await agent
|
|
.get(`/collections/${collectionId}/`)
|
|
.expectStatus(404)
|
|
.matchHeaderSnapshot({
|
|
'content-version': anyContentVersion,
|
|
etag: anyEtag
|
|
})
|
|
.matchBodySnapshot({
|
|
errors: [{
|
|
id: anyErrorId
|
|
}]
|
|
});
|
|
});
|
|
|
|
it('Cannot delete a built in collection', async function () {
|
|
const builtInCollection = await agent
|
|
.get('/collections/?filter=slug:featured')
|
|
.expectStatus(200);
|
|
|
|
assert.ok(builtInCollection.body.collections);
|
|
assert.equal(builtInCollection.body.collections.length, 1);
|
|
|
|
await agent
|
|
.delete(`/collections/${builtInCollection.body.collections[0].id}/`)
|
|
.expectStatus(405)
|
|
.matchHeaderSnapshot({
|
|
'content-version': anyContentVersion,
|
|
etag: anyEtag
|
|
})
|
|
.matchBodySnapshot({
|
|
errors: [{
|
|
id: anyErrorId,
|
|
context: anyString
|
|
}]
|
|
});
|
|
});
|
|
|
|
describe('Automatic Collection Filtering', function () {
|
|
it('Creates an automatic Collection with a featured filter', async function () {
|
|
const collection = {
|
|
title: 'Test Featured Collection',
|
|
description: 'Test Collection Description',
|
|
type: 'automatic',
|
|
filter: 'featured:true'
|
|
};
|
|
|
|
await agent
|
|
.post('/collections/')
|
|
.body({
|
|
collections: [collection]
|
|
})
|
|
.expectStatus(201)
|
|
.matchHeaderSnapshot({
|
|
'content-version': anyContentVersion,
|
|
etag: anyEtag,
|
|
location: anyLocationFor('collections')
|
|
})
|
|
.matchBodySnapshot({
|
|
collections: [buildMatcher(2)]
|
|
});
|
|
});
|
|
|
|
it('Creates an automatic Collection with a published_at filter', async function () {
|
|
const collection = {
|
|
title: 'Test Collection with published_at filter',
|
|
description: 'Test Collection Description with published_at filter',
|
|
type: 'automatic',
|
|
filter: 'published_at:>=2022-05-25'
|
|
};
|
|
|
|
await agent
|
|
.post('/collections/')
|
|
.body({
|
|
collections: [collection]
|
|
})
|
|
.expectStatus(201)
|
|
.matchHeaderSnapshot({
|
|
'content-version': anyContentVersion,
|
|
etag: anyEtag,
|
|
location: anyLocationFor('collections')
|
|
})
|
|
.matchBodySnapshot({
|
|
collections: [buildMatcher(9)]
|
|
});
|
|
});
|
|
|
|
it('Creates an automatic Collection with a tag filter', async function () {
|
|
const collection = {
|
|
title: 'Test Collection with tag filter',
|
|
slug: 'bacon',
|
|
description: 'BACON!',
|
|
type: 'automatic',
|
|
filter: 'tags:[\'bacon\']'
|
|
};
|
|
|
|
await agent
|
|
.post('/collections/')
|
|
.body({
|
|
collections: [collection]
|
|
})
|
|
.expectStatus(201)
|
|
.matchHeaderSnapshot({
|
|
'content-version': anyContentVersion,
|
|
etag: anyEtag,
|
|
location: anyLocationFor('collections')
|
|
})
|
|
.matchBodySnapshot({
|
|
collections: [buildMatcher(2)]
|
|
});
|
|
|
|
await agent.get(`posts/?collection=${collection.slug}`)
|
|
.expectStatus(200)
|
|
.matchHeaderSnapshot({
|
|
'content-version': anyContentVersion,
|
|
etag: anyEtag
|
|
})
|
|
.matchBodySnapshot({
|
|
posts: new Array(2).fill(matchPostShallowIncludes)
|
|
});
|
|
});
|
|
});
|
|
});
|