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

Commit 2d7071c

Browse files
vasco-santosAlan Shaw
authored and
Alan Shaw
committed
test: ipns over pubsub (#361)
1 parent 59f30cd commit 2d7071c

File tree

6 files changed

+312
-0
lines changed

6 files changed

+312
-0
lines changed

SPEC/NAME.md

+86
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Name API
22

33
* [name.publish](#namepublish)
4+
* [name.pubsub.cancel](#namepubsubcancel)
5+
* [name.pubsub.state](#namepubsubstate)
6+
* [name.pubsub.subs](#namepubsubsubs)
47
* [name.resolve](#nameresolve)
58

69
#### `name.publish`
@@ -55,6 +58,89 @@ ipfs.name.publish(addr, function (err, res) {
5558

5659
This way, you can republish a new version of your website under the same address. By default, `ipfs.name.publish` will use the Peer ID. If you want to have multiple websites (for example) under the same IPFS module, you can always check the [key API](./KEY.md).
5760

61+
#### `name.pubsub.cancel`
62+
63+
> Cancel a name subscription.
64+
65+
##### `Go` **WIP**
66+
67+
##### `JavaScript` - ipfs.name.pubsub.cancel(arg, [callback])
68+
69+
`arg` is the name of the subscription to cancel.
70+
71+
`callback` must follow `function (err, result) {}` signature, where `err` is an error if the operation was not successful. `result` is an object that contains the result of the operation, such as:
72+
73+
```JavaScript
74+
{
75+
canceled: true
76+
}
77+
```
78+
79+
If no `callback` is passed, a promise is returned.
80+
81+
**Example:**
82+
83+
```JavaScript
84+
const name = 'QmQrX8hka2BtNHa8N8arAq16TCVx5qHcb46c5yPewRycLm'
85+
86+
ipfs.name.pubsub.cancel(name, function (err, result) {
87+
console.log(result.canceled)
88+
// true
89+
})
90+
```
91+
92+
#### `name.pubsub.state`
93+
94+
> Query the state of IPNS pubsub.
95+
96+
##### `Go` **WIP**
97+
98+
##### `JavaScript` - ipfs.name.pubsub.state([callback])
99+
100+
`callback` must follow `function (err, result) {}` signature, where `err` is an error if the operation was not successful. `result` is an object that contains the result of the operation, such as:
101+
102+
```JavaScript
103+
{
104+
enabled: true
105+
}
106+
```
107+
108+
If no `callback` is passed, a promise is returned.
109+
110+
**Example:**
111+
112+
```JavaScript
113+
ipfs.name.pubsub.state(function (err, result) {
114+
console.log(result.enabled)
115+
// true
116+
})
117+
```
118+
119+
#### `name.pubsub.subs`
120+
121+
> Show current name subscriptions.
122+
123+
##### `Go` **WIP**
124+
125+
##### `JavaScript` - ipfs.name.pubsub.subs([callback])
126+
127+
`callback` must follow `function (err, result) {}` signature, where `err` is an error if the operation was not successful. `result` is an array of subscriptions, such as:
128+
129+
```JavaScript
130+
['/ipns/QmQrX8hka2BtNHa8N8arAq16TCVx5qHcb46c5yPewRycLm']
131+
```
132+
133+
If no `callback` is passed, a promise is returned.
134+
135+
**Example:**
136+
137+
```JavaScript
138+
ipfs.name.pubsub.subs(function (err, result) {
139+
console.log(result)
140+
// ['/ipns/QmQrX8hka2BtNHa8N8arAq16TCVx5qHcb46c5yPewRycLm']
141+
})
142+
```
143+
58144
#### `name.resolve`
59145

60146
> Resolve an IPNS name.

js/src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ exports.key = require('./key')
1111
exports.ls = require('./ls')
1212
exports.miscellaneous = require('./miscellaneous')
1313
exports.name = require('./name')
14+
exports.namePubsub = require('./name-pubsub')
1415
exports.object = require('./object')
1516
exports.pin = require('./pin')
1617
exports.ping = require('./ping')

js/src/name-pubsub/cancel.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* eslint max-nested-callbacks: ["error", 5] */
2+
/* eslint-env mocha */
3+
'use strict'
4+
5+
const series = require('async/series')
6+
const loadFixture = require('aegir/fixtures')
7+
8+
const { spawnNodeWithId } = require('../utils/spawn')
9+
const { getDescribe, getIt, expect } = require('../utils/mocha')
10+
11+
const fixture = Object.freeze({
12+
data: loadFixture('js/test/fixtures/testfile.txt', 'interface-ipfs-core')
13+
})
14+
15+
module.exports = (createCommon, options) => {
16+
const describe = getDescribe(options)
17+
const it = getIt(options)
18+
const common = createCommon()
19+
20+
describe('.name.pubsub.cancel', function () {
21+
let ipfs
22+
let nodeId
23+
let value
24+
25+
before(function (done) {
26+
// CI takes longer to instantiate the daemon, so we need to increase the
27+
// timeout for the before step
28+
this.timeout(60 * 1000)
29+
30+
common.setup((err, factory) => {
31+
expect(err).to.not.exist()
32+
33+
spawnNodeWithId(factory, (err, node) => {
34+
expect(err).to.not.exist()
35+
36+
ipfs = node
37+
nodeId = node.peerId.id
38+
39+
ipfs.files.add(fixture.data, { pin: false }, (err, res) => {
40+
expect(err).to.not.exist()
41+
42+
value = res[0].path
43+
done()
44+
})
45+
})
46+
})
47+
})
48+
49+
after((done) => common.teardown(done))
50+
51+
it('should return false when the name that is intended to cancel is not subscribed', function (done) {
52+
this.timeout(60 * 1000)
53+
54+
ipfs.name.pubsub.cancel(nodeId, (err, res) => {
55+
expect(err).to.not.exist()
56+
expect(res).to.exist()
57+
expect(res).to.have.property('canceled')
58+
expect(res.canceled).to.eql(false)
59+
60+
done()
61+
})
62+
})
63+
64+
it('should cancel a subscription correctly returning true', function (done) {
65+
this.timeout(300 * 1000)
66+
const ipnsPath = `/ipns/${nodeId}`
67+
68+
series([
69+
(cb) => ipfs.name.pubsub.subs(cb),
70+
(cb) => ipfs.name.publish(value, { resolve: false }, cb),
71+
(cb) => ipfs.name.resolve(nodeId, cb),
72+
(cb) => ipfs.name.pubsub.subs(cb),
73+
(cb) => ipfs.name.pubsub.cancel(ipnsPath, cb),
74+
(cb) => ipfs.name.pubsub.subs(cb)
75+
], (err, res) => {
76+
expect(err).to.not.exist()
77+
expect(res).to.exist()
78+
expect(res[0]).to.eql([]) // initally empty
79+
expect(res[4]).to.have.property('canceled')
80+
expect(res[4].canceled).to.eql(true)
81+
expect(res[5]).to.be.an('array').that.does.not.include(ipnsPath)
82+
83+
done()
84+
})
85+
})
86+
})
87+
}

js/src/name-pubsub/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
const { createSuite } = require('../utils/suite')
3+
4+
const tests = {
5+
cancel: require('./cancel'),
6+
state: require('./state'),
7+
subs: require('./subs')
8+
}
9+
10+
module.exports = createSuite(tests)

js/src/name-pubsub/state.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const { spawnNodeWithId } = require('../utils/spawn')
5+
const { getDescribe, getIt, expect } = require('../utils/mocha')
6+
7+
module.exports = (createCommon, options) => {
8+
const describe = getDescribe(options)
9+
const it = getIt(options)
10+
const common = createCommon()
11+
12+
describe('.name.pubsub.state', function () {
13+
let ipfs
14+
15+
before(function (done) {
16+
// CI takes longer to instantiate the daemon, so we need to increase the
17+
// timeout for the before step
18+
this.timeout(60 * 1000)
19+
20+
common.setup((err, factory) => {
21+
expect(err).to.not.exist()
22+
23+
spawnNodeWithId(factory, (err, node) => {
24+
expect(err).to.not.exist()
25+
26+
ipfs = node
27+
done()
28+
})
29+
})
30+
})
31+
32+
after((done) => common.teardown(done))
33+
34+
it('should get the current state of pubsub', function (done) {
35+
this.timeout(50 * 1000)
36+
37+
ipfs.name.pubsub.state((err, res) => {
38+
expect(err).to.not.exist()
39+
expect(res).to.exist()
40+
expect(res).to.have.property('enabled')
41+
expect(res.enabled).to.be.eql(true)
42+
43+
done()
44+
})
45+
})
46+
})
47+
}

js/src/name-pubsub/subs.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/* eslint max-nested-callbacks: ["error", 5] */
2+
/* eslint-env mocha */
3+
'use strict'
4+
5+
const series = require('async/series')
6+
const loadFixture = require('aegir/fixtures')
7+
8+
const { spawnNodeWithId } = require('../utils/spawn')
9+
const { getDescribe, getIt, expect } = require('../utils/mocha')
10+
11+
const fixture = Object.freeze({
12+
data: loadFixture('js/test/fixtures/testfile.txt', 'interface-ipfs-core')
13+
})
14+
15+
module.exports = (createCommon, options) => {
16+
const describe = getDescribe(options)
17+
const it = getIt(options)
18+
const common = createCommon()
19+
20+
describe('.name.pubsub.subs', function () {
21+
let ipfs
22+
let nodeId
23+
let value
24+
25+
before(function (done) {
26+
// CI takes longer to instantiate the daemon, so we need to increase the
27+
// timeout for the before step
28+
this.timeout(60 * 1000)
29+
30+
common.setup((err, factory) => {
31+
expect(err).to.not.exist()
32+
33+
spawnNodeWithId(factory, (err, node) => {
34+
expect(err).to.not.exist()
35+
36+
ipfs = node
37+
nodeId = node.peerId.id
38+
39+
ipfs.files.add(fixture.data, { pin: false }, (err, res) => {
40+
expect(err).to.not.exist()
41+
42+
value = res[0].path
43+
done()
44+
})
45+
})
46+
})
47+
})
48+
49+
after((done) => common.teardown(done))
50+
51+
it('should get an empty array as a result of subscriptions before any resolve', function (done) {
52+
this.timeout(60 * 1000)
53+
54+
ipfs.name.pubsub.subs((err, res) => {
55+
expect(err).to.not.exist()
56+
expect(res).to.exist()
57+
expect(res).to.eql([])
58+
59+
done()
60+
})
61+
})
62+
63+
it('should get the list of subscriptions updated after a resolve', function (done) {
64+
this.timeout(300 * 1000)
65+
66+
series([
67+
(cb) => ipfs.name.pubsub.subs(cb),
68+
(cb) => ipfs.name.publish(value, { resolve: false }, cb),
69+
(cb) => ipfs.name.resolve(nodeId, cb),
70+
(cb) => ipfs.name.pubsub.subs(cb)
71+
], (err, res) => {
72+
expect(err).to.not.exist()
73+
expect(res).to.exist()
74+
expect(res[0]).to.eql([]) // initally empty
75+
expect(res[3]).to.be.an('array').that.does.include(`/ipns/${nodeId}`)
76+
77+
done()
78+
})
79+
})
80+
})
81+
}

0 commit comments

Comments
 (0)