Skip to content

Commit edbbcaf

Browse files
committed
Correctly handle array of routes for a method
Was previously failing as follows because an array has no "callbacks" property. oauth2-server/lib/oauth2server.js:217 app.routes[method].callbacks.forEach(lockdownExpress3); ^ TypeError: Cannot call method 'forEach' of undefined at OAuth2Server.lockdown (oauth2-server/lib/oauth2server.js:217:36)
1 parent bfd4650 commit edbbcaf

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/oauth2server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ OAuth2Server.prototype.lockdown = function (app) {
213213

214214
if (app.routes) {
215215
for (var method in app.routes) {
216-
app.routes[method].callbacks.forEach(lockdownExpress3);
216+
app.routes[method].forEach(function (route) {
217+
lockdownExpress3(route.callbacks);
218+
});
217219
}
218220
} else {
219221
app._router.stack.forEach(lockdownExpress4);

test/lockdown.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,34 @@ describe('Lockdown pattern', function() {
8686
.get('/public')
8787
.expect(200, /hello/i, done);
8888
});
89+
90+
describe('in express 3', function () {
91+
var app, privateAction, publicAction;
92+
93+
beforeEach(function () {
94+
privateAction = function () {};
95+
publicAction = function () {};
96+
97+
// mock express 3 app
98+
app = {
99+
routes: {
100+
get: [
101+
{ callbacks: [ privateAction ] }
102+
]
103+
}
104+
};
105+
106+
app.oauth = oauth2server({ model: {} });
107+
app.routes.get.push({ callbacks: [ app.oauth.bypass, publicAction ] })
108+
app.oauth.lockdown(app);
109+
});
110+
111+
it('adds authorise to non-bypassed routes', function () {
112+
app.routes.get[0].callbacks[1].should.equal(privateAction);
113+
});
114+
115+
it('removes oauth.bypass from bypassed routes', function () {
116+
app.routes.get[1].callbacks[0].should.equal(publicAction);
117+
});
118+
});
89119
});

0 commit comments

Comments
 (0)