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,
|
|
|
|
anyNumber
|
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({
|
|
|
|
collections: [matchCollection]
|
|
|
|
});
|
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-25 18:39:43 +03:00
|
|
|
|
2023-05-31 19:17:22 +03:00
|
|
|
it('Can add Posts and append Post to a Collection', async function () {
|
2023-05-25 18:39:43 +03:00
|
|
|
const postsToAttach = [{
|
|
|
|
id: fixtureManager.get('posts', 0).id
|
|
|
|
}, {
|
|
|
|
id: fixtureManager.get('posts', 2).id
|
|
|
|
}, {
|
|
|
|
id: fixtureManager.get('posts', 3).id
|
|
|
|
}];
|
|
|
|
|
|
|
|
const collectionId = collectionToEdit.id;
|
|
|
|
|
|
|
|
const editResponse = await agent
|
|
|
|
.put(`/collections/${collectionId}/`)
|
|
|
|
.body({
|
|
|
|
collections: [{
|
|
|
|
posts: postsToAttach
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
'content-version': anyContentVersion,
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
collections: [buildMatcher(3)]
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(editResponse.body.collections[0].posts.length, 3, 'Posts should have been added to a Collection');
|
|
|
|
|
|
|
|
// verify the posts are persisted across requests
|
2023-05-31 19:17:22 +03:00
|
|
|
let readResponse = await agent
|
2023-05-25 18:39:43 +03:00
|
|
|
.get(`/collections/${collectionId}/`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
'content-version': anyContentVersion,
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
collections: [buildMatcher(3)]
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(readResponse.body.collections[0].posts.length, 3, 'Posts should have been added to a Collection');
|
2023-05-31 19:17:22 +03:00
|
|
|
|
|
|
|
//adds a single Post to existing Posts attached to a Collection
|
|
|
|
await agent
|
|
|
|
.post(`/collections/${collectionId}/posts`)
|
|
|
|
.body({
|
|
|
|
posts: [{
|
|
|
|
id: fixtureManager.get('posts', 4).id
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
'content-version': anyContentVersion,
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
2023-06-01 07:17:00 +03:00
|
|
|
collections: [buildMatcher(4, {withSortOrder: true})]
|
2023-05-31 19:17:22 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// verify the posts are persisted across requests
|
|
|
|
readResponse = await agent
|
|
|
|
.get(`/collections/${collectionId}/`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
'content-version': anyContentVersion,
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
2023-06-01 07:17:00 +03:00
|
|
|
collections: [buildMatcher(4, {withSortOrder: true})]
|
2023-05-31 19:17:22 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(readResponse.body.collections[0].posts.length, 4, 'Post should have been added to a Collection');
|
2023-05-25 18:39:43 +03:00
|
|
|
});
|
2023-06-01 10:57:47 +03:00
|
|
|
|
|
|
|
it('Can remove a Post from a Collection', async function () {
|
|
|
|
const collectionId = collectionToEdit.id;
|
|
|
|
const readResponse = await agent
|
|
|
|
.get(`/collections/${collectionId}/`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
'content-version': anyContentVersion,
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
collections: [buildMatcher(4, {withSortOrder: true})]
|
|
|
|
});
|
|
|
|
|
|
|
|
const postIdToRemove = readResponse.body.collections[0].posts[0]?.id;
|
|
|
|
|
|
|
|
await agent
|
|
|
|
.delete(`/collections/${collectionId}/posts/${postIdToRemove}`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
'content-version': anyContentVersion,
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
collections: [buildMatcher(3, {withSortOrder: true})]
|
|
|
|
});
|
|
|
|
});
|
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
|
|
|
|
|
|
|
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-01 12:55:48 +03:00
|
|
|
});
|
2023-05-16 10:30:31 +03:00
|
|
|
});
|