orbitdb/test/write-permissions.test.js
Mark Henderson 47a18b05f5 dev:updating keystore in package.json to git branch
pointing to fix/store-performance branch

fix:typo

fix: indentation

test: Changing test to reflect new orbit-store default

Update package.json

test: updating tests

update localstorage-level-migration dep

experiment:Moving keystore up ALL way

orbitdb storage adapter mark 1

fix: more passing tests

more fixes

chore:package-lock.json

reverting mkdir.c for now

package-lock.json for node 10.13

fix: circleci

fix: webpack fs updates

disabling loadCache

Moving storage adapter to its own thing

tests: fixing up

chore: long needed fixing

More linting

tests: fix up look sharp

test: v0 failure only

Reversting lint fixes

fix v0-load test

set cache heads

fix: passing in storage no longer needed

fix: removing artifact from previous merge

fix: honor default keystore and pesky call-by-reference bug

fix: removing directory arg from _addManifestToCache

chore: package-lock

fix: pending drop test

removing directory option for individual dbs

docs: removing directory options

fix: removing line instead of commenting

fix: moving storage setup to createInstance

feat: Upgrading ipfs to 0.36

chore: package-log

fix: restoring onlyHash

workaround: removing memstore from replication tests

fix: this.keystore.close and this.cache.close

chore: removing eslint annotation

chore: package-lock.json

fix: passing preCreate in as option

chore: package files

Fixing package.json

fixing replicate tests

Fixing some tests

Updating orbit-db-store dependency

CircleCI updates - To be obviated via other PR

Restoring ability to pass a custom directory to orbitdb.create

More test fixes

set identity tests fixed

Fixing replication tests

Temporarily disabling concurrency tests

Closing keystore in identities test

Restoring test:all

package.json

More replicate test fixes

successful make rebuild

Linting fixes
2019-08-30 14:18:28 -04:00

219 lines
7.1 KiB
JavaScript

'use strict'
const assert = require('assert')
const rmrf = require('rimraf')
const path = require('path')
const OrbitDB = require('../src/OrbitDB')
// Include test utilities
const {
config,
startIpfs,
stopIpfs,
testAPIs,
databases
} = require('./utils')
const dbPath = './orbitdb/tests/write-permissions'
const ipfsPath = './orbitdb/tests/write-permissions/ipfs'
Object.keys(testAPIs).forEach(API => {
describe(`orbit-db - Write Permissions (${API})`, function() {
this.timeout(20000)
let ipfsd, ipfs, orbitdb1, orbitdb2
before(async () => {
config.daemon1.repo = ipfsPath
rmrf.sync(config.daemon1.repo)
rmrf.sync(dbPath)
ipfsd = await startIpfs(API, config.daemon1)
ipfs = ipfsd.api
orbitdb1 = await OrbitDB.createInstance(ipfs, { directory: path.join(dbPath, '1') })
orbitdb2 = await OrbitDB.createInstance(ipfs, { directory: path.join(dbPath, '2') })
})
after(async () => {
if(orbitdb1)
await orbitdb1.stop()
if(orbitdb2)
await orbitdb2.stop()
if (ipfsd)
await stopIpfs(ipfsd)
})
describe('allows multiple peers to write to the databases', function() {
databases.forEach(async (database) => {
it(database.type + ' allows multiple writers', async () => {
let options = {
// Set write access for both clients
accessController: {
write: [
orbitdb1.identity.id,
orbitdb2.identity.id
]
}
}
const db1 = await database.create(orbitdb1, 'sync-test', options)
options = Object.assign({}, options, { sync: true })
const db2 = await database.create(orbitdb2, db1.address.toString(), options)
await database.tryInsert(db1)
await database.tryInsert(db2)
assert.deepEqual(database.getTestValue(db1), database.expectedValue)
assert.deepEqual(database.getTestValue(db2), database.expectedValue)
await db1.close()
await db2.close()
})
})
})
describe('syncs databases', function() {
databases.forEach(async (database) => {
it(database.type + ' syncs', async () => {
let options = {
// Set write access for both clients
accessController: {
write: [
orbitdb1.identity.id,
orbitdb2.identity.id
]
}
}
const db1 = await database.create(orbitdb1, 'sync-test', options)
options = Object.assign({}, options, { sync: true })
const db2 = await database.create(orbitdb2, db1.address.toString(), options)
await database.tryInsert(db2)
assert.strictEqual(database.query(db1).length, 0)
db1.sync(db2._oplog.heads)
return new Promise(resolve => {
setTimeout(async () => {
const value = database.getTestValue(db1)
assert.deepEqual(value, database.expectedValue)
await db1.close()
await db2.close()
resolve()
}, 300)
})
})
})
})
describe('syncs databases that anyone can write to', function() {
databases.forEach(async (database) => {
it(database.type + ' syncs', async () => {
let options = {
// Set write permission for everyone
accessController: {
write: ['*']
}
}
const db1 = await database.create(orbitdb1, 'sync-test-public-dbs', options)
options = Object.assign({}, options, { sync: true })
const db2 = await database.create(orbitdb2, db1.address.toString(), options)
await database.tryInsert(db2)
assert.strictEqual(database.query(db1).length, 0)
db1.sync(db2._oplog.heads)
return new Promise(resolve => {
setTimeout(async () => {
const value = database.getTestValue(db1)
assert.deepEqual(value, database.expectedValue)
await db1.close()
await db2.close()
resolve()
}, 300)
})
})
})
})
describe('doesn\'t sync if peer is not allowed to write to the database', function() {
databases.forEach(async (database) => {
it(database.type + ' doesn\'t sync', async () => {
let options = {
// Only peer 1 can write
accessController: {
write: [orbitdb1.identity.id]
}
}
let err
options = Object.assign({}, options, { path: path.join(dbPath, '/sync-test/1') })
const db1 = await database.create(orbitdb1, 'write error test 1', options)
options = Object.assign({}, options, { path: path.join(dbPath, '/sync-test/2'), sync: true })
const db2 = await database.create(orbitdb2, 'write error test 1', options)
try {
// Catch replication event if the update from peer 2 got synced and into the database
db1.events.on('replicated', () => err = new Error('Shouldn\'t replicate!'))
// Try to update from peer 2, this shouldn't be allowed
await database.tryInsert(db2)
} catch (e) {
// Make sure peer 2's instance throws an error
err = e.toString()
}
assert.strictEqual(err, `Error: Could not append entry, key "${orbitdb2.identity.id}" is not allowed to write to the log`)
// Make sure nothing was added to the database
assert.strictEqual(database.query(db1).length, 0)
// Try to sync peer 1 with peer 2, this shouldn't produce anything
// at peer 1 (nothing was supposed to be added to the database by peer 2)
db1.sync(db2._oplog.heads)
return new Promise((resolve, reject) => {
setTimeout(async () => {
// Make sure nothing was added
assert.strictEqual(database.query(db1).length, 0)
await db1.close()
await db2.close()
if (!err) {
reject(new Error('tryInsert should throw an err'))
} else {
resolve()
}
}, 300)
})
})
})
})
describe('throws an error if peer is not allowed to write to the database', function() {
databases.forEach(async (database) => {
it(database.type + ' throws an error', async () => {
let options = {
// No write access (only creator of the database can write)
accessController: {
write: []
}
}
let err
try {
const db1 = await database.create(orbitdb1, 'write error test 2', options)
options = Object.assign({}, options, { sync: true })
const db2 = await database.create(orbitdb2, db1.address.toString(), options)
await database.tryInsert(db2)
} catch (e) {
err = e.toString()
}
assert.strictEqual(err, `Error: Could not append entry, key "${orbitdb2.identity.id}" is not allowed to write to the log`)
})
})
})
})
})