-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathtest_mcp.py
More file actions
178 lines (156 loc) · 6.03 KB
/
Copy pathtest_mcp.py
File metadata and controls
178 lines (156 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""Test class for MCP server
:CaseAutomation: Automated
:CaseComponent: MCP
:Team: Endeavour
:Requirement: MCP
:CaseImportance: High
"""
from fastmcp import Client
from fastmcp.client.transports import StreamableHttpTransport
import pytest
from robottelo.config import settings
from robottelo.enums import NetworkType
@pytest.mark.asyncio
@pytest.mark.parametrize(
'mcp_server',
[
'module_target_sat_foreman_mcp',
'module_target_sat_foreman_mcp_stage',
'module_target_sat_foreman_mcp_downstream',
],
ids=['upstream', 'stage', 'downstream'],
)
@pytest.mark.skipif(
settings.server.network_type == NetworkType.IPV6,
reason='IPV6 scenario is not essential for this case',
)
async def test_positive_call_mcp_server(request, mcp_server):
"""Test that the MCP response matches with what is available on Satellite
:id: 6f3c31c2-2f50-43ba-ac52-b3ebc6af4e45
:expectedresults: MCP server is running and returns up to date data
"""
target_sat = request.getfixturevalue(mcp_server)
mcp_settings = settings.get(mcp_server.removeprefix('module_target_sat_'))
async with Client(
transport=StreamableHttpTransport(
f'http://{target_sat.hostname}:{mcp_settings.port}/mcp',
headers={
'FOREMAN_USERNAME': mcp_settings.username,
'FOREMAN_TOKEN': mcp_settings.password,
},
),
) as client:
result = await client.call_tool(
'call_foreman_api_get', {'resource': 'hosts', 'action': 'index', 'params': {}}
)
assert result.data['message'] == "Action 'index' on resource 'hosts' executed successfully."
hosts = target_sat.api.Host().search(query={'per_page': 'all'})
hostnames = sorted([host.name for host in hosts])
mcp_hostnames = sorted([host['name'] for host in result.data['response']['results']])
assert hostnames == mcp_hostnames
# add new host
host = target_sat.api.Host().create()
result = await client.call_tool(
'call_foreman_api_get', {'resource': 'hosts', 'action': 'index', 'params': {}}
)
mcp_hostnames = [host['name'] for host in result.data['response']['results']]
assert host.name in mcp_hostnames
@pytest.mark.asyncio
@pytest.mark.skipif(
settings.server.network_type == NetworkType.IPV6,
reason='IPV6 scenario is not essential for this case',
)
async def test_negative_call_mcp_server(module_target_sat_foreman_mcp):
"""Test that MCP server cannot alter Satellite
:id: 7b643847-4aa0-42ec-92cd-873de853f1ba
:expectedresults: non read-only actions are not allowed
"""
async with Client(
transport=StreamableHttpTransport(
f'http://{module_target_sat_foreman_mcp.hostname}:{settings.foreman_mcp.port}/mcp',
headers={
'FOREMAN_USERNAME': settings.foreman_mcp.username,
'FOREMAN_TOKEN': settings.foreman_mcp.password,
},
),
) as client:
result = await client.call_tool(
'call_foreman_api_get',
{
'resource': 'organizations',
'action': 'create',
'params': {'name': 'test_organization'},
},
)
assert (
result.data['message']
== "Failed to execute action 'create' on resource 'organizations'"
)
assert (
result.data['error']
== "Action 'create' on resource 'organizations' is not allowed: POST method is not allowed, expected GET."
)
@pytest.mark.asyncio
@pytest.mark.skipif(
settings.server.network_type == NetworkType.IPV6,
reason='IPV6 scenario is not essential for this case',
)
@pytest.mark.parametrize(
('user_fixture', 'allowed_resource', 'denied_resource', 'auth_type'),
[
('host_viewer_user', 'hosts', 'domains', 'password'),
('host_viewer_user', 'hosts', 'domains', 'token'),
('domain_viewer_user', 'domains', 'hosts', 'password'),
('domain_viewer_user', 'domains', 'hosts', 'token'),
],
ids=[
'host_viewer_user_password',
'host_viewer_user_token',
'domain_viewer_user_password',
'domain_viewer_user_token',
],
)
async def test_positive_mcp_user_view_permissions(
request,
module_target_sat_foreman_mcp,
user_fixture,
allowed_resource,
denied_resource,
auth_type,
):
"""Test that users with different view permissions can only view their authorized resources through MCP
:id: 9f3c31c2-2f50-43ba-ac52-b3ebc6af4e46
:expectedresults: Users can only view resources they have view permissions for using both password
and token authentication
"""
user, password, token = request.getfixturevalue(user_fixture)
auth_value = password if auth_type == 'password' else token
async with Client(
transport=StreamableHttpTransport(
f'http://{module_target_sat_foreman_mcp.hostname}:{settings.foreman_mcp.port}/mcp',
headers={
'FOREMAN_USERNAME': user.login,
'FOREMAN_TOKEN': auth_value,
},
),
) as client:
result = await client.call_tool(
'call_foreman_api_get', {'resource': allowed_resource, 'action': 'index', 'params': {}}
)
if 'error' in result.data and 'Max retries exceeded' in result.data['error']:
result = await client.call_tool(
'call_foreman_api_get',
{'resource': allowed_resource, 'action': 'index', 'params': {}},
)
assert (
result.data['message']
== f"Action 'index' on resource '{allowed_resource}' executed successfully."
)
result = await client.call_tool(
'call_foreman_api_get', {'resource': denied_resource, 'action': 'index', 'params': {}}
)
assert (
f"Failed to execute action 'index' on resource '{denied_resource}'"
in result.data['message']
)
assert result.data['response']['error']['message'] == 'Access denied'