Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Enhancement: Implement Bootstrap API #298

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/api/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const command = require('../cmd-helpers').command

module.exports = (send) => {
return {
add: (arg, opts, cb) => {
if (typeof opts === 'function' && cb === undefined) {
cb = opts
opts = {}
}
return send('bootstrap/add', arg, opts, null, cb)
},
rm: (arg, opts, cb) => {
if (typeof opts === 'function' && cb === undefined) {
cb = opts
opts = {}
}
return send('bootstrap/rm', arg, opts, null, cb)
},
list: command(send, 'bootstrap/list')
}
}
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const multiaddr = require('multiaddr')

const loadCommands = require('./load-commands')
const getConfig = require('./config')
const getRequestAPI = require('./request-api')
Expand Down
1 change: 1 addition & 0 deletions src/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ function requireCommands () {
const cmds = {
bitswap: require('./api/bitswap'),
block: require('./api/block'),
bootstrap: require('./api/bootstrap'),
cat: require('./api/cat'),
commands: require('./api/commands'),
config: require('./api/config'),
Expand Down
180 changes: 180 additions & 0 deletions test/api/bootstrap.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/* eslint-env mocha */
/* globals apiClients */
'use strict'

const expect = require('chai').expect

const invalidArg = 'this/Is/So/Invalid/'
const validIp4 = '/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z'

describe('.bootstrap', () => {
let peers

describe('.add', () => {
it('returns an error when called without args or options', (done) => {
return apiClients.a.bootstrap.add(null, (err) => {
expect(err).to.be.an.instanceof(Error)
done()
})
})

it('returns an error when called with an invalid arg', (done) => {
return apiClients.a.bootstrap.add(invalidArg, (err) => {
expect(err).to.be.an.instanceof(Error)
done()
})
})

it('returns a list of containing the bootstrap peer when called with a valid arg (ip4)', (done) => {
return apiClients.a.bootstrap.add(validIp4, (err, res) => {
expect(err).to.not.exist
expect(res).to.be.eql({ Peers: [validIp4] })
peers = res.Peers
expect(peers).to.exist
expect(peers.length).to.eql(1)
done()
})
})

it('returns a list of bootstrap peers when called with the default option', (done) => {
return apiClients.a.bootstrap.add(null, { default: true }, (err, res) => {
expect(err).to.not.exist
peers = res.Peers
expect(peers).to.exist
expect(peers.length).to.above(1)
done()
})
})
})

describe('.list', () => {
it('returns a list of peers', (done) => {
return apiClients.a.bootstrap.list((err, res) => {
expect(err).to.not.exist
peers = res.Peers
expect(peers).to.exist
done()
})
})
})

describe('.rm', () => {
it('returns an error when called with an invalid arg', (done) => {
return apiClients.a.bootstrap.rm(invalidArg, (err) => {
expect(err).to.be.an.instanceof(Error)
done()
})
})

it('returns empty list because no peers removed when called without an arg or options', (done) => {
return apiClients.a.bootstrap.rm(null, (err, res) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean 'delete all'?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. According to the spec there is an explicit 'delete all' call—you just have to pass an all: true option in order to remove all bootstrap peers. The returned array contains the list of peers that were removed in the call (I'll also make the it string a better description).

expect(err).to.not.exist
peers = res.Peers
expect(peers).to.exist
expect(peers.length).to.eql(0)
done()
})
})

it('returns list containing the peer removed when called with a valid arg (ip4)', (done) => {
return apiClients.a.bootstrap.rm(null, (err, res) => {
expect(err).to.not.exist
peers = res.Peers
expect(peers).to.exist
expect(peers.length).to.eql(0)
done()
})
})

it('returns list of all peers removed when all option is passed', (done) => {
return apiClients.a.bootstrap.rm(null, { all: true }, (err, res) => {
expect(err).to.not.exist
peers = res.Peers
expect(peers).to.exist
done()
})
})
})

describe('.promise', () => {
describe('.add', () => {
it('returns an error when called without args or options', () => {
return apiClients.a.bootstrap.add(null)
.catch((err) => {
expect(err).to.be.an.instanceof(Error)
})
})

it('returns an error when called with an invalid arg', () => {
return apiClients.a.bootstrap.add(invalidArg)
.catch((err) => {
expect(err).to.be.an.instanceof(Error)
})
})

it('returns a list of peers when called with a valid arg (ip4)', () => {
return apiClients.a.bootstrap.add(validIp4)
.then((res) => {
expect(res).to.be.eql({ Peers: [validIp4] })
peers = res.Peers
expect(peers).to.exist
expect(peers.length).to.eql(1)
})
})

it('returns a list of default peers when called with the default option', () => {
return apiClients.a.bootstrap.add(null, { default: true })
.then((res) => {
peers = res.Peers
expect(peers).to.exist
expect(peers.length).to.above(1)
})
})
})

describe('.list', () => {
it('returns a list of peers', () => {
return apiClients.a.bootstrap.list()
.then((res) => {
peers = res.Peers
expect(peers).to.exist
})
})
})

describe('.rm', () => {
it('returns an error when called with an invalid arg', () => {
return apiClients.a.bootstrap.rm(invalidArg)
.catch((err) => {
expect(err).to.be.an.instanceof(Error)
})
})

it('returns empty list when called without an arg or options', () => {
return apiClients.a.bootstrap.rm(null)
.then((res) => {
peers = res.Peers
expect(peers).to.exist
expect(peers.length).to.eql(0)
})
})

it('returns list containing the peer removed when called with a valid arg (ip4)', () => {
return apiClients.a.bootstrap.rm(null)
.then((res) => {
peers = res.Peers
expect(peers).to.exist
expect(peers.length).to.eql(0)
})
})

it('returns list of all peers removed when all option is passed', () => {
return apiClients.a.bootstrap.rm(null, { all: true })
.then((res) => {
peers = res.Peers
expect(peers).to.exist
})
})
})
})
})