我正在尝试对一个简单的Express中间件进行单元测试,这是一个级联的athenticator,首先使用passport-jwt-strategy
检查JWT令牌,如果失败则使用passport-openid-strategy
。每个策略都经过了很好的测试,所以我要测试的是它们的集成。
我正在测试的模块如下所示:
"use strict";
let passport = require('passport');
let Strategies = require('./strategies');
let setupDone = false;
// set up passport
let setup = function(app) {
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
passport.use('jwt', Strategies.jwt);
passport.use('openid', Strategies.openId);
app.use(passport.initialize());
app.use(passport.session());
setupDone = true;
};
let authenticate = function(req, res, next) {
if (!setupDone) throw new Error('You must have run setup(app) before you can use the middleware');
console.log(' cascadingAuthentication');
// first try the token option
passport.authenticate('jwt', function(jwterr, user, info) {
console.log(' jwt auth', jwterr, user, info);
if (jwterr || !user) {
passport.authenticate('openid, function(oautherr, user, info) {
if (oautherr || !user) {
return next(oautherr);
} else {
next();
}
});
} else {
req.user = user;
next();
}
});
};
module.exports = {
setup: setup,
authenticate: authenticate
}
我的Jasmine
测试看起来像这样
"use strict";
let CascadingAuthentication = require('../../lib/middleware/cascadingAuthentication');
let TokenUtils = require('../support/tokenUtils');
let email = 'testing@test.tes;
describe('cascadingAuthentication', function() {
describe('when there is a token in the header', function() {
let req;
let res = {};
let app = {
use: function(used) { console.log('app.use called with', typeof used); }
};
beforeEach(function(done) {
let token = TokenUtils.makeJWT(email);
req = {
app: app,
header: {
Authorization: `Bearer ${token}`
}
}
CascadingAuthentication.setup(app);
CascadingAuthentication.authenticate(req, res, function() {
done();
});
});
it('populates req.user', function() {
expect(req.user).toEqual(jasmine.any(Object));
});
});
});
我遇到的问题是,当我运行测试时,我会看到第一个console.log(' cascadingAuthentication')
,但我从未看到第二个console.log('jwt auth', err, user, info)
。代码只是在passport.authenticate
内部消失而没有调用回调,没有引发错误,或者根本没有提供任何反馈。
我正在gulp
使用Jasmine
运行我的测试。
我的问题是:按顺序,
req
,res
或app
中是否还有其他任何可能会使此测试工作的内容?console.log
语句(这对我来说似乎有点80年代)。答案 0 :(得分:0)
通过passport
的源代码我已经解决了我的代码存在两个问题。
第一个是passport.authenticate
返回中间件函数,它实际上并不执行该函数。所以解决方案只是调用返回的函数。
所以我的身份验证方法现在看起来像:
let authenticate = function(req, res, next) {
if (!setupDone) throw new Error('You must have run setup(app) before you can use the middleware');
// first try the token option
passport.authenticate('jwt', function(jwterr, user, info) {
if (jwterr || !user) {
passport.authenticate('openid', function(autherr, user, info) {
if (autherr || !user) {
return next(autherr);
} else {
next();
}
})(req, res, next);
} else {
req.user = user;
next();
}
})(req, res, next);
};
(上面的例子被修剪用于问题)
另一个问题是在我的测试中,我在我的模拟header
对象中使用了headers
而不是req
,并且authorization
也应该有一个小写a
ng-bind-html
1}}。
通过这两个修正,现在测试通过了。
答案 1 :(得分:0)
我摆弄了这个很长一段时间,最终进入了以下设置(测试passport.authenticate('local', () => {})
)。
<强> AUTH-router.js 强>
const express = require('express');
const passport = require('passport');
const login = (req, res, next) => {
passport.authenticate('local', (err, user, info) => {
if (err) {
next(err);
return;
}
if (!user) {
const error = new Error(info.message);
error.status = 404;
next(error);
return;
}
// Add the found user record to the request to
// allow other middlewares to access it.
req.user = user;
next();
})(req, res, next);
};
const router = express.Router();
router.post('/auth/login', login);
module.exports = {
login,
router
};
<强> AUTH-router.spec.js 强>
const passport = require('passport');
describe('login', () => {
it('should login and add the user to the request object', (done) => {
spyOn(passport, 'authenticate').and.callFake((strategy, callback) => {
const err = null;
const user = {};
const info = {};
callback(err, user, info);
return (req, res, next) => {};
});
const auth = require('./auth'); // my middleware function
const req = { body: {} };
const res = {};
const next = () => {
expect(req.user).toBeDefined();
done();
};
auth.login(req, res, next);
});
});