57 lines
2.1 KiB
TypeScript
57 lines
2.1 KiB
TypeScript
import { assertEquals } from "@std/assert";
|
|
import {
|
|
instanceHost,
|
|
isFederationEvent,
|
|
parseInstance,
|
|
} from "../federation.ts";
|
|
import { HANDLE_RE, localHandle, splitHandle } from "../validation.ts";
|
|
|
|
Deno.test("parseInstance: a host or a URL names an instance's origin", () => {
|
|
assertEquals(parseInstance("music.example"), "https://music.example");
|
|
assertEquals(parseInstance(" Music.Example/ "), "https://music.example");
|
|
assertEquals(
|
|
parseInstance("http://localhost:8001"),
|
|
"http://localhost:8001",
|
|
);
|
|
assertEquals(parseInstance("localhost:8001"), "https://localhost:8001");
|
|
assertEquals(parseInstance("https://music.example/path"), null);
|
|
assertEquals(parseInstance("ftp://music.example"), null);
|
|
assertEquals(parseInstance("https://u:p@music.example"), null);
|
|
assertEquals(parseInstance(""), null);
|
|
assertEquals(instanceHost("http://localhost:8001"), "localhost:8001");
|
|
});
|
|
|
|
Deno.test("handles: this instance's host is dropped, another's kept", () => {
|
|
const self = (host: string) => host === "gerbeur.example";
|
|
assertEquals(localHandle("bob@gerbeur.example", self), "bob");
|
|
assertEquals(localHandle("bob@music.example", self), "bob@music.example");
|
|
assertEquals(localHandle("bob", self), "bob");
|
|
});
|
|
|
|
Deno.test("handles: a name, and a host for a user from another instance", () => {
|
|
assertEquals(splitHandle("bob"), { name: "bob" });
|
|
assertEquals(splitHandle("alice@music.example"), {
|
|
name: "alice",
|
|
host: "music.example",
|
|
});
|
|
for (const ok of ["bob", "alice@music.example", "c_1@localhost:8001"]) {
|
|
assertEquals(HANDLE_RE.test(ok), true, ok);
|
|
}
|
|
for (const bad of ["", "a@", "a@b.", "a b", "a@b@c", "a@-b"]) {
|
|
assertEquals(HANDLE_RE.test(bad), false, bad);
|
|
}
|
|
});
|
|
|
|
Deno.test("isFederationEvent: an update carries the claims", () => {
|
|
assertEquals(isFederationEvent({ type: "revoke" }), true);
|
|
assertEquals(
|
|
isFederationEvent({
|
|
type: "update",
|
|
claims: { preferred_username: "a", email: "a@b.c" },
|
|
}),
|
|
true,
|
|
);
|
|
assertEquals(isFederationEvent({ type: "update" }), false);
|
|
assertEquals(isFederationEvent({ type: "delete" }), false);
|
|
});
|