Skip to content

Commit 8384c7b

Browse files
committed
auth.validateMyCredentials
1 parent a860b80 commit 8384c7b

File tree

5 files changed

+35
-111
lines changed

5 files changed

+35
-111
lines changed

src/Auth.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,19 @@ class Auth {
197197
.then(res => res.result);
198198
}
199199

200+
/**
201+
* Validate credentials of the specified <strategy> for the current user.
202+
*
203+
* @param strategy
204+
* @param credentials
205+
* @param options
206+
* @returns {Promise|*|PromiseLike<T>|Promise<T>}
207+
*/
208+
validateMyCredentials (strategy, credentials, options) {
209+
return this.kuzzle.query({controller: 'auth', action: 'validateMyCredentials'}, {strategy, body: credentials}, options)
210+
.then(res => res.result);
211+
}
212+
200213
}
201214

202215
module.exports = Auth;

src/Kuzzle.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -441,27 +441,6 @@ class Kuzzle extends KuzzleEventEmitter {
441441
return this.jwt;
442442
}
443443

444-
/**
445-
* Validate credentials of the specified <strategy> for the current user.
446-
*
447-
* @param strategy
448-
* @param credentials
449-
* @param options
450-
* @param cb
451-
*/
452-
validateMyCredentials (strategy, credentials, options, cb) {
453-
if (!cb && typeof options === 'function') {
454-
cb = options;
455-
options = null;
456-
}
457-
458-
this.query({controller: 'auth', action: 'validateMyCredentials'}, {strategy, body: credentials}, options, (err, res) => {
459-
if (typeof cb === 'function') {
460-
cb(err, err ? undefined : res.result);
461-
}
462-
});
463-
}
464-
465444
/**
466445
* Create a kuzzle index
467446
*

test/Auth/methods.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,4 +422,26 @@ describe.only('Kuzzle Auth controller', function () {
422422
});
423423
});
424424
});
425+
426+
describe('#validateMyCredentials', function () {
427+
const expectedQuery = {
428+
controller: 'auth',
429+
action: 'validateMyCredentials'
430+
},
431+
result = {
432+
result: true
433+
};
434+
435+
it('should call query with the right arguments and return Promise which resolves a boolean', () => {
436+
kuzzle.query.resolves(result);
437+
438+
return auth.validateMyCredentials('strategy', {username: 'foo'})
439+
.then(res => {
440+
should(kuzzle.query).be.calledOnce();
441+
should(kuzzle.query).be.calledWith(expectedQuery, {strategy: 'strategy', body: { username: 'foo'}}, undefined);
442+
should(res).be.eql(result.result);
443+
});
444+
});
445+
});
446+
425447
});

test/kuzzle/constructor.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ describe('Kuzzle constructor', function () {
5353
should(kuzzle.startQueuing).be.a.Function();
5454
should(kuzzle.stopQueuing).be.a.Function();
5555
should(kuzzle.unsetJwt).be.a.Function();
56-
should(kuzzle.validateMyCredentials).be.a.Function();
5756
});
5857

5958
it('should expose the documented writable properties', function () {
@@ -285,6 +284,5 @@ describe('Kuzzle constructor', function () {
285284
should(kuzzle.startQueuingPromise).be.undefined();
286285
should(kuzzle.stopQueuingPromise).be.undefined();
287286
should(kuzzle.unsetJwtPromise).be.undefined();
288-
should(kuzzle.validateMyCredentialsPromise).be.a.Function();
289287
});
290288
});

test/kuzzle/methods.test.js

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -840,92 +840,4 @@ describe('Kuzzle methods', function () {
840840
});
841841
});
842842

843-
describe('#updateSelf', function () {
844-
var expectedQuery = {
845-
action: 'updateSelf',
846-
controller: 'auth'
847-
};
848-
849-
beforeEach(function () {
850-
sandbox.stub(kuzzle, 'query').callsFake(queryStub);
851-
result = {
852-
result: {
853-
_id: 'foobar',
854-
_source: {
855-
name: {
856-
first: 'John',
857-
last: 'Doe'
858-
}
859-
}
860-
}
861-
};
862-
});
863-
864-
it('should send the right query to Kuzzle even without callback', function () {
865-
kuzzle.updateSelf({foo: 'bar'});
866-
should(kuzzle.query).be.calledOnce();
867-
should(kuzzle.query).be.calledWith(expectedQuery, {body: {foo: 'bar'}});
868-
});
869-
870-
it('should send the right query to Kuzzle', function () {
871-
var cb = sinon.stub();
872-
873-
kuzzle.updateSelf({foo: 'bar'}, cb);
874-
should(kuzzle.query).be.calledOnce();
875-
should(kuzzle.query).be.calledWith(expectedQuery, {body: {foo: 'bar'}});
876-
should(cb).be.calledOnce();
877-
should(cb).be.calledWithExactly(null, result.result);
878-
879-
});
880-
881-
it('should execute the callback with an error if an error occurs', function () {
882-
var cb = sinon.stub();
883-
884-
error = 'foobar';
885-
kuzzle.updateSelf({foo: 'bar'}, cb);
886-
should(cb).be.calledOnce();
887-
should(cb).be.calledWithExactly('foobar', undefined);
888-
});
889-
});
890-
891-
describe('#validateMyCredentials', function() {
892-
it('should trigger callback with an error', function (done) {
893-
var
894-
cberror = {message: 'i am an error'},
895-
spy = sandbox.stub(kuzzle, 'query').yields(cberror),
896-
args;
897-
898-
kuzzle.validateMyCredentials('strategy', {username: 'foo'}, function (err) {
899-
should(err).be.exactly(cberror);
900-
args = spy.firstCall.args;
901-
902-
should(spy).be.calledOnce();
903-
904-
should(args[0].controller).be.exactly('auth');
905-
should(args[0].action).be.exactly('validateMyCredentials');
906-
should(args[2]).be.exactly(null);
907-
done();
908-
});
909-
});
910-
911-
it('should call query with right arguments', function (done) {
912-
var
913-
doc = {username: 'foo'},
914-
spy = sandbox.stub(kuzzle, 'query').yields(null, {result: true}),
915-
args;
916-
917-
kuzzle.validateMyCredentials('strategy', doc, function (err, res) {
918-
should(res).be.exactly(true);
919-
args = spy.firstCall.args;
920-
921-
should(spy).be.calledOnce();
922-
923-
should(args[0].controller).be.exactly('auth');
924-
should(args[0].action).be.exactly('validateMyCredentials');
925-
should(args[2]).be.exactly(null);
926-
done();
927-
});
928-
});
929-
});
930-
931843
});

0 commit comments

Comments
 (0)