NaiveJWT.spec.ts 833 B

12345678910111213141516171819202122232425262728293031
  1. import { describe, expect, it } from "bun:test";
  2. import * as JWT from "./NaiveJWT";
  3. import { pipe } from "fp-ts/lib/function";
  4. import * as Either from "fp-ts/Either";
  5. const secret = "foo";
  6. const sign = JWT.sign(secret);
  7. const verify = JWT.verify(secret);
  8. describe("sign", () => {
  9. it("should return a string", () => {
  10. expect(typeof sign("bar", {}) === "string").toBe(true);
  11. });
  12. it("should have three parts denoted by a .", () => {
  13. const parts = sign("bar", {}).split(".");
  14. expect(parts.length === 3).toBe(true);
  15. });
  16. });
  17. it("should be possible to retrieve the payload", () => {
  18. const payload = { email: "foo@bar.com" };
  19. const jwt = sign(payload, {});
  20. const decoded = pipe(
  21. verify(jwt),
  22. Either.getOrElseW((error) => {
  23. throw error;
  24. }),
  25. );
  26. expect(decoded.payload).toEqual(payload);
  27. });