first round of review

This commit is contained in:
counterweight 2025-12-19 10:30:23 +01:00
parent 5908660e56
commit 7140cf6f27
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
9 changed files with 61 additions and 63 deletions

View file

@ -105,7 +105,7 @@ class User(Base):
contact_email: Mapped[str | None] = mapped_column(String(255), nullable=True)
telegram: Mapped[str | None] = mapped_column(String(64), nullable=True)
signal: Mapped[str | None] = mapped_column(String(64), nullable=True)
nostr_npub: Mapped[str | None] = mapped_column(String(64), nullable=True)
nostr_npub: Mapped[str | None] = mapped_column(String(63), nullable=True)
# Relationship to roles
roles: Mapped[list[Role]] = relationship(

View file

@ -67,8 +67,6 @@ async def upsert_user(db: AsyncSession, email: str, password: str, role_names: l
async def seed() -> None:
async with engine.begin() as conn:
# Drop all tables and recreate to ensure schema is up to date
await conn.run_sync(Base.metadata.drop_all)
await conn.run_sync(Base.metadata.create_all)
async with async_session() as db:

View file

@ -397,4 +397,3 @@ class TestProfilePrivacy:
assert "telegram" not in data
assert "signal" not in data
assert "nostr_npub" not in data

View file

@ -61,8 +61,8 @@ class TestValidateTelegram:
assert validate_telegram("@alice_bob") is None
def test_valid_handle_min_length(self):
# 5 characters after @
assert validate_telegram("@abcde") is None
# 1 character after @
assert validate_telegram("@a") is None
def test_valid_handle_max_length(self):
# 32 characters after @
@ -77,26 +77,20 @@ class TestValidateTelegram:
result = validate_telegram("@")
assert result is not None
def test_too_short(self):
# Less than 5 characters after @
result = validate_telegram("@abcd")
assert result is not None
assert "5" in result
def test_too_long(self):
# More than 32 characters after @
result = validate_telegram("@" + "a" * 33)
assert result is not None
assert "32" in result
def test_starts_with_number(self):
result = validate_telegram("@1alice")
assert result is not None
assert "letter" in result.lower()
def test_starts_with_number_is_valid(self):
# Now allowed - any character is valid
assert validate_telegram("@1alice") is None
def test_invalid_characters(self):
result = validate_telegram("@alice-bob")
assert result is not None
def test_special_characters_are_valid(self):
# Now allowed - any character is valid
assert validate_telegram("@alice-bob") is None
assert validate_telegram("@test.user") is None
class TestValidateSignal:

View file

@ -1,5 +1,4 @@
"""Validation utilities for user profile fields."""
import re
from email_validator import validate_email, EmailNotValidError
from bech32 import bech32_decode
@ -25,7 +24,7 @@ def validate_telegram(value: str | None) -> str | None:
"""
Validate Telegram handle.
Must start with @ if provided.
Must start with @ if provided, with 1-32 characters after @.
Returns None if valid, error message if invalid.
Empty/None values are valid (field is optional).
"""
@ -35,21 +34,13 @@ def validate_telegram(value: str | None) -> str | None:
if not value.startswith("@"):
return "Telegram handle must start with @"
if len(value) < 2:
return "Telegram handle must have at least one character after @"
# Telegram usernames: 5-32 characters, alphanumeric and underscores
# But we store with @, so check 6-33 total
handle = value[1:] # Remove @
if len(handle) < 5:
return "Telegram handle must be at least 5 characters (after @)"
if len(handle) < 1:
return "Telegram handle must have at least one character after @"
if len(handle) > 32:
return "Telegram handle must be at most 32 characters (after @)"
if not re.match(r'^[a-zA-Z][a-zA-Z0-9_]*$', handle):
return "Telegram handle must start with a letter and contain only letters, numbers, and underscores"
return None