From 46b963b76e530914d1dab7bd425aa4cef9a52f6c Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Tue, 10 Dec 2024 10:20:22 +0100 Subject: [PATCH] mirage: Implement `DELETE /api/v1/crates/:name` endpoint (#10175) --- mirage/route-handlers/crates.js | 17 ++++++++++++ tests/mirage/crates/delete-test.js | 42 ++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 tests/mirage/crates/delete-test.js diff --git a/mirage/route-handlers/crates.js b/mirage/route-handlers/crates.js index 9674330652b..0e84d26eadc 100644 --- a/mirage/route-handlers/crates.js +++ b/mirage/route-handlers/crates.js @@ -67,6 +67,23 @@ export function register(server) { }; }); + server.delete('/api/v1/crates/:name', (schema, request) => { + let { user } = getSession(schema); + if (!user) { + return new Response(403, {}, { errors: [{ detail: 'must be logged in to perform that action' }] }); + } + + let { name } = request.params; + let crate = schema.crates.findBy({ name }); + if (!crate) { + return new Response(404, {}, { errors: [{ detail: `crate \`${name}\` does not exist` }] }); + } + + crate.destroy(); + + return ''; + }); + server.get('/api/v1/crates/:name/following', (schema, request) => { let { user } = getSession(schema); if (!user) { diff --git a/tests/mirage/crates/delete-test.js b/tests/mirage/crates/delete-test.js new file mode 100644 index 00000000000..20a56924f32 --- /dev/null +++ b/tests/mirage/crates/delete-test.js @@ -0,0 +1,42 @@ +import { module, test } from 'qunit'; + +import fetch from 'fetch'; + +import { setupTest } from '../../helpers'; +import setupMirage from '../../helpers/setup-mirage'; + +module('Mirage | DELETE /api/v1/crates/:name', function (hooks) { + setupTest(hooks); + setupMirage(hooks); + + test('returns 403 if unauthenticated', async function (assert) { + let response = await fetch('/api/v1/crates/foo', { method: 'DELETE' }); + assert.strictEqual(response.status, 403); + assert.deepEqual(await response.json(), { + errors: [{ detail: 'must be logged in to perform that action' }], + }); + }); + + test('returns 404 for unknown crates', async function (assert) { + let user = this.server.create('user'); + this.authenticateAs(user); + + let response = await fetch('/api/v1/crates/foo', { method: 'DELETE' }); + assert.strictEqual(response.status, 404); + assert.deepEqual(await response.json(), { errors: [{ detail: 'crate `foo` does not exist' }] }); + }); + + test('deletes crates', async function (assert) { + let user = this.server.create('user'); + this.authenticateAs(user); + + let crate = this.server.create('crate', { name: 'foo' }); + this.server.create('crate-ownership', { crate, user }); + + let response = await fetch('/api/v1/crates/foo', { method: 'DELETE' }); + assert.strictEqual(response.status, 204); + assert.deepEqual(await response.text(), ''); + + assert.strictEqual(this.server.schema.crates.findBy({ name: 'foo' }), null); + }); +});