2023-03-03 20:58:19 +03:00
|
|
|
const logging = require('@tryghost/logging');
|
2022-03-21 20:04:32 +03:00
|
|
|
const {agentProvider, fixtureManager, matchers} = require('../../utils/e2e-framework');
|
2023-01-17 14:56:29 +03:00
|
|
|
const {anyContentVersion, anyObjectId, anyISODateTime, anyErrorId, anyEtag, anyLocationFor} = matchers;
|
2023-03-03 20:58:19 +03:00
|
|
|
const sinon = require('sinon');
|
2020-06-04 14:42:15 +03:00
|
|
|
|
2022-03-21 20:04:32 +03:00
|
|
|
const matchLabel = {
|
|
|
|
id: anyObjectId,
|
|
|
|
created_at: anyISODateTime,
|
|
|
|
updated_at: anyISODateTime
|
|
|
|
};
|
2020-11-30 17:25:22 +03:00
|
|
|
|
2022-03-21 20:04:32 +03:00
|
|
|
describe('Labels API', function () {
|
|
|
|
let agent;
|
2020-06-04 14:42:15 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
before(async function () {
|
2022-03-21 20:04:32 +03:00
|
|
|
agent = await agentProvider.getAdminAPIAgent();
|
|
|
|
await fixtureManager.init();
|
|
|
|
await agent.loginAsOwner();
|
2020-06-04 14:42:15 +03:00
|
|
|
});
|
|
|
|
|
2023-03-03 20:58:19 +03:00
|
|
|
afterEach(function () {
|
|
|
|
sinon.restore();
|
|
|
|
});
|
|
|
|
|
2022-03-24 13:55:25 +03:00
|
|
|
it('Can browse with no labels', async function () {
|
|
|
|
await agent
|
|
|
|
.get('labels')
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchBodySnapshot()
|
|
|
|
.matchHeaderSnapshot({
|
2023-01-17 14:56:29 +03:00
|
|
|
'content-version': anyContentVersion,
|
2022-03-24 13:55:25 +03:00
|
|
|
etag: anyEtag
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can add', async function () {
|
2022-03-21 20:04:32 +03:00
|
|
|
await agent
|
|
|
|
.post('labels')
|
|
|
|
.body({labels: [{
|
|
|
|
name: 'test'
|
|
|
|
}]})
|
|
|
|
.expectStatus(201)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
labels: [matchLabel]
|
|
|
|
})
|
|
|
|
.matchHeaderSnapshot({
|
2023-01-17 14:56:29 +03:00
|
|
|
'content-version': anyContentVersion,
|
2022-03-21 20:04:32 +03:00
|
|
|
etag: anyEtag,
|
|
|
|
location: anyLocationFor('labels')
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Errors when adding label with the same name', async function () {
|
2023-03-03 20:58:19 +03:00
|
|
|
const loggingStub = sinon.stub(logging, 'error');
|
2022-03-21 20:04:32 +03:00
|
|
|
await agent
|
|
|
|
.post('labels')
|
|
|
|
.body({labels: [{
|
|
|
|
name: 'test'
|
|
|
|
}]})
|
|
|
|
.expectStatus(422)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
errors: [{
|
|
|
|
id: anyErrorId
|
2020-06-04 14:42:15 +03:00
|
|
|
|
2022-03-21 20:04:32 +03:00
|
|
|
}]
|
|
|
|
})
|
|
|
|
.matchHeaderSnapshot({
|
2023-01-17 14:56:29 +03:00
|
|
|
'content-version': anyContentVersion,
|
2022-03-21 20:04:32 +03:00
|
|
|
etag: anyEtag
|
|
|
|
});
|
2023-03-03 20:58:19 +03:00
|
|
|
sinon.assert.calledOnce(loggingStub);
|
2022-03-21 20:04:32 +03:00
|
|
|
});
|
2020-11-30 17:25:22 +03:00
|
|
|
|
2022-03-21 20:04:32 +03:00
|
|
|
it('Can browse with member count', async function () {
|
|
|
|
await agent
|
|
|
|
.get('labels/?include=count.members')
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
labels: [matchLabel]
|
|
|
|
})
|
|
|
|
.matchHeaderSnapshot({
|
2023-01-17 14:56:29 +03:00
|
|
|
'content-version': anyContentVersion,
|
2022-03-21 20:04:32 +03:00
|
|
|
etag: anyEtag
|
|
|
|
});
|
|
|
|
});
|
2020-11-30 17:25:22 +03:00
|
|
|
|
2022-03-21 20:04:32 +03:00
|
|
|
it('Can read by slug and edit', async function () {
|
|
|
|
const {body} = await agent
|
|
|
|
.get('labels/slug/test/')
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
labels: [matchLabel]
|
|
|
|
})
|
|
|
|
.matchHeaderSnapshot({
|
2023-01-17 14:56:29 +03:00
|
|
|
'content-version': anyContentVersion,
|
2022-03-21 20:04:32 +03:00
|
|
|
etag: anyEtag
|
|
|
|
});
|
2020-11-30 17:25:22 +03:00
|
|
|
|
2022-03-21 20:04:32 +03:00
|
|
|
const id = body.labels[0].id;
|
|
|
|
|
|
|
|
await agent
|
|
|
|
.put(`labels/${id}`)
|
|
|
|
.body({labels: [{name: 'testing'}]})
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
labels: [matchLabel]
|
|
|
|
})
|
|
|
|
.matchHeaderSnapshot({
|
2023-01-17 14:56:29 +03:00
|
|
|
'content-version': anyContentVersion,
|
2022-03-21 20:04:32 +03:00
|
|
|
etag: anyEtag
|
|
|
|
});
|
2020-06-04 14:42:15 +03:00
|
|
|
});
|
2021-07-09 17:21:17 +03:00
|
|
|
|
2022-03-21 20:04:32 +03:00
|
|
|
it('Can destroy', async function () {
|
|
|
|
const {body} = await agent
|
|
|
|
.get('labels/slug/test/')
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
labels: [matchLabel]
|
|
|
|
})
|
|
|
|
.matchHeaderSnapshot({
|
2023-01-17 14:56:29 +03:00
|
|
|
'content-version': anyContentVersion,
|
2022-03-21 20:04:32 +03:00
|
|
|
etag: anyEtag
|
|
|
|
});
|
2021-07-09 17:21:17 +03:00
|
|
|
|
2022-03-21 20:04:32 +03:00
|
|
|
const id = body.labels[0].id;
|
2021-07-09 17:21:17 +03:00
|
|
|
|
2022-03-21 20:04:32 +03:00
|
|
|
await agent
|
|
|
|
.delete(`labels/${id}`)
|
|
|
|
.expectStatus(204)
|
2022-03-31 20:55:46 +03:00
|
|
|
.expectEmptyBody()
|
2022-03-21 20:04:32 +03:00
|
|
|
.matchHeaderSnapshot({
|
2023-01-17 14:56:29 +03:00
|
|
|
'content-version': anyContentVersion,
|
2022-03-21 20:04:32 +03:00
|
|
|
etag: anyEtag
|
|
|
|
});
|
2021-07-09 17:21:17 +03:00
|
|
|
});
|
2022-08-24 10:28:20 +03:00
|
|
|
|
|
|
|
it('Cannot destroy non-existent label', async function () {
|
|
|
|
await agent
|
|
|
|
.delete('labels/abcd1234abcd1234abcd1234')
|
|
|
|
.expectStatus(404)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
errors: [{
|
|
|
|
id: anyErrorId
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
.matchHeaderSnapshot({
|
2023-01-17 14:56:29 +03:00
|
|
|
'content-version': anyContentVersion,
|
2022-08-24 10:28:20 +03:00
|
|
|
etag: anyEtag
|
|
|
|
});
|
|
|
|
});
|
2020-06-04 14:42:15 +03:00
|
|
|
});
|