From 2a62e1e88fff2ebea8458d9bea39e0d5cf36bbe6 Mon Sep 17 00:00:00 2001 From: Gavin McDermott Date: Thu, 16 Jun 2016 13:37:40 -0700 Subject: [PATCH] implement bootstrap api and tests --- src/api/bootstrap.js | 23 +++++ src/index.js | 1 - src/load-commands.js | 1 + test/api/bootstrap.spec.js | 180 +++++++++++++++++++++++++++++++++++++ 4 files changed, 204 insertions(+), 1 deletion(-) create mode 100644 src/api/bootstrap.js create mode 100644 test/api/bootstrap.spec.js diff --git a/src/api/bootstrap.js b/src/api/bootstrap.js new file mode 100644 index 000000000..1c8ee0d59 --- /dev/null +++ b/src/api/bootstrap.js @@ -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') + } +} diff --git a/src/index.js b/src/index.js index e47af2fd3..a150458d3 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,6 @@ 'use strict' const multiaddr = require('multiaddr') - const loadCommands = require('./load-commands') const getConfig = require('./config') const getRequestAPI = require('./request-api') diff --git a/src/load-commands.js b/src/load-commands.js index 3771528dd..1dcd32249 100644 --- a/src/load-commands.js +++ b/src/load-commands.js @@ -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'), diff --git a/test/api/bootstrap.spec.js b/test/api/bootstrap.spec.js new file mode 100644 index 000000000..5728d0dbd --- /dev/null +++ b/test/api/bootstrap.spec.js @@ -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) => { + 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 + }) + }) + }) + }) +})