Added DELETE /collections/id to Admin API

refs https://github.com/TryGhost/Team/issues/3167

- This is part of scaffolding for collections API. Allows to delete collection resource
This commit is contained in:
Naz 2023-05-19 20:29:23 +07:00 committed by naz
parent e82fcbfc5e
commit e302f8cc1d
5 changed files with 142 additions and 1 deletions

View File

@ -89,5 +89,27 @@ module.exports = {
return model;
}
},
destroy: {
statusCode: 204,
headers: {
cacheInvalidate: true
},
options: [
'id'
],
validation: {
options: {
id: {
required: true
}
}
},
// @NOTE: should have permissions when moving out of Alpha
permissions: false,
async query(frame) {
return await collectionsService.api.destroy(frame.options.id);
}
}
};

View File

@ -13,7 +13,8 @@ class CollectionsServiceWrapper {
browse: collectionsService.getAll.bind(collectionsService),
read: collectionsService.getById.bind(collectionsService),
add: collectionsService.save.bind(collectionsService),
edit: collectionsService.edit.bind(collectionsService)
edit: collectionsService.edit.bind(collectionsService),
destroy: collectionsService.destroy.bind(collectionsService)
};
}
}

View File

@ -23,6 +23,7 @@ module.exports = function apiRoutes() {
router.get('/collections/:id', mw.authAdminApi, labs.enabledMiddleware('collections'), http(api.collections.read));
router.post('/collections', mw.authAdminApi, labs.enabledMiddleware('collections'), http(api.collections.add));
router.put('/collections/:id', mw.authAdminApi, labs.enabledMiddleware('collections'), http(api.collections.edit));
router.del('/collections/:id', mw.authAdminApi, labs.enabledMiddleware('collections'), http(api.collections.destroy));
// ## Configuration
router.get('/config', mw.authAdminApi, http(api.config.read));

View File

@ -64,6 +64,78 @@ Object {
}
`;
exports[`Collections API Can delete a Collection 1: [body] 1`] = `
Object {
"collections": Array [
Object {
"deleted": false,
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
"title": "Test Collection to Delete",
},
],
}
`;
exports[`Collections API Can delete a Collection 2: [headers] 1`] = `
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
"content-length": "103",
"content-type": "application/json; charset=utf-8",
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
"location": StringMatching /https\\?:\\\\/\\\\/\\.\\*\\?\\\\/collections\\\\/\\[a-f0-9\\]\\{24\\}\\\\//,
"vary": "Accept-Version, Origin, Accept-Encoding",
"x-cache-invalidate": "/*",
"x-powered-by": "Express",
}
`;
exports[`Collections API Can delete a Collection 3: [body] 1`] = `Object {}`;
exports[`Collections API Can delete a Collection 4: [headers] 1`] = `
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
"vary": "Accept-Version, Origin",
"x-cache-invalidate": "/*",
"x-powered-by": "Express",
}
`;
exports[`Collections API Can delete a Collection 5: [body] 1`] = `
Object {
"errors": Array [
Object {
"code": null,
"context": "Collection not found.",
"details": null,
"ghostErrorCode": null,
"help": null,
"id": StringMatching /\\[a-f0-9\\]\\{8\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{12\\}/,
"message": "Resource not found error, cannot read collection.",
"property": null,
"type": "NotFoundError",
},
],
}
`;
exports[`Collections API Can delete a Collection 6: [headers] 1`] = `
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
"content-length": "254",
"content-type": "application/json; charset=utf-8",
"content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/,
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
"vary": "Accept-Version, Origin, Accept-Encoding",
"x-powered-by": "Express",
}
`;
exports[`Collections API Can edit a Collection 1: [body] 1`] = `
Object {
"collections": Array [

View File

@ -163,4 +163,49 @@ describe('Collections API', function () {
etag: anyEtag
});
});
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
}]
});
});
});