-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
642 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |
Oops, something went wrong.