| Server IP : 3.147.158.171 / Your IP : 216.73.216.88 Web Server : Apache/2.4.67 (Amazon Linux) OpenSSL/3.5.5 System : Linux ip-172-31-2-178.us-east-2.compute.internal 6.1.172-216.329.amzn2023.x86_64 #1 SMP PREEMPT_DYNAMIC Wed May 20 06:31:34 UTC 2026 x86_64 User : ec2-user ( 1000) PHP Version : 8.4.21 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /tsai/repo/api/tests/unit/ |
Upload File : |
"""Tests for slug validation utility."""
import pytest
from app.utilities.slug_validation import (
RESERVED_SLUGS,
validate_slug_format,
slug_to_db_name,
)
class TestValidateSlugFormat:
"""Tests for validate_slug_format."""
@pytest.mark.parametrize("slug", [
"abc", # min length
"mysite", # simple
"site1", # with numbers
"my-site", # with hyphen
"a1b2c3", # mixed
"my-cool-site", # multiple hyphens
"a" * 20, # max length
"1abc", # starts with number
"abc1", # ends with number
])
def test_valid_slugs(self, slug):
result = validate_slug_format(slug)
assert result['valid'] is True
assert result['message'] == ''
@pytest.mark.parametrize("slug,expected_msg_fragment", [
("ab", "at least 3"), # too short
("a", "at least 3"), # too short
("", "at least 3"), # empty
("a" * 21, "no more than 20"), # too long
("-mysite", "start and end"), # starts with hyphen
("mysite-", "start and end"), # ends with hyphen
("-", "at least 3"), # just a hyphen (too short)
("my--site", "consecutive hyphens"), # consecutive hyphens
("MySite", "lowercase"), # uppercase
("my_site", "lowercase"), # underscore
("my site", "lowercase"), # space
("my.site", "lowercase"), # dot
])
def test_invalid_slugs(self, slug, expected_msg_fragment):
result = validate_slug_format(slug)
assert result['valid'] is False
assert expected_msg_fragment in result['message']
def test_all_reserved_slugs_blocked(self):
for slug in RESERVED_SLUGS:
result = validate_slug_format(slug)
assert result['valid'] is False, f"Reserved slug '{slug}' was not blocked"
assert 'reserved' in result['message']
def test_reserved_slugs_list_not_empty(self):
assert len(RESERVED_SLUGS) > 0
class TestSlugToDbName:
"""Tests for slug_to_db_name."""
def test_no_hyphens(self):
assert slug_to_db_name("mysite") == "mysite"
def test_single_hyphen(self):
assert slug_to_db_name("my-site") == "my_site"
def test_multiple_hyphens(self):
assert slug_to_db_name("my-cool-site") == "my_cool_site"