Files
mostovik-backend/tests/apps/parsers/test_fedresurs_company_profile.py
Aleksandr Meshchryakov 85a3c0650e
Some checks failed
Mostovik Backend CI/CD / Tests and lint (push) Failing after 6m35s
Mostovik Backend CI/CD / Build linux/amd64 release images (push) Has been skipped
Mostovik Backend CI/CD / Deploy dev (push) Has been skipped
Mostovik Backend CI/CD / Deploy and verify internal main (push) Has been skipped
feat: add Fedresurs profiles and integrated structures sources
2026-09-24 14:14:01 +02:00

147 lines
7.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from apps.parsers.fedresurs_browser import _related_company_urls, _wait_public_element
from apps.parsers.fedresurs_company_profile import (
FedresursUpstreamError,
parse_profile_html,
parse_related_page_html,
parse_search_html,
)
from django.test import SimpleTestCase
SEARCH_HTML = """
<app-quick-search-entities-result-card>
<div>АО Тест</div><div><div>ИНН</div><div>6150061451</div></div>
<div><div>ОГРН</div><div>1096100003625</div></div>
</app-quick-search-entities-result-card>
"""
PROFILE_HTML = """
<div>АО Тест</div><div>Действующее</div>
<div page-item-name="general-info"><div>Общая информация</div>
<div><div>ИНН</div><div>6150061451</div></div>
<div><div>ОГРН</div><div>1096100003625</div></div>
<div><div>КПП</div><div>772601001</div></div>
<div><div>Полное наименование</div><div>АКЦИОНЕРНОЕ ОБЩЕСТВО ТЕСТ</div></div>
<div><div>Дата регистрации</div><div>13.11.2009</div></div>
</div>
<div page-item-name="messages"><div>Публикации</div><div>Нет данных</div></div>
<div page-item-name="encumbrances"><div>Обременения</div><div>Нет данных</div></div>
<div page-item-name="eio"><div>Единоличный исполнительный орган</div><div>Нет данных</div></div>
<div page-item-name="sro"><div>Сведения о СРО</div><div>Нет данных</div></div>
<div page-item-name="sro-membership"><div>Членство в СРО</div><div>Нет данных</div></div>
<div page-item-name="licenses"><div>Лицензии</div><div>Нет данных</div></div>
<div page-item-name="networth"><div>Стоимость чистых активов</div><div>Нет данных</div></div>
<div page-item-name="reorganization"><div>Сведения о реорганизации</div><div>Нет данных</div></div>
<div page-item-name="bankrupt"><div>Сведения о банкротстве</div><div>Нет данных</div></div>
<div page-item-name="trades"><div>Торги</div><div>Нет данных</div></div>
"""
class FedresursDomTest(SimpleTestCase):
def test_exact_search_match(self):
result = parse_search_html(SEARCH_HTML, inn="6150061451", ogrn="1096100003625")
self.assertEqual(result, "match")
def test_search_never_accepts_partial_identifier(self):
result = parse_search_html(SEARCH_HTML, inn="615006145", ogrn="1096100003625")
self.assertEqual(result, "no_match")
def test_duplicate_exact_search_is_ambiguous(self):
result = parse_search_html(SEARCH_HTML + SEARCH_HTML, inn="6150061451", ogrn="1096100003625")
self.assertEqual(result, "ambiguous")
def test_profile_requires_complete_identity_and_sections(self):
profile = parse_profile_html(
PROFILE_HTML,
url="https://fedresurs.ru/companies/e1661e0e-d217-4598-9e70-a94f4d4bbe52",
inn="6150061451", ogrn="1096100003625",
)
self.assertEqual(profile["fedresurs_guid"], "e1661e0e-d217-4598-9e70-a94f4d4bbe52")
self.assertEqual(profile["profile"]["kpp"], "772601001")
self.assertEqual(profile["sections"]["encumbrances"], "empty")
self.assertIn("ИНН", profile["rendered_sections"]["profile"]["text"])
self.assertIn("tree", profile["rendered_sections"]["profile"])
self.assertIn("АКЦИОНЕРНОЕ ОБЩЕСТВО ТЕСТ", profile["rendered_page"]["text"])
self.assertIn(
{"label": "ИНН", "value": "6150061451"},
profile["rendered_sections"]["profile"]["attributes"],
)
def test_profile_keeps_new_unknown_sections_without_schema_changes(self):
html = PROFILE_HTML + (
'<div page-item-name="additional-attributes">'
'<div>Новый атрибут</div><div>Полное значение</div></div>'
)
profile = parse_profile_html(
html,
url="https://fedresurs.ru/companies/e1661e0e-d217-4598-9e70-a94f4d4bbe52",
inn="6150061451",
ogrn="1096100003625",
)
self.assertIn("Полное значение", profile["rendered_page"]["text"])
self.assertIn("Полное значение", profile["rendered_sections"]["additional-attributes"]["text"])
def test_related_message_keeps_all_rendered_fields_and_links(self):
page = parse_related_page_html(
'<sfact-message-card><div page-item-name="">'
'<div>Аудируемое лицо</div><div>АО ЭКОС</div>'
'<div><div>Мнение аудитора</div><div>Полный текст заключения</div></div>'
'<a href="/documents/a.pdf">Документ</a></div></sfact-message-card>',
url="https://fedresurs.ru/sfactmessages/b2beb243-fffb-40c6-8b10-6c890ba59578",
page_type="message",
)
self.assertIn("Полный текст заключения", page["text"])
self.assertEqual(page["links"][0]["url"], "https://fedresurs.ru/documents/a.pdf")
self.assertIn(
{"label": "Мнение аудитора", "value": "Полный текст заключения"},
page["attributes"],
)
self.assertNotIn("_ngcontent", str(page["tree"]))
def test_only_follows_discovered_same_company_pages(self):
profile = {
"fedresurs_guid": "e1661e0e-d217-4598-9e70-a94f4d4bbe52",
"rendered_sections": {
"publications": {"links": [
{"url": "https://fedresurs.ru/companies/e1661e0e-d217-4598-9e70-a94f4d4bbe52/publications"},
{"url": "https://fedresurs.ru/companies/00000000-0000-0000-0000-000000000000/publications"},
{"url": "https://evil.example/companies/e1661e0e-d217-4598-9e70-a94f4d4bbe52/publications"},
]},
},
}
self.assertEqual(_related_company_urls(profile), [
("publications", "https://fedresurs.ru/companies/e1661e0e-d217-4598-9e70-a94f4d4bbe52/publications")
])
def test_challenge_and_partial_page_are_upstream_errors(self):
with self.assertRaises(FedresursUpstreamError):
parse_profile_html("<body>Проверка Qrator CAPTCHA</body>", url="https://fedresurs.ru/companies/e1661e0e-d217-4598-9e70-a94f4d4bbe52", inn="6150061451", ogrn="1096100003625")
with self.assertRaises(FedresursUpstreamError):
parse_profile_html(PROFILE_HTML.replace('page-item-name="general-info"', 'data-missing="1"'), url="https://fedresurs.ru/companies/e1661e0e-d217-4598-9e70-a94f4d4bbe52", inn="6150061451", ogrn="1096100003625")
with self.assertRaisesRegex(FedresursUpstreamError, "upstream_access_blocked"):
parse_search_html(
"<body><div>403 Error</div><div>Forbidden</div>"
"<p>Access to fedresurs.ru is forbidden.</p></body>",
inn="6150061451", ogrn="1096100003625",
)
def test_script_mentions_of_challenge_do_not_hide_valid_public_page(self):
profile = parse_profile_html(
PROFILE_HTML + '<script>const marker = "Qrator CAPTCHA";</script>',
url="https://fedresurs.ru/companies/e1661e0e-d217-4598-9e70-a94f4d4bbe52",
inn="6150061451",
ogrn="1096100003625",
)
self.assertEqual(profile["profile"]["inn"], "6150061451")
def test_wait_detects_late_403_instead_of_reporting_timeout(self):
class BlockedBrowser:
page_source = (
"<body><div>403 Error</div>"
"<p>Access to fedresurs.ru is forbidden.</p></body>"
)
def find_elements(self, *_args):
return []
with self.assertRaisesRegex(FedresursUpstreamError, "upstream_access_blocked"):
_wait_public_element(BlockedBrowser(), "css selector", "input")