Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions config/clients/python/template/test/api_test.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -1514,12 +1514,12 @@ class TestOpenFgaApi(IsolatedAsyncioTestCase):
"message": "Rate Limit exceeded"
}
"""
retry_after_in_sec = 5
five_seconds_from_now = (datetime.now(timezone.utc) + timedelta(seconds=retry_after_in_sec)).strftime('%a, %d %b %Y %H:%M:%S GMT')
retry_after_in_sec = 10
ten_seconds_from_now = (datetime.now(timezone.utc) + timedelta(seconds=retry_after_in_sec)).strftime('%a, %d %b %Y %H:%M:%S GMT')
mock_http_response = http_mock_response(
body=error_response_body,
status=429,
headers={"Retry-After": five_seconds_from_now}
headers={"Retry-After": ten_seconds_from_now}
)
mock_request.side_effect = [
RateLimitExceededError(
Expand All @@ -1531,7 +1531,7 @@ class TestOpenFgaApi(IsolatedAsyncioTestCase):
retry = {{packageName}}.configuration.RetryParams(
max_retry=1,
min_wait_in_ms=10,
max_wait_in_sec=1
max_wait_in_sec=15
)
configuration = self.configuration
configuration.store_id = store_id
Expand All @@ -1552,7 +1552,7 @@ class TestOpenFgaApi(IsolatedAsyncioTestCase):
self.assertTrue(api_response.allowed)
mock_request.assert_called()
self.assertEqual(mock_request.call_count, 2)
self.assertTrue(retry_after_in_sec-1 <= mock_sleep.call_args[0][0] <= retry_after_in_sec)
self.assertTrue(retry_after_in_sec - 2 <= mock_sleep.call_args[0][0] <= retry_after_in_sec)

@patch('asyncio.sleep')
@patch.object(rest.RESTClientObject, "request")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2031,12 +2031,6 @@ class TestOpenFgaClient(IsolatedAsyncioTestCase):
Check whether a user is authorized to access an object
"""

# First, mock the response
mock_request.side_effect = [
mock_response('{"allowed": true, "resolution": "1234"}', 200),
mock_response('{"allowed": false, "resolution": "1234"}', 200),
mock_response('{"allowed": true, "resolution": "1234"}', 200),
]
body1 = ClientCheckRequest(
object="document:2021-budget",
relation="reader",
Expand All @@ -2052,6 +2046,20 @@ class TestOpenFgaClient(IsolatedAsyncioTestCase):
relation="reader",
user="user:81684243-9356-4421-8fbf-a4f8d36aa31d",
)

# Mock the response based on request body to avoid race conditions
def mock_side_effect(*args, **kwargs):
body = kwargs.get("body", {})
user = body.get("tuple_key", {}).get("user", "")
if user == "user:81684243-9356-4421-8fbf-a4f8d36aa31b":
return mock_response('{"allowed": true, "resolution": "1234"}', 200)
elif user == "user:81684243-9356-4421-8fbf-a4f8d36aa31c":
return mock_response('{"allowed": false, "resolution": "1234"}', 200)
elif user == "user:81684243-9356-4421-8fbf-a4f8d36aa31d":
return mock_response('{"allowed": true, "resolution": "1234"}', 200)
return mock_response('{"allowed": false, "resolution": "1234"}', 200)

mock_request.side_effect = mock_side_effect
configuration = self.configuration
configuration.store_id = store_id
async with OpenFgaClient(configuration) as api_client:
Expand Down Expand Up @@ -2140,12 +2148,6 @@ class TestOpenFgaClient(IsolatedAsyncioTestCase):
}
"""

# First, mock the response
mock_request.side_effect = [
mock_response('{"allowed": true, "resolution": "1234"}', 200),
ValidationException(http_resp=http_mock_response(response_body, 400)),
mock_response('{"allowed": false, "resolution": "1234"}', 200),
]
body1 = ClientCheckRequest(
object="document:2021-budget",
relation="reader",
Expand All @@ -2161,6 +2163,20 @@ class TestOpenFgaClient(IsolatedAsyncioTestCase):
relation="reader",
user="user:81684243-9356-4421-8fbf-a4f8d36aa31d",
)

# Mock the response based on request body to avoid race conditions
def mock_side_effect(*args, **kwargs):
body = kwargs.get("body", {})
user = body.get("tuple_key", {}).get("user", "")
if user == "user:81684243-9356-4421-8fbf-a4f8d36aa31b":
return mock_response('{"allowed": true, "resolution": "1234"}', 200)
elif user == "user:81684243-9356-4421-8fbf-a4f8d36aa31c":
raise ValidationException(http_resp=http_mock_response(response_body, 400))
elif user == "user:81684243-9356-4421-8fbf-a4f8d36aa31d":
return mock_response('{"allowed": false, "resolution": "1234"}', 200)
return mock_response('{"allowed": false, "resolution": "1234"}', 200)

mock_request.side_effect = mock_side_effect
configuration = self.configuration
configuration.store_id = store_id
async with OpenFgaClient(configuration) as api_client:
Expand Down
14 changes: 9 additions & 5 deletions config/clients/python/template/test/sync/api_test.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -1578,13 +1578,13 @@ class TestOpenFgaApiSync(IsolatedAsyncioTestCase):
"message": "Rate Limit exceeded"
}
"""
retry_after_in_sec = 5
five_seconds_from_now = (datetime.now(timezone.utc) + timedelta(seconds=retry_after_in_sec)).strftime(
retry_after_in_sec = 10
ten_seconds_from_now = (datetime.now(timezone.utc) + timedelta(seconds=retry_after_in_sec)).strftime(
'%a, %d %b %Y %H:%M:%S GMT')
mock_http_response = http_mock_response(
body=error_response_body,
status=429,
headers={"Retry-After": five_seconds_from_now}
headers={"Retry-After": ten_seconds_from_now}
)
mock_request.side_effect = [
RateLimitExceededError(
Expand All @@ -1596,7 +1596,7 @@ class TestOpenFgaApiSync(IsolatedAsyncioTestCase):
retry = {{packageName}}.configuration.RetryParams(
max_retry=1,
min_wait_in_ms=10,
max_wait_in_sec=1
max_wait_in_sec=15
)
configuration = self.configuration
configuration.store_id = store_id
Expand All @@ -1617,7 +1617,11 @@ class TestOpenFgaApiSync(IsolatedAsyncioTestCase):
self.assertTrue(api_response.allowed)
mock_request.assert_called()
self.assertEqual(mock_request.call_count, 2)
self.assertEqual(mock_sleep.call_args[0][0], retry_after_in_sec)
self.assertTrue(
retry_after_in_sec - 2
<= mock_sleep.call_args[0][0]
<= retry_after_in_sec
)

@patch('time.sleep')
@patch.object(rest.RESTClientObject, "request")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2034,12 +2034,6 @@ class TestOpenFgaClient(IsolatedAsyncioTestCase):
Check whether a user is authorized to access an object
"""

# First, mock the response
mock_request.side_effect = [
mock_response('{"allowed": true, "resolution": "1234"}', 200),
mock_response('{"allowed": false, "resolution": "1234"}', 200),
mock_response('{"allowed": true, "resolution": "1234"}', 200),
]
body1 = ClientCheckRequest(
object="document:2021-budget",
relation="reader",
Expand All @@ -2055,6 +2049,20 @@ class TestOpenFgaClient(IsolatedAsyncioTestCase):
relation="reader",
user="user:81684243-9356-4421-8fbf-a4f8d36aa31d",
)

# Mock the response based on request body to avoid race conditions
def mock_side_effect(*args, **kwargs):
body = kwargs.get("body", {})
user = body.get("tuple_key", {}).get("user", "")
if user == "user:81684243-9356-4421-8fbf-a4f8d36aa31b":
return mock_response('{"allowed": true, "resolution": "1234"}', 200)
elif user == "user:81684243-9356-4421-8fbf-a4f8d36aa31c":
return mock_response('{"allowed": false, "resolution": "1234"}', 200)
elif user == "user:81684243-9356-4421-8fbf-a4f8d36aa31d":
return mock_response('{"allowed": true, "resolution": "1234"}', 200)
return mock_response('{"allowed": false, "resolution": "1234"}', 200)

mock_request.side_effect = mock_side_effect
configuration = self.configuration
configuration.store_id = store_id
with OpenFgaClient(configuration) as api_client:
Expand Down Expand Up @@ -2143,12 +2151,6 @@ class TestOpenFgaClient(IsolatedAsyncioTestCase):
}
"""

# First, mock the response
mock_request.side_effect = [
mock_response('{"allowed": true, "resolution": "1234"}', 200),
ValidationException(http_resp=http_mock_response(response_body, 400)),
mock_response('{"allowed": false, "resolution": "1234"}', 200),
]
body1 = ClientCheckRequest(
object="document:2021-budget",
relation="reader",
Expand All @@ -2164,6 +2166,20 @@ class TestOpenFgaClient(IsolatedAsyncioTestCase):
relation="reader",
user="user:81684243-9356-4421-8fbf-a4f8d36aa31d",
)

# Mock the response based on request body to avoid race conditions
def mock_side_effect(*args, **kwargs):
body = kwargs.get("body", {})
user = body.get("tuple_key", {}).get("user", "")
if user == "user:81684243-9356-4421-8fbf-a4f8d36aa31b":
return mock_response('{"allowed": true, "resolution": "1234"}', 200)
elif user == "user:81684243-9356-4421-8fbf-a4f8d36aa31c":
raise ValidationException(http_resp=http_mock_response(response_body, 400))
elif user == "user:81684243-9356-4421-8fbf-a4f8d36aa31d":
return mock_response('{"allowed": false, "resolution": "1234"}', 200)
return mock_response('{"allowed": false, "resolution": "1234"}', 200)

mock_request.side_effect = mock_side_effect
configuration = self.configuration
configuration.store_id = store_id
with OpenFgaClient(configuration) as api_client:
Expand Down
Loading