开玩笑-测试证书验证

时间:2020-06-26 08:04:19

标签: node.js jestjs

寻求关于通过一些证书处理功能的最佳处理单元测试的建议。

我有一个验证功能,可以针对根CA验证传入的证书(通过和API调用),另一个功能使用上述证书对某些数据进行加密,然后再将其传递回调用方。

考虑如何可靠地进行测试,我想到了使用OpenSSL创建自己的CA和所需的测试证书的想法。我可以将它们保存为项目测试目录中的文件,并在进一步将它们用作我正在测试的功能的模拟之前,先进行测试以检查其是否有效。

我在这里的想法是,可以将测试证书毫无问题地保存到源代码控制中,并且将成为可靠的测试用例。

这听起来像是一种合理的方法,还是过度杀伤力?

1 个答案:

答案 0 :(得分:1)

我的观点是,单元测试应该只在正面和负面的情况下测试您的实现,并且应该模拟所有API调用。这样,您可以针对每个代码更改运行测试,并且如果服务不可用,测试也不会失败。

这是示例解决方案概述:

import implementation from "./implementation";
import certificate from "./cerficiates/genuine";
import request from "request-library";

jest.mock("request-library");

describe("Certificate validation", () => {
  describe("validate", () => {
    describe("returning negative response", () => {
      beforeAll(() => {
        let result;
        beforeAll(() => {
          request.post.mockClear();
          request.post.mockResolvedValue({
            body: "invalid certificate",
          });
          result = implementation.validate(certificate);
        });

        it("should have made a post request with certificate", async () => {
          await result;
          expect(request.post).toHaveBeenCalledWith(
            "http://validate.certificate.com",
            {
              body: certificate,
            }
          );
        });

        it("should return error", async () => {
          await expect(result).rejects.toEqual(new Error("invlid certificate"));
        });
      });
    });

    describe("returning positive response", () => {
      let result;
      beforeAll(() => {
        request.post.mockClear();
        request.post.mockResolvedValue({
          body: "valid certificate",
        });
        result = implementation.validate(certificate);
      });

      it("should have made a post request with certificate", async () => {
        await result;
        expect(request.post).toHaveBeenCalledWith(
          "http://validate.certificate.com",
          {
            body: certificate,
          }
        );
      });

      it("should return true", async () => {
        expect(await result).toBe(true);
      });
    });
  });
});

此解决方案遵循单元测试的首要原则

✅[F]:快速,因为它不依赖于本地/远程任何服务。不能因为 测试超时/服务不可用

✅[I]:不依赖任何东西

✅[R]:可重复,因为每次运行它都会再现相同的结果

✅[S]:具有自我验证功能,因为它不会依靠任何东西来检查它是否必须通过或失败

✅[T]:请及时在实现之前/与实现一起编写