Skip to content

Commit

Permalink
delivers a will waits for authorization (#213)
Browse files Browse the repository at this point in the history
* delivers a will waits for authorization

* set will to null should after check error

* prevent finish func in client.js might be called twice

* copy that.will to into the context of the closure of function authorizePublish and use setImmediate instead of setTimeout in will test
  • Loading branch information
nguyenthenguyen authored and mcollina committed Aug 13, 2018
1 parent 12392f4 commit 09f0070
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,10 @@ Client.prototype.close = function (done) {

function finish () {
if (!that.disconnected && that.will) {
that.broker.authorizePublish(that, that.will, function (err) {
var will = Object.assign({}, that.will)
that.broker.authorizePublish(that, will, function (err) {
if (!err) {
that.broker.publish(that.will, that, done)
that.broker.publish(will, that, done)
} else {
done()
}
Expand Down
22 changes: 22 additions & 0 deletions test/will.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,28 @@ test('delivers a will with authorization', function (t) {
s.conn.destroy()
})

test('delivers a will waits for 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; setImmediate(() => { 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 = {}
Expand Down

0 comments on commit 09f0070

Please sign in to comment.