Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

4.1 KB
Raw Blame
import { expect, test } from "@playwright/test";
import { prisma } from "@rallly/database";
import { deleteAllMessages } from "@rallly/test-helpers";
import { createUserInDb, loginWithEmail } from "./test-utils";

const INITIAL_ADMIN_TEST_EMAIL = "[email protected]";
const REGULAR_USER_EMAIL = "[email protected]";
const SUBSEQUENT_ADMIN_EMAIL = "[email protected]";
const OTHER_USER_EMAIL = "[email protected]";

test.describe("Admin Setup Page Access", () => {
  test.beforeEach(async () => {
    await prisma.user.deleteMany({
      where: {
        email: {
          in: [
            INITIAL_ADMIN_TEST_EMAIL,
            REGULAR_USER_EMAIL,
            SUBSEQUENT_ADMIN_EMAIL,
            OTHER_USER_EMAIL,
          ],
        },
      },
    });

    await deleteAllMessages();
  });

  test("should redirect unauthenticated user to login page", async ({
    page,
  }) => {
    await page.goto("/admin-setup");
    await expect(page).toHaveURL(/.*\/login/);
  });

  test("should allow access if user is the designated initial admin (and not yet admin role)", async ({
    page,
  }) => {
    await createUserInDb({
      email: INITIAL_ADMIN_TEST_EMAIL,
      name: "Initial Admin User",
      role: "user",
    });
    await loginWithEmail(page, { email: INITIAL_ADMIN_TEST_EMAIL });

    await page.goto("/control-panel");
    await expect(page).toHaveURL(/.*\/admin-setup/);
    await expect(page.getByText("Are you the admin?")).toBeVisible();
    await expect(
      page.getByRole("button", { name: "Make me an admin" }),
    ).toBeVisible();
  });

  test("should explain missing admin access to a regular user (not initial admin, not admin role)", async ({
    page,
  }) => {
    await createUserInDb({
      email: REGULAR_USER_EMAIL,
      name: "Regular User",
      role: "user",
    });
    await loginWithEmail(page, { email: REGULAR_USER_EMAIL });

    await page.goto("/admin-setup");
    await expect(page.getByText("Administrator access required")).toBeVisible();
    await expect(page.getByText(REGULAR_USER_EMAIL)).toBeVisible();
  });

  test("should redirect an existing admin user to control-panel", async ({
    page,
  }) => {
    await createUserInDb({
      email: SUBSEQUENT_ADMIN_EMAIL,
      name: "Existing Admin",
      role: "admin",
    });
    await loginWithEmail(page, { email: SUBSEQUENT_ADMIN_EMAIL });

    await page.goto("/admin-setup");
    await expect(page).toHaveURL("/control-panel");
  });

  test("should explain missing admin access if INITIAL_ADMIN_EMAIL in env is different from user's email", async ({
    page,
  }) => {
    await createUserInDb({
      email: OTHER_USER_EMAIL,
      name: "Other User",
      role: "user",
    });
    await loginWithEmail(page, { email: OTHER_USER_EMAIL });

    await page.goto("/admin-setup");
    await expect(page.getByText("Administrator access required")).toBeVisible();
  });

  test("initial admin can make themselves admin using the button", async ({
    page,
  }) => {
    await createUserInDb({
      email: INITIAL_ADMIN_TEST_EMAIL,
      name: "Initial Admin To Be",
      role: "user",
    });
    await loginWithEmail(page, { email: INITIAL_ADMIN_TEST_EMAIL });

    await page.goto("/admin-setup");
    await expect(page.getByText("Are you the admin?")).toBeVisible();

    // The button is visible before React hydrates it and its onClick
    // handler is attached, so a click that lands too early is silently
    // lost. Retry until the action redirects.
    await expect(async () => {
      await page.getByRole("button", { name: "Make me an admin" }).click();
      await page.waitForURL("/control-panel", { timeout: 5000 });
    }).toPass();

    // The URL check above can pass on the transient redirect even when a
    // stale session cookie bounces us back to /admin-setup, so assert the
    // control panel actually rendered.
    await expect(
      page.getByRole("heading", { name: "Home", level: 1 }),
    ).toBeVisible();
    await expect(page).toHaveURL("/control-panel");

    const user = await prisma.user.findUnique({
      where: { email: INITIAL_ADMIN_TEST_EMAIL },
    });
    expect(user).toBeTruthy();
    expect(user?.role).toBe("admin");
  });
});