|
| 1 | +import { module, test } from 'qunit'; |
| 2 | + |
| 3 | +import fetch from 'fetch'; |
| 4 | + |
| 5 | +import { setupTest } from '../../../helpers'; |
| 6 | +import setupMirage from '../../../helpers/setup-mirage'; |
| 7 | + |
| 8 | +const YANK_BODY = JSON.stringify({ |
| 9 | + version: { |
| 10 | + yanked: true, |
| 11 | + yank_message: 'some reason', |
| 12 | + }, |
| 13 | +}); |
| 14 | + |
| 15 | +const UNYANK_BODY = JSON.stringify({ |
| 16 | + version: { |
| 17 | + yanked: false, |
| 18 | + }, |
| 19 | +}); |
| 20 | + |
| 21 | +module('Mirage | PATCH /api/v1/crates/:crate/:version', function (hooks) { |
| 22 | + setupTest(hooks); |
| 23 | + setupMirage(hooks); |
| 24 | + |
| 25 | + test('returns 403 if unauthenticated', async function (assert) { |
| 26 | + let response = await fetch('/api/v1/crates/foo/1.0.0', { method: 'PATCH', body: YANK_BODY }); |
| 27 | + assert.strictEqual(response.status, 403); |
| 28 | + assert.deepEqual(await response.json(), { |
| 29 | + errors: [{ detail: 'must be logged in to perform that action' }], |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + test('returns 404 for unknown crates', async function (assert) { |
| 34 | + let user = this.server.create('user'); |
| 35 | + this.authenticateAs(user); |
| 36 | + |
| 37 | + let response = await fetch('/api/v1/crates/foo/1.0.0', { method: 'PATCH', body: YANK_BODY }); |
| 38 | + assert.strictEqual(response.status, 404); |
| 39 | + assert.deepEqual(await response.json(), { errors: [{ detail: 'Not Found' }] }); |
| 40 | + }); |
| 41 | + |
| 42 | + test('returns 404 for unknown versions', async function (assert) { |
| 43 | + this.server.create('crate', { name: 'foo' }); |
| 44 | + |
| 45 | + let user = this.server.create('user'); |
| 46 | + this.authenticateAs(user); |
| 47 | + |
| 48 | + let response = await fetch('/api/v1/crates/foo/1.0.0', { method: 'PATCH', body: YANK_BODY }); |
| 49 | + assert.strictEqual(response.status, 404); |
| 50 | + assert.deepEqual(await response.json(), { errors: [{ detail: 'Not Found' }] }); |
| 51 | + }); |
| 52 | + |
| 53 | + test('yanks the version', async function (assert) { |
| 54 | + let crate = this.server.create('crate', { name: 'foo' }); |
| 55 | + let version = this.server.create('version', { crate, num: '1.0.0', yanked: false }); |
| 56 | + assert.false(version.yanked); |
| 57 | + assert.strictEqual(version.yank_message, null); |
| 58 | + |
| 59 | + let user = this.server.create('user'); |
| 60 | + this.authenticateAs(user); |
| 61 | + |
| 62 | + let response = await fetch('/api/v1/crates/foo/1.0.0', { method: 'PATCH', body: YANK_BODY }); |
| 63 | + assert.strictEqual(response.status, 200); |
| 64 | + assert.deepEqual(await response.json(), { |
| 65 | + version: { |
| 66 | + crate: 'foo', |
| 67 | + crate_size: 0, |
| 68 | + created_at: '2010-06-16T21:30:45Z', |
| 69 | + dl_path: '/api/v1/crates/foo/1.0.0/download', |
| 70 | + downloads: 0, |
| 71 | + id: '1', |
| 72 | + license: 'MIT/Apache-2.0', |
| 73 | + links: { |
| 74 | + dependencies: '/api/v1/crates/foo/1.0.0/dependencies', |
| 75 | + version_downloads: '/api/v1/crates/foo/1.0.0/downloads', |
| 76 | + }, |
| 77 | + num: '1.0.0', |
| 78 | + published_by: null, |
| 79 | + readme_path: '/api/v1/crates/foo/1.0.0/readme', |
| 80 | + rust_version: null, |
| 81 | + updated_at: '2017-02-24T12:34:56Z', |
| 82 | + yank_message: 'some reason', |
| 83 | + yanked: true, |
| 84 | + }, |
| 85 | + }); |
| 86 | + |
| 87 | + user.reload(); |
| 88 | + assert.true(version.yanked); |
| 89 | + assert.strictEqual(version.yank_message, 'some reason'); |
| 90 | + |
| 91 | + response = await fetch('/api/v1/crates/foo/1.0.0', { method: 'PATCH', body: UNYANK_BODY }); |
| 92 | + assert.strictEqual(response.status, 200); |
| 93 | + assert.deepEqual(await response.json(), { |
| 94 | + version: { |
| 95 | + crate: 'foo', |
| 96 | + crate_size: 0, |
| 97 | + created_at: '2010-06-16T21:30:45Z', |
| 98 | + dl_path: '/api/v1/crates/foo/1.0.0/download', |
| 99 | + downloads: 0, |
| 100 | + id: '1', |
| 101 | + license: 'MIT/Apache-2.0', |
| 102 | + links: { |
| 103 | + dependencies: '/api/v1/crates/foo/1.0.0/dependencies', |
| 104 | + version_downloads: '/api/v1/crates/foo/1.0.0/downloads', |
| 105 | + }, |
| 106 | + num: '1.0.0', |
| 107 | + published_by: null, |
| 108 | + readme_path: '/api/v1/crates/foo/1.0.0/readme', |
| 109 | + rust_version: null, |
| 110 | + updated_at: '2017-02-24T12:34:56Z', |
| 111 | + yank_message: null, |
| 112 | + yanked: false, |
| 113 | + }, |
| 114 | + }); |
| 115 | + |
| 116 | + user.reload(); |
| 117 | + assert.false(version.yanked); |
| 118 | + assert.strictEqual(version.yank_message, null); |
| 119 | + }); |
| 120 | +}); |
0 commit comments