带有异步功能的 sinon 的 hapi 身份验证策略业力测试

问题描述

我正在尝试使用 hapi 服务器测试身份验证方案。我在放置身份验证方案的同一文件中有两个辅助函数。我想测试何时成功验证用户。但在我的测试用例中,我总是得到 401,这是未经身份验证的消息。

export const hasLegitItemUser = async (request,email,id) => {
  const {
    status,payload: {users}
  } = await svc.getRel(request,email);
  if (status !== STATUS.OK) {
    return false;
  }

  return users.includes(user) 
};

export const getUser = async request => {
  const token = request.state._token;
  const res = await svc.validatetoken({request,token});
  const {
    userInfo: {email}
  } = res;
  const id = extractId(request.path);
  const isLetgitUser = await hasLegitItemUser(
    request,id
  );
  res.isLegitUser = isLegitUser;
  return res;
};


const scheme = (server,options) => {
  server.state("my_sso",options.cookie);

  server.ext("onPostAuth",(request,h) => {
    return h.continue;
  });

  return {
    async authenticate(request,h) {
      try {
        const {
          tokenValid,isLegitUser,userInfo
        } = await getUser(request);
        if (tokenValid && isLegitUser) {
          request.state["SSO"] = {
            TOKEN: request.state._token
          };

          return h.authenticated({
            credentials: {
              userInfo
            }
          });
        } else {
          throw Boom.unauthorized(null,"my_auth");
        }
      } catch (err) {
        throw Boom.unauthorized(null,"my_auth");
      }
    }
  };
};

我的测试文件

import Hapi from "hapi";
import sinon from "sinon";
import auth,* as authHelpers from "server/auth";
import {expect} from "chai";
import pcSvc from "server/plugins/services/pc-svc";

describe("Authentication Plugin",() => {
const sandBox = sinon.createSandBox();
  const server = new Hapi.Server();
  const authHandler = request => ({
    credentials: request.auth.credentials,artifacts: "boom"
  });
before(() => {
    server.register({
      plugin: auth,});
const route = ["/mypage/{id}/home"];
    route.forEach(path => {
      server.route({
        method: "GET",path,options: {
          auth: auth,handler:{}
        }
      });
    });
  });

  afterEach(() => {
    sandBox.restore();
  });

it("should authorize user if it is a validated user",async () => {
    sandBox
      .stub(authHelpers,"getUser")
      .withArgs(request)
      .resolves({
        tokenValid: true,isLegitUser: true,userInfo: {}
      });

    return server
      .inject({
        method: "GET",url:
          "/mypage/888/home"
      })
      .then(res => {
        expect(res.statusCode).to.equal(200);
        expect(res.result).to.eql({
          userInfo: {
            email: "abc@gmail.com",rlUserId: "abc",userId: "abc@gmail.com"
          }
        });
      });
  });
});

我总是收到未经身份验证的 401 错误。似乎我的测试中的“getUser”函​​数由于某种原因没有触发,它直接进入我代码中 catch 阶段的 throw 语句。请帮忙。

解决方法

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

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

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...