test_public_gateway.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import unittest
  2. from public_gateway import PublicGatewayApp
  3. class FakeSessionStore:
  4. def __init__(self):
  5. self.sessions = {}
  6. self.touched = []
  7. def get(self, gateway_session_id):
  8. return self.sessions.get(gateway_session_id)
  9. def touch_session(self, gateway_session_id):
  10. self.touched.append(gateway_session_id)
  11. class FakeApiClient:
  12. def __init__(self):
  13. self.calls = []
  14. def call_tool(self, token, tool_code, route_path, payload, request_id, client_ip=''):
  15. self.calls.append((token, tool_code, route_path, payload, request_id, client_ip))
  16. return {'code': 'MCP_0000', 'data': {'token_used': token}}
  17. def list_enabled_tools(self, token, request_id=''):
  18. return {
  19. 'code': 'MCP_0000',
  20. 'data': {
  21. 'tool_codes': [
  22. 'query_order',
  23. 'query_track',
  24. 'query_order_exact',
  25. 'list_order_filter_options',
  26. ],
  27. },
  28. }
  29. class PublicGatewayAppTest(unittest.TestCase):
  30. def test_two_employees_use_isolated_tokens(self):
  31. store = FakeSessionStore()
  32. store.sessions['GWS_A'] = {'mcp_token': 'MT_A'}
  33. store.sessions['GWS_B'] = {'mcp_token': 'MT_B'}
  34. api_client = FakeApiClient()
  35. app = PublicGatewayApp(session_store=store, api_client=api_client, auth_client=None)
  36. result_a = app.call_tool('GWS_A', 'query_order', {'keyword': 'A'}, request_id='rq_a')
  37. result_b = app.call_tool('GWS_B', 'query_order', {'keyword': 'B'}, request_id='rq_b')
  38. self.assertEqual('MT_A', result_a['data']['token_used'])
  39. self.assertEqual('MT_B', result_b['data']['token_used'])
  40. self.assertEqual('MT_A', api_client.calls[0][0])
  41. self.assertEqual('MT_B', api_client.calls[1][0])
  42. def test_public_tools_do_not_include_bind_auth_code(self):
  43. store = FakeSessionStore()
  44. store.sessions['GWS_A'] = {'mcp_token': 'MT_A'}
  45. app = PublicGatewayApp(session_store=store, api_client=FakeApiClient(), auth_client=None)
  46. tool_names = [tool['name'] for tool in app.list_tools('GWS_A')]
  47. self.assertNotIn('bind_auth_code', tool_names)
  48. self.assertIn('query_order', tool_names)
  49. self.assertIn('query_track', tool_names)
  50. self.assertIn('query_order_exact', tool_names)
  51. self.assertIn('list_order_filter_options', tool_names)
  52. def test_missing_session_returns_human_device_message(self):
  53. app = PublicGatewayApp(session_store=FakeSessionStore(), api_client=FakeApiClient(), auth_client=None)
  54. with self.assertRaises(RuntimeError) as error:
  55. app.call_tool('GWS_missing', 'query_order', {'keyword': 'A'}, request_id='rq_missing')
  56. self.assertIn('这台设备的 Workbuddy 配置已失效,请重新生成配置', str(error.exception))
  57. def test_tool_call_forwards_client_ip_to_api_client(self):
  58. store = FakeSessionStore()
  59. store.sessions['GWS_A'] = {'mcp_token': 'MT_A'}
  60. api_client = FakeApiClient()
  61. app = PublicGatewayApp(session_store=store, api_client=api_client, auth_client=None)
  62. app.call_tool('GWS_A', 'query_order', {'keyword': 'A'}, request_id='rq_a', client_ip='203.0.113.9')
  63. self.assertEqual('203.0.113.9', api_client.calls[0][5])
  64. def test_successful_tool_call_touches_gateway_session(self):
  65. store = FakeSessionStore()
  66. store.sessions['GWS_A'] = {'mcp_token': 'MT_A'}
  67. app = PublicGatewayApp(session_store=store, api_client=FakeApiClient(), auth_client=None)
  68. app.call_tool('GWS_A', 'query_order', {'keyword': 'A'}, request_id='rq_a')
  69. self.assertEqual(['GWS_A'], store.touched)
  70. if __name__ == '__main__':
  71. unittest.main()