|
| 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-20T12:00:00')); |
| 8 | +}); |
| 9 | + |
| 10 | +afterEach(() => { |
| 11 | + vi.restoreAllMocks(); |
| 12 | +}); |
| 13 | + |
| 14 | +test('returns the list of API token for the authenticated `user`', async function () { |
| 15 | + let user = db.user.create(); |
| 16 | + db.mswSession.create({ user }); |
| 17 | + |
| 18 | + db.apiToken.create({ |
| 19 | + user, |
| 20 | + createdAt: '2017-11-19T12:59:22Z', |
| 21 | + crateScopes: ['serde', 'serde-*'], |
| 22 | + endpointScopes: ['publish-update'], |
| 23 | + }); |
| 24 | + db.apiToken.create({ user, createdAt: '2017-11-19T13:59:22Z', expiredAt: '2023-11-20T10:59:22Z' }); |
| 25 | + db.apiToken.create({ user, createdAt: '2017-11-19T14:59:22Z' }); |
| 26 | + db.apiToken.create({ user, createdAt: '2017-11-19T15:59:22Z', expiredAt: '2017-11-20T10:59:22Z' }); |
| 27 | + |
| 28 | + let response = await fetch('/api/v1/me/tokens'); |
| 29 | + assert.strictEqual(response.status, 200); |
| 30 | + assert.deepEqual(await response.json(), { |
| 31 | + api_tokens: [ |
| 32 | + { |
| 33 | + id: 3, |
| 34 | + crate_scopes: null, |
| 35 | + created_at: '2017-11-19T14:59:22.000Z', |
| 36 | + endpoint_scopes: null, |
| 37 | + expired_at: null, |
| 38 | + last_used_at: null, |
| 39 | + name: 'API Token 3', |
| 40 | + }, |
| 41 | + { |
| 42 | + id: 2, |
| 43 | + crate_scopes: null, |
| 44 | + created_at: '2017-11-19T13:59:22.000Z', |
| 45 | + endpoint_scopes: null, |
| 46 | + expired_at: '2023-11-20T10:59:22.000Z', |
| 47 | + last_used_at: null, |
| 48 | + name: 'API Token 2', |
| 49 | + }, |
| 50 | + { |
| 51 | + id: 1, |
| 52 | + crate_scopes: ['serde', 'serde-*'], |
| 53 | + created_at: '2017-11-19T12:59:22.000Z', |
| 54 | + endpoint_scopes: ['publish-update'], |
| 55 | + expired_at: null, |
| 56 | + last_used_at: null, |
| 57 | + name: 'API Token 1', |
| 58 | + }, |
| 59 | + ], |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +test('empty list case', async function () { |
| 64 | + let user = db.user.create(); |
| 65 | + db.mswSession.create({ user }); |
| 66 | + |
| 67 | + let response = await fetch('/api/v1/me/tokens'); |
| 68 | + assert.strictEqual(response.status, 200); |
| 69 | + assert.deepEqual(await response.json(), { api_tokens: [] }); |
| 70 | +}); |
| 71 | + |
| 72 | +test('returns an error if unauthenticated', async function () { |
| 73 | + let response = await fetch('/api/v1/me/tokens'); |
| 74 | + assert.strictEqual(response.status, 403); |
| 75 | + assert.deepEqual(await response.json(), { |
| 76 | + errors: [{ detail: 'must be logged in to perform that action' }], |
| 77 | + }); |
| 78 | +}); |
0 commit comments