2023-06-21 11:56:59 +03:00
|
|
|
const assert = require('assert/strict');
|
2023-05-16 10:30:31 +03:00
|
|
|
const {
|
|
|
|
agentProvider,
|
|
|
|
fixtureManager,
|
2023-05-17 09:55:07 +03:00
|
|
|
mockManager,
|
|
|
|
matchers
|
2023-05-16 10:30:31 +03:00
|
|
|
} = require('../../utils/e2e-framework');
|
2023-05-17 09:55:07 +03:00
|
|
|
const {
|
|
|
|
anyContentVersion,
|
|
|
|
anyEtag,
|
2023-05-17 17:59:03 +03:00
|
|
|
anyErrorId,
|
2023-05-17 09:55:07 +03:00
|
|
|
anyLocationFor,
|
2023-05-24 12:56:41 +03:00
|
|
|
anyObjectId,
|
2023-06-01 07:17:00 +03:00
|
|
|
anyISODateTime,
|
2023-07-18 14:50:18 +03:00
|
|
|
anyISODateTimeWithTZ,
|
2023-06-06 18:04:02 +03:00
|
|
|
anyNumber,
|
2023-07-18 14:50:18 +03:00
|
|
|
anyLocalURL,
|
2023-06-06 18:04:02 +03:00
|
|
|
anyString
|
2023-05-17 09:55:07 +03:00
|
|
|
} = matchers;
|
|
|
|
|
|
|
|
const matchCollection = {
|
2023-05-24 12:56:41 +03:00
|
|
|
id: anyObjectId,
|
|
|
|
created_at: anyISODateTime,
|
|
|
|
updated_at: anyISODateTime
|
2023-05-17 09:55:07 +03:00
|
|
|
};
|
2023-05-16 10:30:31 +03:00
|
|
|
|
2023-07-18 14:50:18 +03:00
|
|
|
const matchCollectionPost = {
|
|
|
|
id: anyObjectId,
|
|
|
|
url: anyLocalURL,
|
|
|
|
created_at: anyISODateTimeWithTZ,
|
|
|
|
updated_at: anyISODateTimeWithTZ,
|
|
|
|
published_at: anyISODateTimeWithTZ
|
|
|
|
};
|
2023-06-13 13:02:01 +03:00
|
|
|
|
2023-05-25 18:39:43 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {number} postCount
|
|
|
|
*/
|
2023-06-01 07:17:00 +03:00
|
|
|
const buildMatcher = (postCount, opts = {}) => {
|
|
|
|
let obj = {
|
|
|
|
id: anyObjectId
|
|
|
|
};
|
|
|
|
if (opts.withSortOrder) {
|
|
|
|
obj.sort_order = anyNumber;
|
|
|
|
}
|
2023-05-25 18:39:43 +03:00
|
|
|
return {
|
|
|
|
...matchCollection,
|
2023-06-01 07:17:00 +03:00
|
|
|
posts: Array(postCount).fill(obj)
|
2023-05-25 18:39:43 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-05-17 09:55:07 +03:00
|
|
|
describe('Collections API', function () {
|
2023-05-16 10:30:31 +03:00
|
|
|
let agent;
|
|
|
|
|
|
|
|
before(async function () {
|
2023-06-19 17:40:15 +03:00
|
|
|
mockManager.mockLabsEnabled('collections');
|
2023-05-16 10:30:31 +03:00
|
|
|
agent = await agentProvider.getAdminAPIAgent();
|
2023-06-01 12:55:48 +03:00
|
|
|
await fixtureManager.init('users', 'posts');
|
2023-05-16 10:30:31 +03:00
|
|
|
await agent.loginAsOwner();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
mockManager.restore();
|
|
|
|
});
|
|
|
|
|
2023-05-17 09:55:07 +03:00
|
|
|
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]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-07-17 14:36:23 +03:00
|
|
|
// @NOTE: Below are flaky tests. They have inconsistent state due to collection events race conditions.
|
|
|
|
// Should be uncommented and fixed with: https://github.com/TryGhost/Arch/issues/16
|
|
|
|
// 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)
|
|
|
|
// ]
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
// });
|
2023-06-13 13:02:01 +03:00
|
|
|
|
2023-07-17 14:36:23 +03:00
|
|
|
// describe('Browse Posts', function () {
|
|
|
|
// it('Can browse Collections Posts', async function () {
|
|
|
|
// const collections = await agent.get('/collections/');
|
|
|
|
// const latestCollection = collections.body.collections.find(c => c.slug === 'latest');
|
2023-06-13 13:02:01 +03:00
|
|
|
|
2023-07-17 14:36:23 +03:00
|
|
|
// await agent
|
|
|
|
// .get(`/collections/${latestCollection.id}/posts/`)
|
|
|
|
// .expectStatus(200)
|
|
|
|
// .matchHeaderSnapshot({
|
|
|
|
// 'content-version': anyContentVersion,
|
|
|
|
// etag: anyEtag
|
|
|
|
// })
|
|
|
|
// .matchBodySnapshot({
|
|
|
|
// collection_posts: Array(13).fill(matchCollectionPost)
|
|
|
|
// });
|
|
|
|
// });
|
2023-06-13 13:02:01 +03:00
|
|
|
|
2023-07-17 14:36:23 +03:00
|
|
|
// it('Can browse Collections Posts using paging parameters', async function () {
|
|
|
|
// const collections = await agent.get('/collections/');
|
|
|
|
// const indexCollection = collections.body.collections.find(c => c.slug === 'latest');
|
2023-06-13 13:02:01 +03:00
|
|
|
|
2023-07-17 14:36:23 +03:00
|
|
|
// await agent
|
|
|
|
// .get(`/collections/${indexCollection.id}/posts/?limit=2&page=2`)
|
|
|
|
// .expectStatus(200)
|
|
|
|
// .matchHeaderSnapshot({
|
|
|
|
// 'content-version': anyContentVersion,
|
|
|
|
// etag: anyEtag
|
|
|
|
// })
|
|
|
|
// .matchBodySnapshot({
|
|
|
|
// collection_posts: Array(2).fill(matchCollectionPost)
|
|
|
|
// });
|
|
|
|
// });
|
2023-07-10 14:39:02 +03:00
|
|
|
|
2023-07-17 14:36:23 +03:00
|
|
|
// it('Can browse Collections Posts using collection slug', async function () {
|
|
|
|
// await agent
|
|
|
|
// .get(`/collections/latest/posts/`)
|
|
|
|
// .expectStatus(200)
|
|
|
|
// .matchHeaderSnapshot({
|
|
|
|
// 'content-version': anyContentVersion,
|
|
|
|
// etag: anyEtag
|
|
|
|
// })
|
|
|
|
// .matchBodySnapshot({
|
|
|
|
// collection_posts: Array(13).fill(matchCollectionPost)
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
// });
|
2023-05-17 10:42:37 +03:00
|
|
|
|
2023-05-17 18:14:13 +03:00
|
|
|
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');
|
2023-07-11 09:39:14 +03:00
|
|
|
|
|
|
|
await agent
|
|
|
|
.delete(`/collections/${collectionId}/`)
|
|
|
|
.expectStatus(204);
|
2023-05-17 18:14:13 +03:00
|
|
|
});
|
|
|
|
|
2023-06-01 12:55:48 +03:00
|
|
|
describe('Edit', function () {
|
2023-05-25 07:46:20 +03:00
|
|
|
let collectionToEdit;
|
|
|
|
|
|
|
|
before(async function () {
|
2023-05-25 07:39:48 +03:00
|
|
|
const collection = {
|
|
|
|
title: 'Test Collection to Edit'
|
|
|
|
};
|
|
|
|
|
|
|
|
const addResponse = await agent
|
|
|
|
.post('/collections/')
|
|
|
|
.body({
|
|
|
|
collections: [collection]
|
|
|
|
})
|
2023-05-25 07:46:20 +03:00
|
|
|
.expectStatus(201);
|
2023-05-25 07:39:48 +03:00
|
|
|
|
2023-05-25 07:46:20 +03:00
|
|
|
collectionToEdit = addResponse.body.collections[0];
|
|
|
|
});
|
2023-05-25 07:39:48 +03:00
|
|
|
|
2023-05-25 07:46:20 +03:00
|
|
|
it('Can edit a Collection', async function () {
|
2023-05-25 07:39:48 +03:00
|
|
|
const editResponse = await agent
|
2023-05-25 07:46:20 +03:00
|
|
|
.put(`/collections/${collectionToEdit.id}/`)
|
2023-05-25 07:39:48 +03:00
|
|
|
.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
|
|
|
|
});
|
|
|
|
});
|
2023-05-17 17:59:03 +03:00
|
|
|
});
|
2023-05-19 16:29:23 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
2023-06-01 12:55:48 +03:00
|
|
|
|
2023-06-06 18:04:02 +03:00
|
|
|
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
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-06-01 12:55:48 +03:00
|
|
|
describe('Automatic Collection Filtering', function () {
|
|
|
|
it('Creates an automatic Collection with a featured filter', async function () {
|
|
|
|
const collection = {
|
2023-06-29 00:50:50 +03:00
|
|
|
title: 'Test Featured Collection',
|
2023-06-01 12:55:48 +03:00
|
|
|
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({
|
2023-06-01 14:16:24 +03:00
|
|
|
collections: [buildMatcher(2)]
|
2023-06-01 12:55:48 +03:00
|
|
|
});
|
|
|
|
});
|
2023-06-05 10:59:49 +03:00
|
|
|
|
|
|
|
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',
|
|
|
|
// should return all available posts
|
|
|
|
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({
|
2023-07-11 09:39:14 +03:00
|
|
|
collections: [buildMatcher(9)]
|
2023-06-05 10:59:49 +03:00
|
|
|
});
|
|
|
|
});
|
2023-07-18 14:50:18 +03:00
|
|
|
|
|
|
|
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('/collections/bacon/posts/')
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
'content-version': anyContentVersion,
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
collection_posts: [
|
|
|
|
matchCollectionPost,
|
|
|
|
matchCollectionPost
|
|
|
|
]
|
|
|
|
});
|
|
|
|
});
|
2023-06-01 12:55:48 +03:00
|
|
|
});
|
2023-05-16 10:30:31 +03:00
|
|
|
});
|