-
Notifications
You must be signed in to change notification settings - Fork 2
/
test-access.ts
132 lines (116 loc) · 4.62 KB
/
test-access.ts
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { start } from '@libp2p/interfaces/startable'
import { assert, expect } from 'aegir/chai'
import { base32 } from 'multiformats/bases/base32'
import { getTestPaths, tempPath } from './utils/constants.js'
import defaultManifest from './utils/default-manifest.js'
import { singleEntry } from './utils/entries.js'
import { getTestIdentities, getTestIdentity } from './utils/identities.js'
import { getTestIpfs, offlineIpfsOptions } from './utils/ipfs.js'
import { getTestLibp2p } from './utils/libp2p.js'
import type { Entry } from '@/entry/basal/index.js'
import type { Identity } from '@/identity/basal/index.js'
import { wildcard } from '@/access/interface.js'
import { staticAccess } from '@/access/static/index.js'
import protocol, { type AccessProtocol } from '@/access/static/protocol.js'
import { Manifest } from '@/manifest/index.js'
const testName = 'static access'
describe(testName, () => {
let identity: Identity, entry: Entry
const Access = staticAccess()
const name = 'name'
const makeaccess = (write: Array<Uint8Array | string>): AccessProtocol => ({
protocol: Access.protocol,
config: { write }
})
let yesaccess: AccessProtocol
const anyaccess: AccessProtocol = makeaccess([wildcard])
const noaccess: AccessProtocol = makeaccess([new Uint8Array()])
const emptyaccess: AccessProtocol = makeaccess([])
before(async () => {
const testPaths = getTestPaths(tempPath, testName)
const ipfs = await getTestIpfs(testPaths, offlineIpfsOptions)
const libp2p = await getTestLibp2p(ipfs)
const identities = await getTestIdentities(testPaths)
identity = await getTestIdentity(identities, libp2p.services.keychain, name)
entry = await singleEntry(identity)()
yesaccess = makeaccess([identity.id])
await ipfs.stop()
})
describe('Class', () => {
it('exposes static properties', () => {
assert.strictEqual(Access.protocol, protocol)
assert.strictEqual(Access.protocol, '/hldb/access/static')
})
describe('.open', () => {
it('returns an instance of Static Access', async () => {
const manifest = await Manifest.create({
...defaultManifest(name, identity),
access: yesaccess
})
const access = Access.create({ manifest })
await start(access)
assert.strictEqual(access.manifest, manifest)
assert.deepEqual(access.write, new Set([base32.encode(identity.id)]))
})
it('returns an instance with a wildcard write', async () => {
const manifest = await Manifest.create({
...defaultManifest(name, identity),
access: anyaccess
})
const access = Access.create({ manifest })
await start(access)
assert.strictEqual(access.manifest, manifest)
assert.deepEqual(access.write, new Set(anyaccess.config?.write))
})
it('rejects when write access is empty', async () => {
const manifest = await Manifest.create({
...defaultManifest(name, identity),
access: emptyaccess
})
const access = Access.create({ manifest })
await expect(access.start()).to.eventually.be.rejected()
})
})
})
describe('Instance', () => {
it('exposes instance properties', async () => {
const manifest = await Manifest.create({
...defaultManifest(name, identity),
access: yesaccess
})
const access = Access.create({ manifest })
await start(access)
assert.strictEqual(access.manifest, manifest)
assert.deepEqual(access.write, new Set([base32.encode(identity.id)]))
})
describe('.canAppend', () => {
it('returns true if identity has write access', async () => {
const manifest = await Manifest.create({
...defaultManifest(name, identity),
access: yesaccess
})
const access = Access.create({ manifest })
await start(access)
assert.strictEqual(await access.canAppend(entry), true)
})
it('returns true if wildcard has write access', async () => {
const manifest = await Manifest.create({
...defaultManifest(name, identity),
access: anyaccess
})
const access = Access.create({ manifest })
await start(access)
assert.strictEqual(await access.canAppend(entry), true)
})
it('returns false if identity has no write access', async () => {
const manifest = await Manifest.create({
...defaultManifest(name, identity),
access: noaccess
})
const access = Access.create({ manifest })
await start(access)
assert.strictEqual(await access.canAppend(entry), false)
})
})
})
})