diff --git a/package.json b/package.json index d6a5b5447..974feb251 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kuzzle-sdk", - "version": "1.7.2", + "version": "1.7.3", "description": "Official Javascript SDK for Kuzzle", "author": "The Kuzzle Team ", "repository": { diff --git a/src/kuzzle.js b/src/kuzzle.js index 13c5bce58..0c9b873a5 100644 --- a/src/kuzzle.js +++ b/src/kuzzle.js @@ -427,24 +427,40 @@ Kuzzle.prototype.getJwtToken = function() { * @param cb * @returns {Kuzzle} */ -Kuzzle.prototype.login = function (strategy, credentials, expiresIn, cb) { +Kuzzle.prototype.login = function (strategy) { var self = this, request = { strategy: strategy - }; - - if (!cb && typeof expiresIn === 'function') { - cb = expiresIn; - expiresIn = null; + }, + credentials, + cb; + + // Handle arguments (credentials, expiresIn, cb) + if (arguments[1]) { + if (typeof arguments[1] === 'object') { + credentials = arguments[1]; + } else if (typeof arguments[1] === 'number' || typeof arguments[1] === 'string') { + request.expiresIn = arguments[1]; + } else if (typeof arguments[1] === 'function') { + cb = arguments[1]; + } + } + if (arguments[2]) { + if (typeof arguments[2] === 'number' || typeof arguments[2] === 'string') { + request.expiresIn = arguments[2]; + } else if (typeof arguments[2] === 'function') { + cb = arguments[2]; + } + } + if (arguments[3] && typeof arguments[3] === 'function') { + cb = arguments[3]; } - Object.keys(credentials).forEach(function (key) { - request[key] = credentials[key]; - }); - - if (['number', 'string'].indexOf(typeof expiresIn) !== -1) { - request.expiresIn = expiresIn; + if (typeof credentials === 'object') { + Object.keys(credentials).forEach(function (key) { + request[key] = credentials[key]; + }); } this.query({controller: 'auth', action: 'login'}, {body: request}, {queuable: false}, function(error, response) { diff --git a/test/kuzzle/constructor.test.js b/test/kuzzle/constructor.test.js index ab35c3045..04d9ee676 100644 --- a/test/kuzzle/constructor.test.js +++ b/test/kuzzle/constructor.test.js @@ -750,6 +750,115 @@ describe('Kuzzle constructor', () => { }); }); + it('should handle login with only one argument and without callback', function (done) { + var + kuzzle; + + this.timeout(200); + + kuzzle = new Kuzzle('nowhere', { + connect: 'manual' + }); + + kuzzle.query = function(queryArgs, query, options, cb) { + done(); + }; + kuzzle.login('local'); + }); + + it('should handle login with credentials and without callback', function (done) { + var + kuzzle, + loginCredentials = {username: 'foo', password: 'bar'}; + + this.timeout(200); + + kuzzle = new Kuzzle('nowhere', { + connect: 'manual' + }); + + kuzzle.query = function(queryArgs, query, options, cb) { + done(); + }; + + kuzzle.login('local', loginCredentials); + }); + + it('should handle login without credentials, with expiresIn and without callback', function (done) { + var + kuzzle; + + this.timeout(200); + + kuzzle = new Kuzzle('nowhere', { + connect: 'manual' + }); + + kuzzle.query = function(queryArgs, query, options, cb) { + done(); + }; + + kuzzle.login('local', '1h'); + }); + + it('should handle login without credentials, without expiresIn and with callback', function (done) { + var + kuzzle; + + this.timeout(200); + + kuzzle = new Kuzzle('nowhere', { + connect: 'manual' + }); + + kuzzle.query = function(queryArgs, query, options, cb) { + cb(null, {result: {jwt: 'test-toto'}}); + }; + console.log('## before'); + kuzzle.login('local', '1h', function() { + done(); + }); + }); + + it('should handle login without credentials, without expiresIn and with callback', function (done) { + var + kuzzle; + + this.timeout(200); + + kuzzle = new Kuzzle('nowhere', { + connect: 'manual' + }); + + kuzzle.query = function(queryArgs, query, options, cb) { + cb(null, {result: {jwt: 'test-toto'}}); + }; + + kuzzle.login('local', function() { + done(); + }); + }); + + it('should handle login with credentials', function (done) { + var + kuzzle, + loginCredentials = {username: 'foo', password: 'bar'}; + + this.timeout(200); + + kuzzle = new Kuzzle('nowhere', { + connect: 'manual' + }); + + kuzzle.query = function(queryArgs, query, options, cb) { + cb(null, {result: {jwt: 'test-toto'}}); + }; + + kuzzle.login('local', loginCredentials, function() { + done(); + }); + }); + it('should send a failed loginAttempt event if logging in fails', function (done) { var kuzzle = new Kuzzle('nowhere', {connect: 'manual'}),