/ opencodehub / tests / unit / personal-access-token.test.ts
swadhinbiswas fix(auth): hash personal access tokens at rest · 2/15/2026
66091b8
30 lines · 1.1 KB
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
import { describe, expect, it } from "vitest";
import {
  getTokenPrefixForDisplay,
  hashPersonalAccessToken,
  isHashedPersonalAccessToken,
  verifyPersonalAccessTokenValue,
} from "@/lib/personal-access-token";

describe("personal-access-token helpers", () => {
  it("hashes and verifies hashed token values", () => {
    const token = "och_example_token_value";
    const stored = hashPersonalAccessToken(token);

    expect(isHashedPersonalAccessToken(stored)).toBe(true);
    expect(verifyPersonalAccessTokenValue(stored, token)).toBe(true);
    expect(verifyPersonalAccessTokenValue(stored, "och_other_token")).toBe(false);
  });

  it("supports legacy plaintext token rows", () => {
    const token = "och_legacy_plain";
    expect(verifyPersonalAccessTokenValue(token, token)).toBe(true);
    expect(verifyPersonalAccessTokenValue(token, "och_other")).toBe(false);
  });

  it("returns safe display prefixes", () => {
    expect(getTokenPrefixForDisplay("och_1234567890abcd")).toBe("och_12345678...");
    expect(getTokenPrefixForDisplay("pat_sha256:abc")).toBe("och_********");
  });
});