| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- """W2/UT-04: real Italy entry and BaseFlow, synthetic browser only."""
- from contextlib import ExitStack
- from tempfile import TemporaryDirectory
- import unittest
- from unittest.mock import Mock, patch
- from plugins import ita_plugin as ita
- from test.ita.ita_test_fakes import FakeChromiumOptions, FakeKeyboard, FakeMouse, FakePage
- from utils.flow_engine import BaseFlow
- from vs_types import (
- BizLogicError, NotFoundError, PermissionDeniedError, RateLimiteddError,
- SessionExpiredOrInvalidError, VSAccount, VSPlgConfig,
- )
- class DelayedFormPage(FakePage):
- def __init__(self):
- super().__init__()
- self.password_reads = 0
- def ele(self, selector, timeout=None):
- if self.state == "pingid" and selector == "@name=callback_2":
- self.password_reads += 1
- if self.password_reads < 3:
- return None
- return super().ele(selector, timeout)
- class ServerErrorPage(FakePage):
- @property
- def html(self):
- return "503 Service Temporarily Unavailable" if self.state == "pingid" else ""
- class LoginFlowTests(unittest.TestCase):
- def setup_plugin(self, page):
- stack = ExitStack()
- self.addCleanup(stack.close)
- workspace = stack.enter_context(TemporaryDirectory())
- with patch.object(ita.os.path, "abspath", return_value=workspace):
- plugin = ita.ItaPlugin("offline-login-flow")
- plugin.set_config(VSPlgConfig(account=VSAccount(username="fixture@example.test", password="synthetic-password")))
- plugin.set_log(Mock())
- self.addCleanup(plugin.cleanup)
- stack.enter_context(patch("utils.plugin_utils.ChromiumOptions", FakeChromiumOptions))
- stack.enter_context(patch("utils.plugin_utils.get_free_port", return_value=9900))
- stack.enter_context(patch.object(ita, "ChromiumPage", return_value=page))
- stack.enter_context(patch.object(ita, "HumanMouse", FakeMouse))
- stack.enter_context(patch.object(ita, "HumanKeyboard", FakeKeyboard))
- stack.enter_context(patch.object(ita.time, "sleep"))
- return plugin
- def test_login_uses_tls_host_engine_with_no_stuck_refresh(self):
- page = FakePage()
- plugin = self.setup_plugin(page)
- plugin.create_session()
- flow = ita.ItaLoginFlow(plugin)
- self.assertIs(ita.ItaLoginFlow.run, BaseFlow.run)
- self.assertIsNone(flow.max_stuck_cycles)
- self.assertEqual(flow.interval, 3)
- self.assertTrue(plugin.is_healthy)
- self.assertEqual(page.refresh_calls, 0)
- self.assertEqual(page.clicked.count("submit"), 1)
- def test_delayed_password_control_does_not_partially_fill_or_repeat_credentials(self):
- page = DelayedFormPage()
- plugin = self.setup_plugin(page)
- plugin.create_session()
- self.assertEqual(page.password_reads, 3)
- self.assertEqual(page.clicked, ["login", "username", "password", "submit"])
- self.assertTrue(plugin.is_healthy)
- def test_foreign_origin_or_port_never_receives_credentials(self):
- for url in ("https://untrusted.example/signin/", "https://iam.esteri.it.attacker.example/signin/",
- "http://iam.esteri.it/signin/", "https://iam.esteri.it:8443/signin/",
- "https://userinfo-secret@iam.esteri.it/signin/"):
- with self.subTest(url=url):
- page = FakePage()
- page.URLS = {**page.URLS, "pingid": url}
- plugin = self.setup_plugin(page)
- with self.assertRaisesRegex(BizLogicError, "left the expected website") as caught:
- plugin.create_session()
- self.assertEqual(page.input_values, {})
- self.assertEqual(page.clicked, ["login"])
- self.assertFalse(plugin.is_healthy)
- def test_explicit_default_https_port_is_accepted(self):
- page = FakePage()
- page.URLS = {**page.URLS, "pingid": "https://iam.esteri.it:443/signin/?state=private-state"}
- plugin = self.setup_plugin(page)
- plugin.create_session()
- self.assertTrue(plugin.is_healthy)
- def test_stalled_login_keeps_once_only_submission(self):
- page = FakePage(submit_succeeds=False)
- plugin = self.setup_plugin(page)
- with self.assertRaisesRegex(BizLogicError, "流程步数耗尽") as caught:
- plugin.create_session()
- output = str(caught.exception) + "\n".join(c.args[0] for c in plugin.logger.call_args_list)
- self.assertNotIn("synthetic-password", output)
- self.assertEqual(page.clicked.count("submit"), 1)
- self.assertEqual(page.refresh_calls, 0)
- self.assertFalse(plugin.is_healthy)
- def test_server_error_after_entry_does_not_refresh_or_submit(self):
- page = ServerErrorPage()
- plugin = self.setup_plugin(page)
- with self.assertRaisesRegex(BizLogicError, "failed after login started"):
- plugin.create_session()
- self.assertEqual(page.clicked, ["login"])
- self.assertEqual(page.input_values, {})
- self.assertEqual(page.refresh_calls, 0)
- def test_stale_dashboard_on_wrong_host_is_not_authentication(self):
- page = FakePage()
- page.URLS = {**page.URLS, "dashboard": "https://untrusted.example/UserArea"}
- plugin = self.setup_plugin(page)
- with self.assertRaisesRegex(BizLogicError, "left the expected website"):
- plugin.create_session()
- self.assertFalse(plugin.is_healthy)
- def test_framework_login_errors_keep_their_original_category(self):
- for error_type in (SessionExpiredOrInvalidError, RateLimiteddError,
- PermissionDeniedError, NotFoundError, BizLogicError):
- with self.subTest(error_type=error_type):
- plugin = self.setup_plugin(FakePage())
- error = error_type("synthetic failure")
- with patch.object(ita.ItaLoginFlow, "run", side_effect=error), \
- self.assertRaises(error_type) as caught:
- plugin.create_session()
- self.assertIs(caught.exception, error)
- self.assertFalse(plugin.is_healthy)
- def test_missing_credentials_fail_before_starting_browser(self):
- for field, value in (("username", ""), ("username", " "),
- ("username", None), ("password", ""), ("password", None)):
- with self.subTest(field=field, value=value):
- plugin = self.setup_plugin(FakePage())
- setattr(plugin.config.account, field, value)
- with patch.object(ita, "init_chromium_options") as options, \
- patch.object(ita, "ChromiumPage") as browser, \
- self.assertRaisesRegex(BizLogicError, "username and password"):
- plugin.create_session()
- options.assert_not_called()
- browser.assert_not_called()
- self.assertFalse(plugin.is_healthy)
- def test_login_preserves_the_configured_password_including_spaces(self):
- page = FakePage()
- plugin = self.setup_plugin(page)
- plugin.config.account.password = " fixture password "
- plugin.create_session()
- self.assertEqual(plugin.config.account.password, " fixture password ")
- self.assertIn(" fixture password ", [item["value"] for item in page.input_values.values()])
- if __name__ == "__main__":
- unittest.main()
|