Skip to content

Self-hosted, plugin-based server management.

2.4 KB
Raw Blame
import { describe, expect, it } from "vitest";
import { isExternalUserAllowed } from "../../auth/allowed-users.js";

describe("isExternalUserAllowed", () => {
  it("allows everyone when the allow-list is empty", () => {
    expect(isExternalUserAllowed("", "alice", "[email protected]")).toBe(true);
    expect(isExternalUserAllowed("   ", "alice")).toBe(true);
  });

  it("allows everyone with the '*' wildcard", () => {
    expect(isExternalUserAllowed("*", "anyone", "[email protected]")).toBe(true);
  });

  it("matches an exact identifier (case-insensitive)", () => {
    expect(isExternalUserAllowed("alice,bob", "alice")).toBe(true);
    expect(isExternalUserAllowed("Alice", "alice")).toBe(true);
    expect(isExternalUserAllowed("alice", "ALICE")).toBe(true);
  });

  it("matches against the email as well as the identifier", () => {
    expect(isExternalUserAllowed("[email protected]", "sub-123", "[email protected]")).toBe(
      true,
    );
  });

  it("matches an @domain suffix pattern", () => {
    expect(
      isExternalUserAllowed("@company.com", "sub-1", "[email protected]"),
    ).toBe(true);
    expect(
      isExternalUserAllowed("@company.com", "sub-1", "[email protected]"),
    ).toBe(true);
  });

  it("denies users not on the list", () => {
    expect(isExternalUserAllowed("alice,bob", "charlie", "[email protected]")).toBe(
      false,
    );
    expect(
      isExternalUserAllowed("@company.com", "sub-1", "[email protected]"),
    ).toBe(false);
  });

  it("ignores blank entries and surrounding whitespace in the list", () => {
    expect(isExternalUserAllowed(" alice , , bob ", "bob")).toBe(true);
  });

  it("does not match the email against an identifier-only pattern when email differs", () => {
    expect(isExternalUserAllowed("alice", "sub-123", "[email protected]")).toBe(
      false,
    );
  });

  it("matches *@domain.com wildcard pattern against emails", () => {
    expect(
      isExternalUserAllowed("*@company.com", "sub-1", "[email protected]"),
    ).toBe(true);
    expect(
      isExternalUserAllowed("*@company.com", "sub-1", "[email protected]"),
    ).toBe(true);
    expect(
      isExternalUserAllowed("*@company.com", "sub-1", "[email protected]"),
    ).toBe(false);
  });

  it("matches glob patterns with multiple wildcards", () => {
    expect(isExternalUserAllowed("admin*", "admin_user")).toBe(true);
    expect(isExternalUserAllowed("admin*", "user_admin")).toBe(false);
  });
});