【发布时间】:2019-05-29 23:36:00
【问题描述】:
我有一个使用 JWT 令牌保护 API 端点的快速应用程序。我有一个方法可以验证收到的令牌。
// authentication.js
import jwt from 'jsonwebtoken';
import Settings from '../settings';
const AuthenticationMiddleware = {
verifyToken: (req, res, next) => {
const token = req.headers['x-access-token'];
if (!token) {
const msg = 'Include a valid token in the x-access-token header';
return res.status(422).json({
error: 'No token provided',
msg
});
}
try {
req.user = jwt.verify(token, Settings.jwtSecret);
req.token = token;
return next();
}
catch (e) {
return res.status(422).json({ error: 'Invalid token' });
}
}
};
export default AuthenticationMiddleware;
当我从 postman 调用包含令牌标头的 API 端点时,这工作正常。
现在我有一个如下所示的测试。其中大约有 40 个,每个都需要在每个 API 请求中发送一个令牌。
// should is not used directly in the file but is added as a mocha requirement
import supertest from 'supertest';
import app from '../app';
const server = supertest.agent(app);
const BASE_URL = '/api/v1';
describe('/loans: Get all loans', () => {
it('should return a list of all loans', done => {
server
.get(`${BASE_URL}/loans`)
.expect(200)
.end((err, res) => {
res.status.should.equal(200);
res.body.data.should.be.an.instanceOf(Array);
for (const each of res.body.data) {
each.should.have.property('id');
each.should.have.property('userid');
}
done();
});
});
});
我查看了 sinon 并尝试像这样在 mocha 的 before 钩子中存根 verifyToken 函数
import sinon from 'sinon';
import AuthenticationMiddleware from '../middleware/authentication';
before(() => {
const stub = sinon.stub(AuthenticationMiddleware, 'verifyToken');
stub.returnsThis()
});
但我已经看到了这里的问题。虽然可能已经创建了 verifyToken 存根,但在测试期间不使用它。在测试期间被调用的verifyToken 作为中间件从路由传递,就像这样
router.get('/loans', AuthenticationMiddleware.verifyToken, LoansController.get_all_loans);
我想要一种在测试期间存根 verifyToken 的方法,以便我可以立即返回 next()。
我的问题是,是否可以在测试期间存根AuthenticationMiddleware.verifyToken通用,以便对 API 端点的所有调用都调用 存根 版本?
【问题讨论】:
标签: javascript testing mocha.js sinon stub