diff --git a/.travis.yml b/.travis.yml index e6ecd0ae1..f6a47f1a3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,7 +18,7 @@ install: - npm install env: global: - secure: pL9O2VFziHNToD2FW+eZJcZNCEl2ltL7WuTa2aZro4XFjiU30JfY/nTD1hLcPDd+ZgS8gg/+OREHs2GCxsmzOpyLZNj4mR9/jNdfCwwGZrf/oJzjZjBWNzEqgTTrZTGKji4/XonVfVg0wU7sqv+SNpWOhdsfr9NZBnOYSs7vMcmqdhCiILGJIGMqBj37ux5tgtGJJkxR+IHwJKQe634S+HzNuCXaTJ2U1a6pCz9tb25Y8vFQ9rOPd2iZVd7GoNccDAzHGWNBuiRxxETE4Mn6VmHiaAccJ181kcKJ01S0bjoALbX9L5ySKqDLPfs2dKOJdrvGwWfMhZYkXWJGelIzOHUtSBcxYQ4ZfXWpLKbd169EeEwEC9tC6E4h3SaKddUiP+aVzC2zsh+WNag/xIDT+a3uXdDRSieNAL3cCh/45bFyo5BXRxSJamVRVBTuhaAvVGxRjuIc1qBiLKnNXTnkVFmEtnmoPmeaNWvBaGvqvhKOERJXGRCnB3mXaTTgdDEAXfHHdHgLQc0NfS/VgW9qGFJQklCHV15/MJ06+f8od/FeE+/s3FsrFxSD3qxuLvvOUYzYCQUeXI8Q6WfX6vwU9UYsSpDhkmV1k5Eg/1oZeYo+pVI94L3LhtwGw/ZCWeTR9IEFOmZQX66i/4BAfU2xyKkXMLaBOeWd8v9/QCbLacQ= + secure: ATb4QFGG6sPkfquAoye6BeN7u4uQ4abP8qatVK8XG3Qof+pECZwRkFPVoopO1Gb/4bQSPfapc1aGPzGuKi409v633vpDnpcgM7cHX9CPT+YQYcTePMFtq/m+5YY3WIYWM1zCFmJ+bBwQO/MtsubfGfA9EmSAAxtnJV+1JDD3Y6+NQRkQN7K+gxQozec0u1W0Oe1sJXLSKlW3JPL8iRtKcLvmIDysKtBK6X/Wg1qP2Me4reb449a0yGej3QvZDbDYJbdlh7vuFLjXJAz3yExfED8QAm8wf0p9F8emILHnkLxjumTTnGUUqDJLLu8WgkahFiyFo4jMtryhNQ0+p6WhXmEbpNIO+yMGSZPsCGbnpkhWJ1aMAT6ZhgpHf4jDZQd4ZJi02+n9tOpe+Lm2Jk2LXY8SwMk3xGtqm5sk03weTc019TUGj/NLoHNjO/vfNz3xOImD5Gck3bXWNxdrX3bjiyVsr/u77enoWu9drPCEl3JOTIwVP3mTF8R3axSFOQ8M4SI1orCMChkGMB5qExl+Gfx+MHa/jL7QKO1SFRgl1O3DbhsyFE6xiabYzwLcdbI50ypy6VJjmDd5RxK7BkDa8RumlHT1uY1FK9yRXu4XFrrw9FjF2s+xjWbliRZqXRReXk0W1Hz4zj5au3Y9YH5a/UPtB72TY+AUUWHTUPQl3SU= deploy: provider: npm skip_cleanup: true diff --git a/src/Kuzzle.js b/src/Kuzzle.js index 40c26f24b..212b4fb35 100644 --- a/src/Kuzzle.js +++ b/src/Kuzzle.js @@ -472,9 +472,7 @@ Kuzzle.prototype.getJwtToken = function() { Kuzzle.prototype.login = function (strategy) { var self = this, - request = { - strategy: strategy - }, + request = {}, credentials, cb = null; @@ -505,7 +503,7 @@ Kuzzle.prototype.login = function (strategy) { }); } - this.query({controller: 'auth', action: 'login'}, {body: request}, {queuable: false}, function(error, response) { + this.query({controller: 'auth', action: 'login'}, {strategy: strategy, body: request}, {queuable: false}, function(error, response) { if (!error) { if (response.result.jwt) { self.setJwtToken(response.result.jwt); @@ -520,6 +518,128 @@ Kuzzle.prototype.login = function (strategy) { }); }; +/** + * Create credentials of the specified for the current user. + * + * @param credentials + * @param strategy + * @param options + * @param cb + * @returns {Kuzzle} + */ +Kuzzle.prototype.createMyCredentials = function (strategy, credentials, options, cb) { + if (!cb && typeof options === 'function') { + cb = options; + options = null; + } + + this.query({controller: 'auth', action: 'createMyCredentials'}, {strategy: strategy, body: credentials}, options, function(err, res) { + if (!err) { + cb && cb(null, res.result._source); + } else { + cb && cb(err); + } + }); + + return this; +}; + +/** + * Delete credentials of the specified for the current user. + * + * @param strategy + * @param options + * @param cb + * @returns {Kuzzle} + */ +Kuzzle.prototype.deleteMyCredentials = function (strategy, options, cb) { + if (!cb && typeof options === 'function') { + cb = options; + options = null; + } + + this.query({controller: 'auth', action: 'deleteMyCredentials'}, {strategy: strategy}, options, typeof cb !== 'function' ? null : function(err, res) { + if (!err) { + cb && cb(null, res.result); + } else { + cb && cb(err); + } + }); + + return this; +}; + +/** + * Get credential information of the specified for the current user. + * + * @param strategy + * @param options + * @param cb + */ +Kuzzle.prototype.getMyCredentials = function (strategy, options, cb) { + if (!cb && typeof options === 'function') { + cb = options; + options = null; + } + + this.query({controller: 'auth', action: 'getMyCredentials'}, {strategy: strategy}, options, typeof cb !== 'function' ? null : function(err, res) { + if (!err) { + cb && cb(null, res.result); + } else { + cb && cb(err); + } + }); +}; + +/** + * Update credentials of the specified for the current user. + * + * @param strategy + * @param credentals + * @param options + * @param cb + * @returns {Kuzzle} + */ +Kuzzle.prototype.updateMyCredentials = function (strategy, credentials, options, cb) { + if (!cb && typeof options === 'function') { + cb = options; + options = null; + } + + this.query({controller: 'auth', action: 'updateMyCredentials'}, {strategy: strategy, body: credentials}, options, typeof cb !== 'function' ? null : function(err, res) { + if (!err) { + cb && cb(null, res.result); + } else { + cb && cb(err); + } + }); + + return this; +}; + +/** + * Validate credentials of the specified for the current user. + * + * @param strategy + * @param credentials + * @param options + * @param cb + */ +Kuzzle.prototype.validateMyCredentials = function (strategy, credentials, options, cb) { + if (!cb && typeof options === 'function') { + cb = options; + options = null; + } + + this.query({controller: 'auth', action: 'validateMyCredentials'}, {strategy: strategy, body: credentials}, options, typeof cb !== 'function' ? null : function(err, res) { + if (!err) { + cb && cb(null, res.result); + } else { + cb && cb(err); + } + }); +}; + /** * Create a kuzzle index * diff --git a/src/security/Security.js b/src/security/Security.js index e606c0b77..b60ca5caf 100644 --- a/src/security/Security.js +++ b/src/security/Security.js @@ -751,4 +751,197 @@ Security.prototype.getUserRights = function (userId, options, cb) { }); }; +/** + * Create credentials of the specified for the user . + * + * @param strategy + * @param kuid + * @param credentials + * @param options + * @param cb + * @returns {Security} + */ +Security.prototype.createCredentials = function (strategy, kuid, credentials, options, cb) { + if (!cb && typeof options === 'function') { + cb = options; + options = null; + } + + this.kuzzle.query({controller: 'security', action: 'createCredentials'}, {_id: kuid, strategy: strategy, body: credentials}, options, function(err, res) { + if (!err) { + cb && cb(null, res.result._source); + } else { + cb && cb(err); + } + }); + + return this; +}; + +/** + * Delete credentials of the specified for the user . + * + * @param strategy + * @param kuid + * @param options + * @param cb + * @returns {Security} + */ +Security.prototype.deleteCredentials = function (strategy, kuid, options, cb) { + if (!cb && typeof options === 'function') { + cb = options; + options = null; + } + + this.kuzzle.query({controller: 'security', action: 'deleteCredentials'}, {strategy: strategy, _id: kuid}, options, typeof cb !== 'function' ? null : function(err, res) { + if (!err) { + cb && cb(null, res.result); + } else { + cb && cb(err); + } + }); + + return this; +}; + +/** + * Retrieve a list of accepted fields per authentication strategy. + * + * @param options + * @param cb + */ +Security.prototype.getAllCredentialFields = function (options, cb) { + if (!cb && typeof options === 'function') { + cb = options; + options = null; + } + + this.kuzzle.query({controller: 'security', action: 'getAllCredentialFields'}, {}, options, typeof cb !== 'function' ? null : function(err, res) { + if (!err) { + cb && cb(null, res.result); + } else { + cb && cb(err); + } + }); +}; + +/** + * Retrieve the list of accepted field names by the specified . + * + * @param strategy + * @param options + * @param cb + */ +Security.prototype.getCredentialFields = function (strategy, options, cb) { + if (!cb && typeof options === 'function') { + cb = options; + options = null; + } + + this.kuzzle.query({controller: 'security', action: 'getCredentialFields'}, {strategy: strategy}, options, typeof cb !== 'function' ? null : function(err, res) { + if (!err) { + cb && cb(null, res.result); + } else { + cb && cb(err); + } + }); +}; + +/** + * Get credential information of the specified for the user . + * + * @param strategy + * @param kuid + * @param options + * @param cb + */ +Security.prototype.getCredentials = function (strategy, kuid, options, cb) { + if (!cb && typeof options === 'function') { + cb = options; + options = null; + } + + this.kuzzle.query({controller: 'security', action: 'getCredentials'}, {strategy: strategy, _id: kuid}, options, typeof cb !== 'function' ? null : function(err, res) { + if (!err) { + cb && cb(null, res.result); + } else { + cb && cb(err); + } + }); +}; + +/** + * Check the existence of the specified ’s credentials for the user . + * + * @param strategy + * @param kuid + * @param options + * @param cb + */ +Security.prototype.hasCredentials = function (strategy, kuid, options, cb) { + if (!cb && typeof options === 'function') { + cb = options; + options = null; + } + + this.kuzzle.query({controller: 'security', action: 'hasCredentials'}, {strategy: strategy, _id: kuid}, options, typeof cb !== 'function' ? null : function(err, res) { + if (!err) { + cb && cb(null, res.result); + } else { + cb && cb(err); + } + }); +}; + +/** + * Updates credentials of the specified for the user . + * + * @param strategy + * @param kuid + * @param credentials + * @param options + * @param cb + * @returns {Security} + */ +Security.prototype.updateCredentials = function (strategy, kuid, credentials, options, cb) { + if (!cb && typeof options === 'function') { + cb = options; + options = null; + } + + this.kuzzle.query({controller: 'security', action: 'updateCredentials'}, {strategy: strategy, _id: kuid, body: credentials}, options, typeof cb !== 'function' ? null : function(err, res) { + if (!err) { + cb && cb(null, res.result); + } else { + cb && cb(err); + } + }); + + return this; +}; + +/** + * Validate credentials of the specified for the user . + * + * @param strategy + * @param kuid + * @param credentials + * @param options + * @param cb + */ +Security.prototype.validateCredentials = function (strategy, kuid, credentials, options, cb) { + if (!cb && typeof options === 'function') { + cb = options; + options = null; + } + + this.kuzzle.query({controller: 'security', action: 'validateCredentials'}, {strategy: strategy, _id: kuid, body: credentials}, options, typeof cb !== 'function' ? null : function(err, res) { + if (!err) { + cb && cb(null, res.result); + } else { + cb && cb(err); + } + }); +}; + module.exports = Security; diff --git a/test/kuzzle/constructor.test.js b/test/kuzzle/constructor.test.js index b135357ea..0e0c1e591 100644 --- a/test/kuzzle/constructor.test.js +++ b/test/kuzzle/constructor.test.js @@ -832,6 +832,7 @@ describe('Kuzzle constructor', function () { }); kuzzle.query = function(queryArgs, query, options, cb) { + should(query.strategy).be.eql('local'); cb(null, {result: {jwt: 'test-toto'}}); }; diff --git a/test/kuzzle/methods.test.js b/test/kuzzle/methods.test.js index 58afc4580..a3fa4335e 100644 --- a/test/kuzzle/methods.test.js +++ b/test/kuzzle/methods.test.js @@ -1043,4 +1043,223 @@ describe('Kuzzle methods', function () { }); + describe('#createMyCredentials', function() { + beforeEach(function() { + kuzzle = new Kuzzle('foo'); + }); + + it('should trigger callback with an error', function (done) { + var + cberror = {message: 'i am an error'}, + spy = sandbox.stub(kuzzle, 'query').yields(cberror), + args; + + kuzzle.createMyCredentials('strategy', {foo: 'bar'}, function (err) { + should(err).be.exactly(cberror); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('auth'); + should(args[0].action).be.exactly('createMyCredentials'); + should(args[2]).be.exactly(null); + done(); + }); + }); + + it('should call query with right arguments', function (done) { + var + doc = {_id: '42'}, + spy = sandbox.stub(kuzzle, 'query').yields(null, {result: {_source: doc}}), + args; + + kuzzle.createMyCredentials('strategy', {foo: 'bar'}, function (err, res) { + should(res).be.exactly(doc); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('auth'); + should(args[0].action).be.exactly('createMyCredentials'); + should(args[2]).be.exactly(null); + done(); + }); + }); + }); + + describe('#deleteMyCredentials', function() { + beforeEach(function() { + kuzzle = new Kuzzle('foo'); + }); + + it('should trigger callback with an error', function (done) { + var + cberror = {message: 'i am an error'}, + spy = sandbox.stub(kuzzle, 'query').yields(cberror), + args; + + kuzzle.deleteMyCredentials('strategy', function (err) { + should(err).be.exactly(cberror); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('auth'); + should(args[0].action).be.exactly('deleteMyCredentials'); + should(args[2]).be.exactly(null); + done(); + }); + }); + + it('should call query with right arguments', function (done) { + var + spy = sandbox.stub(kuzzle, 'query').yields(null, {result: {acknowledged: true}}), + args; + + kuzzle.deleteMyCredentials('strategy', function (err, res) { + should(res.acknowledged).be.exactly(true); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('auth'); + should(args[0].action).be.exactly('deleteMyCredentials'); + should(args[2]).be.exactly(null); + done(); + }); + }); + }); + + describe('#getMyCredentials', function() { + beforeEach(function() { + kuzzle = new Kuzzle('foo'); + }); + + it('should trigger callback with an error', function (done) { + var + cberror = {message: 'i am an error'}, + spy = sandbox.stub(kuzzle, 'query').yields(cberror), + args; + + kuzzle.getMyCredentials('strategy', function (err) { + should(err).be.exactly(cberror); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('auth'); + should(args[0].action).be.exactly('getMyCredentials'); + should(args[2]).be.exactly(null); + done(); + }); + }); + + it('should call query with right arguments', function (done) { + var + doc = {_id: '42'}, + spy = sandbox.stub(kuzzle, 'query').yields(null, {result: doc}), + args; + + kuzzle.getMyCredentials('strategy', function (err, res) { + should(res).be.exactly(doc); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('auth'); + should(args[0].action).be.exactly('getMyCredentials'); + should(args[2]).be.exactly(null); + done(); + }); + }); + }); + + describe('#updateMyCredentials', function() { + beforeEach(function() { + kuzzle = new Kuzzle('foo'); + }); + + it('should trigger callback with an error', function (done) { + var + cberror = {message: 'i am an error'}, + spy = sandbox.stub(kuzzle, 'query').yields(cberror), + args; + + kuzzle.updateMyCredentials('strategy', {username: 'foo'}, function (err) { + should(err).be.exactly(cberror); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('auth'); + should(args[0].action).be.exactly('updateMyCredentials'); + should(args[2]).be.exactly(null); + done(); + }); + }); + + it('should call query with right arguments', function (done) { + var + doc = {username: 'foo'}, + spy = sandbox.stub(kuzzle, 'query').yields(null, {result: doc}), + args; + + kuzzle.updateMyCredentials('strategy', doc, function (err, res) { + should(res).be.exactly(doc); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('auth'); + should(args[0].action).be.exactly('updateMyCredentials'); + should(args[2]).be.exactly(null); + done(); + }); + }); + }); + + describe('#validateMyCredentials', function() { + beforeEach(function() { + kuzzle = new Kuzzle('foo'); + }); + + it('should trigger callback with an error', function (done) { + var + cberror = {message: 'i am an error'}, + spy = sandbox.stub(kuzzle, 'query').yields(cberror), + args; + + kuzzle.validateMyCredentials('strategy', {username: 'foo'}, function (err) { + should(err).be.exactly(cberror); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('auth'); + should(args[0].action).be.exactly('validateMyCredentials'); + should(args[2]).be.exactly(null); + done(); + }); + }); + + it('should call query with right arguments', function (done) { + var + doc = {username: 'foo'}, + spy = sandbox.stub(kuzzle, 'query').yields(null, {result: true}), + args; + + kuzzle.validateMyCredentials('strategy', doc, function (err, res) { + should(res).be.exactly(true); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('auth'); + should(args[0].action).be.exactly('validateMyCredentials'); + should(args[2]).be.exactly(null); + done(); + }); + }); + }); + }); diff --git a/test/security/kuzzleSecurity/credentialsMethods.test.js b/test/security/kuzzleSecurity/credentialsMethods.test.js new file mode 100644 index 000000000..6ec202c28 --- /dev/null +++ b/test/security/kuzzleSecurity/credentialsMethods.test.js @@ -0,0 +1,355 @@ +var + should = require('should'), + sinon = require('sinon'), + sandbox = sinon.sandbox.create(), + Kuzzle = require('../../../src/Kuzzle'); + +describe('Credentials methods', function() { + var kuzzle; + + describe('#createCredentials', function() { + beforeEach(function() { + kuzzle = new Kuzzle('foo'); + }); + + it('should trigger callback with an error', function (done) { + var + cberror = {message: 'i am an error'}, + spy = sandbox.stub(kuzzle, 'query').yields(cberror), + args; + + kuzzle.security.createCredentials('strategy', 'kuid', {foo: 'bar'}, function (err) { + should(err).be.exactly(cberror); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + should(args[0].controller).be.exactly('security'); + should(args[0].action).be.exactly('createCredentials'); + should(args[2]).be.exactly(null); + done(); + }); + + }); + + it('should call query with right arguments', function (done) { + var + doc = {_id: '42'}, + spy = sandbox.stub(kuzzle, 'query').yields(null, {result: {_source: doc}}), + args; + + kuzzle.security.createCredentials('strategy', 'kuid', {foo: 'bar'}, function (err, res) { + should(res).be.exactly(doc); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('security'); + should(args[0].action).be.exactly('createCredentials'); + should(args[2]).be.exactly(null); + done(); + }); + }); + }); + + describe('#deleteCredentials', function() { + beforeEach(function() { + kuzzle = new Kuzzle('foo'); + }); + + it('should trigger callback with an error', function (done) { + var + cberror = {message: 'i am an error'}, + spy = sandbox.stub(kuzzle, 'query').yields(cberror), + args; + + kuzzle.security.deleteCredentials('strategy', 'kuid', function (err) { + should(err).be.exactly(cberror); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + should(args[0].controller).be.exactly('security'); + should(args[0].action).be.exactly('deleteCredentials'); + should(args[2]).be.exactly(null); + done(); + }); + + }); + + it('should call query with right arguments', function (done) { + var + spy = sandbox.stub(kuzzle, 'query').yields(null, {result: {acknowledged: true}}), + args; + + kuzzle.security.deleteCredentials('strategy', 'kuid', function (err, res) { + should(res.acknowledged).be.exactly(true); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('security'); + should(args[0].action).be.exactly('deleteCredentials'); + should(args[2]).be.exactly(null); + done(); + }); + }); + }); + + describe('#getAllCredentialFields', function() { + beforeEach(function() { + kuzzle = new Kuzzle('foo'); + }); + + it('should trigger callback with an error', function (done) { + var + cberror = {message: 'i am an error'}, + spy = sandbox.stub(kuzzle, 'query').yields(cberror), + args; + + kuzzle.security.getAllCredentialFields(function (err) { + should(err).be.exactly(cberror); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('security'); + should(args[0].action).be.exactly('getAllCredentialFields'); + done(); + }); + }); + + it('should call query with right arguments', function (done) { + var + doc = ['usernae'], + spy = sandbox.stub(kuzzle, 'query').yields(null, {result: doc}), + args; + + kuzzle.security.getAllCredentialFields(function (err, res) { + should(res).be.exactly(doc); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('security'); + should(args[0].action).be.exactly('getAllCredentialFields'); + done(); + }); + }); + }); + + describe('#getCredentialFields', function() { + beforeEach(function() { + kuzzle = new Kuzzle('foo'); + }); + + it('should trigger callback with an error', function (done) { + var + cberror = {message: 'i am an error'}, + spy = sandbox.stub(kuzzle, 'query').yields(cberror), + args; + + kuzzle.security.getCredentialFields('strategy', function (err) { + should(err).be.exactly(cberror); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('security'); + should(args[0].action).be.exactly('getCredentialFields'); + done(); + }); + }); + + it('should call query with right arguments', function (done) { + var + doc = ['usernae'], + spy = sandbox.stub(kuzzle, 'query').yields(null, {result: doc}), + args; + + kuzzle.security.getCredentialFields('strategy', function (err, res) { + should(res).be.exactly(doc); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('security'); + should(args[0].action).be.exactly('getCredentialFields'); + done(); + }); + }); + }); + + describe('#getCredentials', function() { + beforeEach(function() { + kuzzle = new Kuzzle('foo'); + }); + + it('should trigger callback with an error', function (done) { + var + cberror = {message: 'i am an error'}, + spy = sandbox.stub(kuzzle, 'query').yields(cberror), + args; + + kuzzle.security.getCredentials('strategy', 'kuid', function (err) { + should(err).be.exactly(cberror); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('security'); + should(args[0].action).be.exactly('getCredentials'); + should(args[2]).be.exactly(null); + done(); + }); + }); + + it('should call query with right arguments', function (done) { + var + doc = {_id: '42'}, + spy = sandbox.stub(kuzzle, 'query').yields(null, {result: doc}), + args; + + kuzzle.security.getCredentials('strategy', 'kuid', function (err, res) { + should(res).be.exactly(doc); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('security'); + should(args[0].action).be.exactly('getCredentials'); + should(args[2]).be.exactly(null); + done(); + }); + }); + }); + + describe('#hasCredentials', function() { + beforeEach(function() { + kuzzle = new Kuzzle('foo'); + }); + + it('should trigger callback with an error', function (done) { + var + cberror = {message: 'i am an error'}, + spy = sandbox.stub(kuzzle, 'query').yields(cberror), + args; + + kuzzle.security.hasCredentials('strategy', 'kuid', function (err) { + should(err).be.exactly(cberror); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('security'); + should(args[0].action).be.exactly('hasCredentials'); + should(args[2]).be.exactly(null); + done(); + }); + }); + + it('should call query with right arguments', function (done) { + var + spy = sandbox.stub(kuzzle, 'query').yields(null, {result: true}), + args; + + kuzzle.security.hasCredentials('strategy', 'kuid', function (err, res) { + should(res).be.exactly(true); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('security'); + should(args[0].action).be.exactly('hasCredentials'); + should(args[2]).be.exactly(null); + done(); + }); + }); + }); + + describe('#updateCredentials', function() { + beforeEach(function() { + kuzzle = new Kuzzle('foo'); + }); + + it('should trigger callback with an error', function (done) { + var + cberror = {message: 'i am an error'}, + spy = sandbox.stub(kuzzle, 'query').yields(cberror), + args; + + kuzzle.security.updateCredentials('strategy', 'kuid', {username: 'foo'}, function (err) { + should(err).be.exactly(cberror); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('security'); + should(args[0].action).be.exactly('updateCredentials'); + should(args[2]).be.exactly(null); + done(); + }); + }); + + it('should call query with right arguments', function (done) { + var + doc = {username: 'foo'}, + spy = sandbox.stub(kuzzle, 'query').yields(null, {result: doc}), + args; + + kuzzle.security.updateCredentials('strategy', 'kuid', doc, function (err, res) { + should(res).be.exactly(doc); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('security'); + should(args[0].action).be.exactly('updateCredentials'); + should(args[2]).be.exactly(null); + done(); + }); + }); + }); + + describe('#validateCredentials', function() { + beforeEach(function() { + kuzzle = new Kuzzle('foo'); + }); + + it('should trigger callback with an error', function (done) { + var + cberror = {message: 'i am an error'}, + spy = sandbox.stub(kuzzle, 'query').yields(cberror), + args; + + kuzzle.security.validateCredentials('strategy', 'kuid', {username: 'foo'}, function (err) { + should(err).be.exactly(cberror); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('security'); + should(args[0].action).be.exactly('validateCredentials'); + should(args[2]).be.exactly(null); + done(); + }); + }); + + it('should call query with right arguments', function (done) { + var + doc = {username: 'foo'}, + spy = sandbox.stub(kuzzle, 'query').yields(null, {result: true}), + args; + + kuzzle.security.validateCredentials('strategy', 'kuid', doc, function (err, res) { + should(res).be.exactly(true); + args = spy.firstCall.args; + + should(spy.calledOnce).be.true(); + + should(args[0].controller).be.exactly('security'); + should(args[0].action).be.exactly('validateCredentials'); + should(args[2]).be.exactly(null); + done(); + }); + }); + }); +});