test_tools.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. from __future__ import annotations
  2. import httpx
  3. import pytest
  4. from unittest.mock import MagicMock
  5. from mcp.client.session import ClientSession
  6. from conftest import call_tool, call_tool_text
  7. from src.services.client_rest import HTTPMethod
  8. @pytest.mark.anyio
  9. class TestToolsMcpIntegration:
  10. async def test_list_tools_exposes_static_endpoints(self, mcp_session: ClientSession) -> None:
  11. result = await mcp_session.list_tools()
  12. tools_name = {tool.name for tool in result.tools}
  13. assert tools_name == {"createResource", "updateResource", "deleteResource",
  14. "createTemplate", "updateTemplate", "createTv",
  15. "updateTv", "createCategory", "updateCategory", "getPage"}
  16. async def test_create_resource(self, mcp_session: ClientSession, client_rest: MagicMock) -> None:
  17. client_rest.execute.return_value = {
  18. "code": "200",
  19. "data": {
  20. "id": 1,
  21. "alias": "test-resource",
  22. },
  23. }
  24. result = await call_tool(mcp_session, "createResource", {"pagetitle": "Test Resource"})
  25. assert result == {
  26. "code": "200",
  27. "data": {
  28. "id": 1,
  29. "alias": "test-resource",
  30. },
  31. }
  32. client_rest.execute.assert_called_once_with(
  33. method=HTTPMethod.POST,
  34. endpoint="/rest/tools/resource",
  35. json={"pagetitle": "Test Resource"},
  36. )
  37. async def test_update_resource(self, mcp_session: ClientSession, client_rest: MagicMock) -> None:
  38. client_rest.execute.return_value = {
  39. "code": "200",
  40. "data": {
  41. "id": 1
  42. },
  43. }
  44. result = await call_tool(mcp_session, "updateResource", {"id": 1, "pagetitle": "Updated Resource"})
  45. assert result == {
  46. "code": "200",
  47. "data": {
  48. "id": 1,
  49. },
  50. }
  51. client_rest.execute.assert_called_once_with(
  52. method=HTTPMethod.PUT,
  53. endpoint="/rest/tools/resource",
  54. json={"id": 1, "pagetitle": "Updated Resource"},
  55. )
  56. async def test_delete_resource(self, mcp_session: ClientSession, client_rest: MagicMock) -> None:
  57. client_rest.execute.return_value = {
  58. "code": "200",
  59. "data": {
  60. "id": 1,
  61. },
  62. }
  63. result = await call_tool(mcp_session, "deleteResource", {"id": 1})
  64. assert result == {
  65. "code": "200",
  66. "data": {
  67. "id": 1,
  68. },
  69. }
  70. client_rest.execute.assert_called_once_with(
  71. method=HTTPMethod.DELETE,
  72. endpoint="/rest/tools/resource/1",
  73. params={}
  74. )
  75. async def test_create_template(self, mcp_session: ClientSession, client_rest: MagicMock) -> None:
  76. client_rest.execute.return_value = {
  77. "code": "200",
  78. "data": {
  79. "id": 1,
  80. "name": "test-template",
  81. },
  82. }
  83. result = await call_tool(mcp_session, "createTemplate", {"name": "Test Template"})
  84. assert result == {
  85. "code": "200",
  86. "data": {
  87. "id": 1,
  88. "name": "test-template",
  89. },
  90. }
  91. client_rest.execute.assert_called_once_with(
  92. method=HTTPMethod.POST,
  93. endpoint="/rest/tools/template",
  94. json={"name": "Test Template"},
  95. )
  96. async def test_update_template(self, mcp_session: ClientSession, client_rest: MagicMock) -> None:
  97. client_rest.execute.return_value = {
  98. "code": "200",
  99. "data": {
  100. "id": 1,
  101. },
  102. }
  103. result = await call_tool(mcp_session, "updateTemplate", {"id": 1, "name": "Updated Template"})
  104. assert result == {
  105. "code": "200",
  106. "data": {
  107. "id": 1,
  108. },
  109. }
  110. client_rest.execute.assert_called_once_with(
  111. method=HTTPMethod.PUT,
  112. endpoint="/rest/tools/template",
  113. json={"id": 1, "name": "Updated Template"},
  114. )
  115. async def test_create_tv(self, mcp_session: ClientSession, client_rest: MagicMock) -> None:
  116. client_rest.execute.return_value = {
  117. "code": "200",
  118. "data": {
  119. "id": 1,
  120. "name": "test-tv",
  121. },
  122. }
  123. result = await call_tool(mcp_session, "createTv", {"name": "Test TV"})
  124. assert result == {
  125. "code": "200",
  126. "data": {
  127. "id": 1,
  128. "name": "test-tv",
  129. },
  130. }
  131. client_rest.execute.assert_called_once_with(
  132. method=HTTPMethod.POST,
  133. endpoint="/rest/tools/tv",
  134. json={"name": "Test TV"},
  135. )
  136. async def test_update_tv(self, mcp_session: ClientSession, client_rest: MagicMock) -> None:
  137. client_rest.execute.return_value = {
  138. "code": "200",
  139. "data": {
  140. "id": 1,
  141. },
  142. }
  143. result = await call_tool(mcp_session, "updateTv", {"id": 1, "name": "Updated TV"})
  144. assert result == {
  145. "code": "200",
  146. "data": {
  147. "id": 1,
  148. },
  149. }
  150. client_rest.execute.assert_called_once_with(
  151. method=HTTPMethod.PUT,
  152. endpoint="/rest/tools/tv",
  153. json={"id": 1, "name": "Updated TV"},
  154. )
  155. async def test_create_category(self, mcp_session: ClientSession, client_rest: MagicMock) -> None:
  156. client_rest.execute.return_value = {
  157. "code": "200",
  158. "data": {
  159. "id": 1,
  160. "name": "test-category",
  161. },
  162. }
  163. result = await call_tool(mcp_session, "createCategory", {"name": "Test Category"})
  164. assert result == {
  165. "code": "200",
  166. "data": {
  167. "id": 1,
  168. "name": "test-category",
  169. },
  170. }
  171. client_rest.execute.assert_called_once_with(
  172. method=HTTPMethod.POST,
  173. endpoint="/rest/tools/category",
  174. json={"name": "Test Category"},
  175. )
  176. async def test_update_category(self, mcp_session: ClientSession, client_rest: MagicMock) -> None:
  177. client_rest.execute.return_value = {
  178. "code": "200",
  179. "data": {
  180. "id": 1,
  181. },
  182. }
  183. result = await call_tool(mcp_session, "updateCategory", {"id": 1, "name": "Updated Category"})
  184. assert result == {
  185. "code": "200",
  186. "data": {
  187. "id": 1,
  188. },
  189. }
  190. async def test_get_page_returns_html_slice(self, mcp_session: ClientSession, client_rest: MagicMock) -> None:
  191. html = "0123456789" * 20
  192. client_rest.execute.return_value = html
  193. result = await call_tool_text(mcp_session, "getPage", {"url": "accueil/test", "limit": 50})
  194. assert result == html[:50]
  195. client_rest.execute.assert_called_once_with(
  196. method=HTTPMethod.GET,
  197. endpoint="/accueil/test",
  198. )
  199. async def test_get_page_with_keyword(self, mcp_session: ClientSession, client_rest: MagicMock) -> None:
  200. html = "aaaKEYWORDbbb"
  201. client_rest.execute.return_value = html
  202. result = await call_tool_text(
  203. mcp_session,
  204. "getPage",
  205. {"url": "accueil/test", "keyword": "KEYWORD", "offsetTop": 2, "limit": 5},
  206. )
  207. assert result == html[1:15]
  208. client_rest.execute.assert_called_once_with(
  209. method=HTTPMethod.GET,
  210. endpoint="/accueil/test",
  211. )
  212. async def test_get_page_retries_on_301(self, mcp_session: ClientSession, client_rest: MagicMock) -> None:
  213. request = httpx.Request("GET", "https://example.com/accueil/test")
  214. response = httpx.Response(301, request=request)
  215. redirect_error = httpx.HTTPStatusError("Moved", request=request, response=response)
  216. html = "<html><body>redirected</body></html>"
  217. client_rest.execute.side_effect = [redirect_error, html]
  218. result = await call_tool_text(mcp_session, "getPage", {"url": "accueil/test"})
  219. assert result == html[:100]
  220. assert client_rest.execute.call_count == 2
  221. client_rest.execute.assert_any_call(method=HTTPMethod.GET, endpoint="/accueil/test")
  222. client_rest.execute.assert_any_call(method=HTTPMethod.GET, endpoint="/accueil/test/")
  223. async def test_get_page_propagates_http_error(self, mcp_session: ClientSession, client_rest: MagicMock) -> None:
  224. request = httpx.Request("GET", "https://example.com/missing")
  225. response = httpx.Response(404, request=request)
  226. not_found_error = httpx.HTTPStatusError("Not Found", request=request, response=response)
  227. client_rest.execute.side_effect = not_found_error
  228. result = await mcp_session.call_tool("getPage", {"url": "missing"})
  229. assert result.isError is True
  230. assert "Not Found" in result.content[0].text
  231. client_rest.execute.assert_called_once_with(
  232. method=HTTPMethod.GET,
  233. endpoint="/missing",
  234. )