test_ita_login_flow.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. """W2/UT-04: real Italy entry and BaseFlow, synthetic browser only."""
  2. from contextlib import ExitStack
  3. from tempfile import TemporaryDirectory
  4. import unittest
  5. from unittest.mock import Mock, patch
  6. from plugins import ita_plugin as ita
  7. from test.ita.ita_test_fakes import FakeChromiumOptions, FakeKeyboard, FakeMouse, FakePage
  8. from utils.flow_engine import BaseFlow
  9. from vs_types import BizLogicError, VSAccount, VSPlgConfig
  10. class DelayedFormPage(FakePage):
  11. def __init__(self):
  12. super().__init__()
  13. self.password_reads = 0
  14. def ele(self, selector, timeout=None):
  15. if self.state == "pingid" and selector == "@name=callback_2":
  16. self.password_reads += 1
  17. if self.password_reads < 3:
  18. return None
  19. return super().ele(selector, timeout)
  20. class ServerErrorPage(FakePage):
  21. @property
  22. def html(self):
  23. return "503 Service Temporarily Unavailable" if self.state == "pingid" else ""
  24. class LoginFlowTests(unittest.TestCase):
  25. def setup_plugin(self, page):
  26. stack = ExitStack()
  27. self.addCleanup(stack.close)
  28. workspace = stack.enter_context(TemporaryDirectory())
  29. with patch.object(ita.os.path, "abspath", return_value=workspace):
  30. plugin = ita.ItaPlugin("offline-login-flow")
  31. plugin.set_config(VSPlgConfig(account=VSAccount(username="fixture@example.test", password="synthetic-password")))
  32. plugin.set_log(Mock())
  33. self.addCleanup(plugin.cleanup)
  34. stack.enter_context(patch("utils.plugin_utils.ChromiumOptions", FakeChromiumOptions))
  35. stack.enter_context(patch("utils.plugin_utils.get_free_port", return_value=9900))
  36. stack.enter_context(patch.object(ita, "ChromiumPage", return_value=page))
  37. stack.enter_context(patch.object(ita, "HumanMouse", FakeMouse))
  38. stack.enter_context(patch.object(ita, "HumanKeyboard", FakeKeyboard))
  39. stack.enter_context(patch.object(ita.time, "sleep"))
  40. return plugin
  41. def test_login_uses_tls_host_engine_with_no_stuck_refresh(self):
  42. page = FakePage()
  43. plugin = self.setup_plugin(page)
  44. plugin.create_session()
  45. flow = ita.ItaLoginFlow(plugin)
  46. self.assertIs(ita.ItaLoginFlow.run, BaseFlow.run)
  47. self.assertIsNone(flow.max_stuck_cycles)
  48. self.assertEqual(flow.interval, 3)
  49. self.assertTrue(plugin.is_healthy)
  50. self.assertEqual(page.refresh_calls, 0)
  51. self.assertEqual(page.clicked.count("submit"), 1)
  52. def test_delayed_password_control_does_not_partially_fill_or_repeat_credentials(self):
  53. page = DelayedFormPage()
  54. plugin = self.setup_plugin(page)
  55. plugin.create_session()
  56. self.assertEqual(page.password_reads, 3)
  57. self.assertEqual(page.clicked, ["login", "username", "password", "submit"])
  58. self.assertTrue(plugin.is_healthy)
  59. def test_foreign_origin_or_port_never_receives_credentials(self):
  60. for url in ("https://untrusted.example/signin/", "https://iam.esteri.it.attacker.example/signin/",
  61. "http://iam.esteri.it/signin/", "https://iam.esteri.it:8443/signin/",
  62. "https://userinfo-secret@iam.esteri.it/signin/"):
  63. with self.subTest(url=url):
  64. page = FakePage()
  65. page.URLS = {**page.URLS, "pingid": url}
  66. plugin = self.setup_plugin(page)
  67. with self.assertRaisesRegex(BizLogicError, "left the expected website") as caught:
  68. plugin.create_session()
  69. self.assertEqual(page.input_values, {})
  70. self.assertEqual(page.clicked, ["login"])
  71. self.assertFalse(plugin.is_healthy)
  72. def test_explicit_default_https_port_is_accepted(self):
  73. page = FakePage()
  74. page.URLS = {**page.URLS, "pingid": "https://iam.esteri.it:443/signin/?state=private-state"}
  75. plugin = self.setup_plugin(page)
  76. plugin.create_session()
  77. self.assertTrue(plugin.is_healthy)
  78. def test_stalled_login_keeps_once_only_submission(self):
  79. page = FakePage(submit_succeeds=False)
  80. plugin = self.setup_plugin(page)
  81. with self.assertRaisesRegex(BizLogicError, "流程步数耗尽") as caught:
  82. plugin.create_session()
  83. output = str(caught.exception) + "\n".join(c.args[0] for c in plugin.logger.call_args_list)
  84. self.assertNotIn("synthetic-password", output)
  85. self.assertEqual(page.clicked.count("submit"), 1)
  86. self.assertEqual(page.refresh_calls, 0)
  87. self.assertFalse(plugin.is_healthy)
  88. def test_server_error_after_entry_does_not_refresh_or_submit(self):
  89. page = ServerErrorPage()
  90. plugin = self.setup_plugin(page)
  91. with self.assertRaisesRegex(BizLogicError, "failed after login started"):
  92. plugin.create_session()
  93. self.assertEqual(page.clicked, ["login"])
  94. self.assertEqual(page.input_values, {})
  95. self.assertEqual(page.refresh_calls, 0)
  96. def test_stale_dashboard_on_wrong_host_is_not_authentication(self):
  97. page = FakePage()
  98. page.URLS = {**page.URLS, "dashboard": "https://untrusted.example/UserArea"}
  99. plugin = self.setup_plugin(page)
  100. with self.assertRaisesRegex(BizLogicError, "left the expected website"):
  101. plugin.create_session()
  102. self.assertFalse(plugin.is_healthy)
  103. if __name__ == "__main__":
  104. unittest.main()