2
0
mirror of https://github.com/orbitdb/orbitdb.git synced 2025-03-30 15:08:28 +00:00
orbitdb/test/access-controllers/ipfs-access-controller.test.js
Hayden Young 428ce83878
Refactor/ac ()
* refactor: Move Manifest to own module.

* refactor: Modularize orbitdb access controller.

* chore: Check for correct access controller path and modify if necessary.

* fix: Linting.

* refactor: AC interface no longer needed.

* refactor: Move IPFS-specific AC list back into IPFS AC.

* refactor: Explicitly name access controller param.

* refactor: Pass in manifest settings as object.

* refactor: Config access controllers.

* refactor: ACs should expose specific params before being called with generic params.

* feat: Pass write access to root IPFS AC.

* refactor: AC should handle type prefix.

* test: Test for type.

* refactor: Pass generic access to Database (and inheriting dbs).

* refactor: Use AccessControllers module to manage custom ACs.

* chore: Remove excess console logging.

* test: Fix ipfs module import.
2023-04-03 19:56:47 +08:00

133 lines
3.8 KiB
JavaScript

import { strictEqual, deepStrictEqual, notStrictEqual } from 'assert'
import rmrf from 'rimraf'
import * as IPFS from 'ipfs-core'
import Keystore from '../../src/key-store.js'
import Identities from '../../src/identities/identities.js'
import IPFSAccessController from '../../src/access-controllers/ipfs.js'
import config from '../config.js'
import connectPeers from '../utils/connect-nodes.js'
describe('IPFSAccessController', function () {
const dbPath1 = './orbitdb/tests/ipfs-access-controller/1'
const dbPath2 = './orbitdb/tests/ipfs-access-controller/2'
this.timeout(config.timeout)
let ipfs1, ipfs2
let keystore1, keystore2
let identities1, identities2
let testIdentity1, testIdentity2
let orbitdb1, orbitdb2
before(async () => {
ipfs1 = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
ipfs2 = await IPFS.create({ ...config.daemon2, repo: './ipfs2' })
await connectPeers(ipfs1, ipfs2)
keystore1 = await Keystore({ path: dbPath1 + '/keys' })
keystore2 = await Keystore({ path: dbPath2 + '/keys' })
identities1 = await Identities({ keystore: keystore1 })
identities2 = await Identities({ keystore: keystore2 })
testIdentity1 = await identities1.createIdentity({ id: 'userA' })
testIdentity2 = await identities2.createIdentity({ id: 'userB' })
orbitdb1 = { ipfs: ipfs1, identity: testIdentity1 }
orbitdb2 = { ipfs: ipfs2, identity: testIdentity2 }
})
after(async () => {
if (ipfs1) {
await ipfs1.stop()
}
if (ipfs2) {
await ipfs2.stop()
}
if (keystore1) {
await keystore1.close()
}
if (keystore2) {
await keystore2.close()
}
await rmrf('./orbitdb')
await rmrf('./ipfs1')
await rmrf('./ipfs2')
})
let accessController
describe('Default write access', () => {
before(async () => {
accessController = await IPFSAccessController()({
orbitdb: orbitdb1,
identities: identities1
})
})
it('creates an access controller', () => {
notStrictEqual(accessController, null)
notStrictEqual(accessController, undefined)
})
it('sets the controller type', () => {
strictEqual(accessController.type, 'ipfs')
})
it('sets default write', async () => {
deepStrictEqual(accessController.write, [testIdentity1.id])
})
it('user with write access can append', async () => {
const mockEntry = {
identity: testIdentity1.hash,
v: 1
// ...
// doesn't matter what we put here, only identity is used for the check
}
const canAppend = await accessController.canAppend(mockEntry)
strictEqual(canAppend, true)
})
it('user without write cannot append', async () => {
const mockEntry = {
identity: testIdentity2.hash,
v: 1
// ...
// doesn't matter what we put here, only identity is used for the check
}
const canAppend = await accessController.canAppend(mockEntry)
strictEqual(canAppend, false)
})
it('replicates the access controller', async () => {
const replicatedAccessController = await IPFSAccessController()({
orbitdb: orbitdb2,
identities: identities2,
address: accessController.address
})
strictEqual(replicatedAccessController.type, accessController.type)
strictEqual(replicatedAccessController.address, accessController.address)
deepStrictEqual(replicatedAccessController.write, accessController.write)
})
})
describe('Write all access', () => {
before(async () => {
accessController = await IPFSAccessController({ write: ['*'] })({
orbitdb: orbitdb1,
identities: identities1
})
})
it('sets write to \'Anyone\'', async () => {
deepStrictEqual(accessController.write, ['*'])
})
})
})