From 7b6c155d4d5c93fc7103f3f078eff59f3c678c48 Mon Sep 17 00:00:00 2001 From: STAFYNIAK Sacha Date: Wed, 21 Sep 2016 10:34:39 +0200 Subject: [PATCH] add unsetJwtToken() method to kuzzle object --- src/kuzzle.js | 28 ++++++++++++++++++++- test/kuzzle/constructor.test.js | 12 +++++++-- test/kuzzle/methods.test.js | 43 +++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/src/kuzzle.js b/src/kuzzle.js index 11a634172..6fbe610a7 100644 --- a/src/kuzzle.js +++ b/src/kuzzle.js @@ -431,6 +431,18 @@ Kuzzle.prototype.setJwtToken = function(token) { return this; }; +/** + * Unset the jwtToken used to query kuzzle + * @returns {Kuzzle} + */ +Kuzzle.prototype.unsetJwtToken = function() { + this.jwtToken = undefined; + + removeAllSubscriptions.call(this); + + return this; +}; + /** * Get the jwtToken used by kuzzle * @returns {Kuzzle} @@ -517,7 +529,7 @@ Kuzzle.prototype.logout = function (cb) { this.query({controller: 'auth', action: 'logout'}, request, {queuable: false}, typeof cb !== 'function' ? null : function(error) { if (error === null) { - self.jwtToken = undefined; + self.unsetJwtToken(); cb(null, self); } else { @@ -778,6 +790,20 @@ function renewAllSubscriptions() { }); } +/** + * Remove all registered subscriptions. Triggered either by a logout query or by un-setting the token + */ +function removeAllSubscriptions() { + var self = this; + + Object.keys(self.subscriptions).forEach(function (roomId) { + Object.keys(self.subscriptions[roomId]).forEach(function (subscriptionId) { + var subscription = self.subscriptions[roomId][subscriptionId]; + subscription.unsubscribe(); + }); + }); +} + /** * Adds a listener to a Kuzzle global event. When an event is fired, listeners are called in the order of their * insertion. diff --git a/test/kuzzle/constructor.test.js b/test/kuzzle/constructor.test.js index 8c834d197..33742cd55 100644 --- a/test/kuzzle/constructor.test.js +++ b/test/kuzzle/constructor.test.js @@ -49,6 +49,7 @@ describe('Kuzzle constructor', () => { should.exist(kuzzle.startQueuing); should.exist(kuzzle.stopQueuing); should.exist(kuzzle.setJwtToken); + should.exist(kuzzle.unsetJwtToken); }); it('should expose the documented properties', () => { @@ -201,6 +202,7 @@ describe('Kuzzle constructor', () => { should.not.exist(kuzzle.removeListenerPromise); should.not.exist(kuzzle.replayQueuePromise); should.not.exist(kuzzle.setJwtTokenPromise); + should.not.exist(kuzzle.unsetJwtTokenPromise); should.not.exist(kuzzle.setHeadersPromise); should.not.exist(kuzzle.startQueuingPromise); should.not.exist(kuzzle.stopQueuingPromise); @@ -504,6 +506,7 @@ describe('Kuzzle constructor', () => { renew: function (cb) { cb(); } }; + this.timeout(200); kuzzle.subscriptions['foo'] = { bar: stubKuzzleRoom }; @@ -895,6 +898,7 @@ describe('Kuzzle constructor', () => { it('should have a empty token in logout callback', function (done) { var + unsetJwtToken = false, kuzzle; this.timeout(200); @@ -903,12 +907,16 @@ describe('Kuzzle constructor', () => { connect: 'manual' }); + kuzzle.unsetJwtToken = function() { + unsetJwtToken = true; + }; + kuzzle.query = function(queryArgs, query, options, cb) { cb(null, {}); }; - kuzzle.logout(function(error, k) { - should(k.jwtToken).be.exactly(undefined); + kuzzle.logout(function() { + should(unsetJwtToken).be.exactly(true); done(); }); }); diff --git a/test/kuzzle/methods.test.js b/test/kuzzle/methods.test.js index 37a1766c9..074c04c5b 100644 --- a/test/kuzzle/methods.test.js +++ b/test/kuzzle/methods.test.js @@ -705,6 +705,49 @@ describe('Kuzzle methods', function () { }); }); + describe('#unsetJwtToken', function () { + var + subscriptionsRemoved, + revert; + + it('should unset the token and call removeAllSubscriptions', function (done) { + revert = Kuzzle.__set__('removeAllSubscriptions', function () { subscriptionsRemoved = true; }); + + kuzzle = new Kuzzle('nowhere', {connect: 'manual'}); + subscriptionsRemoved = false; + + kuzzle.unsetJwtToken(); + + process.nextTick(() => { + should(kuzzle.getJwtToken()).be.eql(undefined); + should(subscriptionsRemoved).be.true(); + revert(); + done(); + }); + }); + + it('should unsubscribe all rooms when un-setting token', function (done) { + var + unsubscribeCalled, + stubKuzzleRoom; + + stubKuzzleRoom = { + unsubscribe: function () { unsubscribeCalled = true; }, + }; + + kuzzle = new Kuzzle('nowhere', {connect: 'manual'}); + + kuzzle.subscriptions['foo'] = { bar: stubKuzzleRoom }; + + kuzzle.unsetJwtToken(); + + process.nextTick(() => { + should(unsubscribeCalled).be.true(); + done(); + }); + }); + }); + describe('#setJwtToken', function () { var eventEmitted,