Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

3.9 KB
Raw Blame
import { isEmailAllowedByRestriction } from "@cap/utils";
import { describe, expect, it } from "vitest";

describe("isEmailAllowedByRestriction", () => {
	describe("domain matching", () => {
		it("allows email matching a single domain", () => {
			expect(
				isEmailAllowedByRestriction("[email protected]", "company.com"),
			).toBe(true);
		});

		it("rejects email not matching the domain", () => {
			expect(
				isEmailAllowedByRestriction("[email protected]", "company.com"),
			).toBe(false);
		});

		it("rejects subdomain when only parent domain is allowed", () => {
			expect(
				isEmailAllowedByRestriction("[email protected]", "company.com"),
			).toBe(false);
		});

		it("is case insensitive for both email and domain", () => {
			expect(
				isEmailAllowedByRestriction("[email protected]", "company.com"),
			).toBe(true);
			expect(
				isEmailAllowedByRestriction("[email protected]", "Company.COM"),
			).toBe(true);
		});
	});

	describe("specific email matching", () => {
		it("allows an exact email match", () => {
			expect(
				isEmailAllowedByRestriction("[email protected]", "[email protected]"),
			).toBe(true);
		});

		it("rejects a different email", () => {
			expect(
				isEmailAllowedByRestriction("[email protected]", "[email protected]"),
			).toBe(false);
		});

		it("is case insensitive for specific emails", () => {
			expect(
				isEmailAllowedByRestriction("[email protected]", "[email protected]"),
			).toBe(true);
		});
	});

	describe("comma-separated entries", () => {
		it("allows email matching any of multiple domains", () => {
			const restriction = "company.com, partner.org";
			expect(
				isEmailAllowedByRestriction("[email protected]", restriction),
			).toBe(true);
			expect(isEmailAllowedByRestriction("[email protected]", restriction)).toBe(
				true,
			);
		});

		it("rejects email not matching any entry", () => {
			const restriction = "company.com, partner.org";
			expect(isEmailAllowedByRestriction("[email protected]", restriction)).toBe(
				false,
			);
		});

		it("supports mix of domains and specific emails", () => {
			const restriction = "company.com, [email protected], partner.org";
			expect(
				isEmailAllowedByRestriction("[email protected]", restriction),
			).toBe(true);
			expect(isEmailAllowedByRestriction("[email protected]", restriction)).toBe(
				true,
			);
			expect(
				isEmailAllowedByRestriction("[email protected]", restriction),
			).toBe(true);
			expect(isEmailAllowedByRestriction("[email protected]", restriction)).toBe(
				false,
			);
		});

		it("handles extra whitespace around entries", () => {
			const restriction = " company.com ,  [email protected] , partner.org ";
			expect(
				isEmailAllowedByRestriction("[email protected]", restriction),
			).toBe(true);
			expect(isEmailAllowedByRestriction("[email protected]", restriction)).toBe(
				true,
			);
		});

		it("handles trailing comma", () => {
			expect(
				isEmailAllowedByRestriction("[email protected]", "company.com,"),
			).toBe(true);
		});

		it("handles empty entries between commas", () => {
			expect(
				isEmailAllowedByRestriction(
					"[email protected]",
					"company.com,,partner.org",
				),
			).toBe(true);
			expect(
				isEmailAllowedByRestriction(
					"[email protected]",
					"company.com,,partner.org",
				),
			).toBe(true);
		});
	});

	describe("edge cases", () => {
		it("allows any email when restriction is empty string", () => {
			expect(isEmailAllowedByRestriction("[email protected]", "")).toBe(true);
		});

		it("allows any email when restriction is only whitespace/commas", () => {
			expect(isEmailAllowedByRestriction("[email protected]", " , , ")).toBe(
				true,
			);
		});

		it("does not partial-match domain names", () => {
			expect(
				isEmailAllowedByRestriction("[email protected]", "company.com"),
			).toBe(false);
		});

		it("does not match domain entry as email suffix without @", () => {
			expect(
				isEmailAllowedByRestriction("[email protected]", "company.com"),
			).toBe(false);
		});
	});
});