Use js-ipfs only in tests

This commit is contained in:
haad 2023-03-02 09:01:50 +02:00
parent 93473c8fec
commit 9f50fb5e53
33 changed files with 2647 additions and 4578 deletions

2547
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -34,11 +34,9 @@
"cross-env": "^7.0.3",
"fs-extra": "^11.1.0",
"ipfs": "^0.66.0",
"ipfsd-ctl": "^13.0.0",
"it-all": "^2.0.0",
"mocha": "^10.2.0",
"open-cli": "^7.1.0",
"orbit-db-test-utils": "^3.0.0",
"p-map-series": "^3.0.0",
"path-browserify": "^1.0.1",
"remark-cli": "^11.0.0",

View File

@ -1,33 +1,33 @@
import * as io from 'orbit-db-io'
// import * as io from 'orbit-db-io'
export default class AccessControllerManifest {
constructor (type, params = {}) {
this.type = type
this.params = params
}
// export default class AccessControllerManifest {
// constructor (type, params = {}) {
// this.type = type
// this.params = params
// }
static async resolve (ipfs, manifestHash, options = {}) {
if (options.skipManifest) {
if (!options.type) {
throw new Error('No manifest, access-controller type required')
}
return new AccessControllerManifest(options.type, { address: manifestHash })
} else {
// TODO: ensure this is a valid multihash
if (manifestHash.indexOf('/ipfs') === 0) { manifestHash = manifestHash.split('/')[2] }
const { type, params } = await io.read(ipfs, manifestHash)
return new AccessControllerManifest(type, params)
}
}
// static async resolve (ipfs, manifestHash, options = {}) {
// if (options.skipManifest) {
// if (!options.type) {
// throw new Error('No manifest, access-controller type required')
// }
// return new AccessControllerManifest(options.type, { address: manifestHash })
// } else {
// // TODO: ensure this is a valid multihash
// if (manifestHash.indexOf('/ipfs') === 0) { manifestHash = manifestHash.split('/')[2] }
// const { type, params } = await io.read(ipfs, manifestHash)
// return new AccessControllerManifest(type, params)
// }
// }
static async create (ipfs, type, params) {
if (params.skipManifest) {
return params.address
}
const manifest = {
type,
params
}
return io.write(ipfs, 'dag-cbor', manifest)
}
}
// static async create (ipfs, type, params) {
// if (params.skipManifest) {
// return params.address
// }
// const manifest = {
// type,
// params
// }
// return io.write(ipfs, 'dag-cbor', manifest)
// }
// }

View File

@ -1,143 +1,143 @@
import assert from 'assert'
import rmrf from 'rimraf'
import Web3 from 'web3'
import OrbitDB from '../../src/OrbitDB.js'
// import assert from 'assert'
// import rmrf from 'rimraf'
// import Web3 from 'web3'
// import OrbitDB from '../../src/OrbitDB.js'
import IdentityProvider from 'orbit-db-identity-provider'
import Keystore from 'orbit-db-keystore'
import AccessControllers from 'orbit-db-access-controllers'
import ContractAccessController from 'orbit-db-access-controllers/contract'
import ganache from 'ganache-cli'
import Access from './Access.json' assert {type: "json"}
// import IdentityProvider from 'orbit-db-identity-provider'
// import Keystore from 'orbit-db-keystore'
// import AccessControllers from 'orbit-db-access-controllers'
// import ContractAccessController from 'orbit-db-access-controllers/contract'
// import ganache from 'ganache-cli'
// import Access from './Access.json' assert {type: "json"}
// Include test utilities
import {
config,
startIpfs,
stopIpfs,
testAPIs
} from 'orbit-db-test-utils'
// // Include test utilities
// import {
// config,
// startIpfs,
// stopIpfs,
// testAPIs
// } from 'orbit-db-test-utils'
const abi = Access.abi
const bytecode = Access.bytecode
const dbPath1 = './orbitdb/tests/orbitdb-access-controller/1'
const dbPath2 = './orbitdb/tests/orbitdb-access-controller/2'
// const abi = Access.abi
// const bytecode = Access.bytecode
// const dbPath1 = './orbitdb/tests/orbitdb-access-controller/1'
// const dbPath2 = './orbitdb/tests/orbitdb-access-controller/2'
Object.keys(testAPIs).forEach(API => {
describe(`orbit-db - Access Controller Handlers (${API})`, function () {
this.timeout(config.timeout)
// Object.keys(testAPIs).forEach(API => {
// describe(`orbit-db - Access Controller Handlers (${API})`, function () {
// this.timeout(config.timeout)
let web3, contract, ipfsd1, ipfsd2, ipfs1, ipfs2, id1, id2
let orbitdb1, orbitdb2
// let web3, contract, ipfsd1, ipfsd2, ipfs1, ipfs2, id1, id2
// let orbitdb1, orbitdb2
before(async () => {
rmrf.sync(dbPath1)
rmrf.sync(dbPath2)
ipfsd1 = await startIpfs(API, config.daemon1)
ipfsd2 = await startIpfs(API, config.daemon2)
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api
// before(async () => {
// rmrf.sync(dbPath1)
// rmrf.sync(dbPath2)
// ipfsd1 = await startIpfs(API, config.daemon1)
// ipfsd2 = await startIpfs(API, config.daemon2)
// ipfs1 = ipfsd1.api
// ipfs2 = ipfsd2.api
const keystore1 = new Keystore(dbPath1 + '/keys')
const keystore2 = new Keystore(dbPath2 + '/keys')
// const keystore1 = new Keystore(dbPath1 + '/keys')
// const keystore2 = new Keystore(dbPath2 + '/keys')
id1 = await IdentityProvider.createIdentity({ id: 'A', keystore: keystore1 })
id2 = await IdentityProvider.createIdentity({ id: 'B', keystore: keystore2 })
// id1 = await IdentityProvider.createIdentity({ id: 'A', keystore: keystore1 })
// id2 = await IdentityProvider.createIdentity({ id: 'B', keystore: keystore2 })
orbitdb1 = await OrbitDB.createInstance(ipfs1, {
AccessControllers: AccessControllers,
directory: dbPath1,
identity: id1
})
// orbitdb1 = await OrbitDB.createInstance(ipfs1, {
// AccessControllers: AccessControllers,
// directory: dbPath1,
// identity: id1
// })
orbitdb2 = await OrbitDB.createInstance(ipfs2, {
AccessControllers: AccessControllers,
directory: dbPath2,
identity: id2
})
})
// orbitdb2 = await OrbitDB.createInstance(ipfs2, {
// AccessControllers: AccessControllers,
// directory: dbPath2,
// identity: id2
// })
// })
after(async () => {
if (orbitdb1) { await orbitdb1.stop() }
// after(async () => {
// if (orbitdb1) { await orbitdb1.stop() }
if (orbitdb2) { await orbitdb2.stop() }
// if (orbitdb2) { await orbitdb2.stop() }
if (ipfsd1) { await stopIpfs(ipfsd1) }
// if (ipfsd1) { await stopIpfs(ipfsd1) }
if (ipfsd2) { await stopIpfs(ipfsd2) }
})
// if (ipfsd2) { await stopIpfs(ipfsd2) }
// })
describe('isSupported', function () {
it('supports default access controllers', () => {
assert.strictEqual(AccessControllers.isSupported('ipfs'), true)
assert.strictEqual(AccessControllers.isSupported('orbitdb'), true)
})
// describe('isSupported', function () {
// it('supports default access controllers', () => {
// assert.strictEqual(AccessControllers.isSupported('ipfs'), true)
// assert.strictEqual(AccessControllers.isSupported('orbitdb'), true)
// })
it('doesn\'t support smart contract access controller by default', () => {
assert.strictEqual(AccessControllers.isSupported(ContractAccessController.type), false)
})
})
// it('doesn\'t support smart contract access controller by default', () => {
// assert.strictEqual(AccessControllers.isSupported(ContractAccessController.type), false)
// })
// })
describe('addAccessController', function () {
it('supports added access controller', () => {
const options = {
AccessController: ContractAccessController,
web3: web3,
abi: abi
}
AccessControllers.addAccessController(options)
assert.strictEqual(AccessControllers.isSupported(ContractAccessController.type), true)
})
})
// describe('addAccessController', function () {
// it('supports added access controller', () => {
// const options = {
// AccessController: ContractAccessController,
// web3: web3,
// abi: abi
// }
// AccessControllers.addAccessController(options)
// assert.strictEqual(AccessControllers.isSupported(ContractAccessController.type), true)
// })
// })
describe('create access controllers', function () {
let options = {
AccessController: ContractAccessController
}
// describe('create access controllers', function () {
// let options = {
// AccessController: ContractAccessController
// }
before(async () => {
web3 = new Web3(ganache.provider())
const accounts = await web3.eth.getAccounts()
contract = await new web3.eth.Contract(abi)
.deploy({ data: bytecode })
.send({ from: accounts[0], gas: '1000000' })
options = Object.assign({}, options, { web3, abi, contractAddress: contract._address, defaultAccount: accounts[0] })
AccessControllers.addAccessController(options)
})
// before(async () => {
// web3 = new Web3(ganache.provider())
// const accounts = await web3.eth.getAccounts()
// contract = await new web3.eth.Contract(abi)
// .deploy({ data: bytecode })
// .send({ from: accounts[0], gas: '1000000' })
// options = Object.assign({}, options, { web3, abi, contractAddress: contract._address, defaultAccount: accounts[0] })
// AccessControllers.addAccessController(options)
// })
it('throws an error if AccessController is not defined', async () => {
let err
try {
AccessControllers.addAccessController({})
} catch (e) {
err = e.toString()
}
assert.strictEqual(err, 'Error: AccessController class needs to be given as an option')
})
// it('throws an error if AccessController is not defined', async () => {
// let err
// try {
// AccessControllers.addAccessController({})
// } catch (e) {
// err = e.toString()
// }
// assert.strictEqual(err, 'Error: AccessController class needs to be given as an option')
// })
it('throws an error if AccessController doesn\'t define type', async () => {
let err
try {
AccessControllers.addAccessController({ AccessController: {} })
} catch (e) {
err = e.toString()
}
assert.strictEqual(err, 'Error: Given AccessController class needs to implement: static get type() { /* return a string */}.')
})
// it('throws an error if AccessController doesn\'t define type', async () => {
// let err
// try {
// AccessControllers.addAccessController({ AccessController: {} })
// } catch (e) {
// err = e.toString()
// }
// assert.strictEqual(err, 'Error: Given AccessController class needs to implement: static get type() { /* return a string */}.')
// })
it('creates a custom access controller', async () => {
const type = ContractAccessController.type
const acManifestHash = await AccessControllers.create(orbitdb1, type, options)
assert.notStrictEqual(acManifestHash, null)
// it('creates a custom access controller', async () => {
// const type = ContractAccessController.type
// const acManifestHash = await AccessControllers.create(orbitdb1, type, options)
// assert.notStrictEqual(acManifestHash, null)
const ac = await AccessControllers.resolve(orbitdb1, acManifestHash, options)
assert.strictEqual(ac.type, type)
})
// const ac = await AccessControllers.resolve(orbitdb1, acManifestHash, options)
// assert.strictEqual(ac.type, type)
// })
it('removes the custom access controller', async () => {
AccessControllers.removeAccessController(ContractAccessController.type)
assert.strictEqual(AccessControllers.isSupported(ContractAccessController.type), false)
})
})
})
})
// it('removes the custom access controller', async () => {
// AccessControllers.removeAccessController(ContractAccessController.type)
// assert.strictEqual(AccessControllers.isSupported(ContractAccessController.type), false)
// })
// })
// })
// })

View File

@ -1,275 +1,275 @@
import assert from 'assert'
import rmrf from 'rimraf'
import OrbitDB from '../../src/OrbitDB.js'
import IdentityProvider from 'orbit-db-identity-provider'
import EthIdentityProvider from 'orbit-db-identity-provider/ethereum'
import Keystore from 'orbit-db-keystore'
import AccessControllers from 'orbit-db-access-controllers'
import ContractAccessController from 'orbit-db-access-controllers/contract'
import DepositContractAccessController from 'orbit-db-access-controllers/deposit-contract'
import ganache from 'ganache-cli'
import Web3 from 'web3'
import * as io from 'orbit-db-io'
import Access from './Access.json' assert {type: "json"}
import PayDeposit from './PayDeposit.json' assert {type: "json"}
// import assert from 'assert'
// import rmrf from 'rimraf'
// import OrbitDB from '../../src/OrbitDB.js'
// import IdentityProvider from 'orbit-db-identity-provider'
// import EthIdentityProvider from 'orbit-db-identity-provider/ethereum'
// import Keystore from 'orbit-db-keystore'
// import AccessControllers from 'orbit-db-access-controllers'
// import ContractAccessController from 'orbit-db-access-controllers/contract'
// import DepositContractAccessController from 'orbit-db-access-controllers/deposit-contract'
// import ganache from 'ganache-cli'
// import Web3 from 'web3'
// import * as io from 'orbit-db-io'
// import Access from './Access.json' assert {type: "json"}
// import PayDeposit from './PayDeposit.json' assert {type: "json"}
// Include test utilities
import {
config,
startIpfs,
stopIpfs,
testAPIs,
connectPeers
} from 'orbit-db-test-utils'
// // Include test utilities
// import {
// config,
// startIpfs,
// stopIpfs,
// testAPIs,
// connectPeers
// } from 'orbit-db-test-utils'
const dbPath1 = './orbitdb/tests/contract-access-controller-integration/1'
const dbPath2 = './orbitdb/tests/contract-access-controller-integration/2'
// const dbPath1 = './orbitdb/tests/contract-access-controller-integration/1'
// const dbPath2 = './orbitdb/tests/contract-access-controller-integration/2'
const accessControllers = [
{
AccessController: ContractAccessController,
contract: Access
},
{
AccessController: DepositContractAccessController,
contract: PayDeposit
}
]
// const accessControllers = [
// {
// AccessController: ContractAccessController,
// contract: Access
// },
// {
// AccessController: DepositContractAccessController,
// contract: PayDeposit
// }
// ]
Object.keys(testAPIs).forEach(API => {
describe(`orbit-db - ContractAccessController Integration (${API})`, function () {
this.timeout(config.timeout * 2)
// Object.keys(testAPIs).forEach(API => {
// describe(`orbit-db - ContractAccessController Integration (${API})`, function () {
// this.timeout(config.timeout * 2)
let ipfsd1, ipfsd2, ipfs1, ipfs2, id1, id2
let orbitdb1, orbitdb2
let web3, accounts
// let ipfsd1, ipfsd2, ipfs1, ipfs2, id1, id2
// let orbitdb1, orbitdb2
// let web3, accounts
before(async () => {
rmrf.sync(dbPath1)
rmrf.sync(dbPath2)
ipfsd1 = await startIpfs(API, config.daemon1)
ipfsd2 = await startIpfs(API, config.daemon2)
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api
// before(async () => {
// rmrf.sync(dbPath1)
// rmrf.sync(dbPath2)
// ipfsd1 = await startIpfs(API, config.daemon1)
// ipfsd2 = await startIpfs(API, config.daemon2)
// ipfs1 = ipfsd1.api
// ipfs2 = ipfsd2.api
// Connect the peers manually to speed up test times
const isLocalhostAddress = (addr) => addr.toString().includes('127.0.0.1')
await connectPeers(ipfs1, ipfs2, { filter: isLocalhostAddress })
// // Connect the peers manually to speed up test times
// const isLocalhostAddress = (addr) => addr.toString().includes('127.0.0.1')
// await connectPeers(ipfs1, ipfs2, { filter: isLocalhostAddress })
const keystore1 = new Keystore(dbPath1 + '/keys')
const keystore2 = new Keystore(dbPath2 + '/keys')
IdentityProvider.addIdentityProvider(EthIdentityProvider)
// const keystore1 = new Keystore(dbPath1 + '/keys')
// const keystore2 = new Keystore(dbPath2 + '/keys')
// IdentityProvider.addIdentityProvider(EthIdentityProvider)
id1 = await IdentityProvider.createIdentity({ type: 'ethereum', keystore: keystore1 })
id2 = await IdentityProvider.createIdentity({ type: 'ethereum', keystore: keystore2 })
// id1 = await IdentityProvider.createIdentity({ type: 'ethereum', keystore: keystore1 })
// id2 = await IdentityProvider.createIdentity({ type: 'ethereum', keystore: keystore2 })
web3 = new Web3(ganache.provider())
accounts = await web3.eth.getAccounts()
// web3 = new Web3(ganache.provider())
// accounts = await web3.eth.getAccounts()
accessControllers.forEach(ac => AccessControllers.addAccessController(ac))
// accessControllers.forEach(ac => AccessControllers.addAccessController(ac))
orbitdb1 = await OrbitDB.createInstance(ipfs1, {
AccessControllers: AccessControllers,
directory: dbPath1,
identity: id1
})
// orbitdb1 = await OrbitDB.createInstance(ipfs1, {
// AccessControllers: AccessControllers,
// directory: dbPath1,
// identity: id1
// })
orbitdb2 = await OrbitDB.createInstance(ipfs2, {
AccessControllers: AccessControllers,
directory: dbPath2,
identity: id2
})
})
// orbitdb2 = await OrbitDB.createInstance(ipfs2, {
// AccessControllers: AccessControllers,
// directory: dbPath2,
// identity: id2
// })
// })
after(async () => {
if (orbitdb1) {
await orbitdb1.stop()
}
// after(async () => {
// if (orbitdb1) {
// await orbitdb1.stop()
// }
if (orbitdb2) {
await orbitdb2.stop()
}
// if (orbitdb2) {
// await orbitdb2.stop()
// }
if (ipfsd1) {
await stopIpfs(ipfsd1)
}
// if (ipfsd1) {
// await stopIpfs(ipfsd1)
// }
if (ipfsd2) {
await stopIpfs(ipfsd2)
}
})
// if (ipfsd2) {
// await stopIpfs(ipfsd2)
// }
// })
describe('OrbitDB Integration', function () {
accessControllers.forEach(async (ac, i) => {
let db, db2
let dbManifest, acManifest, access
let contract
// describe('OrbitDB Integration', function () {
// accessControllers.forEach(async (ac, i) => {
// let db, db2
// let dbManifest, acManifest, access
// let contract
before(async () => {
contract = await new web3.eth.Contract(ac.contract.abi)
.deploy({ data: ac.contract.bytecode })
.send({ from: accounts[i], gas: '1000000' })
// before(async () => {
// contract = await new web3.eth.Contract(ac.contract.abi)
// .deploy({ data: ac.contract.bytecode })
// .send({ from: accounts[i], gas: '1000000' })
// DB creator needs to provide ac-type, abi and contract-address
db = await orbitdb1.feed('AABB', {
identity: id1,
accessController: {
type: ac.AccessController.type,
web3: web3,
abi: ac.contract.abi,
contractAddress: contract._address,
defaultAccount: accounts[i]
},
timeout: 1000
})
// // DB creator needs to provide ac-type, abi and contract-address
// db = await orbitdb1.feed('AABB', {
// identity: id1,
// accessController: {
// type: ac.AccessController.type,
// web3: web3,
// abi: ac.contract.abi,
// contractAddress: contract._address,
// defaultAccount: accounts[i]
// },
// timeout: 1000
// })
// DB peer needs to provide web3 instance
db2 = await orbitdb2.feed(db.address, {
identity: id2,
accessController: {
web3: web3,
defaultAccount: accounts[(i + 1) % accessControllers.length] // peer owns different eth-account
},
timeout: 1000
})
// // DB peer needs to provide web3 instance
// db2 = await orbitdb2.feed(db.address, {
// identity: id2,
// accessController: {
// web3: web3,
// defaultAccount: accounts[(i + 1) % accessControllers.length] // peer owns different eth-account
// },
// timeout: 1000
// })
await db2.load()
// await db2.load()
dbManifest = await io.read(ipfs1, db.address.root)
const hash = dbManifest.accessController.split('/').pop()
acManifest = await io.read(ipfs1, hash)
access = await io.read(ipfs1, acManifest.params.address)
})
// dbManifest = await io.read(ipfs1, db.address.root)
// const hash = dbManifest.accessController.split('/').pop()
// acManifest = await io.read(ipfs1, hash)
// access = await io.read(ipfs1, acManifest.params.address)
// })
it('makes database use the correct access controller', async () => {
assert.strictEqual(access.contractAddress, db.access.address)
})
// it('makes database use the correct access controller', async () => {
// assert.strictEqual(access.contractAddress, db.access.address)
// })
it('saves database manifest file locally', async () => {
assert.notStrictEqual(dbManifest, null)
})
// it('saves database manifest file locally', async () => {
// assert.notStrictEqual(dbManifest, null)
// })
it('saves access controller manifest file locally', async () => {
assert.notStrictEqual(acManifest, null)
})
// it('saves access controller manifest file locally', async () => {
// assert.notStrictEqual(acManifest, null)
// })
describe('database manifest', () => {
it('has correct name', async () => {
assert.strictEqual(dbManifest.name, 'AABB')
})
// describe('database manifest', () => {
// it('has correct name', async () => {
// assert.strictEqual(dbManifest.name, 'AABB')
// })
it('has correct type', async () => {
assert.strictEqual(dbManifest.type, 'feed')
})
// it('has correct type', async () => {
// assert.strictEqual(dbManifest.type, 'feed')
// })
it('has correct address', async () => {
assert.notStrictEqual(dbManifest.accessController, null)
assert.strictEqual(dbManifest.accessController.indexOf('/ipfs'), 0)
})
})
// it('has correct address', async () => {
// assert.notStrictEqual(dbManifest.accessController, null)
// assert.strictEqual(dbManifest.accessController.indexOf('/ipfs'), 0)
// })
// })
describe('access controller manifest', () => {
it('has correct type', async () => {
assert.strictEqual(acManifest.type, ac.AccessController.type)
})
// describe('access controller manifest', () => {
// it('has correct type', async () => {
// assert.strictEqual(acManifest.type, ac.AccessController.type)
// })
it('has correct address', async () => {
assert.strictEqual(access.contractAddress.indexOf('0x'), 0)
assert.strictEqual(access.contractAddress, db.access.address)
})
})
// it('has correct address', async () => {
// assert.strictEqual(access.contractAddress.indexOf('0x'), 0)
// assert.strictEqual(access.contractAddress, db.access.address)
// })
// })
describe('access controls', () => {
it('throws error if key not permitted to write', async () => {
let err
try {
await db.add('hello?') // should throw error
assert.strictEqual('Should not end here', false)
} catch (e) {
err = e.toString()
}
assert.strictEqual(err, `Error: Could not append entry, key "${id1.id}" is not allowed to write to the log`)
})
// describe('access controls', () => {
// it('throws error if key not permitted to write', async () => {
// let err
// try {
// await db.add('hello?') // should throw error
// assert.strictEqual('Should not end here', false)
// } catch (e) {
// err = e.toString()
// }
// assert.strictEqual(err, `Error: Could not append entry, key "${id1.id}" is not allowed to write to the log`)
// })
it('granting access enables to write to the database', async () => {
await db.access.grant('write', id1.id)
const doChanges = () => {
return new Promise((resolve, reject) => {
try {
db2.events.once('replicated', () => {
// FIXME: timeout to get rid of the "libp2p node not started yet" errors
setTimeout(() => resolve(), 1000)
})
db.add('hello!')
} catch (e) {
reject(e)
}
})
}
// Try adding something again
await doChanges()
// it('granting access enables to write to the database', async () => {
// await db.access.grant('write', id1.id)
// const doChanges = () => {
// return new Promise((resolve, reject) => {
// try {
// db2.events.once('replicated', () => {
// // FIXME: timeout to get rid of the "libp2p node not started yet" errors
// setTimeout(() => resolve(), 1000)
// })
// db.add('hello!')
// } catch (e) {
// reject(e)
// }
// })
// }
// // Try adding something again
// await doChanges()
const res1 = await db.iterator().collect().map(e => e.payload.value)
const res2 = await db2.iterator().collect().map(e => e.payload.value)
assert.deepStrictEqual(res1, ['hello!'])
assert.deepStrictEqual(res2, ['hello!'])
})
// const res1 = await db.iterator().collect().map(e => e.payload.value)
// const res2 = await db2.iterator().collect().map(e => e.payload.value)
// assert.deepStrictEqual(res1, ['hello!'])
// assert.deepStrictEqual(res2, ['hello!'])
// })
it('can\'t grant access if not admin', async () => {
await db2.access.grant('write', id2.id)
const canAppend = await db2.access.canAppend({ identity: id2 }, id2.provider)
assert.strictEqual(canAppend, false)
})
// it('can\'t grant access if not admin', async () => {
// await db2.access.grant('write', id2.id)
// const canAppend = await db2.access.canAppend({ identity: id2 }, id2.provider)
// assert.strictEqual(canAppend, false)
// })
it('can\'t revoke access if not admin', async () => {
await db2.access.revoke('write', id1.id)
const canAppend = await db2.access.canAppend({ identity: id1 }, id1.provider)
assert.strictEqual(canAppend, true)
})
// it('can\'t revoke access if not admin', async () => {
// await db2.access.revoke('write', id1.id)
// const canAppend = await db2.access.canAppend({ identity: id1 }, id1.provider)
// assert.strictEqual(canAppend, true)
// })
it('can check permissions without defaultAccount set', async () => {
db2.access.defaultAccount = null
const canAppend = await db2.access.canAppend({ identity: id1 }, id1.provider)
assert.strictEqual(canAppend, true)
})
// it('can check permissions without defaultAccount set', async () => {
// db2.access.defaultAccount = null
// const canAppend = await db2.access.canAppend({ identity: id1 }, id1.provider)
// assert.strictEqual(canAppend, true)
// })
it('can\'t change permissions without from address if no defaultAccount set', async () => {
let err
db2.access.defaultAccount = null
try {
await db2.access.grant('write', id2.id)
} catch (e) {
err = e.toString()
}
assert.strictEqual(err, 'Error: No "from" address specified in neither the given options, nor the default options.')
})
// it('can\'t change permissions without from address if no defaultAccount set', async () => {
// let err
// db2.access.defaultAccount = null
// try {
// await db2.access.grant('write', id2.id)
// } catch (e) {
// err = e.toString()
// }
// assert.strictEqual(err, 'Error: No "from" address specified in neither the given options, nor the default options.')
// })
it('can change permissions by passing in from address', async () => {
let err
db2.access.defaultAccount = null
try {
await db2.access.grant('write', id2.id, { from: accounts[i] }) // from address can grant/revoke access
} catch (e) {
err = e.toString()
}
assert.strictEqual(err, undefined)
const canAppend = await db2.access.canAppend({ identity: id2 }, id2.provider)
assert.strictEqual(canAppend, true)
})
// it('can change permissions by passing in from address', async () => {
// let err
// db2.access.defaultAccount = null
// try {
// await db2.access.grant('write', id2.id, { from: accounts[i] }) // from address can grant/revoke access
// } catch (e) {
// err = e.toString()
// }
// assert.strictEqual(err, undefined)
// const canAppend = await db2.access.canAppend({ identity: id2 }, id2.provider)
// assert.strictEqual(canAppend, true)
// })
it('revoking access disables ability to write to the database', async () => {
let err
try {
// Revoke user's access
await db.access.revoke('write', id2.id)
await db2.add('hello?')
} catch (e) {
err = e.toString()
}
assert.strictEqual(err, `Error: Could not append entry, key "${id2.id}" is not allowed to write to the log`)
})
})
})
})
})
})
// it('revoking access disables ability to write to the database', async () => {
// let err
// try {
// // Revoke user's access
// await db.access.revoke('write', id2.id)
// await db2.add('hello?')
// } catch (e) {
// err = e.toString()
// }
// assert.strictEqual(err, `Error: Could not append entry, key "${id2.id}" is not allowed to write to the log`)
// })
// })
// })
// })
// })
// })

View File

@ -1,183 +1,183 @@
import assert from 'assert'
import rmrf from 'rimraf'
import OrbitDB from '../../src/OrbitDB.js'
import IdentityProvider from 'orbit-db-identity-provider'
import EthIdentityProvider from 'orbit-db-identity-provider/ethereum'
import Keystore from 'orbit-db-keystore'
import ContractAccessController from 'orbit-db-access-controllers/contract'
import DepositContractAccessController from 'orbit-db-access-controllers/deposit-contract'
import AccessControllers from 'orbit-db-access-controllers'
import Web3 from 'web3'
import ganache from 'ganache-cli'
import * as io from 'orbit-db-io'
import Access from './Access.json' assert { 'type': 'json' }
import PayDeposit from './PayDeposit.json' assert { 'type': 'json' }
// Include test utilities
import {
config,
startIpfs,
stopIpfs,
testAPIs
} from 'orbit-db-test-utils'
// import assert from 'assert'
// import rmrf from 'rimraf'
// import OrbitDB from '../../src/OrbitDB.js'
// import IdentityProvider from 'orbit-db-identity-provider'
// import EthIdentityProvider from 'orbit-db-identity-provider/ethereum'
// import Keystore from 'orbit-db-keystore'
// import ContractAccessController from 'orbit-db-access-controllers/contract'
// import DepositContractAccessController from 'orbit-db-access-controllers/deposit-contract'
// import AccessControllers from 'orbit-db-access-controllers'
// import Web3 from 'web3'
// import ganache from 'ganache-cli'
// import * as io from 'orbit-db-io'
// import Access from './Access.json' assert { 'type': 'json' }
// import PayDeposit from './PayDeposit.json' assert { 'type': 'json' }
// // Include test utilities
// import {
// config,
// startIpfs,
// stopIpfs,
// testAPIs
// } from 'orbit-db-test-utils'
const dbPath1 = './orbitdb/tests/contract-access-controller/1'
const dbPath2 = './orbitdb/tests/contract-access-controller/2'
// const dbPath1 = './orbitdb/tests/contract-access-controller/1'
// const dbPath2 = './orbitdb/tests/contract-access-controller/2'
const accessControllers = [
{
AccessController: ContractAccessController,
contract: Access
},
{
AccessController: DepositContractAccessController,
contract: PayDeposit
}
]
// const accessControllers = [
// {
// AccessController: ContractAccessController,
// contract: Access
// },
// {
// AccessController: DepositContractAccessController,
// contract: PayDeposit
// }
// ]
Object.keys(testAPIs).forEach(API => {
describe(`orbit-db - ContractAccessController (${API})`, function () {
this.timeout(config.timeout)
// Object.keys(testAPIs).forEach(API => {
// describe(`orbit-db - ContractAccessController (${API})`, function () {
// this.timeout(config.timeout)
let ipfsd1, ipfsd2, ipfs1, ipfs2, id1, id2
let orbitdb1, orbitdb2
let web3, accounts
// let ipfsd1, ipfsd2, ipfs1, ipfs2, id1, id2
// let orbitdb1, orbitdb2
// let web3, accounts
before(async () => {
rmrf.sync(dbPath1)
rmrf.sync(dbPath2)
ipfsd1 = await startIpfs(API, config.daemon1)
ipfsd2 = await startIpfs(API, config.daemon2)
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api
// before(async () => {
// rmrf.sync(dbPath1)
// rmrf.sync(dbPath2)
// ipfsd1 = await startIpfs(API, config.daemon1)
// ipfsd2 = await startIpfs(API, config.daemon2)
// ipfs1 = ipfsd1.api
// ipfs2 = ipfsd2.api
const keystore1 = new Keystore(dbPath1 + '/keys')
const keystore2 = new Keystore(dbPath2 + '/keys')
// const keystore1 = new Keystore(dbPath1 + '/keys')
// const keystore2 = new Keystore(dbPath2 + '/keys')
IdentityProvider.addIdentityProvider(EthIdentityProvider)
// IdentityProvider.addIdentityProvider(EthIdentityProvider)
id1 = await IdentityProvider.createIdentity({ type: EthIdentityProvider.type, keystore: keystore1 })
id2 = await IdentityProvider.createIdentity({ type: EthIdentityProvider.type, keystore: keystore2 })
// id1 = await IdentityProvider.createIdentity({ type: EthIdentityProvider.type, keystore: keystore1 })
// id2 = await IdentityProvider.createIdentity({ type: EthIdentityProvider.type, keystore: keystore2 })
web3 = new Web3(ganache.provider())
accounts = await web3.eth.getAccounts()
// web3 = new Web3(ganache.provider())
// accounts = await web3.eth.getAccounts()
orbitdb1 = await OrbitDB.createInstance(ipfs1, {
AccessControllers: AccessControllers,
directory: dbPath1,
identity: id1
})
// orbitdb1 = await OrbitDB.createInstance(ipfs1, {
// AccessControllers: AccessControllers,
// directory: dbPath1,
// identity: id1
// })
orbitdb2 = await OrbitDB.createInstance(ipfs2, {
AccessControllers: AccessControllers,
directory: dbPath2,
identity: id2
})
})
// orbitdb2 = await OrbitDB.createInstance(ipfs2, {
// AccessControllers: AccessControllers,
// directory: dbPath2,
// identity: id2
// })
// })
after(async () => {
if (orbitdb1) {
await orbitdb1.stop()
}
// after(async () => {
// if (orbitdb1) {
// await orbitdb1.stop()
// }
if (orbitdb2) {
await orbitdb2.stop()
}
// if (orbitdb2) {
// await orbitdb2.stop()
// }
if (ipfsd1) {
await stopIpfs(ipfsd1)
}
// if (ipfsd1) {
// await stopIpfs(ipfsd1)
// }
if (ipfsd2) {
await stopIpfs(ipfsd2)
}
})
// if (ipfsd2) {
// await stopIpfs(ipfsd2)
// }
// })
describe('Constructor', function () {
accessControllers.forEach(async (ac, i) => {
let accessController, contract
before(async () => {
contract = await new web3.eth.Contract(ac.contract.abi)
.deploy({ data: ac.contract.bytecode })
.send({ from: accounts[i], gas: '1000000' })
// describe('Constructor', function () {
// accessControllers.forEach(async (ac, i) => {
// let accessController, contract
// before(async () => {
// contract = await new web3.eth.Contract(ac.contract.abi)
// .deploy({ data: ac.contract.bytecode })
// .send({ from: accounts[i], gas: '1000000' })
accessController = await ac.AccessController.create(orbitdb1, {
type: ac.AccessController.type,
web3: web3,
abi: ac.contract.abi,
contractAddress: contract._address,
defaultAccount: accounts[i]
})
await accessController.load()
})
// accessController = await ac.AccessController.create(orbitdb1, {
// type: ac.AccessController.type,
// web3: web3,
// abi: ac.contract.abi,
// contractAddress: contract._address,
// defaultAccount: accounts[i]
// })
// await accessController.load()
// })
it('creates an access controller', () => {
assert.notStrictEqual(accessController, null)
assert.notStrictEqual(accessController, undefined)
})
// it('creates an access controller', () => {
// assert.notStrictEqual(accessController, null)
// assert.notStrictEqual(accessController, undefined)
// })
it('sets the controller type', () => {
assert.strictEqual(accessController.type, ac.AccessController.type)
})
// it('sets the controller type', () => {
// assert.strictEqual(accessController.type, ac.AccessController.type)
// })
it('grants access to key', async () => {
const mockEntry = {
identity: id1
// ...
// doesn't matter what we put here, only identity is used for the check
}
await accessController.grant('write', id1.id)
const canAppend = await accessController.canAppend(mockEntry, id1.provider)
assert.strictEqual(canAppend, true)
})
// it('grants access to key', async () => {
// const mockEntry = {
// identity: id1
// // ...
// // doesn't matter what we put here, only identity is used for the check
// }
// await accessController.grant('write', id1.id)
// const canAppend = await accessController.canAppend(mockEntry, id1.provider)
// assert.strictEqual(canAppend, true)
// })
it('grants access to multiple keys', async () => {
const canAppend1 = await accessController.canAppend({ identity: orbitdb1.identity }, orbitdb1.identity.provider)
const canAppend2 = await accessController.canAppend({ identity: orbitdb2.identity }, orbitdb2.identity.provider)
// it('grants access to multiple keys', async () => {
// const canAppend1 = await accessController.canAppend({ identity: orbitdb1.identity }, orbitdb1.identity.provider)
// const canAppend2 = await accessController.canAppend({ identity: orbitdb2.identity }, orbitdb2.identity.provider)
await accessController.grant('write', orbitdb2.identity.id)
const canAppend3 = await accessController.canAppend({ identity: orbitdb2.identity }, orbitdb2.identity.provider)
// await accessController.grant('write', orbitdb2.identity.id)
// const canAppend3 = await accessController.canAppend({ identity: orbitdb2.identity }, orbitdb2.identity.provider)
assert.strictEqual(canAppend1, true)
assert.strictEqual(canAppend2, false)
assert.strictEqual(canAppend3, true)
})
// assert.strictEqual(canAppend1, true)
// assert.strictEqual(canAppend2, false)
// assert.strictEqual(canAppend3, true)
// })
describe('save and load', function () {
let accessController, manifest
// describe('save and load', function () {
// let accessController, manifest
before(async () => {
accessController = await ac.AccessController.create(orbitdb1, {
type: ac.AccessController.type,
web3: web3,
abi: ac.contract.abi,
contractAddress: contract._address,
defaultAccount: accounts[i]
})
manifest = await accessController.save()
const access = await io.read(ipfs1, manifest.address)
// before(async () => {
// accessController = await ac.AccessController.create(orbitdb1, {
// type: ac.AccessController.type,
// web3: web3,
// abi: ac.contract.abi,
// contractAddress: contract._address,
// defaultAccount: accounts[i]
// })
// manifest = await accessController.save()
// const access = await io.read(ipfs1, manifest.address)
accessController = await ac.AccessController.create(orbitdb1, {
type: ac.AccessController.type,
web3: web3,
abi: JSON.parse(access.abi),
contractAddress: access.contractAddress,
defaultAccount: accounts[i]
})
// accessController = await ac.AccessController.create(orbitdb1, {
// type: ac.AccessController.type,
// web3: web3,
// abi: JSON.parse(access.abi),
// contractAddress: access.contractAddress,
// defaultAccount: accounts[i]
// })
await accessController.load(manifest.address)
})
// await accessController.load(manifest.address)
// })
it('has correct capabalities', async () => {
const canAppend1 = await accessController.canAppend({ identity: orbitdb1.identity }, orbitdb1.identity.provider)
const canAppend2 = await accessController.canAppend({ identity: orbitdb2.identity }, orbitdb2.identity.provider)
const canAppend3 = await accessController.canAppend({ identity: { id: 'someotherid' } }, orbitdb1.identity.provider)
// it('has correct capabalities', async () => {
// const canAppend1 = await accessController.canAppend({ identity: orbitdb1.identity }, orbitdb1.identity.provider)
// const canAppend2 = await accessController.canAppend({ identity: orbitdb2.identity }, orbitdb2.identity.provider)
// const canAppend3 = await accessController.canAppend({ identity: { id: 'someotherid' } }, orbitdb1.identity.provider)
assert.strictEqual(canAppend1, true)
assert.strictEqual(canAppend2, true)
assert.strictEqual(canAppend3, false)
})
})
})
})
})
})
// assert.strictEqual(canAppend1, true)
// assert.strictEqual(canAppend2, true)
// assert.strictEqual(canAppend3, false)
// })
// })
// })
// })
// })
// })

View File

@ -1,159 +1,159 @@
import assert from 'assert'
import rmrf from 'rimraf'
import OrbitDB from '../../src/OrbitDB.js'
import IdentityProvider from 'orbit-db-identity-provider'
import Keystore from 'orbit-db-keystore'
import AccessControllers from 'orbit-db-access-controllers'
import * as io from 'orbit-db-io'
// Include test utilities
import {
config,
startIpfs,
stopIpfs,
testAPIs,
connectPeers
} from 'orbit-db-test-utils'
// import assert from 'assert'
// import rmrf from 'rimraf'
// import OrbitDB from '../../src/OrbitDB.js'
// import IdentityProvider from 'orbit-db-identity-provider'
// import Keystore from 'orbit-db-keystore'
// import AccessControllers from 'orbit-db-access-controllers'
// import * as io from 'orbit-db-io'
// // Include test utilities
// import {
// config,
// startIpfs,
// stopIpfs,
// testAPIs,
// connectPeers
// } from 'orbit-db-test-utils'
const dbPath1 = './orbitdb/tests/orbitdb-access-controller-integration/1'
const dbPath2 = './orbitdb/tests/orbitdb-access-controller-integration/2'
// const dbPath1 = './orbitdb/tests/orbitdb-access-controller-integration/1'
// const dbPath2 = './orbitdb/tests/orbitdb-access-controller-integration/2'
Object.keys(testAPIs).forEach(API => {
describe(`orbit-db - IPFSAccessController Integration (${API})`, function () {
this.timeout(config.timeout)
// Object.keys(testAPIs).forEach(API => {
// describe(`orbit-db - IPFSAccessController Integration (${API})`, function () {
// this.timeout(config.timeout)
let ipfsd1, ipfsd2, ipfs1, ipfs2, id1, id2
let orbitdb1, orbitdb2
// let ipfsd1, ipfsd2, ipfs1, ipfs2, id1, id2
// let orbitdb1, orbitdb2
before(async () => {
rmrf.sync(dbPath1)
rmrf.sync(dbPath2)
ipfsd1 = await startIpfs(API, config.daemon1)
ipfsd2 = await startIpfs(API, config.daemon2)
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api
// before(async () => {
// rmrf.sync(dbPath1)
// rmrf.sync(dbPath2)
// ipfsd1 = await startIpfs(API, config.daemon1)
// ipfsd2 = await startIpfs(API, config.daemon2)
// ipfs1 = ipfsd1.api
// ipfs2 = ipfsd2.api
// Connect the peers manually to speed up test times
const isLocalhostAddress = (addr) => addr.toString().includes('127.0.0.1')
await connectPeers(ipfs1, ipfs2, { filter: isLocalhostAddress })
// // Connect the peers manually to speed up test times
// const isLocalhostAddress = (addr) => addr.toString().includes('127.0.0.1')
// await connectPeers(ipfs1, ipfs2, { filter: isLocalhostAddress })
const keystore1 = new Keystore(dbPath1 + '/keys')
const keystore2 = new Keystore(dbPath2 + '/keys')
// const keystore1 = new Keystore(dbPath1 + '/keys')
// const keystore2 = new Keystore(dbPath2 + '/keys')
id1 = await IdentityProvider.createIdentity({ id: 'A', keystore: keystore1 })
id2 = await IdentityProvider.createIdentity({ id: 'B', keystore: keystore2 })
// id1 = await IdentityProvider.createIdentity({ id: 'A', keystore: keystore1 })
// id2 = await IdentityProvider.createIdentity({ id: 'B', keystore: keystore2 })
orbitdb1 = await OrbitDB.createInstance(ipfs1, {
AccessControllers,
directory: dbPath1,
identity: id1
})
// orbitdb1 = await OrbitDB.createInstance(ipfs1, {
// AccessControllers,
// directory: dbPath1,
// identity: id1
// })
orbitdb2 = await OrbitDB.createInstance(ipfs2, {
AccessControllers,
directory: dbPath2,
identity: id2
})
})
// orbitdb2 = await OrbitDB.createInstance(ipfs2, {
// AccessControllers,
// directory: dbPath2,
// identity: id2
// })
// })
after(async () => {
if (orbitdb1) { await orbitdb1.stop() }
// after(async () => {
// if (orbitdb1) { await orbitdb1.stop() }
if (orbitdb2) { await orbitdb2.stop() }
// if (orbitdb2) { await orbitdb2.stop() }
if (ipfsd1) { await stopIpfs(ipfsd1) }
// if (ipfsd1) { await stopIpfs(ipfsd1) }
if (ipfsd2) { await stopIpfs(ipfsd2) }
})
// if (ipfsd2) { await stopIpfs(ipfsd2) }
// })
describe('OrbitDB Integration', function () {
let db, db2
let dbManifest, acManifest
// describe('OrbitDB Integration', function () {
// let db, db2
// let dbManifest, acManifest
before(async () => {
db = await orbitdb1.feed('AABB', {
identity: id1,
accessController: {
type: 'ipfs',
write: [id1.id]
}
})
// before(async () => {
// db = await orbitdb1.feed('AABB', {
// identity: id1,
// accessController: {
// type: 'ipfs',
// write: [id1.id]
// }
// })
db2 = await orbitdb2.feed(db.address, {
identity: id2
})
await db2.load()
// db2 = await orbitdb2.feed(db.address, {
// identity: id2
// })
// await db2.load()
dbManifest = await io.read(ipfs1, db.address.root)
const hash = dbManifest.accessController.split('/').pop()
acManifest = await io.read(ipfs1, hash)
})
// dbManifest = await io.read(ipfs1, db.address.root)
// const hash = dbManifest.accessController.split('/').pop()
// acManifest = await io.read(ipfs1, hash)
// })
it('has the correct access rights after creating the database', async () => {
assert.deepStrictEqual(db.access.write, [id1.id])
})
// it('has the correct access rights after creating the database', async () => {
// assert.deepStrictEqual(db.access.write, [id1.id])
// })
it('makes database use the correct access controller', async () => {
const { address } = await db.access.save()
assert.strictEqual(acManifest.params.address, address)
})
// it('makes database use the correct access controller', async () => {
// const { address } = await db.access.save()
// assert.strictEqual(acManifest.params.address, address)
// })
it('saves database manifest file locally', async () => {
assert.notStrictEqual(dbManifest, null)
})
// it('saves database manifest file locally', async () => {
// assert.notStrictEqual(dbManifest, null)
// })
it('saves access controller manifest file locally', async () => {
assert.notStrictEqual(acManifest, null)
})
// it('saves access controller manifest file locally', async () => {
// assert.notStrictEqual(acManifest, null)
// })
it('has correct type', async () => {
assert.strictEqual(acManifest.type, 'ipfs')
})
// it('has correct type', async () => {
// assert.strictEqual(acManifest.type, 'ipfs')
// })
describe('database manifest', () => {
it('has correct name', async () => {
assert.strictEqual(dbManifest.name, 'AABB')
})
// describe('database manifest', () => {
// it('has correct name', async () => {
// assert.strictEqual(dbManifest.name, 'AABB')
// })
it('has correct type', async () => {
assert.strictEqual(dbManifest.type, 'feed')
})
// it('has correct type', async () => {
// assert.strictEqual(dbManifest.type, 'feed')
// })
it('has correct address', async () => {
assert.notStrictEqual(dbManifest.accessController, null)
assert.strictEqual(dbManifest.accessController.indexOf('/ipfs'), 0)
})
})
// it('has correct address', async () => {
// assert.notStrictEqual(dbManifest.accessController, null)
// assert.strictEqual(dbManifest.accessController.indexOf('/ipfs'), 0)
// })
// })
describe('access controls', () => {
it('allows to write if user has write access', async () => {
let err
try {
await db.add('hello?')
} catch (e) {
err = e.toString()
}
// describe('access controls', () => {
// it('allows to write if user has write access', async () => {
// let err
// try {
// await db.add('hello?')
// } catch (e) {
// err = e.toString()
// }
const res = await db.iterator().collect().map(e => e.payload.value)
assert.strictEqual(err, undefined)
assert.deepStrictEqual(res, ['hello?'])
})
// const res = await db.iterator().collect().map(e => e.payload.value)
// assert.strictEqual(err, undefined)
// assert.deepStrictEqual(res, ['hello?'])
// })
it('doesn\'t allow to write without write access', async () => {
let err
try {
await db2.add('hello!!')
assert.strictEqual('Should not end here', false)
} catch (e) {
err = e
}
// it('doesn\'t allow to write without write access', async () => {
// let err
// try {
// await db2.add('hello!!')
// assert.strictEqual('Should not end here', false)
// } catch (e) {
// err = e
// }
const res = await db2.iterator().collect().map(e => e.payload.value)
assert.strictEqual(err.message, `Could not append entry, key "${db2.identity.id}" is not allowed to write to the log`)
assert.deepStrictEqual(res.includes(e => e === 'hello!!'), false)
})
})
})
})
// TODO: use two separate peers for testing the AC
// TODO: add tests for revocation correctness with a database (integration tests)
})
// const res = await db2.iterator().collect().map(e => e.payload.value)
// assert.strictEqual(err.message, `Could not append entry, key "${db2.identity.id}" is not allowed to write to the log`)
// assert.deepStrictEqual(res.includes(e => e === 'hello!!'), false)
// })
// })
// })
// })
// // TODO: use two separate peers for testing the AC
// // TODO: add tests for revocation correctness with a database (integration tests)
// })

View File

@ -1,128 +1,128 @@
import assert from 'assert'
import rmrf from 'rimraf'
import OrbitDB from '../../src/OrbitDB.js'
import IdentityProvider from 'orbit-db-identity-provider'
import Keystore from 'orbit-db-keystore'
import IPFSAccessController from 'orbit-db-access-controllers/ipfs'
import AccessControllers from 'orbit-db-access-controllers'
// import assert from 'assert'
// import rmrf from 'rimraf'
// import OrbitDB from '../../src/OrbitDB.js'
// import IdentityProvider from 'orbit-db-identity-provider'
// import Keystore from 'orbit-db-keystore'
// import IPFSAccessController from 'orbit-db-access-controllers/ipfs'
// import AccessControllers from 'orbit-db-access-controllers'
// Include test utilities
import {
config,
startIpfs,
stopIpfs,
testAPIs
} from 'orbit-db-test-utils'
// // Include test utilities
// import {
// config,
// startIpfs,
// stopIpfs,
// testAPIs
// } from 'orbit-db-test-utils'
const dbPath1 = './orbitdb/tests/ipfs-access-controller/1'
const dbPath2 = './orbitdb/tests/ipfs-access-controller/2'
// const dbPath1 = './orbitdb/tests/ipfs-access-controller/1'
// const dbPath2 = './orbitdb/tests/ipfs-access-controller/2'
Object.keys(testAPIs).forEach(API => {
describe(`orbit-db - IPFSAccessController (${API})`, function () {
this.timeout(config.timeout)
// Object.keys(testAPIs).forEach(API => {
// describe(`orbit-db - IPFSAccessController (${API})`, function () {
// this.timeout(config.timeout)
let ipfsd1, ipfsd2, ipfs1, ipfs2, id1, id2
let orbitdb1, orbitdb2
// let ipfsd1, ipfsd2, ipfs1, ipfs2, id1, id2
// let orbitdb1, orbitdb2
before(async () => {
rmrf.sync(dbPath1)
rmrf.sync(dbPath2)
ipfsd1 = await startIpfs(API, config.daemon1)
ipfsd2 = await startIpfs(API, config.daemon2)
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api
// before(async () => {
// rmrf.sync(dbPath1)
// rmrf.sync(dbPath2)
// ipfsd1 = await startIpfs(API, config.daemon1)
// ipfsd2 = await startIpfs(API, config.daemon2)
// ipfs1 = ipfsd1.api
// ipfs2 = ipfsd2.api
const keystore1 = new Keystore(dbPath1 + '/keys')
const keystore2 = new Keystore(dbPath2 + '/keys')
// const keystore1 = new Keystore(dbPath1 + '/keys')
// const keystore2 = new Keystore(dbPath2 + '/keys')
id1 = await IdentityProvider.createIdentity({ id: 'A', keystore: keystore1 })
id2 = await IdentityProvider.createIdentity({ id: 'B', keystore: keystore2 })
// id1 = await IdentityProvider.createIdentity({ id: 'A', keystore: keystore1 })
// id2 = await IdentityProvider.createIdentity({ id: 'B', keystore: keystore2 })
orbitdb1 = await OrbitDB.createInstance(ipfs1, {
AccessControllers,
directory: dbPath1,
identity: id1
})
// orbitdb1 = await OrbitDB.createInstance(ipfs1, {
// AccessControllers,
// directory: dbPath1,
// identity: id1
// })
orbitdb2 = await OrbitDB.createInstance(ipfs2, {
AccessControllers,
directory: dbPath2,
identity: id2
})
})
// orbitdb2 = await OrbitDB.createInstance(ipfs2, {
// AccessControllers,
// directory: dbPath2,
// identity: id2
// })
// })
after(async () => {
if (orbitdb1) {
await orbitdb1.stop()
}
// after(async () => {
// if (orbitdb1) {
// await orbitdb1.stop()
// }
if (orbitdb2) {
await orbitdb2.stop()
}
// if (orbitdb2) {
// await orbitdb2.stop()
// }
if (ipfsd1) {
await stopIpfs(ipfsd1)
}
// if (ipfsd1) {
// await stopIpfs(ipfsd1)
// }
if (ipfsd2) {
await stopIpfs(ipfsd2)
}
})
// if (ipfsd2) {
// await stopIpfs(ipfsd2)
// }
// })
describe('Constructor', function () {
let accessController
// describe('Constructor', function () {
// let accessController
before(async () => {
accessController = await IPFSAccessController.create(orbitdb1, {
write: [id1.id]
})
})
// before(async () => {
// accessController = await IPFSAccessController.create(orbitdb1, {
// write: [id1.id]
// })
// })
it('creates an access controller', () => {
assert.notStrictEqual(accessController, null)
assert.notStrictEqual(accessController, undefined)
})
// it('creates an access controller', () => {
// assert.notStrictEqual(accessController, null)
// assert.notStrictEqual(accessController, undefined)
// })
it('sets the controller type', () => {
assert.strictEqual(accessController.type, 'ipfs')
})
// it('sets the controller type', () => {
// assert.strictEqual(accessController.type, 'ipfs')
// })
it('has IPFS instance', async () => {
const peerId1 = await accessController._ipfs.id()
const peerId2 = await ipfs1.id()
assert.strictEqual(String(peerId1.id), String(peerId2.id))
})
// it('has IPFS instance', async () => {
// const peerId1 = await accessController._ipfs.id()
// const peerId2 = await ipfs1.id()
// assert.strictEqual(String(peerId1.id), String(peerId2.id))
// })
it('sets default capabilities', async () => {
assert.deepStrictEqual(accessController.write, [id1.id])
})
// it('sets default capabilities', async () => {
// assert.deepStrictEqual(accessController.write, [id1.id])
// })
it('allows owner to append after creation', async () => {
const mockEntry = {
identity: id1,
v: 1
// ...
// doesn't matter what we put here, only identity is used for the check
}
const canAppend = await accessController.canAppend(mockEntry, id1.provider)
assert.strictEqual(canAppend, true)
})
})
// it('allows owner to append after creation', async () => {
// const mockEntry = {
// identity: id1,
// v: 1
// // ...
// // doesn't matter what we put here, only identity is used for the check
// }
// const canAppend = await accessController.canAppend(mockEntry, id1.provider)
// assert.strictEqual(canAppend, true)
// })
// })
describe('save and load', function () {
let accessController, manifest
// describe('save and load', function () {
// let accessController, manifest
before(async () => {
accessController = await IPFSAccessController.create(orbitdb1, {
write: ['A', 'B', id1.id]
})
manifest = await accessController.save()
await accessController.load(manifest.address)
})
// before(async () => {
// accessController = await IPFSAccessController.create(orbitdb1, {
// write: ['A', 'B', id1.id]
// })
// manifest = await accessController.save()
// await accessController.load(manifest.address)
// })
it('has correct capabalities', async () => {
assert.deepStrictEqual(accessController.write, ['A', 'B', id1.id])
})
})
})
})
// it('has correct capabalities', async () => {
// assert.deepStrictEqual(accessController.write, ['A', 'B', id1.id])
// })
// })
// })
// })

View File

@ -1,231 +1,231 @@
import assert from 'assert'
import rmrf from 'rimraf'
import OrbitDB from '../../src/OrbitDB.js'
import IdentityProvider from 'orbit-db-identity-provider'
import Keystore from 'orbit-db-keystore'
import AccessControllers from 'orbit-db-access-controllers'
import * as io from 'orbit-db-io'
// Include test utilities
import {
config,
startIpfs,
stopIpfs,
testAPIs,
connectPeers
} from 'orbit-db-test-utils'
// import assert from 'assert'
// import rmrf from 'rimraf'
// import OrbitDB from '../../src/OrbitDB.js'
// import IdentityProvider from 'orbit-db-identity-provider'
// import Keystore from 'orbit-db-keystore'
// import AccessControllers from 'orbit-db-access-controllers'
// import * as io from 'orbit-db-io'
// // Include test utilities
// import {
// config,
// startIpfs,
// stopIpfs,
// testAPIs,
// connectPeers
// } from 'orbit-db-test-utils'
const dbPath1 = './orbitdb/tests/orbitdb-access-controller-integration/1'
const dbPath2 = './orbitdb/tests/orbitdb-access-controller-integration/2'
// const dbPath1 = './orbitdb/tests/orbitdb-access-controller-integration/1'
// const dbPath2 = './orbitdb/tests/orbitdb-access-controller-integration/2'
Object.keys(testAPIs).forEach(API => {
describe(`orbit-db - OrbitDBAccessController Integration (${API})`, function () {
this.timeout(config.timeout)
// Object.keys(testAPIs).forEach(API => {
// describe(`orbit-db - OrbitDBAccessController Integration (${API})`, function () {
// this.timeout(config.timeout)
let ipfsd1, ipfsd2, ipfs1, ipfs2, id1, id2
let orbitdb1, orbitdb2
// let ipfsd1, ipfsd2, ipfs1, ipfs2, id1, id2
// let orbitdb1, orbitdb2
before(async () => {
rmrf.sync(dbPath1)
rmrf.sync(dbPath2)
ipfsd1 = await startIpfs(API, config.daemon1)
ipfsd2 = await startIpfs(API, config.daemon2)
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api
// before(async () => {
// rmrf.sync(dbPath1)
// rmrf.sync(dbPath2)
// ipfsd1 = await startIpfs(API, config.daemon1)
// ipfsd2 = await startIpfs(API, config.daemon2)
// ipfs1 = ipfsd1.api
// ipfs2 = ipfsd2.api
// Connect the peers manually to speed up test times
const isLocalhostAddress = (addr) => addr.toString().includes('127.0.0.1')
await connectPeers(ipfs1, ipfs2, { filter: isLocalhostAddress })
// // Connect the peers manually to speed up test times
// const isLocalhostAddress = (addr) => addr.toString().includes('127.0.0.1')
// await connectPeers(ipfs1, ipfs2, { filter: isLocalhostAddress })
const keystore1 = new Keystore(dbPath1 + '/keys')
const keystore2 = new Keystore(dbPath2 + '/keys')
// const keystore1 = new Keystore(dbPath1 + '/keys')
// const keystore2 = new Keystore(dbPath2 + '/keys')
id1 = await IdentityProvider.createIdentity({ id: 'A', keystore: keystore1 })
id2 = await IdentityProvider.createIdentity({ id: 'B', keystore: keystore2 })
// id1 = await IdentityProvider.createIdentity({ id: 'A', keystore: keystore1 })
// id2 = await IdentityProvider.createIdentity({ id: 'B', keystore: keystore2 })
orbitdb1 = await OrbitDB.createInstance(ipfs1, {
AccessControllers,
directory: dbPath1,
identity: id1
})
// orbitdb1 = await OrbitDB.createInstance(ipfs1, {
// AccessControllers,
// directory: dbPath1,
// identity: id1
// })
orbitdb2 = await OrbitDB.createInstance(ipfs2, {
AccessControllers,
directory: dbPath2,
identity: id2
})
})
// orbitdb2 = await OrbitDB.createInstance(ipfs2, {
// AccessControllers,
// directory: dbPath2,
// identity: id2
// })
// })
after(async () => {
if (orbitdb1) {
await orbitdb1.stop()
}
// after(async () => {
// if (orbitdb1) {
// await orbitdb1.stop()
// }
if (orbitdb2) {
await orbitdb2.stop()
}
// if (orbitdb2) {
// await orbitdb2.stop()
// }
if (ipfsd1) {
await stopIpfs(ipfsd1)
}
// if (ipfsd1) {
// await stopIpfs(ipfsd1)
// }
if (ipfsd2) {
await stopIpfs(ipfsd2)
}
})
// if (ipfsd2) {
// await stopIpfs(ipfsd2)
// }
// })
describe('OrbitDB Integration', function () {
let db, db2
let dbManifest, acManifest
// describe('OrbitDB Integration', function () {
// let db, db2
// let dbManifest, acManifest
before(async () => {
db = await orbitdb1.feed('AABB', {
identity: id1,
accessController: {
type: 'orbitdb',
write: [id1.id]
}
})
// before(async () => {
// db = await orbitdb1.feed('AABB', {
// identity: id1,
// accessController: {
// type: 'orbitdb',
// write: [id1.id]
// }
// })
db2 = await orbitdb2.feed(db.address, { identity: id2 })
await db2.load()
// db2 = await orbitdb2.feed(db.address, { identity: id2 })
// await db2.load()
dbManifest = await io.read(ipfs1, db.address.root)
const hash = dbManifest.accessController.split('/').pop()
acManifest = await io.read(ipfs1, hash)
})
// dbManifest = await io.read(ipfs1, db.address.root)
// const hash = dbManifest.accessController.split('/').pop()
// acManifest = await io.read(ipfs1, hash)
// })
it('has the correct access rights after creating the database', async () => {
assert.deepStrictEqual(db.access.capabilities, {
admin: new Set([id1.id]),
write: new Set([id1.id])
})
})
// it('has the correct access rights after creating the database', async () => {
// assert.deepStrictEqual(db.access.capabilities, {
// admin: new Set([id1.id]),
// write: new Set([id1.id])
// })
// })
it('makes database use the correct access controller', async () => {
assert.strictEqual(acManifest.params.address, db.access._db.address.toString())
})
// it('makes database use the correct access controller', async () => {
// assert.strictEqual(acManifest.params.address, db.access._db.address.toString())
// })
it('saves database manifest file locally', async () => {
assert.notStrictEqual(dbManifest, null)
})
// it('saves database manifest file locally', async () => {
// assert.notStrictEqual(dbManifest, null)
// })
it('saves access controller manifest file locally', async () => {
assert.notStrictEqual(acManifest, null)
})
// it('saves access controller manifest file locally', async () => {
// assert.notStrictEqual(acManifest, null)
// })
describe('database manifest', () => {
it('has correct name', async () => {
assert.strictEqual(dbManifest.name, 'AABB')
})
// describe('database manifest', () => {
// it('has correct name', async () => {
// assert.strictEqual(dbManifest.name, 'AABB')
// })
it('has correct type', async () => {
assert.strictEqual(dbManifest.type, 'feed')
})
// it('has correct type', async () => {
// assert.strictEqual(dbManifest.type, 'feed')
// })
it('has correct address', async () => {
assert.notStrictEqual(dbManifest.accessController, null)
assert.strictEqual(dbManifest.accessController.indexOf('/ipfs'), 0)
})
})
// it('has correct address', async () => {
// assert.notStrictEqual(dbManifest.accessController, null)
// assert.strictEqual(dbManifest.accessController.indexOf('/ipfs'), 0)
// })
// })
describe('access controller manifest', () => {
it('has correct type', async () => {
assert.strictEqual(acManifest.type, 'orbitdb')
})
// describe('access controller manifest', () => {
// it('has correct type', async () => {
// assert.strictEqual(acManifest.type, 'orbitdb')
// })
it('has correct address', async () => {
assert.strictEqual(acManifest.params.address.indexOf('/orbitdb'), 0)
assert.strictEqual(acManifest.params.address.split('/').pop(), '_access')
})
})
// it('has correct address', async () => {
// assert.strictEqual(acManifest.params.address.indexOf('/orbitdb'), 0)
// assert.strictEqual(acManifest.params.address.split('/').pop(), '_access')
// })
// })
describe('access controls', () => {
it('granting access enables to write to the database', async () => {
let err
try {
await db2.add('hello?')
assert.strictEqual('Should not end here', false)
} catch (e) {
err = e.toString()
}
// describe('access controls', () => {
// it('granting access enables to write to the database', async () => {
// let err
// try {
// await db2.add('hello?')
// assert.strictEqual('Should not end here', false)
// } catch (e) {
// err = e.toString()
// }
assert.strictEqual(err, `Error: Could not append entry, key "${db2.identity.id}" is not allowed to write to the log`)
// assert.strictEqual(err, `Error: Could not append entry, key "${db2.identity.id}" is not allowed to write to the log`)
const doChanges = () => {
return new Promise((resolve, reject) => {
try {
// Wait for the second user's AC to notify it was updated
db2.access.once('updated', async () => {
// Wait for the first user's db to replicate the update
db.events.once('replicated', () => {
// FIXME: timeout to get rid of the "libp2p node not started yet" errors
setTimeout(() => resolve(), 1000)
})
// Try adding something again
await db2.add('hello!')
})
// Give access to the second user
db.access.grant('write', id2.id)
} catch (e) {
reject(e)
}
})
}
await doChanges()
const res1 = await db.iterator().collect().map(e => e.payload.value)
const res2 = await db2.iterator().collect().map(e => e.payload.value)
assert.deepStrictEqual(res1, ['hello!'])
assert.deepStrictEqual(res2, ['hello!'])
})
// const doChanges = () => {
// return new Promise((resolve, reject) => {
// try {
// // Wait for the second user's AC to notify it was updated
// db2.access.once('updated', async () => {
// // Wait for the first user's db to replicate the update
// db.events.once('replicated', () => {
// // FIXME: timeout to get rid of the "libp2p node not started yet" errors
// setTimeout(() => resolve(), 1000)
// })
// // Try adding something again
// await db2.add('hello!')
// })
// // Give access to the second user
// db.access.grant('write', id2.id)
// } catch (e) {
// reject(e)
// }
// })
// }
// await doChanges()
// const res1 = await db.iterator().collect().map(e => e.payload.value)
// const res2 = await db2.iterator().collect().map(e => e.payload.value)
// assert.deepStrictEqual(res1, ['hello!'])
// assert.deepStrictEqual(res2, ['hello!'])
// })
it('can\'t grant access if doesn\'t have write access', async () => {
let err
try {
await db2.access.grant('write', id2.id)
} catch (e) {
err = e.toString()
}
assert.strictEqual(err, `Error: Could not append entry, key "${db2.identity.id}" is not allowed to write to the log`)
})
// it('can\'t grant access if doesn\'t have write access', async () => {
// let err
// try {
// await db2.access.grant('write', id2.id)
// } catch (e) {
// err = e.toString()
// }
// assert.strictEqual(err, `Error: Could not append entry, key "${db2.identity.id}" is not allowed to write to the log`)
// })
it('can\'t revoke access if doesn\'t have write access', async () => {
let err
try {
await db2.access.revoke('write', id1.id)
} catch (e) {
err = e.toString()
}
assert.strictEqual(err, `Error: Could not append entry, key "${db2.identity.id}" is not allowed to write to the log`)
})
// it('can\'t revoke access if doesn\'t have write access', async () => {
// let err
// try {
// await db2.access.revoke('write', id1.id)
// } catch (e) {
// err = e.toString()
// }
// assert.strictEqual(err, `Error: Could not append entry, key "${db2.identity.id}" is not allowed to write to the log`)
// })
it('revoking access disables ability to write to the database', async () => {
const getError = () => {
return new Promise((resolve, reject) => {
try {
// Wait for the second user's AC to notify it was updated
db2.access.once('updated', async () => {
let err
try {
// Try adding something again
await db2.add('hello?')
} catch (e) {
err = e.toString()
}
resolve(err)
})
// Revoke user's access
db.access.revoke('write', id2.id)
} catch (e) {
reject(e)
}
})
}
const err = await getError()
assert.strictEqual(err, `Error: Could not append entry, key "${db2.identity.id}" is not allowed to write to the log`)
})
})
})
})
// TODO: use two separate peers for testing the AC
// TODO: add tests for revocation correctness with a database (integration tests)
})
// it('revoking access disables ability to write to the database', async () => {
// const getError = () => {
// return new Promise((resolve, reject) => {
// try {
// // Wait for the second user's AC to notify it was updated
// db2.access.once('updated', async () => {
// let err
// try {
// // Try adding something again
// await db2.add('hello?')
// } catch (e) {
// err = e.toString()
// }
// resolve(err)
// })
// // Revoke user's access
// db.access.revoke('write', id2.id)
// } catch (e) {
// reject(e)
// }
// })
// }
// const err = await getError()
// assert.strictEqual(err, `Error: Could not append entry, key "${db2.identity.id}" is not allowed to write to the log`)
// })
// })
// })
// })
// // TODO: use two separate peers for testing the AC
// // TODO: add tests for revocation correctness with a database (integration tests)
// })

View File

@ -1,336 +1,336 @@
import assert from 'assert'
import rmrf from 'rimraf'
import OrbitDB from '../../src/OrbitDB.js'
import IdentityProvider from 'orbit-db-identity-provider'
import Keystore from 'orbit-db-keystore'
import OrbitDBAccessController from 'orbit-db-access-controllers/orbitdb'
import AccessControllers from 'orbit-db-access-controllers'
// import assert from 'assert'
// import rmrf from 'rimraf'
// import OrbitDB from '../../src/OrbitDB.js'
// import IdentityProvider from 'orbit-db-identity-provider'
// import Keystore from 'orbit-db-keystore'
// import OrbitDBAccessController from 'orbit-db-access-controllers/orbitdb'
// import AccessControllers from 'orbit-db-access-controllers'
// Include test utilities
import {
config,
startIpfs,
stopIpfs,
testAPIs
} from 'orbit-db-test-utils'
// // Include test utilities
// import {
// config,
// startIpfs,
// stopIpfs,
// testAPIs
// } from 'orbit-db-test-utils'
const dbPath1 = './orbitdb/tests/orbitdb-access-controller/1'
const dbPath2 = './orbitdb/tests/orbitdb-access-controller/2'
// const dbPath1 = './orbitdb/tests/orbitdb-access-controller/1'
// const dbPath2 = './orbitdb/tests/orbitdb-access-controller/2'
Object.keys(testAPIs).forEach(API => {
describe(`orbit-db - OrbitDBAccessController (${API})`, function () {
this.timeout(config.timeout)
// Object.keys(testAPIs).forEach(API => {
// describe(`orbit-db - OrbitDBAccessController (${API})`, function () {
// this.timeout(config.timeout)
let ipfsd1, ipfsd2, ipfs1, ipfs2, id1, id2
let orbitdb1, orbitdb2
// let ipfsd1, ipfsd2, ipfs1, ipfs2, id1, id2
// let orbitdb1, orbitdb2
before(async () => {
rmrf.sync(dbPath1)
rmrf.sync(dbPath2)
ipfsd1 = await startIpfs(API, config.daemon1)
ipfsd2 = await startIpfs(API, config.daemon2)
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api
// before(async () => {
// rmrf.sync(dbPath1)
// rmrf.sync(dbPath2)
// ipfsd1 = await startIpfs(API, config.daemon1)
// ipfsd2 = await startIpfs(API, config.daemon2)
// ipfs1 = ipfsd1.api
// ipfs2 = ipfsd2.api
const keystore1 = new Keystore(dbPath1 + '/keys')
const keystore2 = new Keystore(dbPath2 + '/keys')
// const keystore1 = new Keystore(dbPath1 + '/keys')
// const keystore2 = new Keystore(dbPath2 + '/keys')
id1 = await IdentityProvider.createIdentity({ id: 'A', keystore: keystore1 })
id2 = await IdentityProvider.createIdentity({ id: 'B', keystore: keystore2 })
// id1 = await IdentityProvider.createIdentity({ id: 'A', keystore: keystore1 })
// id2 = await IdentityProvider.createIdentity({ id: 'B', keystore: keystore2 })
orbitdb1 = await OrbitDB.createInstance(ipfs1, {
AccessControllers,
directory: dbPath1,
identity: id1
})
// orbitdb1 = await OrbitDB.createInstance(ipfs1, {
// AccessControllers,
// directory: dbPath1,
// identity: id1
// })
orbitdb2 = await OrbitDB.createInstance(ipfs2, {
AccessControllers,
directory: dbPath2,
identity: id2
})
})
// orbitdb2 = await OrbitDB.createInstance(ipfs2, {
// AccessControllers,
// directory: dbPath2,
// identity: id2
// })
// })
after(async () => {
if (orbitdb1) {
await orbitdb1.stop()
}
// after(async () => {
// if (orbitdb1) {
// await orbitdb1.stop()
// }
if (orbitdb2) {
await orbitdb2.stop()
}
// if (orbitdb2) {
// await orbitdb2.stop()
// }
if (ipfsd1) {
await stopIpfs(ipfsd1)
}
// if (ipfsd1) {
// await stopIpfs(ipfsd1)
// }
if (ipfsd2) {
await stopIpfs(ipfsd2)
}
})
// if (ipfsd2) {
// await stopIpfs(ipfsd2)
// }
// })
describe('Constructor', function () {
let accessController
// describe('Constructor', function () {
// let accessController
before(async () => {
accessController = await OrbitDBAccessController.create(orbitdb1)
})
// before(async () => {
// accessController = await OrbitDBAccessController.create(orbitdb1)
// })
it('creates an access controller', () => {
assert.notStrictEqual(accessController, null)
assert.notStrictEqual(accessController, undefined)
})
// it('creates an access controller', () => {
// assert.notStrictEqual(accessController, null)
// assert.notStrictEqual(accessController, undefined)
// })
it('sets the controller type', () => {
assert.strictEqual(accessController.type, 'orbitdb')
})
// it('sets the controller type', () => {
// assert.strictEqual(accessController.type, 'orbitdb')
// })
it('has OrbitDB instance', async () => {
assert.notStrictEqual(accessController._orbitdb, null)
assert.strictEqual(accessController._orbitdb.id, orbitdb1.id)
})
// it('has OrbitDB instance', async () => {
// assert.notStrictEqual(accessController._orbitdb, null)
// assert.strictEqual(accessController._orbitdb.id, orbitdb1.id)
// })
it('has IPFS instance', async () => {
const peerId1 = await accessController._orbitdb._ipfs.id()
const peerId2 = await ipfs1.id()
assert.strictEqual(String(peerId1.id), String(peerId2.id))
})
// it('has IPFS instance', async () => {
// const peerId1 = await accessController._orbitdb._ipfs.id()
// const peerId2 = await ipfs1.id()
// assert.strictEqual(String(peerId1.id), String(peerId2.id))
// })
it('sets default capabilities', async () => {
assert.deepStrictEqual(accessController.capabilities, {
admin: new Set([id1.id])
})
})
// it('sets default capabilities', async () => {
// assert.deepStrictEqual(accessController.capabilities, {
// admin: new Set([id1.id])
// })
// })
it('allows owner to append after creation', async () => {
const mockEntry = {
identity: id1
// ...
// doesn't matter what we put here, only identity is used for the check
}
const canAppend = await accessController.canAppend(mockEntry, id1.provider)
assert.strictEqual(canAppend, true)
})
})
// it('allows owner to append after creation', async () => {
// const mockEntry = {
// identity: id1
// // ...
// // doesn't matter what we put here, only identity is used for the check
// }
// const canAppend = await accessController.canAppend(mockEntry, id1.provider)
// assert.strictEqual(canAppend, true)
// })
// })
describe('grant', function () {
let accessController
// describe('grant', function () {
// let accessController
before(async () => {
accessController = new OrbitDBAccessController(orbitdb1)
await accessController.load('testdb/add')
})
// before(async () => {
// accessController = new OrbitDBAccessController(orbitdb1)
// await accessController.load('testdb/add')
// })
it('loads the root access controller from IPFS', () => {
assert.strictEqual(accessController._db.access.type, 'ipfs')
assert.deepStrictEqual(accessController._db.access.write, [id1.id])
})
// it('loads the root access controller from IPFS', () => {
// assert.strictEqual(accessController._db.access.type, 'ipfs')
// assert.deepStrictEqual(accessController._db.access.write, [id1.id])
// })
it('adds a capability', async () => {
try {
await accessController.grant('write', id1.id)
} catch (e) {
assert(e, null)
}
assert.deepStrictEqual(accessController.capabilities, {
admin: new Set([id1.id]),
write: new Set([id1.id])
})
})
// it('adds a capability', async () => {
// try {
// await accessController.grant('write', id1.id)
// } catch (e) {
// assert(e, null)
// }
// assert.deepStrictEqual(accessController.capabilities, {
// admin: new Set([id1.id]),
// write: new Set([id1.id])
// })
// })
it('adds more capabilities', async () => {
try {
await accessController.grant('read', 'ABCD')
await accessController.grant('delete', 'ABCD')
} catch (e) {
assert.strictEqual(e, null)
}
assert.deepStrictEqual(accessController.capabilities, {
admin: new Set([id1.id]),
write: new Set([id1.id]),
read: new Set(['ABCD']),
delete: new Set(['ABCD'])
})
})
// it('adds more capabilities', async () => {
// try {
// await accessController.grant('read', 'ABCD')
// await accessController.grant('delete', 'ABCD')
// } catch (e) {
// assert.strictEqual(e, null)
// }
// assert.deepStrictEqual(accessController.capabilities, {
// admin: new Set([id1.id]),
// write: new Set([id1.id]),
// read: new Set(['ABCD']),
// delete: new Set(['ABCD'])
// })
// })
it('emit \'updated\' event when a capability was added', async () => {
return new Promise((resolve, reject) => {
accessController.on('updated', () => {
try {
assert.deepStrictEqual(accessController.capabilities, {
admin: new Set([id1.id]),
write: new Set([id1.id]),
read: new Set(['ABCD', 'AXES']),
delete: new Set(['ABCD'])
})
resolve()
} catch (e) {
reject(e)
}
})
accessController.grant('read', 'AXES')
})
})
// it('emit \'updated\' event when a capability was added', async () => {
// return new Promise((resolve, reject) => {
// accessController.on('updated', () => {
// try {
// assert.deepStrictEqual(accessController.capabilities, {
// admin: new Set([id1.id]),
// write: new Set([id1.id]),
// read: new Set(['ABCD', 'AXES']),
// delete: new Set(['ABCD'])
// })
// resolve()
// } catch (e) {
// reject(e)
// }
// })
// accessController.grant('read', 'AXES')
// })
// })
it('can append after acquiring capability', async () => {
try {
await accessController.grant('write', id1.id)
await accessController.grant('write', id2.id)
} catch (e) {
assert(e, null)
}
const mockEntry1 = {
identity: id1
}
const mockEntry2 = {
identity: id2
}
const canAppend1 = await accessController.canAppend(mockEntry1, id1.provider)
const canAppend2 = await accessController.canAppend(mockEntry2, id2.provider)
assert.strictEqual(canAppend1, true)
assert.strictEqual(canAppend2, true)
})
})
// it('can append after acquiring capability', async () => {
// try {
// await accessController.grant('write', id1.id)
// await accessController.grant('write', id2.id)
// } catch (e) {
// assert(e, null)
// }
// const mockEntry1 = {
// identity: id1
// }
// const mockEntry2 = {
// identity: id2
// }
// const canAppend1 = await accessController.canAppend(mockEntry1, id1.provider)
// const canAppend2 = await accessController.canAppend(mockEntry2, id2.provider)
// assert.strictEqual(canAppend1, true)
// assert.strictEqual(canAppend2, true)
// })
// })
describe('revoke', function () {
let accessController
// describe('revoke', function () {
// let accessController
before(async () => {
accessController = new OrbitDBAccessController(orbitdb1)
await accessController.load('testdb/remove')
})
// before(async () => {
// accessController = new OrbitDBAccessController(orbitdb1)
// await accessController.load('testdb/remove')
// })
it('removes a capability', async () => {
try {
await accessController.grant('write', id1.id)
await accessController.grant('write', 'AABB')
await accessController.revoke('write', 'AABB')
} catch (e) {
assert.strictEqual(e, null)
}
assert.deepStrictEqual(accessController.capabilities, {
admin: new Set([id1.id]),
write: new Set([id1.id])
})
})
// it('removes a capability', async () => {
// try {
// await accessController.grant('write', id1.id)
// await accessController.grant('write', 'AABB')
// await accessController.revoke('write', 'AABB')
// } catch (e) {
// assert.strictEqual(e, null)
// }
// assert.deepStrictEqual(accessController.capabilities, {
// admin: new Set([id1.id]),
// write: new Set([id1.id])
// })
// })
it('can remove the creator\'s write access', async () => {
try {
await accessController.revoke('write', id1.id)
} catch (e) {
assert.strictEqual(e, null)
}
assert.deepStrictEqual(accessController.capabilities, {
admin: new Set([id1.id])
})
})
// it('can remove the creator\'s write access', async () => {
// try {
// await accessController.revoke('write', id1.id)
// } catch (e) {
// assert.strictEqual(e, null)
// }
// assert.deepStrictEqual(accessController.capabilities, {
// admin: new Set([id1.id])
// })
// })
it('can\'t remove the creator\'s admin access', async () => {
try {
await accessController.revoke('admin', id1.id)
} catch (e) {
assert.strictEqual(e, null)
}
assert.deepStrictEqual(accessController.capabilities, {
admin: new Set([id1.id])
})
})
// it('can\'t remove the creator\'s admin access', async () => {
// try {
// await accessController.revoke('admin', id1.id)
// } catch (e) {
// assert.strictEqual(e, null)
// }
// assert.deepStrictEqual(accessController.capabilities, {
// admin: new Set([id1.id])
// })
// })
it('removes more capabilities', async () => {
try {
await accessController.grant('read', 'ABCD')
await accessController.grant('delete', 'ABCD')
await accessController.grant('write', id1.id)
await accessController.revoke('read', 'ABCDE')
await accessController.revoke('delete', 'ABCDE')
} catch (e) {
assert.strictEqual(e, null)
}
assert.deepStrictEqual(accessController.capabilities, {
admin: new Set([id1.id]),
delete: new Set(['ABCD']),
read: new Set(['ABCD']),
write: new Set([id1.id])
})
})
// it('removes more capabilities', async () => {
// try {
// await accessController.grant('read', 'ABCD')
// await accessController.grant('delete', 'ABCD')
// await accessController.grant('write', id1.id)
// await accessController.revoke('read', 'ABCDE')
// await accessController.revoke('delete', 'ABCDE')
// } catch (e) {
// assert.strictEqual(e, null)
// }
// assert.deepStrictEqual(accessController.capabilities, {
// admin: new Set([id1.id]),
// delete: new Set(['ABCD']),
// read: new Set(['ABCD']),
// write: new Set([id1.id])
// })
// })
it('can\'t append after revoking capability', async () => {
try {
await accessController.grant('write', id2.id)
await accessController.revoke('write', id2.id)
} catch (e) {
assert(e, null)
}
const mockEntry1 = {
identity: id1
}
const mockEntry2 = {
identity: id2
}
const canAppend = await accessController.canAppend(mockEntry1, id1.provider)
const noAppend = await accessController.canAppend(mockEntry2, id2.provider)
assert.strictEqual(canAppend, true)
assert.strictEqual(noAppend, false)
})
// it('can\'t append after revoking capability', async () => {
// try {
// await accessController.grant('write', id2.id)
// await accessController.revoke('write', id2.id)
// } catch (e) {
// assert(e, null)
// }
// const mockEntry1 = {
// identity: id1
// }
// const mockEntry2 = {
// identity: id2
// }
// const canAppend = await accessController.canAppend(mockEntry1, id1.provider)
// const noAppend = await accessController.canAppend(mockEntry2, id2.provider)
// assert.strictEqual(canAppend, true)
// assert.strictEqual(noAppend, false)
// })
it('emits \'updated\' event when a capability was removed', async () => {
await accessController.grant('admin', 'cats')
await accessController.grant('admin', 'dogs')
// it('emits \'updated\' event when a capability was removed', async () => {
// await accessController.grant('admin', 'cats')
// await accessController.grant('admin', 'dogs')
return new Promise((resolve, reject) => {
accessController.on('updated', () => {
try {
assert.deepStrictEqual(accessController.capabilities, {
admin: new Set([id1.id, 'dogs']),
delete: new Set(['ABCD']),
read: new Set(['ABCD']),
write: new Set([id1.id])
})
resolve()
} catch (e) {
reject(e)
}
})
accessController.revoke('admin', 'cats')
})
})
})
// return new Promise((resolve, reject) => {
// accessController.on('updated', () => {
// try {
// assert.deepStrictEqual(accessController.capabilities, {
// admin: new Set([id1.id, 'dogs']),
// delete: new Set(['ABCD']),
// read: new Set(['ABCD']),
// write: new Set([id1.id])
// })
// resolve()
// } catch (e) {
// reject(e)
// }
// })
// accessController.revoke('admin', 'cats')
// })
// })
// })
describe('save and load', function () {
let accessController, dbName
// describe('save and load', function () {
// let accessController, dbName
before(async () => {
dbName = 'testdb-load-' + new Date().getTime()
accessController = new OrbitDBAccessController(orbitdb1)
await accessController.load(dbName)
await accessController.grant('write', 'A')
await accessController.grant('write', 'B')
await accessController.grant('write', 'C')
await accessController.grant('write', 'C') // double entry
await accessController.grant('another', 'AA')
await accessController.grant('another', 'BB')
await accessController.revoke('another', 'AA')
await accessController.grant('admin', id1.id)
return new Promise((resolve) => {
// Test that the access controller emits 'updated' after it was loaded
accessController.on('updated', () => resolve())
accessController.load(accessController.address)
})
})
// before(async () => {
// dbName = 'testdb-load-' + new Date().getTime()
// accessController = new OrbitDBAccessController(orbitdb1)
// await accessController.load(dbName)
// await accessController.grant('write', 'A')
// await accessController.grant('write', 'B')
// await accessController.grant('write', 'C')
// await accessController.grant('write', 'C') // double entry
// await accessController.grant('another', 'AA')
// await accessController.grant('another', 'BB')
// await accessController.revoke('another', 'AA')
// await accessController.grant('admin', id1.id)
// return new Promise((resolve) => {
// // Test that the access controller emits 'updated' after it was loaded
// accessController.on('updated', () => resolve())
// accessController.load(accessController.address)
// })
// })
it('has the correct database address for the internal db', async () => {
const addr = accessController._db.address.toString().split('/')
assert.strictEqual(addr[addr.length - 1], '_access')
assert.strictEqual(addr[addr.length - 2], dbName)
})
// it('has the correct database address for the internal db', async () => {
// const addr = accessController._db.address.toString().split('/')
// assert.strictEqual(addr[addr.length - 1], '_access')
// assert.strictEqual(addr[addr.length - 2], dbName)
// })
it('has correct capabalities', async () => {
assert.deepStrictEqual(accessController.get('admin'), new Set([id1.id]))
assert.deepStrictEqual(accessController.get('write'), new Set(['A', 'B', 'C']))
assert.deepStrictEqual(accessController.get('another'), new Set(['BB']))
})
})
})
// TODO: use two separate peers for testing the AC
// TODO: add tests for revocation correctness with a database (integration tests)
})
// it('has correct capabalities', async () => {
// assert.deepStrictEqual(accessController.get('admin'), new Set([id1.id]))
// assert.deepStrictEqual(accessController.get('write'), new Set(['A', 'B', 'C']))
// assert.deepStrictEqual(accessController.get('another'), new Set(['BB']))
// })
// })
// })
// // TODO: use two separate peers for testing the AC
// // TODO: add tests for revocation correctness with a database (integration tests)
// })

View File

@ -1,6 +0,0 @@
export default async (ipfs1, ipfs2) => {
const id1 = await ipfs1.id()
const id2 = await ipfs2.id()
await ipfs1.swarm.connect(id2.addresses[0])
await ipfs2.swarm.connect(id1.addresses[0])
}

View File

@ -1,54 +0,0 @@
// import pkg from 'elliptic'
// const { ec: EC } = pkg
// const ec = new EC('secp256k1')
// /**
// * A custom keystore example
// */
// class CustomTestKeystore {
// constructor (signer) {
// this.createKey()
// }
// createKey () {
// const key = ec.genKeyPair()
// this.key = ec.keyPair({
// pub: key.getPublic('hex'),
// priv: key.getPrivate('hex'),
// privEnc: 'hex',
// pubEnc: 'hex'
// })
// return this.key
// }
// getKey () {
// return this.key
// }
// // TODO: check if this is really in use
// generateKey () {
// return Promise.resolve(this.createKey())
// }
// importPublicKey (key) {
// return Promise.resolve(ec.keyFromPublic(key, 'hex'))
// }
// importPrivateKey (key) {
// return Promise.resolve(ec.keyFromPrivate(key, 'hex'))
// }
// sign (key, data) {
// const sig = ec.sign(data, key)
// return Promise.resolve(sig.toDER('hex'))
// }
// verify (signature, key, data) {
// let res = false
// res = ec.verify(data, signature, key)
// return Promise.resolve(res)
// }
// }
// export default new CustomTestKeystore()

View File

@ -1,8 +0,0 @@
// TODO: these should be replace with orbit-db-test-utils
export { default as config } from './config.js'
export { testAPIs } from './test-apis.js'
export { default as startIpfs } from './start-ipfs.js'
export { default as stopIpfs } from './stop-ipfs.js'
export { default as waitForPeers } from './wait-for-peers.js'
export { default as connectPeers } from './connect-peers.js'
export { default as CustomTestKeystore } from './custom-test-keystore.js'

View File

@ -1,26 +0,0 @@
import * as IPFSFactory from 'ipfsd-ctl'
import { testAPIs } from './test-apis.js'
/**
* Start an IPFS instance
* @param {Object} config [IPFS configuration to use]
* @return {[Promise<IPFS>]} [IPFS instance]
*/
export default (type, config = {}) => {
return new Promise((resolve, reject) => {
if (!testAPIs[type]) {
reject(new Error(`Wanted API type ${JSON.stringify(type)} is unknown. Available types: ${Object.keys(testAPIs).join(', ')}`))
}
// Spawn an IPFS daemon (type defined in)
IPFSFactory
.create(testAPIs[type])
.spawn(config, async (err, ipfsd) => {
if (err) {
reject(err)
}
resolve(ipfsd)
})
})
}

View File

@ -1,13 +0,0 @@
/**
* Stop an IPFS or ipfsd-ctl instance
* @param {Object} config [IPFS ipfsd-ctl to stop]
* @return {None}
*/
export default (ipfs) => {
return new Promise((resolve, reject) => {
ipfs.stop((err) => {
if (err) { reject(err) }
resolve()
})
})
}

View File

@ -1,32 +0,0 @@
import * as IPFS from 'ipfs'
/**
* IPFS daemons to run the tests with.
*/
// Available daemon types are defined in:
// https://github.com/ipfs/js-ipfsd-ctl#ipfsfactory---const-f--ipfsfactorycreateoptions
const jsIpfs = {
'js-ipfs': {
type: 'proc',
exec: IPFS
}
}
const goIpfs = {
'go-ipfs': {
type: 'go'
}
}
// By default, we run tests against js-ipfs.
let testAPIs = Object.assign({}, jsIpfs)
// Setting env variable 'TEST=all' will make tests run with js-ipfs and go-ipfs.
// Setting env variable 'TEST=go' will make tests run with go-ipfs.
// Eg. 'TEST=go mocha' runs tests with go-ipfs
if (process.env.TEST === 'all') { testAPIs = Object.assign({}, testAPIs, goIpfs) } else if (process.env.TEST === 'go') { testAPIs = Object.assign({}, goIpfs) }
export {
testAPIs
}

View File

@ -1,12 +0,0 @@
export default (ipfs, peersToWait, topic, callback) => {
return new Promise((resolve, reject) => {
const i = setInterval(async () => {
const peers = await ipfs.pubsub.peers(topic)
const hasAllPeers = peersToWait.map((e) => peers.includes(e)).filter((e) => e === false).length === 0
if (hasAllPeers) {
clearInterval(i)
resolve()
}
}, 500)
})
}

View File

@ -1,8 +1,9 @@
export default {
timeout: 30000,
dbname: 'orbit-db-tests',
defaultIpfsConfig: {
start: true,
preload: {
enabled: false
},
EXPERIMENTAL: {
pubsub: true
},
@ -16,7 +17,7 @@ export default {
Discovery: {
MDNS: {
Enabled: true,
Interval: 1
Interval: 0
},
webRTCStar: {
Enabled: false
@ -25,8 +26,6 @@ export default {
}
},
daemon1: {
repo: './ipfs/orbitdb/tests/daemon1',
start: true,
EXPERIMENTAL: {
pubsub: true
},
@ -40,7 +39,7 @@ export default {
Discovery: {
MDNS: {
Enabled: true,
Interval: 1
Interval: 0
},
webRTCStar: {
Enabled: false
@ -49,8 +48,6 @@ export default {
}
},
daemon2: {
repo: './ipfs/orbitdb/tests/daemon2',
start: true,
EXPERIMENTAL: {
pubsub: true
},
@ -64,7 +61,7 @@ export default {
Discovery: {
MDNS: {
Enabled: true,
Interval: 1
Interval: 0
},
webRTCStar: {
Enabled: false

View File

@ -1,17 +1,16 @@
import { deepStrictEqual, strictEqual } from 'assert'
import rmrf from 'rimraf'
import { copy } from 'fs-extra'
import * as IPFS from 'ipfs'
import { Log, Entry, Database, KeyStore, Identities } from '../../src/index.js'
import { DocumentStore } from '../../src/db/index.js'
import { config, startIpfs, stopIpfs } from 'orbit-db-test-utils'
import config from '../config.js'
import testKeysPath from '../fixtures/test-keys-path.js '
const OpLog = { Log, Entry }
const keysPath = './testkeys'
const IPFS = 'js-ipfs'
describe('DocumentStore Database', function () {
let ipfsd
let ipfs
let keystore
let accessController
@ -22,8 +21,7 @@ describe('DocumentStore Database', function () {
const databaseId = 'documentstore-AAA'
before(async () => {
ipfsd = await startIpfs(IPFS, config.daemon1)
ipfs = ipfsd.api
ipfs = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
await copy(testKeysPath, keysPath)
keystore = await KeyStore({ path: keysPath })
@ -32,8 +30,8 @@ describe('DocumentStore Database', function () {
})
after(async () => {
if (ipfsd) {
await stopIpfs(ipfsd)
if (ipfs) {
await ipfs.stop()
}
if (keystore) {
@ -42,6 +40,7 @@ describe('DocumentStore Database', function () {
await rmrf(keysPath)
await rmrf('./orbitdb')
await rmrf('./ipfs1')
})
describe('Default index \'_id\'', () => {

View File

@ -2,17 +2,16 @@ import { deepStrictEqual, strictEqual } from 'assert'
import mapSeries from 'p-map-series'
import rmrf from 'rimraf'
import { copy } from 'fs-extra'
import * as IPFS from 'ipfs'
import { Log, Entry, Database, KeyStore, Identities } from '../../src/index.js'
import { EventStore } from '../../src/db/index.js'
import { config, startIpfs, stopIpfs } from 'orbit-db-test-utils'
import config from '../config.js'
import testKeysPath from '../fixtures/test-keys-path.js '
const OpLog = { Log, Entry }
const keysPath = './testkeys'
const IPFS = 'js-ipfs'
describe('EventStore Database', function () {
let ipfsd
let ipfs
let keystore
let accessController
@ -23,8 +22,7 @@ describe('EventStore Database', function () {
const databaseId = 'eventstore-AAA'
before(async () => {
ipfsd = await startIpfs(IPFS, config.daemon1)
ipfs = ipfsd.api
ipfs = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
await copy(testKeysPath, keysPath)
keystore = await KeyStore({ path: keysPath })
@ -33,8 +31,8 @@ describe('EventStore Database', function () {
})
after(async () => {
if (ipfsd) {
await stopIpfs(ipfsd)
if (ipfs) {
await ipfs.stop()
}
if (keystore) {
@ -43,6 +41,7 @@ describe('EventStore Database', function () {
await rmrf(keysPath)
await rmrf('./orbitdb')
await rmrf('./ipfs1')
})
beforeEach(async () => {

View File

@ -1,17 +1,16 @@
import { deepStrictEqual, strictEqual } from 'assert'
import rmrf from 'rimraf'
import { copy } from 'fs-extra'
import * as IPFS from 'ipfs'
import { Log, Entry, Database, KeyStore, Identities } from '../../src/index.js'
import { KeyValuePersisted, KeyValue } from '../../src/db/index.js'
import { config, startIpfs, stopIpfs } from 'orbit-db-test-utils'
import config from '../config.js'
import testKeysPath from '../fixtures/test-keys-path.js '
const OpLog = { Log, Entry }
const keysPath = './testkeys'
const IPFS = 'js-ipfs'
describe('KeyValuePersisted Database', function () {
let ipfsd
let ipfs
let keystore
let accessController
@ -22,8 +21,7 @@ describe('KeyValuePersisted Database', function () {
const databaseId = 'keyvalue-AAA'
before(async () => {
ipfsd = await startIpfs(IPFS, config.daemon1)
ipfs = ipfsd.api
ipfs = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
await copy(testKeysPath, keysPath)
keystore = await KeyStore({ path: keysPath })
@ -32,8 +30,8 @@ describe('KeyValuePersisted Database', function () {
})
after(async () => {
if (ipfsd) {
await stopIpfs(ipfsd)
if (ipfs) {
await ipfs.stop()
}
if (keystore) {
@ -42,6 +40,7 @@ describe('KeyValuePersisted Database', function () {
await rmrf(keysPath)
await rmrf('./orbitdb')
await rmrf('./ipfs1')
})
beforeEach(async () => {

View File

@ -1,17 +1,16 @@
import { deepStrictEqual, strictEqual } from 'assert'
import rmrf from 'rimraf'
import { copy } from 'fs-extra'
import * as IPFS from 'ipfs'
import { Log, Entry, Database, KeyStore, Identities } from '../../src/index.js'
import { KeyValue } from '../../src/db/index.js'
import { config, startIpfs, stopIpfs } from 'orbit-db-test-utils'
import config from '../config.js'
import testKeysPath from '../fixtures/test-keys-path.js '
const OpLog = { Log, Entry }
const keysPath = './testkeys'
const IPFS = 'js-ipfs'
describe('KeyValue Database', function () {
let ipfsd
let ipfs
let keystore
let accessController
@ -22,8 +21,7 @@ describe('KeyValue Database', function () {
const databaseId = 'keyvalue-AAA'
before(async () => {
ipfsd = await startIpfs(IPFS, config.daemon1)
ipfs = ipfsd.api
ipfs = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
await copy(testKeysPath, keysPath)
keystore = await KeyStore({ path: keysPath })
@ -32,8 +30,8 @@ describe('KeyValue Database', function () {
})
after(async () => {
if (ipfsd) {
await stopIpfs(ipfsd)
if (ipfs) {
await ipfs.stop()
}
if (keystore) {
@ -42,6 +40,7 @@ describe('KeyValue Database', function () {
await rmrf(keysPath)
await rmrf('./orbitdb')
await rmrf('./ipfs1')
})
beforeEach(async () => {

View File

@ -1,21 +1,20 @@
import { deepStrictEqual } from 'assert'
import rmrf from 'rimraf'
import { copy } from 'fs-extra'
import * as IPFS from 'ipfs'
import { Log, Entry, Database, KeyStore, Identities } from '../../../src/index.js'
import { DocumentStore } from '../../../src/db/index.js'
import { config, startIpfs, stopIpfs } from 'orbit-db-test-utils'
import config from '../../config.js'
import testKeysPath from '../../fixtures/test-keys-path.js '
import connectPeers from '../../utils/connect-nodes.js'
import waitFor from '../../utils/wait-for.js'
const OpLog = { Log, Entry }
const keysPath = './testkeys'
const IPFS = 'js-ipfs'
describe('Documents Database Replication', function () {
this.timeout(30000)
let ipfsd1, ipfsd2
let ipfs1, ipfs2
let keystore
let identities
@ -33,11 +32,8 @@ describe('Documents Database Replication', function () {
}
before(async () => {
ipfsd1 = await startIpfs(IPFS, config.daemon1)
ipfsd2 = await startIpfs(IPFS, config.daemon2)
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api
ipfs1 = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
ipfs2 = await IPFS.create({ ...config.daemon2, repo: './ipfs2' })
await connectPeers(ipfs1, ipfs2)
await copy(testKeysPath, keysPath)
@ -48,12 +44,12 @@ describe('Documents Database Replication', function () {
})
after(async () => {
if (ipfsd1) {
await stopIpfs(ipfsd1)
if (ipfs1) {
await ipfs1.stop()
}
if (ipfsd2) {
await stopIpfs(ipfsd2)
if (ipfs2) {
await ipfs2.stop()
}
if (keystore) {
@ -63,6 +59,8 @@ describe('Documents Database Replication', function () {
await rmrf(keysPath)
await rmrf('./orbitdb1')
await rmrf('./orbitdb2')
await rmrf('./ipfs1')
await rmrf('./ipfs2')
})
beforeEach(async () => {

View File

@ -1,21 +1,20 @@
import { deepStrictEqual } from 'assert'
import rmrf from 'rimraf'
import { copy } from 'fs-extra'
import * as IPFS from 'ipfs'
import { Log, Entry, Database, KeyStore, Identities } from '../../../src/index.js'
import { EventStore } from '../../../src/db/index.js'
import { config, startIpfs, stopIpfs } from 'orbit-db-test-utils'
import config from '../../config.js'
import testKeysPath from '../../fixtures/test-keys-path.js '
import connectPeers from '../../utils/connect-nodes.js'
import waitFor from '../../utils/wait-for.js'
const OpLog = { Log, Entry }
const keysPath = './testkeys'
const IPFS = 'js-ipfs'
describe('Events Database Replication', function () {
this.timeout(5000)
this.timeout(30000)
let ipfsd1, ipfsd2
let ipfs1, ipfs2
let keystore
let identities
@ -43,11 +42,8 @@ describe('Events Database Replication', function () {
]
before(async () => {
ipfsd1 = await startIpfs(IPFS, config.daemon1)
ipfsd2 = await startIpfs(IPFS, config.daemon2)
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api
ipfs1 = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
ipfs2 = await IPFS.create({ ...config.daemon2, repo: './ipfs2' })
await connectPeers(ipfs1, ipfs2)
await copy(testKeysPath, keysPath)
@ -58,12 +54,12 @@ describe('Events Database Replication', function () {
})
after(async () => {
if (ipfsd1) {
await stopIpfs(ipfsd1)
if (ipfs1) {
await ipfs1.stop()
}
if (ipfsd2) {
await stopIpfs(ipfsd2)
if (ipfs2) {
await ipfs2.stop()
}
if (keystore) {
@ -73,6 +69,8 @@ describe('Events Database Replication', function () {
await rmrf(keysPath)
await rmrf('./orbitdb1')
await rmrf('./orbitdb2')
await rmrf('./ipfs1')
await rmrf('./ipfs2')
})
afterEach(async () => {

View File

@ -1,21 +1,20 @@
import { deepStrictEqual } from 'assert'
import rmrf from 'rimraf'
import { copy } from 'fs-extra'
import * as IPFS from 'ipfs'
import { Log, Entry, Database, KeyStore, Identities } from '../../../src/index.js'
import { KeyValue, KeyValuePersisted } from '../../../src/db/index.js'
import { config, startIpfs, stopIpfs } from 'orbit-db-test-utils'
import config from '../../config.js'
import testKeysPath from '../../fixtures/test-keys-path.js '
import connectPeers from '../../utils/connect-nodes.js'
import waitFor from '../../utils/wait-for.js'
const OpLog = { Log, Entry }
const keysPath = './testkeys'
const IPFS = 'js-ipfs'
describe('KeyValue Database Replication', function () {
this.timeout(5000)
this.timeout(30000)
let ipfsd1, ipfsd2
let ipfs1, ipfs2
let keystore
let identities
@ -32,11 +31,8 @@ describe('KeyValue Database Replication', function () {
}
before(async () => {
ipfsd1 = await startIpfs(IPFS, config.daemon1)
ipfsd2 = await startIpfs(IPFS, config.daemon2)
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api
ipfs1 = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
ipfs2 = await IPFS.create({ ...config.daemon2, repo: './ipfs2' })
await connectPeers(ipfs1, ipfs2)
await copy(testKeysPath, keysPath)
@ -47,12 +43,12 @@ describe('KeyValue Database Replication', function () {
})
after(async () => {
if (ipfsd1) {
await stopIpfs(ipfsd1)
if (ipfs1) {
await ipfs1.stop()
}
if (ipfsd2) {
await stopIpfs(ipfsd2)
if (ipfs2) {
await ipfs2.stop()
}
if (keystore) {
@ -62,6 +58,8 @@ describe('KeyValue Database Replication', function () {
await rmrf(keysPath)
await rmrf('./orbitdb1')
await rmrf('./orbitdb2')
await rmrf('./ipfs1')
await rmrf('./ipfs2')
})
afterEach(async () => {

File diff suppressed because it is too large Load Diff

View File

@ -1,17 +1,19 @@
import { strictEqual } from 'assert'
import rmrf from 'rimraf'
import { copy } from 'fs-extra'
import * as IPFS from 'ipfs'
import { Log, Entry, Identities, KeyStore, IPFSBlockStorage } from '../../src/index.js'
import { config, startIpfs, stopIpfs, getIpfsPeerId, waitForPeers, connectPeers } from 'orbit-db-test-utils'
import config from '../config.js'
import testKeysPath from '../fixtures/test-keys-path.js '
import connectPeers from '../utils/connect-nodes.js'
import getIpfsPeerId from '../utils/get-ipfs-peer-id.js'
import waitForPeers from '../utils/wait-for-peers.js'
const keysPath = './testkeys'
const IPFS = 'js-ipfs'
describe('Log - Replication', function () {
this.timeout(60000)
let ipfsd1, ipfsd2
let ipfs1, ipfs2
let id1, id2
let keystore
@ -20,11 +22,8 @@ describe('Log - Replication', function () {
let storage1, storage2
before(async () => {
ipfsd1 = await startIpfs(IPFS, config.daemon1)
ipfsd2 = await startIpfs(IPFS, config.daemon2)
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api
ipfs1 = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
ipfs2 = await IPFS.create({ ...config.daemon2, repo: './ipfs2' })
await connectPeers(ipfs1, ipfs2)
id1 = await getIpfsPeerId(ipfs1)
@ -43,14 +42,24 @@ describe('Log - Replication', function () {
})
after(async () => {
await stopIpfs(ipfsd1)
await stopIpfs(ipfsd2)
if (ipfs1) {
await ipfs1.stop()
}
if (ipfs2) {
await ipfs2.stop()
}
if (keystore) {
await keystore.close()
}
await storage1.close()
await storage2.close()
await rmrf(keysPath)
await rmrf('./ipfs1')
await rmrf('./ipfs2')
})
describe('replicates logs deterministically', async function () {

View File

@ -2,40 +2,36 @@ import { deepStrictEqual, strictEqual, notStrictEqual } from 'assert'
import rmrf from 'rimraf'
import fs from 'fs'
import path from 'path'
import * as IPFS from 'ipfs'
import { OrbitDB, isValidAddress } from '../src/index.js'
import { KeyValuePersisted } from '../src/db/index.js'
import { config, startIpfs, stopIpfs } from 'orbit-db-test-utils'
import config from './config.js'
import connectPeers from './utils/connect-nodes.js'
import waitFor from './utils/wait-for.js'
const IPFS = 'js-ipfs'
describe('Open databases', function () {
this.timeout(5000)
let ipfsd1, ipfsd2
let ipfs1, ipfs2
let orbitdb1, orbitdb2
before(async () => {
ipfsd1 = await startIpfs(IPFS, config.daemon1)
ipfsd2 = await startIpfs(IPFS, config.daemon2)
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api
ipfs1 = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
ipfs2 = await IPFS.create({ ...config.daemon2, repo: './ipfs2' })
await connectPeers(ipfs1, ipfs2)
})
after(async () => {
if (ipfsd1) {
await stopIpfs(ipfsd1)
if (ipfs1) {
await ipfs1.stop()
}
if (ipfsd2) {
await stopIpfs(ipfsd2)
if (ipfs2) {
await ipfs2.stop()
}
await rmrf('./orbitdb1')
await rmrf('./orbitdb2')
await rmrf('./ipfs1')
await rmrf('./ipfs2')
})
describe('creating a database instance', () => {

View File

@ -1,36 +1,34 @@
import { strictEqual, deepStrictEqual } from 'assert'
import rmrf from 'rimraf'
import * as IPFS from 'ipfs'
import { OrbitDB } from '../src/index.js'
import { config, startIpfs, stopIpfs } from 'orbit-db-test-utils'
import config from './config.js'
import connectPeers from './utils/connect-nodes.js'
import waitFor from './utils/wait-for.js'
const IPFS = 'js-ipfs'
describe('Replicating databases', function () {
this.timeout(60000)
let ipfsd1, ipfsd2
let ipfs1, ipfs2
let orbitdb1, orbitdb2
before(async () => {
ipfsd1 = await startIpfs(IPFS, config.daemon1)
ipfsd2 = await startIpfs(IPFS, config.daemon2)
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api
ipfs1 = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
ipfs2 = await IPFS.create({ ...config.daemon2, repo: './ipfs2' })
await connectPeers(ipfs1, ipfs2)
})
after(async () => {
if (ipfsd1) {
await stopIpfs(ipfsd1)
if (ipfs1) {
await ipfs1.stop()
}
if (ipfsd2) {
await stopIpfs(ipfsd2)
if (ipfs2) {
await ipfs2.stop()
}
await rmrf('./orbitdb1')
await rmrf('./orbitdb2')
await rmrf('./ipfs1')
await rmrf('./ipfs2')
})
describe('replicating a database', () => {

View File

@ -2,36 +2,32 @@ import { strictEqual, notStrictEqual } from 'assert'
import rmrf from 'rimraf'
import fs from 'fs'
import path from 'path'
import * as IPFS from 'ipfs'
import { OrbitDB, isIdentity } from '../src/index.js'
import { config, startIpfs, stopIpfs } from 'orbit-db-test-utils'
import config from './config.js'
import connectPeers from './utils/connect-nodes.js'
const IPFS = 'js-ipfs'
describe('OrbitDB', function () {
this.timeout(5000)
let ipfsd1, ipfsd2
let ipfs1, ipfs2
let orbitdb1
before(async () => {
ipfsd1 = await startIpfs(IPFS, config.daemon1)
ipfsd2 = await startIpfs(IPFS, config.daemon2)
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api
ipfs1 = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
ipfs2 = await IPFS.create({ ...config.daemon2, repo: './ipfs2' })
await connectPeers(ipfs1, ipfs2)
})
after(async () => {
if (ipfsd1) {
await stopIpfs(ipfsd1)
if (ipfs1) {
await ipfs1.stop()
}
if (ipfsd2) {
await stopIpfs(ipfsd2)
if (ipfs2) {
await ipfs2.stop()
}
await rmrf('./ipfs1')
await rmrf('./ipfs2')
})
describe('OrbitDB instance creation - defaults', () => {

View File

@ -4,7 +4,7 @@ import { copy } from 'fs-extra'
import * as IPFS from 'ipfs'
import { Log, Identities, KeyStore } from '../src/index.js'
import { IPFSBlockStorage, MemoryStorage, LRUStorage, ComposedStorage } from '../src/storage/index.js'
import { config } from 'orbit-db-test-utils'
import config from './config.js'
import testKeysPath from './fixtures/test-keys-path.js '
const keysPath = './testkeys'
@ -12,25 +12,23 @@ const keysPath = './testkeys'
describe('Storages', function () {
this.timeout(5000)
let ipfs1
let ipfs
let keystore
let testIdentity1
let testIdentity
before(async () => {
ipfs = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
await copy(testKeysPath, keysPath)
// Start an IPFS instance
ipfs1 = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
keystore = await KeyStore({ path: keysPath })
const identities = await Identities({ keystore })
testIdentity1 = await identities.createIdentity({ id: 'userA' })
testIdentity = await identities.createIdentity({ id: 'userA' })
})
after(async () => {
if (ipfs1) {
await ipfs1.stop()
if (ipfs) {
await ipfs.stop()
}
if (keystore) {
@ -43,8 +41,8 @@ describe('Storages', function () {
const runTestWithStorage = async (storage) => {
const amount = 100
const log1 = await Log(testIdentity1, { logId: 'A', storage })
const log2 = await Log(testIdentity1, { logId: 'A', storage })
const log1 = await Log(testIdentity, { logId: 'A', storage })
const log2 = await Log(testIdentity, { logId: 'A', storage })
for (let i = 0; i < amount; i++) {
await log1.append('hello' + i)
await log2.append('hello' + i)
@ -82,7 +80,7 @@ describe('Storages', function () {
describe('IPFSBlockStorage', () => {
it('tests the storage', async () => {
const storage = await IPFSBlockStorage({ ipfs: ipfs1 })
const storage = await IPFSBlockStorage({ ipfs })
notStrictEqual(storage, undefined)
await runTestWithStorage(storage)
})
@ -91,7 +89,7 @@ describe('Storages', function () {
describe('Composed Storages', () => {
it('tests Memory + IPFSBlockStorage composition', async () => {
const storage1 = await MemoryStorage()
const storage2 = await IPFSBlockStorage({ ipfs: ipfs1 })
const storage2 = await IPFSBlockStorage({ ipfs })
const storage = await ComposedStorage(storage1, storage2)
notStrictEqual(storage, undefined)
await runTestWithStorage(storage)
@ -99,7 +97,7 @@ describe('Storages', function () {
it('tests LRU + IPFSBlockStorage composition', async () => {
const storage1 = await LRUStorage({ size: -1 })
const storage2 = await IPFSBlockStorage({ ipfs: ipfs1 })
const storage2 = await IPFSBlockStorage({ ipfs })
const storage = await ComposedStorage(storage1, storage2)
notStrictEqual(storage, undefined)
await runTestWithStorage(storage)

View File

@ -0,0 +1,8 @@
'use strict'
const getIpfsPeerId = async (ipfs) => {
const peerId = await ipfs.id()
return peerId.id
}
export default getIpfsPeerId

View File

@ -0,0 +1,27 @@
'use strict'
const waitForPeers = (ipfs, peersToWait, topic) => {
return new Promise((resolve, reject) => {
const interval = setInterval(async () => {
try {
const peers = await ipfs.pubsub.peers(topic)
const peerIds = peers.map(peer => peer.toString())
const peerIdsToWait = peersToWait.map(peer => peer.toString())
const hasAllPeers = peerIdsToWait.map((e) => peerIds.includes(e)).filter((e) => e === false).length === 0
// FIXME: Does not fail on timeout, not easily fixable
if (hasAllPeers) {
console.log('Found peers!')
clearInterval(interval)
resolve()
}
} catch (e) {
clearInterval(interval)
reject(e)
}
}, 200)
})
}
export default waitForPeers