|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { mockWhere, envRef } = vi.hoisted(() => ({ |
| 7 | + mockWhere: vi.fn(), |
| 8 | + envRef: { BLOCKED_SIGNUP_DOMAINS: undefined as string | undefined }, |
| 9 | +})) |
| 10 | + |
| 11 | +vi.mock('@sim/db', () => ({ |
| 12 | + db: { select: vi.fn(() => ({ from: vi.fn(() => ({ where: mockWhere })) })) }, |
| 13 | + user: { id: 'id', email: 'email', banned: 'banned', banExpires: 'banExpires' }, |
| 14 | +})) |
| 15 | +vi.mock('drizzle-orm', () => ({ inArray: vi.fn() })) |
| 16 | +vi.mock('@/lib/core/config/appconfig', () => ({ fetchAppConfigProfile: vi.fn() })) |
| 17 | +vi.mock('@/lib/core/config/env', () => ({ |
| 18 | + get env() { |
| 19 | + return envRef |
| 20 | + }, |
| 21 | +})) |
| 22 | +vi.mock('@/lib/core/config/feature-flags', () => ({ isAppConfigEnabled: false })) |
| 23 | + |
| 24 | +import { getActivelyBannedUserIds, isBanActive } from '@/lib/auth/ban' |
| 25 | + |
| 26 | +describe('isBanActive', () => { |
| 27 | + it('returns true for a permanent ban', () => { |
| 28 | + expect(isBanActive({ banned: true, banExpires: null })).toBe(true) |
| 29 | + }) |
| 30 | + |
| 31 | + it('returns false for an expired temporary ban', () => { |
| 32 | + expect(isBanActive({ banned: true, banExpires: new Date(Date.now() - 1000) })).toBe(false) |
| 33 | + }) |
| 34 | + |
| 35 | + it('returns true for an unexpired temporary ban', () => { |
| 36 | + expect(isBanActive({ banned: true, banExpires: new Date(Date.now() + 60_000) })).toBe(true) |
| 37 | + }) |
| 38 | + |
| 39 | + it('returns false when not banned', () => { |
| 40 | + expect(isBanActive({ banned: false, banExpires: null })).toBe(false) |
| 41 | + expect(isBanActive({ banned: null, banExpires: null })).toBe(false) |
| 42 | + }) |
| 43 | +}) |
| 44 | + |
| 45 | +describe('getActivelyBannedUserIds', () => { |
| 46 | + beforeEach(() => { |
| 47 | + vi.clearAllMocks() |
| 48 | + envRef.BLOCKED_SIGNUP_DOMAINS = undefined |
| 49 | + mockWhere.mockResolvedValue([]) |
| 50 | + }) |
| 51 | + |
| 52 | + it('short-circuits on empty input without querying', async () => { |
| 53 | + expect(await getActivelyBannedUserIds([])).toEqual([]) |
| 54 | + expect(await getActivelyBannedUserIds([''])).toEqual([]) |
| 55 | + expect(mockWhere).not.toHaveBeenCalled() |
| 56 | + }) |
| 57 | + |
| 58 | + it('returns ids with an active db ban', async () => { |
| 59 | + mockWhere.mockResolvedValue([ |
| 60 | + { id: 'u1', email: 'a@ok.com', banned: true, banExpires: null }, |
| 61 | + { id: 'u2', email: 'b@ok.com', banned: false, banExpires: null }, |
| 62 | + ]) |
| 63 | + expect(await getActivelyBannedUserIds(['u1', 'u2'])).toEqual(['u1']) |
| 64 | + }) |
| 65 | + |
| 66 | + it('treats an expired ban as lifted', async () => { |
| 67 | + mockWhere.mockResolvedValue([ |
| 68 | + { id: 'u1', email: 'a@ok.com', banned: true, banExpires: new Date(Date.now() - 1000) }, |
| 69 | + ]) |
| 70 | + expect(await getActivelyBannedUserIds(['u1'])).toEqual([]) |
| 71 | + }) |
| 72 | + |
| 73 | + it('returns ids whose email domain is in the blocked-domains list, including subdomains', async () => { |
| 74 | + envRef.BLOCKED_SIGNUP_DOMAINS = 'bad.com' |
| 75 | + mockWhere.mockResolvedValue([ |
| 76 | + { id: 'u1', email: 'a@bad.com', banned: false, banExpires: null }, |
| 77 | + { id: 'u2', email: 'b@mail.bad.com', banned: false, banExpires: null }, |
| 78 | + { id: 'u3', email: 'c@good.com', banned: false, banExpires: null }, |
| 79 | + ]) |
| 80 | + expect(await getActivelyBannedUserIds(['u1', 'u2', 'u3'])).toEqual(['u1', 'u2']) |
| 81 | + }) |
| 82 | + |
| 83 | + it('propagates db failures so callers fail closed', async () => { |
| 84 | + mockWhere.mockRejectedValue(new Error('db down')) |
| 85 | + await expect(getActivelyBannedUserIds(['u1'])).rejects.toThrow('db down') |
| 86 | + }) |
| 87 | +}) |
0 commit comments