|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { isEmailInDenylist } from '@/lib/auth/auth' |
| 6 | + |
| 7 | +describe('isEmailInDenylist', () => { |
| 8 | + it('returns false when denylist is null, empty, or email is missing', () => { |
| 9 | + expect(isEmailInDenylist('a@example.com', null)).toBe(false) |
| 10 | + expect(isEmailInDenylist('a@example.com', [])).toBe(false) |
| 11 | + expect(isEmailInDenylist(null, ['example.com'])).toBe(false) |
| 12 | + expect(isEmailInDenylist(undefined, ['example.com'])).toBe(false) |
| 13 | + expect(isEmailInDenylist('', ['example.com'])).toBe(false) |
| 14 | + }) |
| 15 | + |
| 16 | + it('returns false when email has no @', () => { |
| 17 | + expect(isEmailInDenylist('not-an-email', ['example.com'])).toBe(false) |
| 18 | + }) |
| 19 | + |
| 20 | + it('matches exact domain', () => { |
| 21 | + expect(isEmailInDenylist('user@dpdns.org', ['dpdns.org'])).toBe(true) |
| 22 | + expect(isEmailInDenylist('user@DPDNS.ORG', ['dpdns.org'])).toBe(true) |
| 23 | + }) |
| 24 | + |
| 25 | + it('matches arbitrary-depth subdomains of a listed parent zone', () => { |
| 26 | + expect(isEmailInDenylist('user@xx.lucky04.dpdns.org', ['dpdns.org'])).toBe(true) |
| 27 | + expect(isEmailInDenylist('user@a.b.c.qzz.io', ['qzz.io'])).toBe(true) |
| 28 | + }) |
| 29 | + |
| 30 | + it('does not match look-alike domains', () => { |
| 31 | + expect(isEmailInDenylist('user@xdpdns.org', ['dpdns.org'])).toBe(false) |
| 32 | + expect(isEmailInDenylist('user@notdpdns.org', ['dpdns.org'])).toBe(false) |
| 33 | + }) |
| 34 | + |
| 35 | + it('does not match disallowed domains', () => { |
| 36 | + expect(isEmailInDenylist('user@gmail.com', ['dpdns.org', 'qzz.io'])).toBe(false) |
| 37 | + expect(isEmailInDenylist('user@example.com', ['dpdns.org'])).toBe(false) |
| 38 | + }) |
| 39 | + |
| 40 | + it('handles multiple denylist entries', () => { |
| 41 | + const denylist = ['dpdns.org', 'qzz.io', 'cc.cd'] |
| 42 | + expect(isEmailInDenylist('user@foo.dpdns.org', denylist)).toBe(true) |
| 43 | + expect(isEmailInDenylist('user@bar.qzz.io', denylist)).toBe(true) |
| 44 | + expect(isEmailInDenylist('user@baz.cc.cd', denylist)).toBe(true) |
| 45 | + expect(isEmailInDenylist('user@example.com', denylist)).toBe(false) |
| 46 | + }) |
| 47 | +}) |
0 commit comments