Skip to content

Commit

Permalink
mirage: Implement DELETE /api/v1/crates/:name endpoint (#10175)
Browse files Browse the repository at this point in the history
  • Loading branch information
Turbo87 authored Dec 10, 2024
1 parent 6f81f3d commit 46b963b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
17 changes: 17 additions & 0 deletions mirage/route-handlers/crates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
42 changes: 42 additions & 0 deletions tests/mirage/crates/delete-test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 46b963b

Please sign in to comment.