Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
test: add test to name-pubsub: should handle event on publish correct…
Browse files Browse the repository at this point in the history
…ly (#2208)
  • Loading branch information
MicrowaveDev authored and Alan Shaw committed Jul 17, 2019
1 parent 58a9bc4 commit 298e541
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions test/core/name-pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const parallel = require('async/parallel')
const retry = require('async/retry')
const series = require('async/series')

const peerId = require('peer-id')
const isNode = require('detect-node')
const ipns = require('ipns')
const IPFS = require('../../src')
Expand Down Expand Up @@ -166,4 +167,62 @@ describe('name-pubsub', function () {
const resolveA = await nodeA.name.resolve(idB.id)
expect(resolveA).to.be.eq(`/ipfs/${path}`)
})

it('should handle event on publish correctly', function (done) {
this.timeout(80 * 1000)

const testAccountName = 'test-account'

let publishedMessage = null
let publishedMessageData = null
let publishedMessageDataValue = null

function checkMessage (msg) {
publishedMessage = msg
publishedMessageData = ipns.unmarshal(msg.data)
publishedMessageDataValue = publishedMessageData.value.toString('utf8')
}

const alreadySubscribed = (cb) => {
return cb(null, publishedMessage !== null)
}

// Create account for publish
nodeA.key.gen(testAccountName, {
type: 'rsa',
size: 2048
}, (err, testAccount) => {
expect(err).to.not.exist()

const keys = ipns.getIdKeys(fromB58String(testAccount.id))
const topic = `${namespace}${base64url.encode(keys.routingKey.toBuffer())}`

series([
(cb) => nodeB.pubsub.subscribe(topic, checkMessage, cb),
(cb) => nodeA.name.publish(ipfsRef, { resolve: false, key: testAccountName }, cb),
(cb) => waitFor((callback) => alreadySubscribed(callback), cb),
(cb) => peerId.createFromPubKey(publishedMessage.key, cb),
(cb) => peerId.createFromPubKey(publishedMessageData.pubKey, cb)
], (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()

const messageKey = res[3]
const pubKeyPeerId = res[4]

expect(pubKeyPeerId.toB58String()).not.to.equal(messageKey.toB58String())

expect(pubKeyPeerId.toB58String()).to.equal(testAccount.id)
expect(publishedMessage.from).to.equal(idA.id)
expect(messageKey.toB58String()).to.equal(idA.id)
expect(publishedMessageDataValue).to.equal(ipfsRef)

// Verify the signature
ipns.validate(pubKeyPeerId._pubKey, publishedMessageData, (err) => {
expect(err).to.not.exist()
done()
})
})
})
})
})

0 comments on commit 298e541

Please sign in to comment.