Radish alpha
r
Radicle web interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
Add httpd test fixture to vitest httpd-api tests
Sebastian Martinez committed 2 years ago
commit 3d4e1bbfd977a45e26ae35e0cf8cd27581b40e6f
parent 92f19d0ca4cc15583f1eb856cb5f669d1a1f6be0
2 files changed +56 -0
added httpd-client/tests/support/fixtures.ts
@@ -0,0 +1,32 @@
+
import * as FsSync from "node:fs";
+
import * as Path from "node:path";
+
import { test } from "vitest";
+

+
import { HttpdClient } from "@httpd-client";
+
import { RadiclePeer, createPeerManager } from "@tests/support/peerManager.js";
+
import { gitOptions } from "@tests/support/fixtures.js";
+
import { tmpDir } from "@tests/support/support.js";
+

+
interface TestFixtures {
+
  httpd: { api: HttpdClient; peer: RadiclePeer };
+
}
+

+
export const testFixture = test.extend<TestFixtures>({
+
  // eslint-disable-next-line no-empty-pattern
+
  httpd: async ({}, use) => {
+
    const peerManager = await createPeerManager({
+
      dataDir: Path.resolve(Path.join(tmpDir, "peers")),
+
      outputLog: FsSync.createWriteStream(
+
        Path.join(tmpDir, "peerManager.log"),
+
      ).setMaxListeners(16),
+
    });
+
    const peer = await peerManager.startPeer({
+
      name: "palm",
+
      gitOptions: gitOptions["alice"],
+
    });
+
    await peer.startHttpd();
+
    const api = new HttpdClient(peer.httpdBaseUrl);
+
    await use({ api, peer });
+
    await peer.stopHttpd();
+
  },
+
});
added httpd-client/tests/support/httpd.ts
@@ -0,0 +1,24 @@
+
import type { HttpdClient } from "@httpd-client";
+
import type { RadiclePeer } from "@tests/support/peerManager.js";
+

+
import { sessionPayloadSchema } from "@httpd-client/lib/session.js";
+

+
export async function authenticate(
+
  api: HttpdClient,
+
  peer: RadiclePeer,
+
): Promise<string> {
+
  const { stdout } = await peer.rad(["web", "--backend", api.url, "--json"]);
+
  const session = sessionPayloadSchema.safeParse(JSON.parse(stdout));
+

+
  if (!session.success) {
+
    throw new Error("Failed to parse session payload");
+
  }
+

+
  const { sessionId, signature, publicKey } = session.data;
+
  await api.session.update(sessionId, {
+
    sig: signature,
+
    pk: publicKey,
+
  });
+

+
  return sessionId;
+
}