测试用jwt令牌保护的路由的正确方法是什么?

问题描述

虽然以下测试通过,但我感觉自己做错了。我是否需要在每次需要测试安全路由时登录?我已经尝试在获得初始令牌后传递全局变量,但是传递变量却使我感到非常不直观。在before()调用中传递变量给我带来了与在嵌套的promise中传递/访问全局var相同的问题。

describe('Users',function(done) {
  var testToken = 'my-test-token'
  it('logs in',function(done) { // <= Pass in done callback
    var rT = 'tttttt'
    chai.request(urlroot)
      .post('/login')
      .type('form')
      .send({ email: '[email protected]',password: '9999' })
      .end(function(err,res) {
        expect(res).to.have.status(200);
        expect(res.body.token).to.be.a('string');
        done()
      });
  });

  it('gets all users',function(done) { // <= Pass in done callback
    // console.log(urlroot + '/users');
    chai.request(urlroot)
      .post('/login')
      .type('form')
      .send({ email: '[email protected]',res) {
        chai.request(urlapi)
          .get('/users?secret_token='+res.body.token)
          .end(function(err,res){
            console.log('data',res.body);
            // expect(res.body).to.be.json()
          })
      });
  });
});

解决方法

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

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

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