|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { emailDomainIsListed } from "../app/utils/emailDomainList.js"; |
| 3 | + |
| 4 | +describe("emailDomainIsListed", () => { |
| 5 | + it("matches an address at a listed domain", () => { |
| 6 | + expect(emailDomainIsListed("blocked.example", "bot@blocked.example")).toBe(true); |
| 7 | + }); |
| 8 | + |
| 9 | + it("matches subdomains of a listed domain", () => { |
| 10 | + expect(emailDomainIsListed("blocked.example", "bot@mail.blocked.example")).toBe(true); |
| 11 | + expect(emailDomainIsListed("blocked.example", "bot@a.b.blocked.example")).toBe(true); |
| 12 | + }); |
| 13 | + |
| 14 | + it("ignores case and surrounding whitespace in both the list and the address", () => { |
| 15 | + expect(emailDomainIsListed(" Blocked.Example ", "Bot@BLOCKED.example")).toBe(true); |
| 16 | + }); |
| 17 | + |
| 18 | + it("accepts comma and whitespace separated lists", () => { |
| 19 | + const list = "one.example, two.example\nthree.example four.example"; |
| 20 | + expect(emailDomainIsListed(list, "a@one.example")).toBe(true); |
| 21 | + expect(emailDomainIsListed(list, "a@two.example")).toBe(true); |
| 22 | + expect(emailDomainIsListed(list, "a@three.example")).toBe(true); |
| 23 | + expect(emailDomainIsListed(list, "a@four.example")).toBe(true); |
| 24 | + expect(emailDomainIsListed(list, "a@five.example")).toBe(false); |
| 25 | + }); |
| 26 | + |
| 27 | + it("accepts entries written with a leading @ or dot", () => { |
| 28 | + expect(emailDomainIsListed("@blocked.example", "bot@blocked.example")).toBe(true); |
| 29 | + expect(emailDomainIsListed(".blocked.example", "bot@sub.blocked.example")).toBe(true); |
| 30 | + }); |
| 31 | + |
| 32 | + it("matches a fully qualified domain with a trailing dot", () => { |
| 33 | + expect(emailDomainIsListed("blocked.example", "bot@blocked.example.")).toBe(true); |
| 34 | + }); |
| 35 | + |
| 36 | + it("does not match look-alike domains", () => { |
| 37 | + expect(emailDomainIsListed("blocked.example", "user@notblocked.example")).toBe(false); |
| 38 | + expect(emailDomainIsListed("blocked.example", "user@blocked.example.attacker.example")).toBe( |
| 39 | + false |
| 40 | + ); |
| 41 | + expect(emailDomainIsListed("blocked.example", "blocked.example@other.example")).toBe(false); |
| 42 | + }); |
| 43 | + |
| 44 | + it("does not let a listed domain block its parent", () => { |
| 45 | + expect(emailDomainIsListed("mail.company.example", "user@company.example")).toBe(false); |
| 46 | + }); |
| 47 | + |
| 48 | + it("matches nothing when the list is empty or only separators", () => { |
| 49 | + expect(emailDomainIsListed("", "user@company.example")).toBe(false); |
| 50 | + expect(emailDomainIsListed(" , ,", "user@company.example")).toBe(false); |
| 51 | + }); |
| 52 | + |
| 53 | + it("matches nothing for an address without a domain", () => { |
| 54 | + expect(emailDomainIsListed("blocked.example", "user@")).toBe(false); |
| 55 | + }); |
| 56 | +}); |
0 commit comments