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

Commit 46d2130

Browse files
implement list and rm api and tests
1 parent 17ea0e3 commit 46d2130

File tree

2 files changed

+178
-38
lines changed

2 files changed

+178
-38
lines changed

src/api/bootstrap.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
'use strict'
22

3-
// const command = require('../cmd-helpers').command
4-
// const argCommand = require('../cmd-helpers').argCommand
3+
const command = require('../cmd-helpers').command
54

65
module.exports = (send) => {
76
return {
8-
add: function add (address, opts, cb) {
7+
add: (arg, opts, cb) => {
98
if (typeof opts === 'function' && cb === undefined) {
109
cb = opts
1110
opts = {}
1211
}
13-
// var send = function (path, args, qs, files, buffer, cb) {
14-
return send('bootstrap/add', address, opts, null, cb)
15-
}
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')
1622
}
1723
}

test/api/bootstrap.spec.js

Lines changed: 166 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,187 @@
33
'use strict'
44

55
const expect = require('chai').expect
6-
// const multiaddr = require('multiaddr')
76

8-
describe('.bootstrap', () => {
9-
const emptyArg = ''
10-
const invalidArg = 'QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ'
11-
const validIp4 = '/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z'
7+
const invalidArg = 'this/Is/So/Invalid/'
8+
const validIp4 = '/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z'
129

10+
describe('.bootstrap', () => {
1311
let peers
1412

15-
it('add returns an error when not passing an arg', (done) => {
16-
return apiClients.a.bootstrap.add(null, (err) => {
17-
expect(err).to.be.an.instanceof(Error)
18-
done()
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 peers 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+
})
1937
})
20-
})
2138

22-
it('add returns an error when passing an invalid arg', (done) => {
23-
return apiClients.a.bootstrap.add(invalidArg, (err) => {
24-
expect(err).to.be.an.instanceof(Error)
25-
done()
39+
it('returns a list of default 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+
})
2647
})
2748
})
2849

29-
it('add returns an error when passing an empty arg', (done) => {
30-
return apiClients.a.bootstrap.add(emptyArg, (err) => {
31-
expect(err).to.be.an.instanceof(Error)
32-
done()
50+
describe('.list', () => {
51+
it('returns a list of peers', (done) => {
52+
return apiClients.a.bootstrap.list(null, (err, res) => {
53+
expect(err).to.not.exist
54+
peers = res.Peers
55+
expect(peers).to.exist
56+
done()
57+
})
3358
})
3459
})
3560

36-
it('adds a valid address (ip4)', (done) => {
37-
return apiClients.a.bootstrap.add(validIp4, (err, res) => {
38-
expect(err).to.not.exist
39-
expect(res).to.be.eql({ Peers: [validIp4] })
40-
peers = res.Peers
41-
expect(peers).to.exist
42-
expect(peers.length).to.eql(1)
43-
done()
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 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+
})
4496
})
4597
})
4698

47-
it('add returns a list of default peers when default arg passed', (done) => {
48-
return apiClients.a.bootstrap.add(null, { default: true }, (err, res) => {
49-
expect(err).to.not.exist
50-
peers = res.Peers
51-
expect(peers).to.exist
52-
done()
99+
describe('.promise', () => {
100+
describe('.add', () => {
101+
it('returns an error when called without args or options', (done) => {
102+
return apiClients.a.bootstrap.add(null)
103+
.catch((err) => {
104+
expect(err).to.be.an.instanceof(Error)
105+
done()
106+
})
107+
})
108+
109+
it('returns an error when called with an invalid arg', (done) => {
110+
return apiClients.a.bootstrap.add(invalidArg)
111+
.catch((err) => {
112+
expect(err).to.be.an.instanceof(Error)
113+
done()
114+
})
115+
})
116+
117+
it('returns a list of peers when called with a valid arg (ip4)', (done) => {
118+
return apiClients.a.bootstrap.add(validIp4)
119+
.then((res) => {
120+
expect(res).to.be.eql({ Peers: [validIp4] })
121+
peers = res.Peers
122+
expect(peers).to.exist
123+
expect(peers.length).to.eql(1)
124+
done()
125+
})
126+
})
127+
128+
it('returns a list of default peers when called with the default option', (done) => {
129+
return apiClients.a.bootstrap.add(null, { default: true })
130+
.then((res) => {
131+
peers = res.Peers
132+
expect(peers).to.exist
133+
expect(peers.length).to.above(1)
134+
done()
135+
})
136+
})
137+
})
138+
139+
describe('.list', () => {
140+
it('returns a list of peers', (done) => {
141+
return apiClients.a.bootstrap.list(null)
142+
.then((res) => {
143+
peers = res.Peers
144+
expect(peers).to.exist
145+
done()
146+
})
147+
})
148+
})
149+
150+
describe('.rm', () => {
151+
it('returns an error when called with an invalid arg', (done) => {
152+
return apiClients.a.bootstrap.rm(invalidArg)
153+
.catch((err) => {
154+
expect(err).to.be.an.instanceof(Error)
155+
done()
156+
})
157+
})
158+
159+
it('returns empty list when called without an arg or options', (done) => {
160+
return apiClients.a.bootstrap.rm(null)
161+
.then((res) => {
162+
peers = res.Peers
163+
expect(peers).to.exist
164+
expect(peers.length).to.eql(0)
165+
done()
166+
})
167+
})
168+
169+
it('returns list containing the peer removed when called with a valid arg (ip4)', (done) => {
170+
return apiClients.a.bootstrap.rm(null)
171+
.then((res) => {
172+
peers = res.Peers
173+
expect(peers).to.exist
174+
expect(peers.length).to.eql(0)
175+
done()
176+
})
177+
})
178+
179+
it('returns list of all peers removed when all option is passed', (done) => {
180+
return apiClients.a.bootstrap.rm(null, { all: true })
181+
.then((res) => {
182+
peers = res.Peers
183+
expect(peers).to.exist
184+
done()
185+
})
186+
})
53187
})
54188
})
55189
})

0 commit comments

Comments
 (0)