Skip to content

Commit 10ae9ef

Browse files
SharonStratsGPT-5.4 Mini
andcommitted
Add tests for new acl functions
Prompt: Add basic tests to cover the new acl logic functions that have been added Co-authored-by: GPT-5.4 Mini <gpt-5.4-mini@openai.com>
1 parent d9f617b commit 10ae9ef

1 file changed

Lines changed: 111 additions & 23 deletions

File tree

‎test/aclLogic.test.ts‎

Lines changed: 111 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,113 @@
1-
import { beforeAll, describe, expect, it } from 'vitest'
1+
import { beforeEach, describe, expect, it, vi } from 'vitest'
22
import { Fetcher, Store, sym, UpdateManager } from 'rdflib'
3-
import { createAclLogic } from '../src/acl/aclLogic'
4-
5-
describe('setACLUserPublic', () => {
6-
let aclLogic: any
7-
let store: Store
8-
beforeAll(() => {
9-
const options = { fetch: fetch }
10-
store = new Store()
11-
store.fetcher = new Fetcher(store, options)
12-
store.updater = new UpdateManager(store)
13-
aclLogic = createAclLogic(store)
14-
})
15-
it('exists', () => {
16-
expect(aclLogic.setACLUserPublic).toBeInstanceOf(Function)
17-
})
18-
it.skip('runs', async () => {
19-
expect(await aclLogic.setACLUserPublic(
20-
'https://test.test#',
21-
sym('https://test.test#'),
22-
{}
23-
)).toEqual({})
24-
})
3+
import { findEffectiveACL, planGrant, planPublicRead } from '@dokieli/web-access-control'
4+
import { ACL_LINK, createAclLogic } from '../src/acl/aclLogic'
5+
6+
vi.mock('@dokieli/web-access-control', () => ({
7+
findEffectiveACL: vi.fn(),
8+
planGrant: vi.fn(),
9+
planPublicRead: vi.fn(),
10+
Authenticated: Symbol('Authenticated'),
11+
Public: Symbol('Public'),
12+
planOwnerControl: vi.fn(),
13+
planRevoke: vi.fn()
14+
}))
15+
16+
describe('createAclLogic', () => {
17+
let store: Store & {
18+
fetcher: Fetcher & {
19+
_fetch: ReturnType<typeof vi.fn>
20+
load: ReturnType<typeof vi.fn>
21+
webOperation: ReturnType<typeof vi.fn>
22+
}
23+
any: ReturnType<typeof vi.fn>
24+
}
25+
let aclLogic: ReturnType<typeof createAclLogic>
26+
27+
beforeEach(() => {
28+
const fetcher = {
29+
_fetch: vi.fn(),
30+
load: vi.fn(),
31+
webOperation: vi.fn(),
32+
unload: vi.fn()
33+
} as unknown as Fetcher & {
34+
_fetch: ReturnType<typeof vi.fn>
35+
load: ReturnType<typeof vi.fn>
36+
webOperation: ReturnType<typeof vi.fn>
37+
unload: ReturnType<typeof vi.fn>
38+
}
39+
40+
store = {
41+
fetcher,
42+
updater: new UpdateManager(new Store()),
43+
any: vi.fn()
44+
} as unknown as Store & {
45+
fetcher: typeof fetcher
46+
any: ReturnType<typeof vi.fn>
47+
}
48+
49+
aclLogic = createAclLogic(store)
50+
})
51+
52+
it('exports the ACL link relation constant', () => {
53+
expect(ACL_LINK.value).toBe('http://www.iana.org/assignments/link-relations/acl')
54+
})
55+
56+
it('finds the ACL document URL from the discovered link', async () => {
57+
const resource = sym('https://example.com/resource.ttl')
58+
const aclDoc = sym('https://example.com/resource.ttl.acl')
59+
const loadResult = { ok: true }
60+
61+
store.fetcher.load.mockResolvedValue(loadResult)
62+
store.any.mockReturnValue(aclDoc)
63+
64+
await expect(aclLogic.findAclDocUrl(resource)).resolves.toBe(aclDoc.value)
65+
expect(store.fetcher.load).toHaveBeenCalledWith(resource)
66+
expect(store.any).toHaveBeenCalledWith(resource, ACL_LINK)
67+
})
68+
69+
it('returns the authorizations from the effective ACL context', async () => {
70+
const resource = sym('https://example.com/resource.ttl')
71+
const context = { authorizations: [{ id: 'a1' }] }
72+
73+
vi.mocked(findEffectiveACL).mockResolvedValue(context as any)
74+
store.fetcher._fetch.mockResolvedValue(new Response('', { status: 200 }))
75+
76+
await expect(aclLogic.findAccessGrants(resource)).resolves.toEqual(context.authorizations)
77+
expect(findEffectiveACL).toHaveBeenCalledWith(resource.value, expect.objectContaining({ fetch: expect.any(Function) }))
78+
})
79+
80+
it('delegates planGrant through the effective ACL context', async () => {
81+
const resource = sym('https://example.com/resource.ttl')
82+
const subject = { type: 'agent', value: 'https://example.com/profile/card#me' }
83+
const plan = { updates: [] }
84+
85+
vi.mocked(findEffectiveACL).mockResolvedValue({ authorizations: [] } as any)
86+
vi.mocked(planGrant).mockReturnValue(plan as any)
87+
store.fetcher._fetch.mockResolvedValue(new Response('', { status: 200 }))
88+
89+
await expect(aclLogic.planGrant(resource, subject as any, ['Read'])).resolves.toBe(plan)
90+
expect(planGrant).toHaveBeenCalledWith(expect.objectContaining({ authorizations: [] }), subject, ['Read'])
91+
})
92+
93+
it('delegates planPublicRead through the effective ACL context', async () => {
94+
const resource = sym('https://example.com/resource.ttl')
95+
const plan = { updates: [] }
96+
97+
vi.mocked(findEffectiveACL).mockResolvedValue({ authorizations: [] } as any)
98+
vi.mocked(planPublicRead).mockReturnValue(plan as any)
99+
store.fetcher._fetch.mockResolvedValue(new Response('', { status: 200 }))
100+
101+
await expect(aclLogic.planPublicRead(resource, true)).resolves.toBe(plan)
102+
expect(planPublicRead).toHaveBeenCalledWith(expect.objectContaining({ authorizations: [] }), true)
103+
})
104+
105+
it('throws when the store cannot supply a fetch function', async () => {
106+
const resource = sym('https://example.com/resource.ttl')
107+
store.fetcher = { _fetch: vi.fn(), load: vi.fn(), webOperation: vi.fn(), unload: vi.fn() } as any
108+
delete (store.fetcher as any).fetch
109+
delete (store.fetcher as any)._fetch
110+
111+
await expect(aclLogic.findEffectiveAcl(resource)).rejects.toThrow('Cannot find effective ACL, store has no fetcher')
112+
})
25113
})

0 commit comments

Comments
 (0)