|
1 | | -import { beforeAll, describe, expect, it } from 'vitest' |
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
2 | 2 | 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 { applyPlan, findEffectiveACL, planGrant, planPublicRead, planRevoke } 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 | + planRevoke: vi.fn(), |
| 11 | + applyPlan: vi.fn(), |
| 12 | + Authenticated: Symbol('Authenticated'), |
| 13 | + Public: Symbol('Public'), |
| 14 | + planOwnerControl: vi.fn() |
| 15 | +})) |
| 16 | + |
| 17 | +describe('createAclLogic', () => { |
| 18 | + let store: Store & { |
| 19 | + fetcher: Fetcher & { |
| 20 | + _fetch: ReturnType<typeof vi.fn> |
| 21 | + load: ReturnType<typeof vi.fn> |
| 22 | + webOperation: ReturnType<typeof vi.fn> |
| 23 | + } |
| 24 | + any: ReturnType<typeof vi.fn> |
| 25 | + } |
| 26 | + let aclLogic: ReturnType<typeof createAclLogic> |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + const fetcher = { |
| 30 | + _fetch: vi.fn(), |
| 31 | + load: vi.fn(), |
| 32 | + webOperation: vi.fn(), |
| 33 | + unload: vi.fn() |
| 34 | + } as unknown as Fetcher & { |
| 35 | + _fetch: ReturnType<typeof vi.fn> |
| 36 | + load: ReturnType<typeof vi.fn> |
| 37 | + webOperation: ReturnType<typeof vi.fn> |
| 38 | + unload: ReturnType<typeof vi.fn> |
| 39 | + } |
| 40 | + |
| 41 | + store = { |
| 42 | + fetcher, |
| 43 | + updater: new UpdateManager(new Store()), |
| 44 | + any: vi.fn() |
| 45 | + } as unknown as Store & { |
| 46 | + fetcher: typeof fetcher |
| 47 | + any: ReturnType<typeof vi.fn> |
| 48 | + } |
| 49 | + |
| 50 | + aclLogic = createAclLogic(store) |
| 51 | + }) |
| 52 | + |
| 53 | + it('exports the ACL link relation constant', () => { |
| 54 | + expect(ACL_LINK.value).toBe('http://www.iana.org/assignments/link-relations/acl') |
| 55 | + }) |
| 56 | + |
| 57 | + it('finds the ACL document URL from the discovered link', async () => { |
| 58 | + const resource = sym('https://example.com/resource.ttl') |
| 59 | + const aclDoc = sym('https://example.com/resource.ttl.acl') |
| 60 | + const loadResult = { ok: true } |
| 61 | + |
| 62 | + store.fetcher.load.mockResolvedValue(loadResult) |
| 63 | + store.any.mockReturnValue(aclDoc) |
| 64 | + |
| 65 | + await expect(aclLogic.findAclDocUrl(resource)).resolves.toBe(aclDoc.value) |
| 66 | + expect(store.fetcher.load).toHaveBeenCalledWith(resource) |
| 67 | + expect(store.any).toHaveBeenCalledWith(resource, ACL_LINK) |
| 68 | + }) |
| 69 | + |
| 70 | + it('returns the authorizations from the effective ACL context', async () => { |
| 71 | + const resource = sym('https://example.com/resource.ttl') |
| 72 | + const context = { authorizations: [{ id: 'a1' }] } |
| 73 | + |
| 74 | + vi.mocked(findEffectiveACL).mockResolvedValue(context as any) |
| 75 | + store.fetcher._fetch.mockResolvedValue(new Response('', { status: 200 })) |
| 76 | + |
| 77 | + await expect(aclLogic.findAccessGrants(resource)).resolves.toEqual(context.authorizations) |
| 78 | + expect(findEffectiveACL).toHaveBeenCalledWith(resource.value, expect.objectContaining({ fetch: expect.any(Function) })) |
| 79 | + }) |
| 80 | + |
| 81 | + it('delegates planGrant through the effective ACL context', async () => { |
| 82 | + const resource = sym('https://example.com/resource.ttl') |
| 83 | + const subject = { type: 'agent', value: 'https://example.com/profile/card#me' } |
| 84 | + const plan = { updates: [] } |
| 85 | + |
| 86 | + vi.mocked(findEffectiveACL).mockResolvedValue({ authorizations: [] } as any) |
| 87 | + vi.mocked(planGrant).mockReturnValue(plan as any) |
| 88 | + store.fetcher._fetch.mockResolvedValue(new Response('', { status: 200 })) |
| 89 | + |
| 90 | + await expect(aclLogic.planGrant(resource, subject as any, ['Read'])).resolves.toBe(plan) |
| 91 | + expect(planGrant).toHaveBeenCalledWith(expect.objectContaining({ authorizations: [] }), subject, ['Read']) |
| 92 | + }) |
| 93 | + |
| 94 | + it('delegates planPublicRead through the effective ACL context', async () => { |
| 95 | + const resource = sym('https://example.com/resource.ttl') |
| 96 | + const plan = { updates: [] } |
| 97 | + |
| 98 | + vi.mocked(findEffectiveACL).mockResolvedValue({ authorizations: [] } as any) |
| 99 | + vi.mocked(planPublicRead).mockReturnValue(plan as any) |
| 100 | + store.fetcher._fetch.mockResolvedValue(new Response('', { status: 200 })) |
| 101 | + |
| 102 | + await expect(aclLogic.planPublicRead(resource, true)).resolves.toBe(plan) |
| 103 | + expect(planPublicRead).toHaveBeenCalledWith(expect.objectContaining({ authorizations: [] }), true) |
| 104 | + }) |
| 105 | + |
| 106 | + it('delegates planRevoke through the effective ACL context', async () => { |
| 107 | + const resource = sym('https://example.com/resource.ttl') |
| 108 | + const subject = { type: 'agent', iri: 'https://example.com/profile/card#me' } |
| 109 | + const plan = { target: 'https://example.com/resource.ttl.acl', deletes: [], inserts: [] } |
| 110 | + |
| 111 | + vi.mocked(findEffectiveACL).mockResolvedValue({ authorizations: [] } as any) |
| 112 | + vi.mocked(planRevoke).mockReturnValue(plan as any) |
| 113 | + store.fetcher._fetch.mockResolvedValue(new Response('', { status: 200 })) |
| 114 | + |
| 115 | + await expect(aclLogic.planRevoke(resource, subject as any)).resolves.toBe(plan) |
| 116 | + expect(planRevoke).toHaveBeenCalledWith(expect.objectContaining({ authorizations: [] }), subject) |
| 117 | + }) |
| 118 | + |
| 119 | + it('applies a patch plan with the store fetcher', async () => { |
| 120 | + const plan = { target: 'https://example.com/resource.ttl.acl', deletes: [], inserts: [] } |
| 121 | + const response = new Response('ok', { status: 200 }) |
| 122 | + |
| 123 | + store.fetcher._fetch.mockResolvedValue(new Response('', { status: 200 })) |
| 124 | + vi.mocked(applyPlan).mockResolvedValue(response) |
| 125 | + |
| 126 | + await expect(aclLogic.applyPlan(plan as any)).resolves.toBe(response) |
| 127 | + expect(applyPlan).toHaveBeenCalledWith(plan, expect.objectContaining({ fetch: expect.any(Function) })) |
| 128 | + }) |
| 129 | + |
| 130 | + it('throws when the store cannot supply a fetch function', async () => { |
| 131 | + const resource = sym('https://example.com/resource.ttl') |
| 132 | + store.fetcher = { _fetch: vi.fn(), load: vi.fn(), webOperation: vi.fn(), unload: vi.fn() } as any |
| 133 | + delete (store.fetcher as any).fetch |
| 134 | + delete (store.fetcher as any)._fetch |
| 135 | + |
| 136 | + await expect(aclLogic.findEffectiveAcl(resource)).rejects.toThrow('Cannot find effective ACL, store has no fetcher') |
| 137 | + }) |
25 | 138 | }) |
0 commit comments