我使用的是koa-jwt,而后者又使用jsonwebtoken。以下是我的路由器实现:
index.ts
const jwt = require('koa-jwt');
...
// some other code
...
export const publicEndpoints = ['/', '/openapi.json', '/healthcheck'];
export default new Router<ApplicationState, ApplicationContext>()
.use(configure)
.use((ctx,next) => {
console.log("REACH 02 opts",ctx,next);
console.log("REACH 02 jwt", jwt);
})
.use(
jwt({
secret: customSecretLoader,
audience: (jwksConfig.audience as unknown) as string,
algorithms: ['RS256'],
}).unless({ path: publicEndpoints })
)
// Public endpoints
.use('/openapi.json', swagger)
.use('/', helloworld)
.use('/healthcheck', healthcheck)
// Secure endpoints
.get('/secure', helloworld)
.middleware();调用/secure应该通过调用jwt并传递令牌的中间件。
我希望测试每一条安全路由,以确保它不会传递任何没有正确令牌的请求,并传递那些没有正确令牌的请求。
以前的情况很简单,我只需要打电话给一条安全的路线:
index.test.ts
it('Unauthenticated secure request returns 401', async () => {
const response = await request(server).get('/secure');
expect(response.status).toEqual(401);
});但是,为了使它正常工作,我需要模拟jwt()函数调用并让它返回200,但是问题是,不管我在测试中写了什么,我仍然调用koa-jwt的原始实现。
*为了有一些上下文,这是我正在尝试模拟的koa-js库https://github.com/koajs/jwt/blob/master/lib/index.js。它返回middleware(),它调用verify,而后者又使用jsonwebtoken


模拟整个导出的函数
index.test.ts
`var jwt = require('koa-jwt');`
...
// some code
...
it('Authenticated secure request returns 200', async () => {
jwt = jest.fn(() => {
Promise.resolve({
status: 200,
success: 'Token is valid'
});
});
console.log("REACH 01 jwt", jwt);
const response = await request(server).get('/secure');
console.log("REACH RESPONSE",response);
expect(response.status).toEqual(200);
});我从控制台日志中获得的输出是:
REACH 01 jwt function mockConstructor() {
return fn.apply(this, arguments);
}这正是我所期望的,但是当jwt()在index.ts中被击中时,我得到的输出是:
REACH 02 jwt (opts = {}) => {
const { debug, getToken, isRevoked, key = 'user', passthrough, tokenKey } = opts;
const tokenResolvers = [resolveCookies, resolveAuthHeader];
if (getToken && typeof getToken === 'function') {
tokenResolvers.unshift(getToken);
}
const middleware = async function jwt(ctx, next) {
let token;
tokenResolvers.find(resolver => token = resolver(ctx, opts));
.....我希望在模拟了koa-jwt之后,我会在两个控制台日志中看到相同的输出。
我尝试了几种不同的方法,给出了相同的结果:-在koa-js中模拟导出的默认函数中的中间件函数--模拟koa-js的依赖关系,即jsonwebtoken。
我遗漏了什么?
发布于 2019-05-06 05:30:00
解决方案是在测试的外部模拟整个模块:
import....
// some other code
jest.mock('koa-jwt', () => {
const fn = jest.fn((opts) => // 1st level i.e. jwt()
{
const middlewareMock = jest.fn(async (ctx, next) => { // 2nd level i.e. middleware()
// Unreachable
});
// @ts-ignore
middlewareMock.unless = jest.fn(() => jest.fn((ctx, next) => {
next();
})); // 4th level i.e. middleware().unless()
return middlewareMock;
});
return fn;
});
...
describe('routes: index', () => {
// Testing each secure endpoint with authentication
it('Authenticated requests to secure endpoints return 200', async () => {
secureEndpoints.forEach(async (endpoint) => {
const response = await request(server).get(endpoint);
expect(response.status).toEqual(200);
});
});
});https://stackoverflow.com/questions/55997485
复制相似问题