Skip to content

Commit

Permalink
Merge pull request #52 from kuzzleio/KUZ-378-whoami
Browse files Browse the repository at this point in the history
KUZ-378 OMG Who Am I? (identity crisis)
  • Loading branch information
AnthonySendra committed Feb 4, 2016
2 parents 6a40485 + 8e578b0 commit 924618f
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 4 deletions.
19 changes: 18 additions & 1 deletion dist/kuzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ module.exports = Kuzzle = function (url, options, cb) {
filter: function (name, func, target, passes) {
var whitelist = ['getAllStatistics', 'getServerInfo', 'getStatistics',
'listCollections', 'listIndexes', 'login', 'logout', 'now', 'query',
'checkToken'];
'checkToken', 'whoAmI'];

return passes && whitelist.indexOf(name) !== -1;
}
Expand Down Expand Up @@ -753,6 +753,23 @@ Kuzzle.prototype.checkToken = function (token, callback) {
return self;
};

/**
* Fetches the current user.
*
* @param {function} callback The callback to be called when the response is
* available. The signature is `function(error, response)`.
* @return {Kuzzle} The Kuzzle instance to enable chaining.
*/
Kuzzle.prototype.whoAmI = function (callback) {
var self = this;

this.callbackRequired('Kuzzle.whoAmI', callback);

this.query({controller: 'auth', action: 'getCurrentUser'}, {}, {}, callback);

return self;
};

/**
* Clean up the queue, ensuring the queryTTL and queryMaxSize properties are respected
*/
Expand Down
2 changes: 1 addition & 1 deletion dist/kuzzle.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/kuzzle.min.map

Large diffs are not rendered by default.

19 changes: 18 additions & 1 deletion src/kuzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ module.exports = Kuzzle = function (url, options, cb) {
filter: function (name, func, target, passes) {
var whitelist = ['getAllStatistics', 'getServerInfo', 'getStatistics',
'listCollections', 'listIndexes', 'login', 'logout', 'now', 'query',
'checkToken'];
'checkToken', 'whoAmI'];

return passes && whitelist.indexOf(name) !== -1;
}
Expand Down Expand Up @@ -478,6 +478,23 @@ Kuzzle.prototype.checkToken = function (token, callback) {
return self;
};

/**
* Fetches the current user.
*
* @param {function} callback The callback to be called when the response is
* available. The signature is `function(error, response)`.
* @return {Kuzzle} The Kuzzle instance to enable chaining.
*/
Kuzzle.prototype.whoAmI = function (callback) {
var self = this;

this.callbackRequired('Kuzzle.whoAmI', callback);

this.query({controller: 'auth', action: 'getCurrentUser'}, {}, {}, callback);

return self;
};

/**
* Clean up the queue, ensuring the queryTTL and queryMaxSize properties are respected
*/
Expand Down
1 change: 1 addition & 0 deletions test/kuzzle/constructor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ describe('Kuzzle constructor', () => {
should.exist(kuzzle.nowPromise);
should.exist(kuzzle.queryPromise);
should.exist(kuzzle.checkTokenPromise);
should.exist(kuzzle.whoAmIPromise);
should.not.exist(kuzzle.removeAllListenersPromise);
should.not.exist(kuzzle.removeListenerPromise);
should.not.exist(kuzzle.replayQueuePromise);
Expand Down
20 changes: 20 additions & 0 deletions test/kuzzle/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,4 +542,24 @@ describe('Kuzzle methods', function () {
}
});
});

describe('#whoAmI', function () {
it('should send the getCurrentUser after call', function () {
var kuzzle;

this.timeout(200);

kuzzle = new Kuzzle('nowhere', {
connect: 'manual'
});

kuzzle.queuing = true;

kuzzle.whoAmI(function (err, res) {});

should(kuzzle.offlineQueue.length).be.exactly(1);
should(kuzzle.offlineQueue[0].query.action).be.exactly('getCurrentUser');
should(kuzzle.offlineQueue[0].query.controller).be.exactly('auth');
});
});
});

0 comments on commit 924618f

Please sign in to comment.