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

Commit 59e6656

Browse files
authored
Merge pull request #298 from gavinmcdermott/feat/implement_bootstrap
Enhancement: Implement Bootstrap API
2 parents 7af117d + 2a62e1e commit 59e6656

File tree

4 files changed

+204
-1
lines changed

4 files changed

+204
-1
lines changed

src/api/bootstrap.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict'
2+
3+
const command = require('../cmd-helpers').command
4+
5+
module.exports = (send) => {
6+
return {
7+
add: (arg, opts, cb) => {
8+
if (typeof opts === 'function' && cb === undefined) {
9+
cb = opts
10+
opts = {}
11+
}
12+
return send('bootstrap/add', arg, opts, null, cb)
13+
},
14+
rm: (arg, opts, cb) => {
15+
if (typeof opts === 'function' && cb === undefined) {
16+
cb = opts
17+
opts = {}
18+
}
19+
return send('bootstrap/rm', arg, opts, null, cb)
20+
},
21+
list: command(send, 'bootstrap/list')
22+
}
23+
}

src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const multiaddr = require('multiaddr')
4-
54
const loadCommands = require('./load-commands')
65
const getConfig = require('./config')
76
const getRequestAPI = require('./request-api')

src/load-commands.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ function requireCommands () {
44
const cmds = {
55
bitswap: require('./api/bitswap'),
66
block: require('./api/block'),
7+
bootstrap: require('./api/bootstrap'),
78
cat: require('./api/cat'),
89
commands: require('./api/commands'),
910
config: require('./api/config'),

test/api/bootstrap.spec.js

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/* eslint-env mocha */
2+
/* globals apiClients */
3+
'use strict'
4+
5+
const expect = require('chai').expect
6+
7+
const invalidArg = 'this/Is/So/Invalid/'
8+
const validIp4 = '/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z'
9+
10+
describe('.bootstrap', () => {
11+
let peers
12+
13+
describe('.add', () => {
14+
it('returns an error when called without args or options', (done) => {
15+
return apiClients.a.bootstrap.add(null, (err) => {
16+
expect(err).to.be.an.instanceof(Error)
17+
done()
18+
})
19+
})
20+
21+
it('returns an error when called with an invalid arg', (done) => {
22+
return apiClients.a.bootstrap.add(invalidArg, (err) => {
23+
expect(err).to.be.an.instanceof(Error)
24+
done()
25+
})
26+
})
27+
28+
it('returns a list of containing the bootstrap peer when called with a valid arg (ip4)', (done) => {
29+
return apiClients.a.bootstrap.add(validIp4, (err, res) => {
30+
expect(err).to.not.exist
31+
expect(res).to.be.eql({ Peers: [validIp4] })
32+
peers = res.Peers
33+
expect(peers).to.exist
34+
expect(peers.length).to.eql(1)
35+
done()
36+
})
37+
})
38+
39+
it('returns a list of bootstrap peers when called with the default option', (done) => {
40+
return apiClients.a.bootstrap.add(null, { default: true }, (err, res) => {
41+
expect(err).to.not.exist
42+
peers = res.Peers
43+
expect(peers).to.exist
44+
expect(peers.length).to.above(1)
45+
done()
46+
})
47+
})
48+
})
49+
50+
describe('.list', () => {
51+
it('returns a list of peers', (done) => {
52+
return apiClients.a.bootstrap.list((err, res) => {
53+
expect(err).to.not.exist
54+
peers = res.Peers
55+
expect(peers).to.exist
56+
done()
57+
})
58+
})
59+
})
60+
61+
describe('.rm', () => {
62+
it('returns an error when called with an invalid arg', (done) => {
63+
return apiClients.a.bootstrap.rm(invalidArg, (err) => {
64+
expect(err).to.be.an.instanceof(Error)
65+
done()
66+
})
67+
})
68+
69+
it('returns empty list because no peers removed when called without an arg or options', (done) => {
70+
return apiClients.a.bootstrap.rm(null, (err, res) => {
71+
expect(err).to.not.exist
72+
peers = res.Peers
73+
expect(peers).to.exist
74+
expect(peers.length).to.eql(0)
75+
done()
76+
})
77+
})
78+
79+
it('returns list containing the peer removed when called with a valid arg (ip4)', (done) => {
80+
return apiClients.a.bootstrap.rm(null, (err, res) => {
81+
expect(err).to.not.exist
82+
peers = res.Peers
83+
expect(peers).to.exist
84+
expect(peers.length).to.eql(0)
85+
done()
86+
})
87+
})
88+
89+
it('returns list of all peers removed when all option is passed', (done) => {
90+
return apiClients.a.bootstrap.rm(null, { all: true }, (err, res) => {
91+
expect(err).to.not.exist
92+
peers = res.Peers
93+
expect(peers).to.exist
94+
done()
95+
})
96+
})
97+
})
98+
99+
describe('.promise', () => {
100+
describe('.add', () => {
101+
it('returns an error when called without args or options', () => {
102+
return apiClients.a.bootstrap.add(null)
103+
.catch((err) => {
104+
expect(err).to.be.an.instanceof(Error)
105+
})
106+
})
107+
108+
it('returns an error when called with an invalid arg', () => {
109+
return apiClients.a.bootstrap.add(invalidArg)
110+
.catch((err) => {
111+
expect(err).to.be.an.instanceof(Error)
112+
})
113+
})
114+
115+
it('returns a list of peers when called with a valid arg (ip4)', () => {
116+
return apiClients.a.bootstrap.add(validIp4)
117+
.then((res) => {
118+
expect(res).to.be.eql({ Peers: [validIp4] })
119+
peers = res.Peers
120+
expect(peers).to.exist
121+
expect(peers.length).to.eql(1)
122+
})
123+
})
124+
125+
it('returns a list of default peers when called with the default option', () => {
126+
return apiClients.a.bootstrap.add(null, { default: true })
127+
.then((res) => {
128+
peers = res.Peers
129+
expect(peers).to.exist
130+
expect(peers.length).to.above(1)
131+
})
132+
})
133+
})
134+
135+
describe('.list', () => {
136+
it('returns a list of peers', () => {
137+
return apiClients.a.bootstrap.list()
138+
.then((res) => {
139+
peers = res.Peers
140+
expect(peers).to.exist
141+
})
142+
})
143+
})
144+
145+
describe('.rm', () => {
146+
it('returns an error when called with an invalid arg', () => {
147+
return apiClients.a.bootstrap.rm(invalidArg)
148+
.catch((err) => {
149+
expect(err).to.be.an.instanceof(Error)
150+
})
151+
})
152+
153+
it('returns empty list when called without an arg or options', () => {
154+
return apiClients.a.bootstrap.rm(null)
155+
.then((res) => {
156+
peers = res.Peers
157+
expect(peers).to.exist
158+
expect(peers.length).to.eql(0)
159+
})
160+
})
161+
162+
it('returns list containing the peer removed when called with a valid arg (ip4)', () => {
163+
return apiClients.a.bootstrap.rm(null)
164+
.then((res) => {
165+
peers = res.Peers
166+
expect(peers).to.exist
167+
expect(peers.length).to.eql(0)
168+
})
169+
})
170+
171+
it('returns list of all peers removed when all option is passed', () => {
172+
return apiClients.a.bootstrap.rm(null, { all: true })
173+
.then((res) => {
174+
peers = res.Peers
175+
expect(peers).to.exist
176+
})
177+
})
178+
})
179+
})
180+
})

0 commit comments

Comments
 (0)