test_public_gateway.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. class PublicGatewayAppTest(unittest.TestCase):
  18. def test_two_employees_use_isolated_tokens(self):
  19. store = FakeSessionStore()
  20. store.sessions['GWS_A'] = {'mcp_token': 'MT_A'}
  21. store.sessions['GWS_B'] = {'mcp_token': 'MT_B'}
  22. api_client = FakeApiClient()
  23. app = PublicGatewayApp(session_store=store, api_client=api_client, auth_client=None)
  24. result_a = app.call_tool('GWS_A', 'query_order', {'keyword': 'A'}, request_id='rq_a')
  25. result_b = app.call_tool('GWS_B', 'query_order', {'keyword': 'B'}, request_id='rq_b')
  26. self.assertEqual('MT_A', result_a['data']['token_used'])
  27. self.assertEqual('MT_B', result_b['data']['token_used'])
  28. self.assertEqual('MT_A', api_client.calls[0][0])
  29. self.assertEqual('MT_B', api_client.calls[1][0])
  30. def test_public_tools_do_not_include_bind_auth_code(self):
  31. app = PublicGatewayApp(session_store=FakeSessionStore(), api_client=FakeApiClient(), auth_client=None)
  32. tool_names = [tool['name'] for tool in app.list_tools()]
  33. self.assertNotIn('bind_auth_code', tool_names)
  34. self.assertIn('query_order', tool_names)
  35. self.assertIn('query_track', tool_names)
  36. def test_missing_session_returns_human_device_message(self):
  37. app = PublicGatewayApp(session_store=FakeSessionStore(), api_client=FakeApiClient(), auth_client=None)
  38. with self.assertRaises(RuntimeError) as error:
  39. app.call_tool('GWS_missing', 'query_order', {'keyword': 'A'}, request_id='rq_missing')
  40. self.assertIn('这台设备的 Workbuddy 配置已失效,请重新生成配置', str(error.exception))
  41. def test_tool_call_forwards_client_ip_to_api_client(self):
  42. store = FakeSessionStore()
  43. store.sessions['GWS_A'] = {'mcp_token': 'MT_A'}
  44. api_client = FakeApiClient()
  45. app = PublicGatewayApp(session_store=store, api_client=api_client, auth_client=None)
  46. app.call_tool('GWS_A', 'query_order', {'keyword': 'A'}, request_id='rq_a', client_ip='203.0.113.9')
  47. self.assertEqual('203.0.113.9', api_client.calls[0][5])
  48. def test_successful_tool_call_touches_gateway_session(self):
  49. store = FakeSessionStore()
  50. store.sessions['GWS_A'] = {'mcp_token': 'MT_A'}
  51. app = PublicGatewayApp(session_store=store, api_client=FakeApiClient(), auth_client=None)
  52. app.call_tool('GWS_A', 'query_order', {'keyword': 'A'}, request_id='rq_a')
  53. self.assertEqual(['GWS_A'], store.touched)
  54. if __name__ == '__main__':
  55. unittest.main()