Skip to content

Commit fb57e16

Browse files
authored
Merge pull request #234 from kuzzleio/fix-790
Update to new notifications format
2 parents 00dcaca + eec2721 commit fb57e16

File tree

6 files changed

+25
-37
lines changed

6 files changed

+25
-37
lines changed

src/Kuzzle.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function Kuzzle (host, options, cb) {
5555
'networkError',
5656
'disconnected',
5757
'reconnected',
58-
'jwtTokenExpired',
58+
'tokenExpired',
5959
'loginAttempt',
6060
'offlineQueuePush',
6161
'offlineQueuePop',
@@ -273,7 +273,7 @@ function Kuzzle (host, options, cb) {
273273
error: {timeout: this.eventTimeout},
274274
disconnected: {timeout: this.eventTimeout},
275275
reconnected: {timeout: this.eventTimeout},
276-
jwtTokenExpired: {timeout: this.eventTimeout},
276+
tokenExpired: {timeout: this.eventTimeout},
277277
loginAttempt: {timeout: this.eventTimeout}
278278
},
279279
writeable: false
@@ -414,7 +414,7 @@ Kuzzle.prototype.connect = function () {
414414
// shouldn't obtain an error but let's invalidate the token anyway
415415
if (err || !res.valid) {
416416
self.jwtToken = undefined;
417-
self.emitEvent('jwtTokenExpired');
417+
self.emitEvent('tokenExpired');
418418
}
419419

420420
reconnect();
@@ -869,7 +869,7 @@ function emitRequest (request, cb) {
869869

870870
if (request.action !== 'logout' && response.error && response.error.message === 'Token expired') {
871871
self.jwtToken = undefined;
872-
self.emitEvent('jwtTokenExpired', request, cb);
872+
self.emitEvent('tokenExpired', request, cb);
873873
}
874874

875875
if (response.error) {

src/Room.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -311,22 +311,16 @@ Room.prototype.setHeaders = function (content, replace) {
311311
* @returns {*}
312312
*/
313313
function notificationCallback (data) {
314-
if (data.error) {
315-
return this.callback(data.error);
316-
}
317-
318-
if (data.action === 'jwtTokenExpired') {
314+
if (data.type === 'TokenExpired') {
319315
this.kuzzle.jwtToken = undefined;
320-
return this.kuzzle.emitEvent('jwtTokenExpired');
316+
return this.kuzzle.emitEvent('tokenExpired');
321317
}
322318

323-
if (data.controller === 'document' || (data.controller === 'realtime' && data.action === 'publish')) {
324-
data.type = 'document';
319+
if (data.type === 'document') {
325320
data.document = new Document(this.collection, data.result._id, data.result._source, data.result._meta);
326321
delete data.result;
327322
}
328-
else if (data.controller === 'realtime') {
329-
data.type = 'user';
323+
else if (data.type === 'user') {
330324
data.user = {count: data.result.count};
331325
delete data.result;
332326
}

test/Room/methods.test.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -342,18 +342,10 @@ describe('Room methods', function () {
342342
room.callback = sinon.stub();
343343
});
344344

345-
it('should call back with an error if query returns an error', function () {
346-
notifCB.call(room, {error: 'foobar', result: {}});
347-
should(room.callback)
348-
.be.calledOnce()
349-
.be.calledWith('foobar');
350-
should(room.callback.firstCall.args)
351-
.have.length(1);
352-
});
353-
354345
it('should handle document notifications', function () {
355346
notifCB.call(room, {
356347
controller: 'document',
348+
type: 'document',
357349
result: {
358350
_id: 'id',
359351
_source: {
@@ -382,6 +374,7 @@ describe('Room methods', function () {
382374
notifCB.call(room, {
383375
controller: 'realtime',
384376
action: 'publish',
377+
type: 'document',
385378
result: {
386379
_source: {
387380
foo: 'bar'
@@ -409,7 +402,8 @@ describe('Room methods', function () {
409402
it('should handle user notifications', function () {
410403
notifCB.call(room, {
411404
controller: 'realtime',
412-
result: { count: 3 }
405+
result: { count: 3 },
406+
type: 'user'
413407
});
414408

415409
should(room.callback)
@@ -425,7 +419,7 @@ describe('Room methods', function () {
425419
it('should delete the result from history if emitted by this instance', function () {
426420
room.subscribeToSelf = true;
427421
kuzzle.requestHistory.bar = {};
428-
notifCB.call(room, {error: null, result: {}, action: 'foo', requestId: 'bar'});
422+
notifCB.call(room, {type: 'document', result: {}, action: 'foo', requestId: 'bar'});
429423

430424
should(room.callback)
431425
.be.calledOnce();
@@ -435,18 +429,18 @@ describe('Room methods', function () {
435429
it('should not forward the message if subscribeToSelf is false and the response comes from a query emitted by this instance', function () {
436430
room.subscribeToSelf = false;
437431
kuzzle.requestHistory.bar = {};
438-
notifCB.call(room, {error: null, result: {}, requestId: 'bar', action: 'foo'});
432+
notifCB.call(room, {type: 'document', result: {}, requestId: 'bar', action: 'foo'});
439433

440434
should(room.callback).not.be.called();
441435
should(kuzzle.requestHistory).be.empty();
442436
});
443437

444-
it('should fire a "jwtTokenExpired" event when receiving a jwtTokenExpired notification', function () {
438+
it('should fire a "tokenExpired" event when receiving a TokenExpired notification', function () {
445439
kuzzle.emitEvent = sinon.stub();
446440

447-
notifCB.call(room, {error: null, result: {}, action: 'jwtTokenExpired'});
441+
notifCB.call(room, {type: 'TokenExpired'});
448442
should(kuzzle.emitEvent).be.calledOnce();
449-
should(kuzzle.emitEvent).be.calledWith('jwtTokenExpired');
443+
should(kuzzle.emitEvent).be.calledWith('tokenExpired');
450444
});
451445
});
452446
});

test/kuzzle/connect.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,21 +303,21 @@ describe('Kuzzle connect', function () {
303303
it('should empty the JWT Token if it has expired', function (done) {
304304
var
305305
kuzzle = new Kuzzle('somewhereagain', {connect: 'manual'}),
306-
jwtTokenExpiredStub = sinon.stub();
306+
tokenExpiredStub = sinon.stub();
307307

308308
sinon.stub(kuzzle, 'checkToken', function (token, cb) {
309309
should(token).be.eql(kuzzle.jwtToken);
310310
cb(null, {valid: false});
311311
});
312312

313313
kuzzle.jwtToken = 'foobar';
314-
kuzzle.addListener('jwtTokenExpired', jwtTokenExpiredStub);
314+
kuzzle.addListener('tokenExpired', tokenExpiredStub);
315315
kuzzle.connect();
316316

317317
process.nextTick(function () {
318318
should(kuzzle.state).be.exactly('connected');
319319
should(kuzzle.jwtToken).be.undefined();
320-
should(jwtTokenExpiredStub).be.calledOnce();
320+
should(tokenExpiredStub).be.calledOnce();
321321
done();
322322
});
323323
});

test/kuzzle/listenersManagement.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('Kuzzle listeners management', function () {
3535
'networkError',
3636
'disconnected',
3737
'reconnected',
38-
'jwtTokenExpired',
38+
'tokenExpired',
3939
'loginAttempt',
4040
'offlineQueuePush',
4141
'offlineQueuePop',

test/kuzzle/query.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@ describe('Query management', function () {
2929
should(kuzzle.requestHistory.bar).be.within(start, Date.now());
3030
});
3131

32-
it('should fire a jwtTokenExpired event if the token has expired', function (done) {
32+
it('should trigger a tokenExpired event if the token has expired', function (done) {
3333
var
34-
jwtTokenExpiredStub = sinon.stub(),
34+
tokenExpiredStub = sinon.stub(),
3535
response = {
3636
error: {
3737
message: 'Token expired'
3838
}
3939
};
4040

41-
kuzzle.addListener('jwtTokenExpired', jwtTokenExpiredStub);
41+
kuzzle.addListener('tokenExpired', tokenExpiredStub);
4242

4343
emitRequest.call(kuzzle, {requestId: 'foobar', response: response}, function(error) {
44-
should(jwtTokenExpiredStub).be.calledOnce();
44+
should(tokenExpiredStub).be.calledOnce();
4545
should(error.message).be.exactly('Token expired');
4646
done();
4747
});

0 commit comments

Comments
 (0)