af94855349
refs: https://github.com/TryGhost/Ghost/issues/14882 - I found a common pattern where catch predicates were being used to catch non-existent models in destroy methods, and sometimes elsewhere in the API endpoints - The use of predicates is deprecated, and we're working to remove them from everywhere, so that we can remove bluebird - In order to still handle these errors correctly, we needed a small change to mw-error-handler so that it can detect EmptyResponse errors from bookshelf, as well as 404s Note: there is a small change as a result of this - the context on these errors now says "Resource not found" instead of "{ModelName} not found". - I think this is acceptable for now, as we will be reviewing these errors in more depth later. It's quite easy to make changes, we just have to decide what with proper design input
136 lines
3.5 KiB
JavaScript
136 lines
3.5 KiB
JavaScript
const {agentProvider, fixtureManager, matchers} = require('../../utils/e2e-framework');
|
|
const {anyObjectId, anyISODateTime, anyErrorId, anyEtag, anyLocationFor} = matchers;
|
|
|
|
const matchLabel = {
|
|
id: anyObjectId,
|
|
created_at: anyISODateTime,
|
|
updated_at: anyISODateTime
|
|
};
|
|
|
|
describe('Labels API', function () {
|
|
let agent;
|
|
|
|
before(async function () {
|
|
agent = await agentProvider.getAdminAPIAgent();
|
|
await fixtureManager.init();
|
|
await agent.loginAsOwner();
|
|
});
|
|
|
|
it('Can browse with no labels', async function () {
|
|
await agent
|
|
.get('labels')
|
|
.expectStatus(200)
|
|
.matchBodySnapshot()
|
|
.matchHeaderSnapshot({
|
|
etag: anyEtag
|
|
});
|
|
});
|
|
|
|
it('Can add', async function () {
|
|
await agent
|
|
.post('labels')
|
|
.body({labels: [{
|
|
name: 'test'
|
|
}]})
|
|
.expectStatus(201)
|
|
.matchBodySnapshot({
|
|
labels: [matchLabel]
|
|
})
|
|
.matchHeaderSnapshot({
|
|
etag: anyEtag,
|
|
location: anyLocationFor('labels')
|
|
});
|
|
});
|
|
|
|
it('Errors when adding label with the same name', async function () {
|
|
await agent
|
|
.post('labels')
|
|
.body({labels: [{
|
|
name: 'test'
|
|
}]})
|
|
.expectStatus(422)
|
|
.matchBodySnapshot({
|
|
errors: [{
|
|
id: anyErrorId
|
|
|
|
}]
|
|
})
|
|
.matchHeaderSnapshot({
|
|
etag: anyEtag
|
|
});
|
|
});
|
|
|
|
it('Can browse with member count', async function () {
|
|
await agent
|
|
.get('labels/?include=count.members')
|
|
.expectStatus(200)
|
|
.matchBodySnapshot({
|
|
labels: [matchLabel]
|
|
})
|
|
.matchHeaderSnapshot({
|
|
etag: anyEtag
|
|
});
|
|
});
|
|
|
|
it('Can read by slug and edit', async function () {
|
|
const {body} = await agent
|
|
.get('labels/slug/test/')
|
|
.expectStatus(200)
|
|
.matchBodySnapshot({
|
|
labels: [matchLabel]
|
|
})
|
|
.matchHeaderSnapshot({
|
|
etag: anyEtag
|
|
});
|
|
|
|
const id = body.labels[0].id;
|
|
|
|
await agent
|
|
.put(`labels/${id}`)
|
|
.body({labels: [{name: 'testing'}]})
|
|
.expectStatus(200)
|
|
.matchBodySnapshot({
|
|
labels: [matchLabel]
|
|
})
|
|
.matchHeaderSnapshot({
|
|
etag: anyEtag
|
|
});
|
|
});
|
|
|
|
it('Can destroy', async function () {
|
|
const {body} = await agent
|
|
.get('labels/slug/test/')
|
|
.expectStatus(200)
|
|
.matchBodySnapshot({
|
|
labels: [matchLabel]
|
|
})
|
|
.matchHeaderSnapshot({
|
|
etag: anyEtag
|
|
});
|
|
|
|
const id = body.labels[0].id;
|
|
|
|
await agent
|
|
.delete(`labels/${id}`)
|
|
.expectStatus(204)
|
|
.expectEmptyBody()
|
|
.matchHeaderSnapshot({
|
|
etag: anyEtag
|
|
});
|
|
});
|
|
|
|
it('Cannot destroy non-existent label', async function () {
|
|
await agent
|
|
.delete('labels/abcd1234abcd1234abcd1234')
|
|
.expectStatus(404)
|
|
.matchBodySnapshot({
|
|
errors: [{
|
|
id: anyErrorId
|
|
}]
|
|
})
|
|
.matchHeaderSnapshot({
|
|
etag: anyEtag
|
|
});
|
|
});
|
|
});
|