Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mirage: Implement PATCH /crates/:name/:version route handler #9791

Merged
merged 6 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion mirage/route-handlers/crates.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,32 @@ export function register(server) {
return { ok: true, msg: 'owners successfully removed' };
});

server.patch('/api/v1/crates/:name/:version', function (schema, request) {
let { user } = getSession(schema);
if (!user) {
return new Response(403, {}, { errors: [{ detail: 'must be logged in to perform that action' }] });
}

const { name, version: versionNum } = request.params;
const crate = schema.crates.findBy({ name });
if (!crate) {
return notFound();
}

const version = schema.versions.findBy({ crateId: crate.id, num: versionNum });
if (!version) {
return notFound();
}

const body = JSON.parse(request.requestBody);
version.update({
yanked: body.version.yanked,
yank_message: body.version.yanked ? body.version.yank_message || null : null,
});

return this.serialize(version);
});

server.delete('/api/v1/crates/:name/:version/yank', (schema, request) => {
let { user } = getSession(schema);
if (!user) {
Expand Down Expand Up @@ -303,7 +329,7 @@ export function register(server) {
return notFound();
}

version.update({ yanked: false });
version.update({ yanked: false, yank_message: null });

return { ok: true };
});
Expand Down
120 changes: 120 additions & 0 deletions tests/mirage/crates/versions/patch-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { module, test } from 'qunit';

import fetch from 'fetch';

import { setupTest } from '../../../helpers';
import setupMirage from '../../../helpers/setup-mirage';

const YANK_BODY = JSON.stringify({
version: {
yanked: true,
yank_message: 'some reason',
},
});

const UNYANK_BODY = JSON.stringify({
version: {
yanked: false,
},
});

module('Mirage | PATCH /api/v1/crates/:crate/:version', function (hooks) {
setupTest(hooks);
setupMirage(hooks);

test('returns 403 if unauthenticated', async function (assert) {
let response = await fetch('/api/v1/crates/foo/1.0.0', { method: 'PATCH', body: YANK_BODY });
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/1.0.0', { method: 'PATCH', body: YANK_BODY });
assert.strictEqual(response.status, 404);
assert.deepEqual(await response.json(), { errors: [{ detail: 'Not Found' }] });
});

test('returns 404 for unknown versions', async function (assert) {
this.server.create('crate', { name: 'foo' });

let user = this.server.create('user');
this.authenticateAs(user);

let response = await fetch('/api/v1/crates/foo/1.0.0', { method: 'PATCH', body: YANK_BODY });
assert.strictEqual(response.status, 404);
assert.deepEqual(await response.json(), { errors: [{ detail: 'Not Found' }] });
});

test('yanks the version', async function (assert) {
let crate = this.server.create('crate', { name: 'foo' });
let version = this.server.create('version', { crate, num: '1.0.0', yanked: false });
assert.false(version.yanked);
assert.strictEqual(version.yank_message, null);

let user = this.server.create('user');
this.authenticateAs(user);

let response = await fetch('/api/v1/crates/foo/1.0.0', { method: 'PATCH', body: YANK_BODY });
assert.strictEqual(response.status, 200);
assert.deepEqual(await response.json(), {
version: {
crate: 'foo',
crate_size: 0,
created_at: '2010-06-16T21:30:45Z',
dl_path: '/api/v1/crates/foo/1.0.0/download',
downloads: 0,
id: '1',
license: 'MIT/Apache-2.0',
links: {
dependencies: '/api/v1/crates/foo/1.0.0/dependencies',
version_downloads: '/api/v1/crates/foo/1.0.0/downloads',
},
num: '1.0.0',
published_by: null,
readme_path: '/api/v1/crates/foo/1.0.0/readme',
rust_version: null,
updated_at: '2017-02-24T12:34:56Z',
yank_message: 'some reason',
yanked: true,
},
});

user.reload();
assert.true(version.yanked);
assert.strictEqual(version.yank_message, 'some reason');

response = await fetch('/api/v1/crates/foo/1.0.0', { method: 'PATCH', body: UNYANK_BODY });
assert.strictEqual(response.status, 200);
assert.deepEqual(await response.json(), {
version: {
crate: 'foo',
crate_size: 0,
created_at: '2010-06-16T21:30:45Z',
dl_path: '/api/v1/crates/foo/1.0.0/download',
downloads: 0,
id: '1',
license: 'MIT/Apache-2.0',
links: {
dependencies: '/api/v1/crates/foo/1.0.0/dependencies',
version_downloads: '/api/v1/crates/foo/1.0.0/downloads',
},
num: '1.0.0',
published_by: null,
readme_path: '/api/v1/crates/foo/1.0.0/readme',
rust_version: null,
updated_at: '2017-02-24T12:34:56Z',
yank_message: null,
yanked: false,
},
});

user.reload();
assert.false(version.yanked);
assert.strictEqual(version.yank_message, null);
});
});
4 changes: 3 additions & 1 deletion tests/mirage/crates/versions/yank/unyank-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ module('Mirage | PUT /api/v1/crates/:crateId/unyank', function (hooks) {

test('unyanks the version', async function (assert) {
let crate = this.server.create('crate', { name: 'foo' });
let version = this.server.create('version', { crate, num: '1.0.0', yanked: true });
let version = this.server.create('version', { crate, num: '1.0.0', yanked: true, yank_message: 'some reason' });
assert.true(version.yanked);
assert.strictEqual(version.yank_message, 'some reason');

let user = this.server.create('user');
this.authenticateAs(user);
Expand All @@ -51,5 +52,6 @@ module('Mirage | PUT /api/v1/crates/:crateId/unyank', function (hooks) {

user.reload();
assert.false(version.yanked);
assert.strictEqual(version.yank_message, null);
});
});
Loading