"""Unit tests for federation identity validation.""" from __future__ import annotations import pytest from federation.bp.identity.routes import USERNAME_RE, RESERVED class TestUsernameRegex: def test_valid_simple(self): assert USERNAME_RE.match("alice") def test_valid_with_numbers(self): assert USERNAME_RE.match("user42") def test_valid_with_underscore(self): assert USERNAME_RE.match("alice_bob") def test_min_length(self): assert USERNAME_RE.match("abc") def test_too_short(self): assert not USERNAME_RE.match("ab") def test_single_char(self): assert not USERNAME_RE.match("a") def test_max_length(self): assert USERNAME_RE.match("a" * 32) def test_too_long(self): assert not USERNAME_RE.match("a" * 33) def test_starts_with_digit(self): assert not USERNAME_RE.match("1abc") def test_starts_with_underscore(self): assert not USERNAME_RE.match("_abc") def test_uppercase_rejected(self): assert not USERNAME_RE.match("Alice") def test_hyphen_rejected(self): assert not USERNAME_RE.match("alice-bob") def test_spaces_rejected(self): assert not USERNAME_RE.match("alice bob") def test_empty_rejected(self): assert not USERNAME_RE.match("") class TestReservedUsernames: def test_admin_reserved(self): assert "admin" in RESERVED def test_root_reserved(self): assert "root" in RESERVED def test_api_reserved(self): assert "api" in RESERVED def test_inbox_reserved(self): assert "inbox" in RESERVED def test_normal_name_not_reserved(self): assert "alice" not in RESERVED def test_at_least_20_reserved(self): assert len(RESERVED) >= 20