Files
wehub-resource-sync e52ddb7dd4
CodeQL / Analyze (python) (push) Failing after 1s
Linting and testing / build (3.11) (push) Failing after 0s
Package exe with PyInstaller - Windows / build (push) Failing after 2s
Linting and testing / build (3.10) (push) Failing after 1s
Linting and testing / minimal-install (push) Failing after 1s
Update sites rating and statistics / build (push) Failing after 1s
Build docker image and push to DockerHub / docker (push) Failing after 1s
Linting and testing / build (3.12) (push) Failing after 1s
Linting and testing / build (3.14) (push) Failing after 1s
Linting and testing / build (3.13) (push) Failing after 2s
chore: import upstream snapshot with attribution
2026-07-13 12:10:17 +08:00

78 lines
1.6 KiB
Python

"""
Unit tests for username extraction helpers.
"""
from maigret.utils import extract_usernames
from maigret.maigret import extract_ids_from_page
from unittest.mock import Mock, patch
def test_extract_username():
logger = Mock()
result = extract_usernames(
{"profile_username": "emily"},
logger,
)
assert result == ["emily"]
def test_extract_list_usernames():
logger = Mock()
result = extract_usernames(
{"profile_usernames": "['emily','ashton']"},
logger,
)
assert set(result) == {"emily", "ashton"}
def test_reject_invalid_username():
logger = Mock()
result = extract_usernames(
{"profile_username": "https.example.com/au"},
logger,
)
assert result == []
def test_ignore_invalid_username_list():
logger = Mock()
result = extract_usernames(
{"profile_usernames": "not-a-list"},
logger,
)
assert result == []
assert logger.warning.called
def test_extract_ids_from_page_username_contract():
logger = Mock()
with patch("maigret.maigret.parse") as mock_parse, \
patch("maigret.maigret.extract") as mock_extract, \
patch("maigret.maigret.extract_usernames") as mock_usernames:
# fake page fetch
mock_parse.return_value = ("<html></html>", {})
# no structured IDs
mock_extract.return_value = {}
# username detection
mock_usernames.return_value = ["emily"]
result = extract_ids_from_page(
"https://example.com/profile",
logger,
timeout=5,
)
assert result == {"emily": "username"}