存根 express-jwt用于身份验证的中间件库

问题描述

在这里,我正在努力尝试存根直接从 express-jwt 调用的中间件。这是我的 auth.middleware.ts 文件:

import jwt from 'express-jwt';
import jwks from 'jwks-rsa';


const isAuth = jwt({
  secret: jwks.expressJwtSecret({
    cache                 : true,rateLimit             : true,jwksRequestsPerMinute : 5,jwksUri               : `https://${some.domain.name}/.well-known/jwks.json`,}),audience   : '',issuer     : `https://${some.audience.name}/`,algorithms : [ 'RS256' ],});

export default [ auth ];

当我需要使用它时,只需按如下方式调用它:

import isAuth from './auth.middleware';

app.get('/',isAuth);

问题是当我想存根这个中间件旨在验证每个用户的身份验证时:

//service.spec.ts

import sinon from 'sinon';
import { expect } from 'chai';
import request from 'supertest';
import app from 'my/path/to/app';
import mocks from 'my/path/to/mocks';
import mockTokens from 'my/path/to/mockTokens';
import isAuth from 'my/path/to/auth.middleware';

describe('Users endpoint',function() {
  const sandbox = sinon.createSandbox();

  describe.only('Get by id',function() {
    before('Initial setup',async () => {
      const authentication = sandbox.stub(isAuth);
      authentication[0].callsFake(function(options: any): any {
        console.log('FAKE AUTH!');
        return (res,req,next) => next();
      });
    });

    it('When I am trying to make something nothing works lol',function(done) {
      // Arrange
      const userId = '1234567^';

      // Act
      request(app)
        .get(`/endpoint/to/test/${userId}`)
        .set('Authorization',`Bearer ${mockTokens.no_permissions}`)
        .end((err,res) => {
          if (err) {
            return done(err);
          }

          // Assert
          expect(res.body.data).to.be.eql(mocks.getById.expectedResponse);
          return done();
        });
    });
  });
});

希望有人能帮助我,为什么 auth 方法没有像我想要的那样存根。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)