diff --git a/mirage/route-handlers/crates.js b/mirage/route-handlers/crates.js index 9674330652..0e84d26ead 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 0000000000..20a56924f3 --- /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); + }); +});