Skip to content

Commit

Permalink
Fix #211 (#212)
Browse files Browse the repository at this point in the history
* Added test cases for #211.

* Fixed #211.
  • Loading branch information
mhverbakel authored and mcollina committed Aug 7, 2018
1 parent 1189ffe commit ffbc170
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
20 changes: 14 additions & 6 deletions lib/client.js
Expand Up @@ -242,12 +242,20 @@ Client.prototype.close = function (done) {

function finish () {
if (!that.disconnected && that.will) {
that.broker.publish(that.will, that, function (err) {
that.broker.authorizePublish(that, that.will, function (err) {
if (!err) {
that.broker.persistence.delWill({
id: that.id,
brokerId: that.broker.id
}, nop)
that.broker.publish(that.will, that, done)
} else {
done()
}

function done (err) {
if (!err) {
that.broker.persistence.delWill({
id: that.id,
brokerId: that.broker.id
}, nop)
}
}
})
that.will = null // this function might be called twice
Expand Down Expand Up @@ -298,4 +306,4 @@ function enqueue (packet) {
handle(this.client, packet, this.client._nextBatch)
}

function nop () {}
function nop () { }
41 changes: 41 additions & 0 deletions test/will.js
Expand Up @@ -161,3 +161,44 @@ test('delete the will in the persistence after publish', function (t) {
cb()
}
})

test('delivers a will with authorization', function (t) {
let authorized = false
var opts = {}
// willConnect populates opts with a will
var s = willConnect(setup(aedes({ authorizePublish: (_1, _2, callback) => { authorized = true; callback(null) } })), opts)

s.broker.on('clientDisconnect', function () {
t.end()
})

s.broker.mq.on('mywill', function (packet, cb) {
t.equal(packet.topic, opts.will.topic, 'topic matches')
t.deepEqual(packet.payload, opts.will.payload, 'payload matches')
t.equal(packet.qos, opts.will.qos, 'qos matches')
t.equal(packet.retain, opts.will.retain, 'retain matches')
t.equal(authorized, true, 'authorization called')
cb()
})

s.conn.destroy()
})

test('does not deliver a will without authorization', function (t) {
let authorized = false
var opts = {}
// willConnect populates opts with a will
var s = willConnect(setup(aedes({ authorizePublish: (_1, _2, callback) => { authorized = true; callback(new Error()) } })), opts)

s.broker.on('clientDisconnect', function () {
t.equal(authorized, true, 'authorization called')
t.end()
})

s.broker.mq.on('mywill', function (packet, cb) {
t.fail('received will without authorization')
cb()
})

s.conn.destroy()
})

0 comments on commit ffbc170

Please sign in to comment.