|
| 1 | +/* eslint max-nested-callbacks: ["error", 8] */ |
| 2 | +/* eslint-env mocha */ |
| 3 | +'use strict' |
| 4 | + |
| 5 | +const hat = require('hat') |
| 6 | +const chai = require('chai') |
| 7 | +const dirtyChai = require('dirty-chai') |
| 8 | +const expect = chai.expect |
| 9 | +chai.use(dirtyChai) |
| 10 | + |
| 11 | +const IPFS = require('../../src') |
| 12 | +const createTempRepo = require('../utils/create-repo-nodejs') |
| 13 | + |
| 14 | +describe('pubsub disabled', () => { |
| 15 | + let ipfs |
| 16 | + let repo |
| 17 | + |
| 18 | + before(function (done) { |
| 19 | + this.timeout(20 * 1000) |
| 20 | + |
| 21 | + repo = createTempRepo() |
| 22 | + ipfs = new IPFS({ |
| 23 | + repo, |
| 24 | + config: { |
| 25 | + Addresses: { |
| 26 | + Swarm: [] |
| 27 | + } |
| 28 | + }, |
| 29 | + preload: { |
| 30 | + enabled: false |
| 31 | + }, |
| 32 | + EXPERIMENTAL: { |
| 33 | + pubsub: false |
| 34 | + } |
| 35 | + }) |
| 36 | + |
| 37 | + ipfs.on('ready', done) |
| 38 | + }) |
| 39 | + |
| 40 | + after((done) => ipfs.stop(done)) |
| 41 | + |
| 42 | + after((done) => repo.teardown(done)) |
| 43 | + |
| 44 | + it('should not allow subscribe if disabled', done => { |
| 45 | + const topic = hat() |
| 46 | + const handler = () => done(new Error('unexpected message')) |
| 47 | + ipfs.pubsub.subscribe(topic, handler, (err) => { |
| 48 | + expect(err).to.exist() |
| 49 | + expect(err.code).to.equal('ERR_PUBSUB_DISABLED') |
| 50 | + done() |
| 51 | + }) |
| 52 | + }) |
| 53 | + |
| 54 | + it('should not allow subscribe if disabled (promised)', async () => { |
| 55 | + try { |
| 56 | + const topic = hat() |
| 57 | + const handler = () => { throw new Error('unexpected message') } |
| 58 | + await ipfs.pubsub.subscribe(topic, handler) |
| 59 | + } catch (err) { |
| 60 | + return expect(err.code).to.equal('ERR_PUBSUB_DISABLED') |
| 61 | + } |
| 62 | + throw new Error('expected error to be thrown') |
| 63 | + }) |
| 64 | + |
| 65 | + it('should not allow unsubscribe if disabled', done => { |
| 66 | + const topic = hat() |
| 67 | + const handler = () => done(new Error('unexpected message')) |
| 68 | + ipfs.pubsub.unsubscribe(topic, handler, (err) => { |
| 69 | + expect(err).to.exist() |
| 70 | + expect(err.code).to.equal('ERR_PUBSUB_DISABLED') |
| 71 | + done() |
| 72 | + }) |
| 73 | + }) |
| 74 | + |
| 75 | + it('should not allow unsubscribe if disabled (promised)', async () => { |
| 76 | + try { |
| 77 | + const topic = hat() |
| 78 | + const handler = () => { throw new Error('unexpected message') } |
| 79 | + await ipfs.pubsub.unsubscribe(topic, handler) |
| 80 | + } catch (err) { |
| 81 | + return expect(err.code).to.equal('ERR_PUBSUB_DISABLED') |
| 82 | + } |
| 83 | + throw new Error('expected error to be thrown') |
| 84 | + }) |
| 85 | + |
| 86 | + it('should not allow publish if disabled', done => { |
| 87 | + const topic = hat() |
| 88 | + const msg = Buffer.from(hat()) |
| 89 | + ipfs.pubsub.publish(topic, msg, (err) => { |
| 90 | + expect(err).to.exist() |
| 91 | + expect(err.code).to.equal('ERR_PUBSUB_DISABLED') |
| 92 | + done() |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + it('should not allow publish if disabled (promised)', async () => { |
| 97 | + try { |
| 98 | + const topic = hat() |
| 99 | + const msg = Buffer.from(hat()) |
| 100 | + await ipfs.pubsub.publish(topic, msg) |
| 101 | + } catch (err) { |
| 102 | + return expect(err.code).to.equal('ERR_PUBSUB_DISABLED') |
| 103 | + } |
| 104 | + throw new Error('expected error to be thrown') |
| 105 | + }) |
| 106 | + |
| 107 | + it('should not allow ls if disabled', done => { |
| 108 | + ipfs.pubsub.ls((err) => { |
| 109 | + expect(err).to.exist() |
| 110 | + expect(err.code).to.equal('ERR_PUBSUB_DISABLED') |
| 111 | + done() |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + it('should not allow ls if disabled (promised)', async () => { |
| 116 | + try { |
| 117 | + await ipfs.pubsub.ls() |
| 118 | + } catch (err) { |
| 119 | + return expect(err.code).to.equal('ERR_PUBSUB_DISABLED') |
| 120 | + } |
| 121 | + throw new Error('expected error to be thrown') |
| 122 | + }) |
| 123 | + |
| 124 | + it('should not allow peers if disabled', done => { |
| 125 | + const topic = hat() |
| 126 | + ipfs.pubsub.peers(topic, (err) => { |
| 127 | + expect(err).to.exist() |
| 128 | + expect(err.code).to.equal('ERR_PUBSUB_DISABLED') |
| 129 | + done() |
| 130 | + }) |
| 131 | + }) |
| 132 | + |
| 133 | + it('should not allow peers if disabled (promised)', async () => { |
| 134 | + try { |
| 135 | + const topic = hat() |
| 136 | + await ipfs.pubsub.peers(topic) |
| 137 | + } catch (err) { |
| 138 | + return expect(err.code).to.equal('ERR_PUBSUB_DISABLED') |
| 139 | + } |
| 140 | + throw new Error('expected error to be thrown') |
| 141 | + }) |
| 142 | + |
| 143 | + it('should not allow setMaxListeners if disabled', async () => { |
| 144 | + try { |
| 145 | + await ipfs.pubsub.setMaxListeners(100) |
| 146 | + } catch (err) { |
| 147 | + return expect(err.code).to.equal('ERR_PUBSUB_DISABLED') |
| 148 | + } |
| 149 | + throw new Error('expected error to be thrown') |
| 150 | + }) |
| 151 | +}) |
0 commit comments