|
| 1 | +import { afterEach, assert, beforeEach, test, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { db } from '../../index.js'; |
| 4 | + |
| 5 | +beforeEach(() => { |
| 6 | + vi.useFakeTimers(); |
| 7 | + vi.setSystemTime(new Date('2017-11-20T11:23:45Z')); |
| 8 | +}); |
| 9 | + |
| 10 | +afterEach(() => { |
| 11 | + vi.restoreAllMocks(); |
| 12 | +}); |
| 13 | + |
| 14 | +test('creates a new API token', async function () { |
| 15 | + let user = db.user.create(); |
| 16 | + db.mswSession.create({ user }); |
| 17 | + |
| 18 | + let body = JSON.stringify({ api_token: { name: 'foooo' } }); |
| 19 | + let response = await fetch('/api/v1/me/tokens', { method: 'PUT', body }); |
| 20 | + assert.strictEqual(response.status, 200); |
| 21 | + |
| 22 | + let token = db.apiToken.findMany({})[0]; |
| 23 | + assert.ok(token); |
| 24 | + |
| 25 | + assert.deepEqual(await response.json(), { |
| 26 | + api_token: { |
| 27 | + id: 1, |
| 28 | + crate_scopes: null, |
| 29 | + created_at: '2017-11-20T11:23:45.000Z', |
| 30 | + endpoint_scopes: null, |
| 31 | + expired_at: null, |
| 32 | + last_used_at: null, |
| 33 | + name: 'foooo', |
| 34 | + revoked: false, |
| 35 | + token: token.token, |
| 36 | + }, |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +test('creates a new API token with scopes', async function () { |
| 41 | + let user = db.user.create(); |
| 42 | + db.mswSession.create({ user }); |
| 43 | + |
| 44 | + let body = JSON.stringify({ |
| 45 | + api_token: { |
| 46 | + name: 'foooo', |
| 47 | + crate_scopes: ['serde', 'serde-*'], |
| 48 | + endpoint_scopes: ['publish-update'], |
| 49 | + }, |
| 50 | + }); |
| 51 | + let response = await fetch('/api/v1/me/tokens', { method: 'PUT', body }); |
| 52 | + assert.strictEqual(response.status, 200); |
| 53 | + |
| 54 | + let token = db.apiToken.findMany({})[0]; |
| 55 | + assert.ok(token); |
| 56 | + |
| 57 | + assert.deepEqual(await response.json(), { |
| 58 | + api_token: { |
| 59 | + id: 1, |
| 60 | + crate_scopes: ['serde', 'serde-*'], |
| 61 | + created_at: '2017-11-20T11:23:45.000Z', |
| 62 | + endpoint_scopes: ['publish-update'], |
| 63 | + expired_at: null, |
| 64 | + last_used_at: null, |
| 65 | + name: 'foooo', |
| 66 | + revoked: false, |
| 67 | + token: token.token, |
| 68 | + }, |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +test('creates a new API token with expiry date', async function () { |
| 73 | + let user = db.user.create(); |
| 74 | + db.mswSession.create({ user }); |
| 75 | + |
| 76 | + let body = JSON.stringify({ |
| 77 | + api_token: { |
| 78 | + name: 'foooo', |
| 79 | + expired_at: '2023-12-24T12:34:56Z', |
| 80 | + }, |
| 81 | + }); |
| 82 | + let response = await fetch('/api/v1/me/tokens', { method: 'PUT', body }); |
| 83 | + assert.strictEqual(response.status, 200); |
| 84 | + |
| 85 | + let token = db.apiToken.findMany({})[0]; |
| 86 | + assert.ok(token); |
| 87 | + |
| 88 | + assert.deepEqual(await response.json(), { |
| 89 | + api_token: { |
| 90 | + id: 1, |
| 91 | + crate_scopes: null, |
| 92 | + created_at: '2017-11-20T11:23:45.000Z', |
| 93 | + endpoint_scopes: null, |
| 94 | + expired_at: '2023-12-24T12:34:56.000Z', |
| 95 | + last_used_at: null, |
| 96 | + name: 'foooo', |
| 97 | + revoked: false, |
| 98 | + token: token.token, |
| 99 | + }, |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +test('returns an error if unauthenticated', async function () { |
| 104 | + let body = JSON.stringify({ api_token: {} }); |
| 105 | + let response = await fetch('/api/v1/me/tokens', { method: 'PUT', body }); |
| 106 | + assert.strictEqual(response.status, 403); |
| 107 | + assert.deepEqual(await response.json(), { |
| 108 | + errors: [{ detail: 'must be logged in to perform that action' }], |
| 109 | + }); |
| 110 | +}); |
0 commit comments