mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
* fix(validation): reject email addresses ending in a newline * fix(validation): reject usernames ending in a newline The username pattern had the same trailing-newline gap as the email one. Both patterns now rely on fullmatch, so their anchors are dropped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(user): use the renamed bearer helper Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
266 lines
9.8 KiB
Python
266 lines
9.8 KiB
Python
"""Tests for validation utilities."""
|
|
|
|
import pytest
|
|
from hypothesis import given
|
|
from hypothesis import strategies as st
|
|
|
|
from models.user import TEXT_FIELD_LENGTH
|
|
from utils.validation import (
|
|
ValidationError,
|
|
narrow_rom_id_scope,
|
|
sanitize_username,
|
|
validate_ascii_only,
|
|
validate_email,
|
|
validate_password,
|
|
validate_username,
|
|
)
|
|
|
|
|
|
class TestValidateAsciiOnly:
|
|
"""Test ASCII-only validation."""
|
|
|
|
def test_valid_ascii_string(self):
|
|
"""Test that valid ASCII strings pass validation."""
|
|
validate_ascii_only("hello123", "test_field")
|
|
validate_ascii_only("user_name", "test_field")
|
|
validate_ascii_only("test@example.com", "test_field")
|
|
|
|
def test_invalid_non_ascii_string(self):
|
|
"""Test that non-ASCII strings fail validation."""
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
validate_ascii_only("café", "test_field")
|
|
assert "ASCII characters" in exc_info.value.message
|
|
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
validate_ascii_only("naïve", "test_field")
|
|
assert "ASCII characters" in exc_info.value.message
|
|
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
validate_ascii_only("résumé", "test_field")
|
|
assert "ASCII characters" in exc_info.value.message
|
|
|
|
def test_empty_string(self):
|
|
"""Test that empty strings pass validation."""
|
|
validate_ascii_only("", "test_field")
|
|
|
|
|
|
class TestValidateUsername:
|
|
"""Test username validation."""
|
|
|
|
def test_valid_usernames(self):
|
|
"""Test that valid usernames pass validation."""
|
|
validate_username("user123")
|
|
validate_username("test_user")
|
|
validate_username("admin")
|
|
validate_username("user-name")
|
|
|
|
def test_invalid_empty_username(self):
|
|
"""Test that empty usernames fail validation."""
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
validate_username("")
|
|
assert "cannot be empty" in exc_info.value.message
|
|
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
validate_username(" ")
|
|
assert True
|
|
|
|
def test_invalid_short_username(self):
|
|
"""Test that short usernames fail validation."""
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
validate_username("ab")
|
|
assert "at least 3 characters" in exc_info.value.message
|
|
|
|
def test_invalid_long_username(self):
|
|
"""Test that long usernames fail validation."""
|
|
long_username = "a" * 256
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
validate_username(long_username)
|
|
assert "no more than 255 characters" in exc_info.value.message
|
|
|
|
def test_invalid_characters_username(self):
|
|
"""Test that usernames with invalid characters fail validation."""
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
validate_username("user@domain")
|
|
assert "letters, numbers, underscores, and hyphens" in exc_info.value.message
|
|
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
validate_username("user.name")
|
|
assert True
|
|
|
|
@pytest.mark.parametrize("suffix", ["\n", "\r\n", "\t"])
|
|
def test_trailing_control_characters_are_rejected(self, suffix):
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
validate_username(f"username{suffix}")
|
|
assert "letters, numbers, underscores, and hyphens" in exc_info.value.message
|
|
assert exc_info.value.field_name == "Username"
|
|
|
|
def test_invalid_non_ascii_username(self):
|
|
"""Test that usernames with non-ASCII characters fail validation."""
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
validate_username("naïve")
|
|
assert "ASCII characters" in exc_info.value.message
|
|
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
validate_username("résumé")
|
|
assert True
|
|
|
|
|
|
class TestSanitizeUsername:
|
|
"""Test coercion of provider-supplied usernames."""
|
|
|
|
@pytest.mark.parametrize(
|
|
("supplied", "expected"),
|
|
[
|
|
("already_valid-1", "already_valid-1"),
|
|
("first.last", "first-last"),
|
|
("first.last@example.com", "first-last-example-com"),
|
|
(" spaced out ", "spaced-out"),
|
|
(".leading.and.trailing.", "leading-and-trailing"),
|
|
("naïve", "na-ve"),
|
|
],
|
|
)
|
|
def test_sanitized_usernames_pass_validation(self, supplied, expected):
|
|
sanitized = sanitize_username(supplied)
|
|
assert sanitized == expected
|
|
validate_username(sanitized)
|
|
|
|
def test_falls_back_when_nothing_usable_survives(self):
|
|
assert sanitize_username("ユーザー", fallback="someone") == "someone"
|
|
assert sanitize_username("...", fallback="...") == "user"
|
|
|
|
def test_truncates_to_the_column_length(self):
|
|
assert len(sanitize_username("a" * 300)) == TEXT_FIELD_LENGTH
|
|
|
|
@given(st.text(min_size=1))
|
|
def test_any_input_sanitizes_to_a_valid_username(self, supplied):
|
|
validate_username(sanitize_username(supplied))
|
|
|
|
|
|
class TestValidatePassword:
|
|
"""Test password validation."""
|
|
|
|
def test_valid_passwords(self):
|
|
"""Test that valid passwords pass validation."""
|
|
validate_password("password123")
|
|
validate_password("my_secret_password")
|
|
validate_password("admin123")
|
|
|
|
def test_invalid_empty_password(self):
|
|
"""Test that empty passwords fail validation."""
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
validate_password("")
|
|
assert "cannot be empty" in exc_info.value.message
|
|
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
validate_password(" ")
|
|
assert True
|
|
|
|
def test_invalid_short_password(self):
|
|
"""Test that short passwords fail validation."""
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
validate_password("12345")
|
|
assert "at least 6 characters" in exc_info.value.message
|
|
|
|
def test_invalid_non_ascii_password(self):
|
|
"""Test that passwords with non-ASCII characters fail validation."""
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
validate_password("résumé")
|
|
assert "ASCII characters" in exc_info.value.message
|
|
|
|
|
|
class TestValidateEmail:
|
|
"""Test email validation."""
|
|
|
|
def test_valid_emails(self):
|
|
"""Test that valid emails pass validation."""
|
|
validate_email("user@example.com")
|
|
validate_email("test.user@domain.org")
|
|
validate_email("admin@company.co.uk")
|
|
|
|
def test_empty_email(self):
|
|
"""Test that empty emails pass validation (email is optional)."""
|
|
validate_email("")
|
|
|
|
def test_invalid_email_format(self):
|
|
"""Test that invalid email formats fail validation."""
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
validate_email("invalid-email")
|
|
assert "Invalid email format" in exc_info.value.message
|
|
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
validate_email("user@")
|
|
assert True
|
|
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
validate_email("@domain.com")
|
|
assert True
|
|
|
|
@pytest.mark.parametrize("suffix", ["\n", "\r\n", "\t"])
|
|
def test_trailing_control_characters_are_rejected(self, suffix):
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
validate_email(f"user@example.com{suffix}")
|
|
assert exc_info.value.message == "Invalid email format"
|
|
assert exc_info.value.field_name == "Email"
|
|
|
|
def test_invalid_non_ascii_email(self):
|
|
"""Test that emails with non-ASCII characters fail validation."""
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
validate_email("résumé@example.com")
|
|
assert "ASCII characters" in exc_info.value.message
|
|
|
|
def test_email_over_column_length(self):
|
|
"""An email the column cannot hold fails validation, not the insert."""
|
|
domain = "@example.com"
|
|
validate_email("a" * (TEXT_FIELD_LENGTH - len(domain)) + domain)
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
validate_email("a" * (TEXT_FIELD_LENGTH - len(domain) + 1) + domain)
|
|
assert "no more than 255 characters" in exc_info.value.message
|
|
|
|
|
|
_USERNAME_ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-"
|
|
_LOWER_ALNUM = "abcdefghijklmnopqrstuvwxyz0123456789"
|
|
_LOWER = "abcdefghijklmnopqrstuvwxyz"
|
|
|
|
|
|
class TestValidateUsernameProperties:
|
|
@given(st.text(alphabet=_USERNAME_ALPHABET, min_size=3, max_size=255))
|
|
def test_well_formed_usernames_pass(self, username):
|
|
validate_username(username)
|
|
|
|
@given(st.text(alphabet=_USERNAME_ALPHABET, min_size=1, max_size=2))
|
|
def test_too_short_usernames_are_rejected(self, username):
|
|
with pytest.raises(ValidationError):
|
|
validate_username(username)
|
|
|
|
|
|
class TestValidateEmailProperties:
|
|
@given(
|
|
st.text(alphabet=_LOWER_ALNUM, min_size=1, max_size=20),
|
|
st.text(alphabet=_LOWER_ALNUM, min_size=1, max_size=20),
|
|
st.text(alphabet=_LOWER, min_size=2, max_size=6),
|
|
)
|
|
def test_well_formed_emails_pass(self, local, domain, tld):
|
|
validate_email(f"{local}@{domain}.{tld}")
|
|
|
|
|
|
class TestNarrowRomIdScope:
|
|
"""Test folding a single-ROM filter into a `rom_ids` scope."""
|
|
|
|
def test_no_filters_leaves_the_scope_absent(self):
|
|
assert narrow_rom_id_scope(None, None) is None
|
|
|
|
def test_rom_id_alone_becomes_a_single_id_scope(self):
|
|
assert narrow_rom_id_scope(7, None) == [7]
|
|
|
|
def test_rom_ids_alone_passes_through(self):
|
|
assert narrow_rom_id_scope(None, [1, 2]) == [1, 2]
|
|
|
|
def test_both_narrow_to_their_intersection(self):
|
|
assert narrow_rom_id_scope(2, [1, 2, 3]) == [2]
|
|
|
|
def test_disjoint_filters_yield_an_empty_scope(self):
|
|
assert narrow_rom_id_scope(9, [1, 2]) == []
|
|
|
|
def test_empty_scope_stays_empty(self):
|
|
assert narrow_rom_id_scope(1, []) == []
|