mirror of
https://github.com/fluencelabs/js-libp2p-interfaces
synced 2025-06-12 14:51:23 +00:00
feat: interface pubsub (#60)
* feat: interface pubsub * chore: pubsub router tests * chore: move pubsub abstractions from gossipsub * chore: address review * chore: revamp docs * chore: add emit self tests to interface * chore: refactor base tests * chore: publish should only accept one topic per api call * chore: normalize msg before emit * chore: do not reset inbound stream * chore: apply suggestions from code review Co-authored-by: Jacob Heun <jacobheun@gmail.com> * chore: address review * fix: remove subscribe handler * chore: remove bits from create peerId Co-authored-by: Jacob Heun <jacobheun@gmail.com> * chore: remove delay from topic validators tests * chore: add event emitter information * fix: topic validator docs Co-authored-by: Jacob Heun <jacobheun@gmail.com>
This commit is contained in:
73
test/pubsub/message.spec.js
Normal file
73
test/pubsub/message.spec.js
Normal file
@ -0,0 +1,73 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const { expect } = require('aegir/utils/chai')
|
||||
const sinon = require('sinon')
|
||||
|
||||
const PubsubBaseImpl = require('../../src/pubsub')
|
||||
const { randomSeqno } = require('../../src/pubsub/utils')
|
||||
const {
|
||||
createPeerId,
|
||||
mockRegistrar
|
||||
} = require('./utils')
|
||||
|
||||
describe('pubsub base messages', () => {
|
||||
let peerId
|
||||
let pubsub
|
||||
|
||||
before(async () => {
|
||||
peerId = await createPeerId()
|
||||
pubsub = new PubsubBaseImpl({
|
||||
debugName: 'pubsub',
|
||||
multicodecs: '/pubsub/1.0.0',
|
||||
libp2p: {
|
||||
peerId: peerId,
|
||||
registrar: mockRegistrar
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
it('_buildMessage normalizes and signs messages', async () => {
|
||||
const message = {
|
||||
receivedFrom: peerId.id,
|
||||
from: peerId.id,
|
||||
data: 'hello',
|
||||
seqno: randomSeqno(),
|
||||
topicIDs: ['test-topic']
|
||||
}
|
||||
|
||||
const signedMessage = await pubsub._buildMessage(message)
|
||||
expect(pubsub.validate(signedMessage)).to.not.be.rejected()
|
||||
})
|
||||
|
||||
it('validate with strict signing off will validate a present signature', async () => {
|
||||
const message = {
|
||||
receivedFrom: peerId.id,
|
||||
from: peerId.id,
|
||||
data: 'hello',
|
||||
seqno: randomSeqno(),
|
||||
topicIDs: ['test-topic']
|
||||
}
|
||||
|
||||
sinon.stub(pubsub, 'strictSigning').value(false)
|
||||
|
||||
const signedMessage = await pubsub._buildMessage(message)
|
||||
expect(pubsub.validate(signedMessage)).to.not.be.rejected()
|
||||
})
|
||||
|
||||
it('validate with strict signing requires a signature', async () => {
|
||||
const message = {
|
||||
receivedFrom: peerId.id,
|
||||
from: peerId.id,
|
||||
data: 'hello',
|
||||
seqno: randomSeqno(),
|
||||
topicIDs: ['test-topic']
|
||||
}
|
||||
|
||||
await expect(pubsub.validate(message)).to.be.rejectedWith(Error, 'Signing required and no signature was present')
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user