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 = """
АО Тест
ИНН
6150061451
ОГРН
1096100003625
""" PROFILE_HTML = """
АО Тест
Действующее
Общая информация
ИНН
6150061451
ОГРН
1096100003625
КПП
772601001
Полное наименование
АКЦИОНЕРНОЕ ОБЩЕСТВО ТЕСТ
Дата регистрации
13.11.2009
Публикации
Нет данных
Обременения
Нет данных
Единоличный исполнительный орган
Нет данных
Сведения о СРО
Нет данных
Членство в СРО
Нет данных
Лицензии
Нет данных
Стоимость чистых активов
Нет данных
Сведения о реорганизации
Нет данных
Сведения о банкротстве
Нет данных
Торги
Нет данных
""" 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 + ( '
' '
Новый атрибут
Полное значение
' ) 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( '
' '
Аудируемое лицо
АО ЭКОС
' '
Мнение аудитора
Полный текст заключения
' 'Документ
', 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("Проверка Qrator CAPTCHA", 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( "
403 Error
Forbidden
" "

Access to fedresurs.ru is forbidden.

", inn="6150061451", ogrn="1096100003625", ) def test_script_mentions_of_challenge_do_not_hide_valid_public_page(self): profile = parse_profile_html( PROFILE_HTML + '', 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 = ( "
403 Error
" "

Access to fedresurs.ru is forbidden.

" ) def find_elements(self, *_args): return [] with self.assertRaisesRegex(FedresursUpstreamError, "upstream_access_blocked"): _wait_public_element(BlockedBrowser(), "css selector", "input")