Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

3.7 KB
Raw Blame
import { describe, expect, it } from "vitest";

import { allowedScopes, createEmailSignIn, parseAllowList, signInEnabled } from "./account-signin.ts";
import { startControlPlaneStub } from "./testing/control-plane-stub.ts";

describe("the sign-in allow-list", () => {
  it("splits on commas, spaces and newlines and ignores case", () => {
    expect(parseAllowList(" [email protected], @Agentada.test\n[email protected] ")).toEqual(["[email protected]", "@agentada.test", "[email protected]"]);
    expect(parseAllowList(undefined)).toEqual([]);
  });

  it("maps addresses and @domains to scopes, and never matches a look-alike domain", () => {
    const list = { admins: ["[email protected]", "@agentada.test"], members: ["[email protected]", "@partner.test"] };
    expect(allowedScopes("[email protected]", list)).toEqual(["admin", "client"]);
    expect(allowedScopes("[email protected]", list)).toEqual(["admin", "client"]);
    expect(allowedScopes("[email protected]", list)).toEqual(["client"]);
    expect(allowedScopes("[email protected]", list)).toEqual(["client"]);
    expect(allowedScopes("[email protected]", list)).toBeNull();
    expect(allowedScopes("[email protected]", list)).toBeNull();
    expect(allowedScopes("@agentada.test", list)).toBeNull();
    expect(allowedScopes("[email protected]", list)).toBeNull();
    expect(signInEnabled(list)).toBe(true);
    expect(signInEnabled({ admins: [], members: [] })).toBe(false);
  });
});

describe("the exchange with the control plane", () => {
  it("sends a code only to welcome addresses, turns the right code into scopes, and explains the wrong one", async () => {
    const stub = await startControlPlaneStub();
    try {
      const signIn = createEmailSignIn({
        allow: { admins: ["[email protected]"], members: ["@team.test"] },
        env: { ...process.env, OMB_CONTROL_PLANE_URL: stub.url },
      });
      expect(signIn.enabled()).toBe(true);
      expect(await signIn.start("[email protected]")).toMatchObject({ ok: false, status: 403 });
      expect(await signIn.start("not an email")).toMatchObject({ ok: false, status: 400 });
      expect(stub.calls).not.toContain("POST /api/auth/email-otp/send-verification-otp");
      expect(await signIn.start("[email protected]")).toEqual({ ok: true });
      expect(stub.calls).toContain("POST /api/auth/email-otp/send-verification-otp");

      const wrong = await signIn.verify("[email protected]", "00000000");
      expect(wrong).toMatchObject({ ok: false, status: 401 });
      expect(wrong.ok ? "" : wrong.error).toMatch(/wrong or has expired/);
      expect(await signIn.verify("[email protected]", "12")).toMatchObject({ ok: false, status: 400 });
      expect(await signIn.verify("[email protected]", stub.otp)).toMatchObject({ ok: false, status: 403 });

      const admin = await signIn.verify("[email protected]", stub.otp);
      expect(admin).toEqual({ ok: true, email: "[email protected]", userId: "user_stub", scopes: ["admin", "client"] });
      const member = await signIn.verify("[email protected]", stub.otp);
      expect(member).toMatchObject({ ok: true, scopes: ["client"] });
      await new Promise((r) => setTimeout(r, 50));
      expect(stub.calls.filter((call) => call === "POST /api/auth/sign-out").length).toBe(2);
    } finally {
      await stub.close();
    }
  });

  it("is off with an empty allow-list and says so when the sign-in service is down", async () => {
    const off = createEmailSignIn({ allow: () => ({ admins: [], members: [] }) });
    expect(off.enabled()).toBe(false);
    const down = createEmailSignIn({ allow: { admins: ["[email protected]"], members: [] }, env: { ...process.env, OMB_CONTROL_PLANE_URL: "http://127.0.0.1:9" } });
    expect(await down.start("[email protected]")).toMatchObject({ ok: false, status: 502 });
  });
});