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,
|
|
|
|
anyISODateTime
|
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
|
|
|
|
*/
|
|
|
|
const buildMatcher = (postCount) => {
|
|
|
|
return {
|
|
|
|
...matchCollection,
|
|
|
|
posts: Array(postCount).fill({
|
|
|
|
id: anyObjectId
|
|
|
|
})
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
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();
|
|
|
|
await fixtureManager.init('users');
|
|
|
|
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-05-25 07:39: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
|
|
|
|
|
|
|
it('Can add a Post to a Collection', async function () {
|
|
|
|
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
|
|
|
|
const readResponse = await agent
|
|
|
|
.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-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-05-16 10:30:31 +03:00
|
|
|
});
|