aboutsummaryrefslogtreecommitdiff
path: root/test/js/third_party/jsonwebtoken/claim-private.test.js
blob: 51c56edb25671165c32f5753d181058a62893dca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"use strict";

import { expect, describe, it } from "bun:test";
import util from "util";
import testUtils from "./test-utils";

function signWithPayload(payload, callback) {
  testUtils.signJWTHelper(payload, "secret", { algorithm: "HS256" }, callback);
}

describe("with a private claim", function () {
  [true, false, null, -1, 0, 1, -1.1, 1.1, "", "private claim", "UTF8 - José", [], ["foo"], {}, { foo: "bar" }].forEach(
    privateClaim => {
      it(`should sign and verify with claim of ${util.inspect(privateClaim)}`, function (done) {
        signWithPayload({ privateClaim }, (e1, token) => {
          testUtils.verifyJWTHelper(token, "secret", {}, (e2, decoded) => {
            testUtils.asyncCheck(done, () => {
              expect(e1).toBeNull();
              expect(e2).toBeNull();
              expect(decoded).toHaveProperty("privateClaim", privateClaim);
            });
          });
        });
      });
    },
  );

  // these values JSON.stringify to null
  [-Infinity, Infinity, NaN].forEach(privateClaim => {
    it(`should sign and verify with claim of ${util.inspect(privateClaim)}`, function (done) {
      signWithPayload({ privateClaim }, (e1, token) => {
        testUtils.verifyJWTHelper(token, "secret", {}, (e2, decoded) => {
          testUtils.asyncCheck(done, () => {
            expect(e1).toBeNull();
            expect(e2).toBeNull();
            expect(decoded).toHaveProperty("privateClaim", null);
          });
        });
      });
    });
  });

  // private claims with value undefined are not added to the payload
  it(`should sign and verify with claim of undefined`, function (done) {
    signWithPayload({ privateClaim: undefined }, (e1, token) => {
      testUtils.verifyJWTHelper(token, "secret", {}, (e2, decoded) => {
        testUtils.asyncCheck(done, () => {
          expect(e1).toBeNull();
          expect(e2).toBeNull();
          expect(decoded).not.toHaveProperty("privateClaim");
        });
      });
    });
  });
});