2023-05-17 10:42:37 +03:00
|
|
|
const assert = require('assert');
|
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-06-06 18:04:02 +03:00
|
|
|
anyNumber,
|
|
|
|
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-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 () {
|
|
|
|
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-05-16 10:30:31 +03:00
|
|
|
it('Can browse Collections', async function () {
|
|
|
|
await agent
|
|
|
|
.get('/collections/')
|
|
|
|
.expectStatus(200)
|
2023-05-17 09:55:07 +03:00
|
|
|
.matchHeaderSnapshot({
|
|
|
|
'content-version': anyContentVersion,
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
2023-06-06 08:50:55 +03:00
|
|
|
collections: [
|
2023-06-12 08:57:52 +03:00
|
|
|
buildMatcher(11, {withSortOrder: true}),
|
2023-06-06 09:35:06 +03:00
|
|
|
buildMatcher(2, {withSortOrder: true}),
|
2023-06-06 08:50:55 +03:00
|
|
|
buildMatcher(0)
|
|
|
|
]
|
2023-05-17 09:55:07 +03:00
|
|
|
});
|
2023-05-16 10:30:31 +03:00
|
|
|
});
|
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-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 = {
|
|
|
|
title: 'Test 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({
|
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({
|
|
|
|
collections: [buildMatcher(7)]
|
|
|
|
});
|
|
|
|
});
|
2023-06-05 12:37:17 +03:00
|
|
|
|
|
|
|
it('Creates an automatic Collection with a relational tag filter', async function () {
|
|
|
|
const collection = {
|
|
|
|
title: 'Test Collection with relational tag filter',
|
|
|
|
description: 'Test Collection Description with relational tag filter',
|
|
|
|
type: 'automatic',
|
|
|
|
filter: 'tag:kitchen-sink'
|
|
|
|
};
|
|
|
|
|
|
|
|
const automaticTagCollection = await agent
|
|
|
|
.post('/collections/')
|
|
|
|
.body({
|
|
|
|
collections: [collection]
|
|
|
|
})
|
|
|
|
.expectStatus(201)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
'content-version': anyContentVersion,
|
|
|
|
etag: anyEtag,
|
|
|
|
location: anyLocationFor('collections')
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
collections: [buildMatcher(2)]
|
|
|
|
});
|
|
|
|
|
|
|
|
const kitchenSinkTagPosts = await agent
|
|
|
|
.get('/posts/?filter=tag:kitchen-sink');
|
|
|
|
|
|
|
|
assert.equal(automaticTagCollection.body.collections[0].posts.length, 2);
|
|
|
|
assert.equal(kitchenSinkTagPosts.body.posts.length, 2);
|
|
|
|
|
|
|
|
const collectionPostIds = automaticTagCollection.body.collections[0].posts.map(p => p.id);
|
|
|
|
const tagFilteredPosts = kitchenSinkTagPosts.body.posts.map(p => p.id);
|
|
|
|
|
|
|
|
assert.deepEqual(collectionPostIds, tagFilteredPosts);
|
|
|
|
});
|
2023-06-01 12:55:48 +03:00
|
|
|
});
|
2023-05-16 10:30:31 +03:00
|
|
|
});
|