Repository navigation
Expand file tree
/
Copy pathauth_tests.yaml
More file actions
296 lines (256 loc) · 8.87 KB
/
Copy pathauth_tests.yaml
File metadata and controls
296 lines (256 loc) · 8.87 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
version: "1.0"
name: "Authentication Test Suite"
description: "Comprehensive test suite for OAuth2, JWT, and Bearer token authentication"
# This test suite demonstrates how to test different authentication flows with testmcpy.
# It validates authentication success, token validity, OAuth2 flow completion, and error handling.
#
# To run these tests, you'll need to set up environment variables:
# - OAUTH_CLIENT_ID: Your OAuth client ID
# - OAUTH_CLIENT_SECRET: Your OAuth client secret
# - OAUTH_TOKEN_URL: Your OAuth token endpoint (e.g., https://auth.example.com/oauth/token)
# - JWT_API_URL: Your JWT API endpoint
# - JWT_API_TOKEN: Your JWT API token
# - JWT_API_SECRET: Your JWT API secret
# - BEARER_TOKEN: Your static bearer token
#
# Run with: testmcpy run examples/auth_tests.yaml --verbose
tests:
# ============================================================================
# OAuth2 Client Credentials Flow Tests
# ============================================================================
- name: "test_oauth_success"
description: "Verify OAuth client credentials flow succeeds"
auth:
type: oauth
client_id: ${OAUTH_CLIENT_ID}
client_secret: ${OAUTH_CLIENT_SECRET}
token_url: ${OAUTH_TOKEN_URL}
scopes: ["read", "write"]
prompt: "List all available datasets"
evaluators:
# Check authentication succeeded
- name: "auth_successful"
# Validate token format and length
- name: "token_valid"
args:
format: "jwt"
min_length: 100
# Verify all OAuth2 flow steps completed
- name: "oauth2_flow_complete"
# Ensure the tool was called (auth worked)
- name: "was_mcp_tool_called"
args:
tool_name: "list_datasets"
# Verify execution succeeded
- name: "execution_successful"
- name: "test_oauth_with_minimal_scopes"
description: "Test OAuth with minimal scope requirements"
auth:
type: oauth
client_id: ${OAUTH_CLIENT_ID}
client_secret: ${OAUTH_CLIENT_SECRET}
token_url: ${OAUTH_TOKEN_URL}
scopes: ["read"]
prompt: "List datasets"
evaluators:
- name: "auth_successful"
- name: "oauth2_flow_complete"
- name: "test_oauth_token_is_jwt"
description: "Verify OAuth returns a valid JWT token"
auth:
type: oauth
client_id: ${OAUTH_CLIENT_ID}
client_secret: ${OAUTH_CLIENT_SECRET}
token_url: ${OAUTH_TOKEN_URL}
scopes: ["read", "write"]
prompt: "Show me a dataset"
evaluators:
- name: "auth_successful"
- name: "token_valid"
args:
format: "jwt"
check_expiration: true
# ============================================================================
# JWT Dynamic Token Fetch Tests
# ============================================================================
- name: "test_jwt_dynamic_fetch"
description: "Verify dynamic JWT token fetch from API"
auth:
type: jwt
api_url: ${JWT_API_URL}
api_token: ${JWT_API_TOKEN}
api_secret: ${JWT_API_SECRET}
prompt: "List all datasets"
evaluators:
- name: "auth_successful"
- name: "token_valid"
args:
format: "jwt"
min_length: 100
- name: "was_mcp_tool_called"
args:
tool_name: "list_datasets"
- name: "execution_successful"
- name: "test_jwt_token_structure"
description: "Validate JWT token structure and claims"
auth:
type: jwt
api_url: ${JWT_API_URL}
api_token: ${JWT_API_TOKEN}
api_secret: ${JWT_API_SECRET}
prompt: "Get datasets"
evaluators:
- name: "auth_successful"
- name: "token_valid"
args:
format: "jwt"
# ============================================================================
# Bearer Token Authentication Tests
# ============================================================================
- name: "test_bearer_token"
description: "Verify static bearer token authentication"
auth:
type: bearer
token: ${BEARER_TOKEN}
prompt: "List datasets"
evaluators:
- name: "auth_successful"
- name: "token_valid"
args:
min_length: 10
- name: "was_mcp_tool_called"
args:
tool_name: "list_datasets"
- name: "execution_successful"
# ============================================================================
# Authentication Error Handling Tests
# ============================================================================
- name: "test_invalid_oauth_credentials"
description: "Verify proper error handling for invalid OAuth credentials"
auth:
type: oauth
client_id: "invalid-client-id"
client_secret: "invalid-client-secret"
token_url: ${OAUTH_TOKEN_URL}
prompt: "List datasets"
evaluators:
# This test expects authentication to fail
# We're checking that the error message is informative
- name: "auth_error_handling"
args:
required_info: ["invalid", "unauthorized", "401"]
min_length: 20
- name: "test_invalid_jwt_credentials"
description: "Verify proper error handling for invalid JWT credentials"
auth:
type: jwt
api_url: ${JWT_API_URL}
api_token: "invalid-token"
api_secret: "invalid-secret"
prompt: "List datasets"
evaluators:
- name: "auth_error_handling"
args:
required_info: ["invalid", "unauthorized"]
min_length: 15
- name: "test_invalid_bearer_token"
description: "Verify MCP server rejects invalid bearer token"
auth:
type: bearer
token: "invalid-token-12345"
prompt: "List datasets"
evaluators:
# Bearer token validation happens at MCP server level
# This might succeed at auth level but fail at tool call level
# So we just check the token is present
- name: "token_valid"
args:
min_length: 10
- name: "test_malformed_token_url"
description: "Test error handling for malformed OAuth token URL"
auth:
type: oauth
client_id: ${OAUTH_CLIENT_ID}
client_secret: ${OAUTH_CLIENT_SECRET}
token_url: "not-a-valid-url"
scopes: ["read"]
prompt: "List datasets"
evaluators:
- name: "auth_error_handling"
args:
min_length: 10
# ============================================================================
# Combined Auth + MCP Tool Tests
# ============================================================================
- name: "test_oauth_with_tool_parameter_validation"
description: "Test OAuth authentication combined with tool parameter validation"
auth:
type: oauth
client_id: ${OAUTH_CLIENT_ID}
client_secret: ${OAUTH_CLIENT_SECRET}
token_url: ${OAUTH_TOKEN_URL}
scopes: ["read", "write"]
prompt: "List datasets with limit 10"
evaluators:
- name: "auth_successful"
- name: "oauth2_flow_complete"
- name: "was_mcp_tool_called"
args:
tool_name: "list_datasets"
- name: "tool_called_with_parameter"
args:
tool_name: "list_datasets"
parameter_name: "limit"
- name: "execution_successful"
- name: "test_jwt_with_response_validation"
description: "Test JWT authentication with response content validation"
auth:
type: jwt
api_url: ${JWT_API_URL}
api_token: ${JWT_API_TOKEN}
api_secret: ${JWT_API_SECRET}
prompt: "Show me information about datasets"
evaluators:
- name: "auth_successful"
- name: "was_mcp_tool_called"
- name: "execution_successful"
- name: "final_answer_contains"
args:
expected_content: "dataset"
- name: "test_bearer_with_performance"
description: "Test bearer token authentication with performance requirements"
auth:
type: bearer
token: ${BEARER_TOKEN}
prompt: "Quickly list all datasets"
evaluators:
- name: "auth_successful"
- name: "was_mcp_tool_called"
args:
tool_name: "list_datasets"
- name: "within_time_limit"
args:
max_seconds: 10
- name: "execution_successful"
# Usage examples:
#
# 1. Run all auth tests:
# testmcpy run examples/auth_tests.yaml
#
# 2. Run with verbose output to see auth flow details:
# testmcpy run examples/auth_tests.yaml --verbose
#
# 3. Run only OAuth tests (filter by name pattern):
# testmcpy run examples/auth_tests.yaml --filter oauth
#
# 4. Run only error handling tests:
# testmcpy run examples/auth_tests.yaml --filter invalid
#
# 5. Generate a test report:
# testmcpy run examples/auth_tests.yaml --report auth-report.json
#
# Tips:
# - Use environment variables or .env file for sensitive credentials
# - Set up different profiles in MCP config for different environments
# - Check evaluator reference docs for all available auth evaluators
# - Use --verbose flag to debug authentication issues