forked from koshikraj/ottopus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagents.test.ts
More file actions
108 lines (93 loc) · 3.71 KB
/
Copy pathagents.test.ts
File metadata and controls
108 lines (93 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { PGlite } from '@electric-sql/pglite'
import { drizzle } from 'drizzle-orm/pglite'
import type { MiddlewareHandler } from 'hono'
import { beforeAll, beforeEach, describe, expect, it } from 'vitest'
import { userIdForDid } from '../auth/session.js'
import { migrationFiles, statementsIn } from '../db/migrate.js'
import * as schema from '../db/schema.js'
import { oauthGrants } from '../db/schema.js'
import { registerClient } from '../oauth/index.js'
import { agentRoutes } from './agents.js'
/**
* Tenant isolation at the HTTP surface. The store tests already prove that
* revokeGrant refuses a stranger; this checks that the route never lets one
* user's session reach another's grants, whichever way the id arrives.
*
* Grants are inserted directly rather than through the OAuth dance: how a
* grant comes to exist is the store's concern, and it has its own tests.
*/
let db: ReturnType<typeof drizzle<typeof schema>>
let pg: PGlite
let alice: string
let bob: string
let clientId: string
/** Stands in for requireSession, as whoever `userId` is. */
const signedInAs = (userId: string): MiddlewareHandler => {
return async (c, next) => {
c.set('userId', userId)
await next()
}
}
const app = (userId: string) => agentRoutes(db, signedInAs(userId))
async function grantTo(userId: string): Promise<string> {
const [row] = await db
.insert(oauthGrants)
.values({ userId, clientId, scopes: ['wallets:read'] })
.returning({ id: oauthGrants.id })
return row!.id
}
async function agentsOf(userId: string): Promise<{ id: string; revokedAt: string | null }[]> {
const res = await app(userId).request('/')
expect(res.status).toBe(200)
return ((await res.json()) as { agents: { id: string; revokedAt: string | null }[] }).agents
}
beforeAll(async () => {
pg = await PGlite.create()
await pg.exec(`create role anon; create role authenticated; create role service_role;`)
for (const file of await migrationFiles(new URL('../../drizzle', import.meta.url).pathname)) {
for (const stmt of await statementsIn(file)) await pg.exec(stmt)
}
db = drizzle(pg, { schema, casing: 'snake_case' })
alice = await userIdForDid(db, 'did:privy:alice')
bob = await userIdForDid(db, 'did:privy:bob')
const client = await registerClient(db, {
clientName: 'Test agent',
redirectUris: ['https://agent.example/callback'],
})
clientId = client.clientId
}, 60_000)
beforeEach(async () => {
await pg.exec(`delete from oauth_grants`)
})
describe('GET /', () => {
it('lists only the caller’s grants', async () => {
const mine = await grantTo(alice)
await grantTo(bob)
expect((await agentsOf(alice)).map((a) => a.id)).toEqual([mine])
})
it('is empty for a user who never authorised anything', async () => {
await grantTo(bob)
expect(await agentsOf(alice)).toEqual([])
})
})
describe('DELETE /:id', () => {
it('revokes the caller’s own grant', async () => {
const mine = await grantTo(alice)
expect((await app(alice).request(`/${mine}`, { method: 'DELETE' })).status).toBe(204)
expect((await agentsOf(alice))[0]!.revokedAt).not.toBeNull()
})
/**
* Someone else's grant id must look exactly like a missing one. A 403 would
* confirm the id exists, and a 204 would be a cross-tenant revoke.
*/
it('answers 404 for a grant belonging to someone else, and leaves it live', async () => {
const bobs = await grantTo(bob)
expect((await app(alice).request(`/${bobs}`, { method: 'DELETE' })).status).toBe(404)
const [grant] = await agentsOf(bob)
expect(grant!.id).toBe(bobs)
expect(grant!.revokedAt).toBeNull()
})
it('answers 404 for a malformed id', async () => {
expect((await app(alice).request('/not-a-uuid', { method: 'DELETE' })).status).toBe(404)
})
})