1- import pytest
21import asyncio
32from unittest .mock import AsyncMock , patch
3+
4+ import pytest
5+
46from agentic_security .mcp import ClientSession
7+
58from ..agentic_security .mcp .client import run
69
7- # Fixtures
10+
11+ # Fixtures
812@pytest .fixture (scope = "session" )
913def event_loop ():
1014 """Create an instance of the default event loop for each test case."""
1115 loop = asyncio .get_event_loop_policy ().new_event_loop ()
1216 yield loop
1317 loop .close ()
1418
19+
1520@pytest .fixture
1621async def mock_session ():
17- with patch ('mcp.client.stdio.stdio_client' ) as mock_client :
18-
22+ with patch ("mcp.client.stdio.stdio_client" ) as mock_client :
1923 mock_read = AsyncMock ()
2024 mock_write = AsyncMock ()
21-
22- # Configures mock client such that mock responses are returned
25+
26+ # Configures mock client such that mock responses are returned
2327 mock_client .return_value .__aenter__ .return_value = (mock_read , mock_write )
24-
28+
2529 # Creates a mock session
2630 mock_session = AsyncMock (spec = ClientSession )
27-
31+
2832 # Expected responses
2933 mock_session .initialize = AsyncMock ()
3034 mock_session .list_prompts = AsyncMock (return_value = ["test_prompt" ])
3135 mock_session .list_resources = AsyncMock (return_value = ["test_resource" ])
3236 mock_session .list_tools = AsyncMock (return_value = ["echo_tool" ])
3337 mock_session .call_tool = AsyncMock (return_value = "Hello from client!" )
34-
35- with patch (' mcp.ClientSession' , return_value = mock_session ):
38+
39+ with patch (" mcp.ClientSession" , return_value = mock_session ):
3640 yield mock_session
3741
38- # Tests
42+
43+ # Tests
44+
3945
4046@pytest .mark .asyncio
4147async def test_initialization (mock_session ):
4248 """Test initialization success and failure cases"""
43- # Test initialization success case
49+ # Test initialization success case
4450 await run ()
4551 mock_session .initialize .assert_called_once ()
46-
47- # Resetting the mock to test for failure case
52+
53+ # Resetting the mock to test for failure case
4854 mock_session .initialize .reset_mock ()
4955 mock_session .initialize .side_effect = ConnectionError ("Failed to connect" )
50-
56+
5157 # Test connection error
5258 with pytest .raises (ConnectionError ):
5359 await run ()
5460
61+
5562@pytest .mark .asyncio
5663async def test_list_resources (mock_session ):
5764 """Test listing available resources"""
5865 await run ()
5966 mock_session .list_resources .assert_called_once ()
6067 assert await mock_session .list_resources () == ["test_resource" ]
6168
69+
6270@pytest .mark .asyncio
6371async def test_list_tools (mock_session ):
6472 """Test listing available tools"""
6573 await run ()
6674 mock_session .list_tools .assert_called_once ()
6775 assert await mock_session .list_tools () == ["echo_tool" ]
6876
77+
6978@pytest .mark .asyncio
7079async def test_echo_tool (mock_session ):
7180 """Test the echo tool functionality"""
7281 await run ()
7382 mock_session .call_tool .assert_called_once_with (
74- "echo_tool" ,
75- arguments = {"message" : "Hello from client!" }
76- )
83+ "echo_tool" , arguments = {"message" : "Hello from client!" }
84+ )
0 commit comments