Files
maigret/tests/test_extractors.py
T
Sanjay SanthanamandGitHub e08310da87 fix: don't let the SUPPORTED_IDS branch re-add a rejected username in extract_ids_from_page (#2924)
extract_ids_from_page() has the same bug #2907 fixed in
checking.py::parse_usernames(). "username" is itself in SUPPORTED_IDS, so
the `if k in SUPPORTED_IDS` loop stores a bare `username` value directly,
bypassing the is_plausible_username() guard that extract_usernames()
applies in its own separate pass. A URL or email returned by
socid_extractor under a bare `username` key therefore becomes a recursive
search target via `maigret --parse-url`, reproducing the #1403
false-error cascade.

Skip keys containing "username" in the SUPPORTED_IDS loop; those are
already owned (and validated) by extract_usernames(). Every other
SUPPORTED_ID (gaia_id, vk_id, orcid, ...) is unaffected since none of
those keys contain "username".

Add regression tests for the bare `username` key with a URL value and for
the unchanged handling of other supported IDs.

Closes #2911
2026-08-03 08:43:50 +09:00

119 lines
2.8 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"}
def test_extract_ids_from_page_rejects_bare_username_url():
logger = Mock()
with patch("maigret.maigret.parse") as mock_parse, \
patch("maigret.maigret.extract") as mock_extract:
mock_parse.return_value = ("<html></html>", {})
# socid_extractor returned a URL under the bare `username` key
mock_extract.return_value = {"username": "https://instagram.com/zuck"}
result = extract_ids_from_page(
"https://example.com/profile",
logger,
timeout=5,
)
assert result == {}
def test_extract_ids_from_page_keeps_other_supported_ids():
logger = Mock()
with patch("maigret.maigret.parse") as mock_parse, \
patch("maigret.maigret.extract") as mock_extract:
mock_parse.return_value = ("<html></html>", {})
mock_extract.return_value = {
"gaia_id": "123456789",
"profile_username": "emily",
}
result = extract_ids_from_page(
"https://example.com/profile",
logger,
timeout=5,
)
assert result == {"123456789": "gaia_id", "emily": "username"}