Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/kuzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,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}
Expand Down Expand Up @@ -522,7 +534,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 {
Expand Down Expand Up @@ -783,6 +795,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.
Expand Down
12 changes: 10 additions & 2 deletions test/kuzzle/constructor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -521,6 +523,7 @@ describe('Kuzzle constructor', () => {
renew: function (cb) { cb(); }
};


this.timeout(200);

kuzzle.subscriptions['foo'] = { bar: stubKuzzleRoom };
Expand Down Expand Up @@ -912,6 +915,7 @@ describe('Kuzzle constructor', () => {

it('should have a empty token in logout callback', function (done) {
var
unsetJwtToken = false,
kuzzle;

this.timeout(200);
Expand All @@ -920,12 +924,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();
});
});
Expand Down
43 changes: 43 additions & 0 deletions test/kuzzle/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down