-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy patherror_integration_test.py
More file actions
517 lines (426 loc) · 17.9 KB
/
error_integration_test.py
File metadata and controls
517 lines (426 loc) · 17.9 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
"""
Integration tests for enhanced error handling.
These tests verify that:
1. Error messages include operation context
2. Error codes and messages are properly exposed
3. Request IDs are available for debugging
4. Helper methods work correctly for error categorization
NOTE: These tests require a running OpenFGA server.
Set FGA_API_URL environment variable or use default http://localhost:8080
To run these tests with a local OpenFGA instance:
1. Start OpenFGA: docker run -p 8080:8080 openfga/openfga run
2. Run tests: FGA_API_URL=http://localhost:8080 pytest test/error_integration_test.py -v
"""
import os
import pytest
import pytest_asyncio
from openfga_sdk.client import ClientConfiguration
from openfga_sdk.client.client import OpenFgaClient
from openfga_sdk.exceptions import NotFoundException, ValidationException
# Skip all tests if FGA_API_URL is not set (for CI/CD environments without OpenFGA)
pytestmark = pytest.mark.skipif(
not os.environ.get("FGA_API_URL") and not os.path.exists("/.dockerenv"),
reason="OpenFGA server not available. Set FGA_API_URL to run integration tests.",
)
# Sample authorization model for testing
AUTH_MODEL = {
"schema_version": "1.1",
"type_definitions": [
{"type": "user", "relations": {}},
{
"type": "document",
"relations": {
"viewer": {"this": {}},
"owner": {"this": {}},
},
"metadata": {
"relations": {
"viewer": {"directly_related_user_types": [{"type": "user"}]},
"owner": {"directly_related_user_types": [{"type": "user"}]},
}
},
},
],
}
@pytest.mark.integration
@pytest.mark.asyncio
class TestErrorIntegration:
"""Integration tests for enhanced error handling."""
@pytest_asyncio.fixture
async def fga_client(self):
"""
Create a test client with a store and authorization model.
Note: This requires a running OpenFGA server.
Set FGA_API_URL environment variable or use default localhost:8080.
"""
from openfga_sdk.models import CreateStoreRequest
api_url = os.environ.get("FGA_API_URL", "http://localhost:8080")
config = ClientConfiguration(
api_url=api_url,
)
client = OpenFgaClient(config)
# Create a test store
store = await client.create_store(CreateStoreRequest(name="ErrorTestStore"))
config.store_id = store.id
client = OpenFgaClient(config) # Recreate client with store_id
# Write the authorization model
model_response = await client.write_authorization_model(AUTH_MODEL)
config.authorization_model_id = model_response.authorization_model_id
client = OpenFgaClient(config) # Recreate client with auth model id
yield client
# Cleanup: delete the store
try:
await client.delete_store()
except Exception:
# Ignore exceptions during cleanup (e.g., store may already be deleted or server unavailable)
pass # Ignore cleanup errors
async def test_write_validation_error_invalid_type(self, fga_client):
"""Test that write with invalid type shows proper error details."""
from openfga_sdk.client.models import ClientTuple, ClientWriteRequest
# Try to write a tuple with invalid type
with pytest.raises(ValidationException) as exc_info:
await fga_client.write(
ClientWriteRequest(
writes=[
ClientTuple(
user="user:anne",
relation="viewer",
object="invalid_type:readme",
)
]
)
)
exception = exc_info.value
# Verify error details are accessible
assert exception.code is not None
assert "validation" in exception.code.lower()
assert "invalid_type" in exception.error_message.lower()
assert exception.operation_name == "write"
# request_id might be None in local dev environments
# assert exception.request_id is not None
assert exception.status == 400
# Verify formatted message includes all components
error_str = str(exception)
assert "[write]" in error_str
assert "HTTP 400" in error_str
assert "validation" in error_str.lower()
# request_id might be None in local dev, so it might not be in the message
# assert "[request-id:" in error_str
async def test_write_validation_error_invalid_relation(self, fga_client):
"""Test that write with invalid relation shows proper error details."""
from openfga_sdk.client.models import ClientTuple, ClientWriteRequest
# Try to write a tuple with valid type but invalid relation
with pytest.raises(ValidationException) as exc_info:
await fga_client.write(
ClientWriteRequest(
writes=[
ClientTuple(
user="user:anne",
relation="invalid_relation",
object="document:readme",
)
]
)
)
exception = exc_info.value
# Verify error details
assert exception.code is not None
assert "validation" in exception.code.lower()
assert "relation" in exception.error_message.lower()
assert exception.operation_name == "write"
# request_id might be None in local dev environments
# assert exception.request_id is not None
# Verify formatted message
error_str = str(exception)
assert "[write]" in error_str
assert "HTTP 400" in error_str
async def test_check_validation_error(self, fga_client):
"""Test that check with invalid type shows proper error details."""
from openfga_sdk.client.models import ClientCheckRequest
# Try check with invalid type
with pytest.raises(ValidationException) as exc_info:
await fga_client.check(
ClientCheckRequest(
user="user:anne",
relation="viewer",
object="invalid_type:readme",
)
)
exception = exc_info.value
assert exception.operation_name == "check"
assert exception.status == 400
assert exception.code is not None
assert "validation" in exception.code.lower()
error_str = str(exception)
assert "[check]" in error_str
assert "HTTP 400" in error_str
async def test_expand_validation_error(self, fga_client):
"""Test that expand with invalid type shows proper error details."""
from openfga_sdk.client.models import ClientExpandRequest
# Try expand with invalid type
with pytest.raises(ValidationException) as exc_info:
await fga_client.expand(
ClientExpandRequest(
object="invalid_type:readme",
relation="viewer",
)
)
exception = exc_info.value
assert exception.operation_name == "expand"
assert exception.status == 400
assert exception.code is not None
async def test_not_found_error(self, fga_client):
"""Test that not found errors are properly categorized."""
# Delete the store first
await fga_client.delete_store()
# Now try to get the deleted store
with pytest.raises(NotFoundException) as exc_info:
await fga_client.get_store()
exception = exc_info.value
assert exception.status == 404
assert exception.is_not_found_error()
assert exception.is_client_error()
assert not exception.is_server_error()
assert not exception.is_retryable()
error_str = str(exception)
assert "HTTP 404" in error_str
async def test_error_helper_methods_validation(self, fga_client):
"""Test helper methods for validation errors."""
from openfga_sdk.client.models import ClientTuple, ClientWriteRequest
with pytest.raises(ValidationException) as exc_info:
await fga_client.write(
ClientWriteRequest(
writes=[
ClientTuple(
user="user:anne",
relation="viewer",
object="invalid_type:readme",
)
]
)
)
exception = exc_info.value
# Test all helper methods
assert exception.is_validation_error()
assert exception.is_client_error()
assert not exception.is_server_error()
assert not exception.is_retryable()
assert not exception.is_authentication_error()
assert not exception.is_not_found_error()
assert not exception.is_rate_limit_error()
async def test_error_message_format_consistency(self, fga_client):
"""Test that error messages follow consistent format across operations."""
from openfga_sdk.client.models import (
ClientCheckRequest,
ClientTuple,
ClientWriteRequest,
)
# Test write error format
with pytest.raises(ValidationException) as write_exc:
await fga_client.write(
ClientWriteRequest(
writes=[
ClientTuple(
user="user:anne",
relation="viewer",
object="invalid:x",
)
]
)
)
write_error = str(write_exc.value)
assert write_error.startswith("[write]")
assert "HTTP 400" in write_error
# request_id might not be present in local dev
# assert "[request-id:" in write_error
# Test check error format
with pytest.raises(ValidationException) as check_exc:
await fga_client.check(
ClientCheckRequest(
user="user:anne",
relation="viewer",
object="invalid:x",
)
)
check_error = str(check_exc.value)
assert check_error.startswith("[check]")
assert "HTTP 400" in check_error
# request_id might not be present in local dev
# assert "[request-id:" in check_error
# Both should follow same pattern (with or without request-id)
import re
# Pattern with optional request-id at the end
pattern = r"^\[\w+\] HTTP \d{3} .+ \(.+\)( \[request-id: .+\])?$"
assert re.match(pattern, write_error)
assert re.match(pattern, check_error)
async def test_error_code_fields_accessibility(self, fga_client):
"""Test that all error fields are accessible."""
from openfga_sdk.client.models import ClientTuple, ClientWriteRequest
with pytest.raises(ValidationException) as exc_info:
await fga_client.write(
ClientWriteRequest(
writes=[
ClientTuple(
user="user:anne",
relation="viewer",
object="invalid_type:readme",
)
]
)
)
exception = exc_info.value
# Verify all fields are accessible
assert exception.status == 400
assert exception.code is not None
assert isinstance(exception.code, str)
assert exception.operation_name == "write"
assert exception.error_message is not None
assert isinstance(exception.error_message, str)
# request_id might be None in local dev environments
# assert exception.request_id is not None
# assert isinstance(exception.request_id, str)
# Request ID should match expected format if present
if exception.request_id:
import re
assert re.match(r"[a-zA-Z0-9-]+", exception.request_id)
async def test_different_validation_errors_have_different_messages(
self, fga_client
):
"""Test that different validation errors surface different messages."""
from openfga_sdk.client.models import ClientTuple, ClientWriteRequest
# Case 1: Invalid type
with pytest.raises(ValidationException) as exc1:
await fga_client.write(
ClientWriteRequest(
writes=[
ClientTuple(
user="user:anne",
relation="viewer",
object="invalid_type:readme",
)
]
)
)
error1 = exc1.value
assert "type" in error1.error_message.lower()
# Case 2: Invalid relation
with pytest.raises(ValidationException) as exc2:
await fga_client.write(
ClientWriteRequest(
writes=[
ClientTuple(
user="user:anne",
relation="invalid_relation",
object="document:readme",
)
]
)
)
error2 = exc2.value
assert "relation" in error2.error_message.lower()
# Both should have same error code but different messages
assert error1.code == error2.code
assert error1.error_message != error2.error_message
async def test_error_details_not_lost_in_traceback(self, fga_client):
"""Test that error details are preserved in exception traceback."""
from openfga_sdk.client.models import ClientTuple, ClientWriteRequest
try:
await fga_client.write(
ClientWriteRequest(
writes=[
ClientTuple(
user="user:anne",
relation="viewer",
object="invalid_type:readme",
)
]
)
)
pytest.fail("Expected ValidationException to be raised")
except ValidationException as e:
# String representation should include all details
error_string = str(e)
assert "invalid_type" in error_string.lower()
# Exception message should be properly formatted
error_message = e.error_message
assert "invalid_type" in error_message.lower()
# Fields should be accessible
assert e.status == 400
assert e.code is not None
assert e.operation_name == "write"
# request_id might be None in local dev environments
# assert e.request_id is not None
# Sync version of tests
@pytest.mark.integration
class TestErrorIntegrationSync:
"""Synchronous integration tests for enhanced error handling."""
@pytest.fixture
def fga_client_sync(self):
"""
Create a sync test client with a store and authorization model.
Note: This requires a running OpenFGA server.
"""
from openfga_sdk.models import CreateStoreRequest
from openfga_sdk.sync.client.client import OpenFgaClient as SyncOpenFgaClient
api_url = os.environ.get("FGA_API_URL", "http://localhost:8080")
config = ClientConfiguration(
api_url=api_url,
)
client = SyncOpenFgaClient(config)
# Create a test store
store = client.create_store(CreateStoreRequest(name="ErrorTestStoreSync"))
config.store_id = store.id
client = SyncOpenFgaClient(config) # Recreate client with store_id
# Write the authorization model
model_response = client.write_authorization_model(AUTH_MODEL)
config.authorization_model_id = model_response.authorization_model_id
client = SyncOpenFgaClient(config) # Recreate client with auth model id
yield client
# Cleanup
try:
client.delete_store()
except Exception:
pass
def test_sync_write_validation_error(self, fga_client_sync):
"""Test sync client error handling."""
from openfga_sdk.client.models import ClientTuple, ClientWriteRequest
with pytest.raises(ValidationException) as exc_info:
fga_client_sync.write(
ClientWriteRequest(
writes=[
ClientTuple(
user="user:anne",
relation="viewer",
object="invalid_type:readme",
)
]
)
)
exception = exc_info.value
assert exception.operation_name == "write"
assert exception.status == 400
assert exception.code is not None
assert exception.is_validation_error()
error_str = str(exception)
assert "[write]" in error_str
assert "HTTP 400" in error_str
def test_sync_error_helper_methods(self, fga_client_sync):
"""Test that helper methods work in sync client."""
from openfga_sdk.client.models import ClientTuple, ClientWriteRequest
with pytest.raises(ValidationException) as exc_info:
fga_client_sync.write(
ClientWriteRequest(
writes=[
ClientTuple(
user="user:anne",
relation="viewer",
object="invalid_type:readme",
)
]
)
)
exception = exc_info.value
assert exception.is_validation_error()
assert exception.is_client_error()
assert not exception.is_server_error()
assert not exception.is_retryable()