@fedify/fedify
Version:
An ActivityPub server framework
124 lines (120 loc) • 4.78 kB
JavaScript
import { Temporal } from "@js-temporal/polyfill";
import { URLPattern } from "urlpattern-polyfill";
globalThis.addEventListener = () => {};
import { assertEquals } from "../assert_equals-CTYbeopb.js";
import "../assert-DmFG7ppO.js";
import "../assert_instance_of-CF09JHYM.js";
import "../docloader-C8mmLN-Y.js";
import "../url-kTAI6_KP.js";
import { lookupWebFinger } from "../lookup-Ozuw-rxN.js";
import { test } from "../testing-BZ0dJ4qn.js";
import "../std__assert-vp0TKMS1.js";
import "../assert_rejects-C-sxEMM5.js";
import "../assert_is_error-nrwA1GeT.js";
import "../assert_not_equals-Dc7y-V5Q.js";
import "../assert_throws-Cn9C6Jur.js";
import { esm_default } from "../esm-Db4De7AS.js";
import { withTimeout } from "@es-toolkit/es-toolkit";
//#region webfinger/lookup.test.ts
test({
name: "lookupWebFinger()",
sanitizeOps: false,
sanitizeResources: false,
async fn(t) {
await t.step("invalid resource", async () => {
assertEquals(await lookupWebFinger("acct:johndoe"), null);
assertEquals(await lookupWebFinger(new URL("acct:johndoe")), null);
assertEquals(await lookupWebFinger("acct:johndoe@"), null);
assertEquals(await lookupWebFinger(new URL("acct:johndoe@")), null);
});
await t.step("connection refused", async () => {
assertEquals(await lookupWebFinger("acct:johndoe@fedify-test.internal"), null);
assertEquals(await lookupWebFinger("https://fedify-test.internal/foo"), null);
});
esm_default.spyGlobal();
esm_default.get("begin:https://example.com/.well-known/webfinger?", { status: 404 });
await t.step("not found", async () => {
assertEquals(await lookupWebFinger("acct:johndoe@example.com"), null);
assertEquals(await lookupWebFinger("https://example.com/foo"), null);
});
const expected = {
subject: "acct:johndoe@example.com",
links: []
};
esm_default.removeRoutes();
esm_default.get("https://example.com/.well-known/webfinger?resource=acct%3Ajohndoe%40example.com", { body: expected });
await t.step("acct", async () => {
assertEquals(await lookupWebFinger("acct:johndoe@example.com"), expected);
});
const expected2 = {
subject: "https://example.com/foo",
links: []
};
esm_default.removeRoutes();
esm_default.get("https://example.com/.well-known/webfinger?resource=https%3A%2F%2Fexample.com%2Ffoo", { body: expected2 });
await t.step("https", async () => {
assertEquals(await lookupWebFinger("https://example.com/foo"), expected2);
});
esm_default.removeRoutes();
esm_default.get("begin:https://example.com/.well-known/webfinger?", { body: "not json" });
await t.step("invalid response", async () => {
assertEquals(await lookupWebFinger("acct:johndoe@example.com"), null);
});
esm_default.removeRoutes();
esm_default.get("begin:https://localhost/.well-known/webfinger?", {
subject: "acct:test@localhost",
links: [{
rel: "self",
type: "application/activity+json",
href: "https://localhost/actor"
}]
});
await t.step("private address", async () => {
assertEquals(await lookupWebFinger("acct:test@localhost"), null);
assertEquals(await lookupWebFinger("acct:test@localhost", { allowPrivateAddress: true }), {
subject: "acct:test@localhost",
links: [{
rel: "self",
type: "application/activity+json",
href: "https://localhost/actor"
}]
});
});
esm_default.removeRoutes();
esm_default.get("begin:https://example.com/.well-known/webfinger?", {
status: 302,
headers: { Location: "/.well-known/webfinger2" }
});
esm_default.get("begin:https://example.com/.well-known/webfinger2", { body: expected });
await t.step("redirection", async () => {
assertEquals(await lookupWebFinger("acct:johndoe@example.com"), expected);
});
esm_default.removeRoutes();
esm_default.get("begin:https://example.com/.well-known/webfinger?", {
status: 302,
headers: { Location: "/.well-known/webfinger" }
});
await t.step("infinite redirection", async () => {
const result = await withTimeout(() => lookupWebFinger("acct:johndoe@example.com"), 2e3);
assertEquals(result, null);
});
esm_default.removeRoutes();
esm_default.get("begin:https://example.com/.well-known/webfinger?", {
status: 302,
headers: { Location: "ftp://example.com/" }
});
await t.step("redirection to different protocol", async () => {
assertEquals(await lookupWebFinger("acct:johndoe@example.com"), null);
});
esm_default.removeRoutes();
esm_default.get("begin:https://example.com/.well-known/webfinger?", {
status: 302,
headers: { Location: "https://localhost/" }
});
await t.step("redirection to private address", async () => {
assertEquals(await lookupWebFinger("acct:johndoe@example.com"), null);
});
esm_default.hardReset();
}
});
//#endregion