|
| 1 | +import { assert, test } from 'vitest'; |
| 2 | + |
| 3 | +import { db } from '../../index.js'; |
| 4 | + |
| 5 | +test('returns 403 if unauthenticated', async function () { |
| 6 | + let response = await fetch('/api/v1/crates/foo', { |
| 7 | + method: 'PATCH', |
| 8 | + headers: { 'Content-Type': 'application/json' }, |
| 9 | + body: JSON.stringify({ crate: { trustpub_only: true } }), |
| 10 | + }); |
| 11 | + assert.strictEqual(response.status, 403); |
| 12 | + assert.deepEqual(await response.json(), { |
| 13 | + errors: [{ detail: 'must be logged in to perform that action' }], |
| 14 | + }); |
| 15 | +}); |
| 16 | + |
| 17 | +test('returns 404 for unknown crates', async function () { |
| 18 | + let user = db.user.create(); |
| 19 | + db.mswSession.create({ user }); |
| 20 | + |
| 21 | + let response = await fetch('/api/v1/crates/foo', { |
| 22 | + method: 'PATCH', |
| 23 | + headers: { 'Content-Type': 'application/json' }, |
| 24 | + body: JSON.stringify({ crate: { trustpub_only: true } }), |
| 25 | + }); |
| 26 | + assert.strictEqual(response.status, 404); |
| 27 | + assert.deepEqual(await response.json(), { errors: [{ detail: 'crate `foo` does not exist' }] }); |
| 28 | +}); |
| 29 | + |
| 30 | +test('updates trustpub_only flag', async function () { |
| 31 | + let user = db.user.create(); |
| 32 | + db.mswSession.create({ user }); |
| 33 | + |
| 34 | + let crate = db.crate.create({ name: 'foo', trustpubOnly: false }); |
| 35 | + assert.strictEqual(crate.trustpubOnly, false); |
| 36 | + |
| 37 | + db.version.create({ crate, num: '1.0.0' }); |
| 38 | + db.crateOwnership.create({ crate, user }); |
| 39 | + |
| 40 | + let response = await fetch('/api/v1/crates/foo', { |
| 41 | + method: 'PATCH', |
| 42 | + headers: { 'Content-Type': 'application/json' }, |
| 43 | + body: JSON.stringify({ crate: { trustpub_only: true } }), |
| 44 | + }); |
| 45 | + assert.strictEqual(response.status, 200); |
| 46 | + |
| 47 | + let json = await response.json(); |
| 48 | + assert.strictEqual(json.crate.trustpub_only, true); |
| 49 | + |
| 50 | + let updatedCrate = db.crate.findFirst({ where: { name: { equals: 'foo' } } }); |
| 51 | + assert.strictEqual(updatedCrate.trustpubOnly, true); |
| 52 | +}); |
0 commit comments