mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-24 16:32:29 +00:00
test(nextcloud-talk): cover signature and format helpers
This commit is contained in:
36
extensions/nextcloud-talk/src/format.test.ts
Normal file
36
extensions/nextcloud-talk/src/format.test.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
escapeNextcloudTalkMarkdown,
|
||||
formatNextcloudTalkCodeBlock,
|
||||
formatNextcloudTalkInlineCode,
|
||||
formatNextcloudTalkMention,
|
||||
markdownToNextcloudTalk,
|
||||
stripNextcloudTalkFormatting,
|
||||
truncateNextcloudTalkText,
|
||||
} from "./format.js";
|
||||
|
||||
describe("nextcloud talk format helpers", () => {
|
||||
it("keeps markdown mostly intact while trimming outer whitespace", () => {
|
||||
expect(markdownToNextcloudTalk(" **hello** ")).toBe("**hello**");
|
||||
});
|
||||
|
||||
it("escapes markdown-sensitive characters", () => {
|
||||
expect(escapeNextcloudTalkMarkdown("*hello* [x](y)")).toBe("\\*hello\\* \\[x\\]\\(y\\)");
|
||||
});
|
||||
|
||||
it("formats mentions and code consistently", () => {
|
||||
expect(formatNextcloudTalkMention("@alice")).toBe("@alice");
|
||||
expect(formatNextcloudTalkMention("bob")).toBe("@bob");
|
||||
expect(formatNextcloudTalkCodeBlock("const x = 1;", "ts")).toBe("```ts\nconst x = 1;\n```");
|
||||
expect(formatNextcloudTalkInlineCode("x")).toBe("`x`");
|
||||
expect(formatNextcloudTalkInlineCode("x ` y")).toBe("`` x ` y ``");
|
||||
});
|
||||
|
||||
it("strips markdown formatting and truncates on word boundaries", () => {
|
||||
expect(
|
||||
stripNextcloudTalkFormatting("**bold** [link](https://example.com) `code`"),
|
||||
).toBe("bold link");
|
||||
expect(truncateNextcloudTalkText("alpha beta gamma delta", 14)).toBe("alpha beta...");
|
||||
expect(truncateNextcloudTalkText("short", 14)).toBe("short");
|
||||
});
|
||||
});
|
||||
66
extensions/nextcloud-talk/src/signature.test.ts
Normal file
66
extensions/nextcloud-talk/src/signature.test.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
extractNextcloudTalkHeaders,
|
||||
generateNextcloudTalkSignature,
|
||||
verifyNextcloudTalkSignature,
|
||||
} from "./signature.js";
|
||||
|
||||
describe("nextcloud talk signature helpers", () => {
|
||||
it("verifies generated signatures against the same body and secret", () => {
|
||||
const body = JSON.stringify({ hello: "world" });
|
||||
const generated = generateNextcloudTalkSignature({
|
||||
body,
|
||||
secret: "secret-123",
|
||||
});
|
||||
|
||||
expect(generated.random).toMatch(/^[0-9a-f]{64}$/);
|
||||
expect(generated.signature).toMatch(/^[0-9a-f]{64}$/);
|
||||
expect(
|
||||
verifyNextcloudTalkSignature({
|
||||
signature: generated.signature,
|
||||
random: generated.random,
|
||||
body,
|
||||
secret: "secret-123",
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects missing fields and mismatched signatures", () => {
|
||||
expect(
|
||||
verifyNextcloudTalkSignature({
|
||||
signature: "",
|
||||
random: "abc",
|
||||
body: "body",
|
||||
secret: "secret",
|
||||
}),
|
||||
).toBe(false);
|
||||
expect(
|
||||
verifyNextcloudTalkSignature({
|
||||
signature: "deadbeef",
|
||||
random: "abc",
|
||||
body: "body",
|
||||
secret: "secret",
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("extracts normalized webhook headers", () => {
|
||||
expect(
|
||||
extractNextcloudTalkHeaders({
|
||||
"x-nextcloud-talk-signature": "sig",
|
||||
"x-nextcloud-talk-random": "rand",
|
||||
"x-nextcloud-talk-backend": "backend",
|
||||
}),
|
||||
).toEqual({
|
||||
signature: "sig",
|
||||
random: "rand",
|
||||
backend: "backend",
|
||||
});
|
||||
|
||||
expect(
|
||||
extractNextcloudTalkHeaders({
|
||||
"X-Nextcloud-Talk-Signature": "sig",
|
||||
}),
|
||||
).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user