Skip to content

Commit

Permalink
test: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kuoruan committed Jan 4, 2025
1 parent 49f76a2 commit c50708c
Show file tree
Hide file tree
Showing 6 changed files with 642 additions and 2 deletions.
88 changes: 88 additions & 0 deletions tests/plugin/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import type { Request } from "express";

import {
base64Decode,
base64Encode,
getBaseUrl,
getClaimsFromIdToken,
hashObject,
isNowBefore,
} from "@/server/plugin/utils";

describe("base64Encode", () => {
it("should encode string to base64", () => {
expect(base64Encode("test")).toBe("dGVzdA");
});
});

describe("base64Decode", () => {
it("should decode base64 string", () => {
expect(base64Decode("dGVzdA")).toBe("test");
});
});

describe("hashObject", () => {
it("should return string directly if input is string", () => {
expect(hashObject("test")).toBe("test");
});

it("should hash object to sha256", () => {
const obj = { test: "value" };
expect(hashObject(obj)).toMatch(/^[a-f0-9]{64}$/);
});
});

describe("getClaimsFromIdToken", () => {
it("should extract claims from id token", () => {
const token = "header.eyJ0ZXN0IjoidmFsdWUifQ.signature";
expect(getClaimsFromIdToken(token)).toEqual({ test: "value" });
});

it("should throw on invalid token format", () => {
expect(() => getClaimsFromIdToken("invalid")).toThrow("Invalid id token");
});
});

describe("isNowBefore", () => {
it("should return true if now is before expireAt", () => {
const future = Math.floor(Date.now() / 1000) + 3600;
expect(isNowBefore(future)).toBe(true);
});

it("should return false if now is after expireAt", () => {
const past = Math.floor(Date.now() / 1000) - 3600;
expect(isNowBefore(past)).toBe(false);
});
});

describe("getBaseUrl", () => {
it("should return base url with trailing slash", () => {
const req = {
hostname: "localhost",
protocol: "http",
ip: "127.0.0.1",
headers: {},
} as Request;

vi.mock("@verdaccio/url", () => ({
getPublicUrl: vi.fn().mockReturnValue("http://localhost/"),
}));

expect(getBaseUrl("/prefix", req)).toBe("http://localhost/");
});

it("should return base url without trailing slash", () => {
const req = {
hostname: "localhost",
protocol: "http",
ip: "127.0.0.1",
headers: {},
} as Request;

vi.mock("@verdaccio/url", () => ({
getPublicUrl: vi.fn().mockReturnValue("http://localhost/"),
}));

expect(getBaseUrl("/prefix", req, true)).toBe("http://localhost");
});
});
46 changes: 46 additions & 0 deletions tests/query-params.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { parseQueryParams, stringifyQueryParams } from "@/query-params";

describe("parseQueryParams", () => {
it("should return empty object for empty string", () => {
expect(parseQueryParams("")).toEqual({});
});

it("should parse single key-value pair", () => {
expect(parseQueryParams("foo=bar")).toEqual({ foo: "bar" });
});

it("should parse multiple key-value pairs", () => {
expect(parseQueryParams("foo=bar&baz=qux")).toEqual({
foo: "bar",
baz: "qux",
});
});

it("should handle question mark prefix", () => {
expect(parseQueryParams("?foo=bar")).toEqual({ foo: "bar" });
});

it("should decode URI components", () => {
expect(parseQueryParams("foo=hello%20world")).toEqual({
foo: "hello world",
});
});
});

describe("stringifyQueryParams", () => {
it("should return empty string for empty object", () => {
expect(stringifyQueryParams({})).toBe("");
});

it("should stringify single key-value pair", () => {
expect(stringifyQueryParams({ foo: "bar" })).toBe("foo=bar");
});

it("should stringify multiple key-value pairs", () => {
expect(stringifyQueryParams({ foo: "bar", baz: "qux" })).toBe("foo=bar&baz=qux");
});

it("should encode URI components", () => {
expect(stringifyQueryParams({ foo: "hello world" })).toBe("foo=hello%20world");
});
});
Loading

0 comments on commit c50708c

Please sign in to comment.