From a603ffa0f53c73bd078e8a1eb0a74c81f1d556c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Fri, 14 Nov 2014 10:37:22 +0100 Subject: [PATCH] AccessToken: optional `options` in findForRequest Fix `AccessToken.findForRequest` to correctly handle the case when the options argument was omitted: AccessToken.findForRequest(req, cb); --- common/models/access-token.js | 5 +++++ test/access-token.test.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/common/models/access-token.js b/common/models/access-token.js index 1c4f1f7e..4e01a33d 100644 --- a/common/models/access-token.js +++ b/common/models/access-token.js @@ -88,6 +88,11 @@ module.exports = function(AccessToken) { */ AccessToken.findForRequest = function(req, options, cb) { + if (cb === undefined && typeof options === 'function') { + cb = options; + options = {}; + } + var id = tokenIdForRequest(req, options); if (id) { diff --git a/test/access-token.test.js b/test/access-token.test.js index 1a604dc2..14905b75 100644 --- a/test/access-token.test.js +++ b/test/access-token.test.js @@ -1,4 +1,5 @@ var loopback = require('../'); +var extend = require('util')._extend; var Token = loopback.AccessToken.extend('MyToken'); var ACL = loopback.ACL; @@ -106,6 +107,38 @@ describe('AccessToken', function () { done(); }); }); + + describe('.findForRequest()', function() { + beforeEach(createTestingToken); + + it('supports two-arg variant with no options', function(done) { + var expectedTokenId = this.token.id; + var req = mockRequest({ + headers: { 'authorization': expectedTokenId } + }); + + Token.findForRequest(req, function(err, token) { + if (err) return done(err); + expect(token.id).to.eql(expectedTokenId); + done(); + }); + }); + + function mockRequest(opts) { + return extend( + { + method: 'GET', + url: '/a-test-path', + headers: {}, + _params: {}, + + // express helpers + param: function(name) { return this._params[name]; }, + header: function(name) { return this.headers[name]; } + }, + opts); + } + }); }); describe('app.enableAuth()', function() {