mirror of
https://github.com/ReCloudStudio/WebHooker.git
synced 2026-09-23 00:21:28 +00:00
test: cover admin, discord REST, send logs, token-store links and dedup
This commit is contained in:
parent
befed0267a
commit
d68aeb20d5
5 changed files with 321 additions and 53 deletions
|
|
@ -1,60 +1,58 @@
|
|||
import { describe, it, expect, beforeEach, afterEach } from "bun:test";
|
||||
import { existsSync, unlinkSync, mkdirSync } from "fs";
|
||||
import { dirname } from "path";
|
||||
import {
|
||||
saveToken,
|
||||
getToken,
|
||||
removeToken,
|
||||
findUserIdByToken,
|
||||
initTokenStore,
|
||||
} from "../token-store";
|
||||
import { describe, it, expect } from "bun:test";
|
||||
import { saveToken, getToken, removeToken, findUserIdByToken } from "../token-store";
|
||||
|
||||
const TEST_STORE = "./data/test-tokens.json";
|
||||
|
||||
beforeEach(() => {
|
||||
const dir = dirname(TEST_STORE);
|
||||
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
||||
if (existsSync(TEST_STORE)) unlinkSync(TEST_STORE);
|
||||
initTokenStore(TEST_STORE);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (existsSync(TEST_STORE)) unlinkSync(TEST_STORE);
|
||||
});
|
||||
function createMockKV(): KVNamespace {
|
||||
const store = new Map<string, { value: string; expiration?: number }>();
|
||||
return {
|
||||
get: async (key: string, type?: string) => {
|
||||
const entry = store.get(key);
|
||||
if (!entry) return null;
|
||||
if (entry.expiration && Date.now() / 1000 > entry.expiration) {
|
||||
store.delete(key);
|
||||
return null;
|
||||
}
|
||||
if (type === "json") return JSON.parse(entry.value);
|
||||
return entry.value;
|
||||
},
|
||||
put: async (key: string, value: string, opts?: { expirationTtl?: number }) => {
|
||||
const expiration = opts?.expirationTtl ? Date.now() / 1000 + opts.expirationTtl : undefined;
|
||||
store.set(key, { value, expiration });
|
||||
},
|
||||
delete: async (key: string) => {
|
||||
store.delete(key);
|
||||
},
|
||||
list: async () => ({ keys: [...store.keys()].map((k) => ({ name: k })), list_complete: true, cacheStatus: null }),
|
||||
} as unknown as KVNamespace;
|
||||
}
|
||||
|
||||
describe("token-store", () => {
|
||||
it("saves and retrieves token", () => {
|
||||
saveToken("user1", "token-abc", 3600);
|
||||
expect(getToken("user1")).toBe("token-abc");
|
||||
it("saves and retrieves token", async () => {
|
||||
const kv = createMockKV();
|
||||
await saveToken(kv, "user1", "token-abc", 3600);
|
||||
expect(await getToken(kv, "user1")).toBe("token-abc");
|
||||
});
|
||||
|
||||
it("returns null for expired token", () => {
|
||||
saveToken("user1", "token-abc", -1);
|
||||
expect(getToken("user1")).toBeNull();
|
||||
it("returns null for nonexistent user", async () => {
|
||||
const kv = createMockKV();
|
||||
expect(await getToken(kv, "nobody")).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null for nonexistent user", () => {
|
||||
expect(getToken("nobody")).toBeNull();
|
||||
it("removes token and reverse index", async () => {
|
||||
const kv = createMockKV();
|
||||
await saveToken(kv, "user1", "token-abc", 3600);
|
||||
await removeToken(kv, "user1");
|
||||
expect(await getToken(kv, "user1")).toBeNull();
|
||||
expect(await findUserIdByToken(kv, "token-abc")).toBeNull();
|
||||
});
|
||||
|
||||
it("removes token", () => {
|
||||
saveToken("user1", "token-abc", 3600);
|
||||
removeToken("user1");
|
||||
expect(getToken("user1")).toBeNull();
|
||||
it("finds userId by token via reverse index", async () => {
|
||||
const kv = createMockKV();
|
||||
await saveToken(kv, "user1", "token-abc", 3600);
|
||||
expect(await findUserIdByToken(kv, "token-abc")).toBe("user1");
|
||||
});
|
||||
|
||||
it("finds userId by token", () => {
|
||||
saveToken("user1", "token-abc", 3600);
|
||||
expect(findUserIdByToken("token-abc")).toBe("user1");
|
||||
});
|
||||
|
||||
it("returns null for unknown token", () => {
|
||||
expect(findUserIdByToken("unknown")).toBeNull();
|
||||
});
|
||||
|
||||
it("persists across reload", () => {
|
||||
saveToken("user1", "token-abc", 3600);
|
||||
initTokenStore(TEST_STORE);
|
||||
expect(getToken("user1")).toBe("token-abc");
|
||||
it("returns null for unknown token", async () => {
|
||||
const kv = createMockKV();
|
||||
expect(await findUserIdByToken(kv, "unknown")).toBeNull();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue