diff --git a/test/api/open_fga_api_test.py b/test/api/open_fga_api_test.py index 62f1556..b93441f 100644 --- a/test/api/open_fga_api_test.py +++ b/test/api/open_fga_api_test.py @@ -1526,14 +1526,14 @@ async def test_429_error_retry_configured_with_http_date( "message": "Rate Limit exceeded" } """ - retry_after_in_sec = 5 - five_seconds_from_now = ( + 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(http_resp=mock_http_response), @@ -1541,7 +1541,7 @@ async def test_429_error_retry_configured_with_http_date( ] retry = openfga_sdk.configuration.RetryParams( - max_retry=1, min_wait_in_ms=10, max_wait_in_sec=1 + max_retry=1, min_wait_in_ms=10, max_wait_in_sec=15 ) configuration = self.configuration configuration.store_id = store_id @@ -1563,7 +1563,7 @@ async def test_429_error_retry_configured_with_http_date( mock_request.assert_called() self.assertEqual(mock_request.call_count, 2) self.assertTrue( - retry_after_in_sec - 1 + retry_after_in_sec - 2 <= mock_sleep.call_args[0][0] <= retry_after_in_sec ) diff --git a/test/client/client_test.py b/test/client/client_test.py index f25d209..893fc56 100644 --- a/test/client/client_test.py +++ b/test/client/client_test.py @@ -2041,12 +2041,6 @@ async def test_client_batch_check_multiple_request(self, mock_request): 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", @@ -2062,6 +2056,20 @@ async def test_client_batch_check_multiple_request(self, mock_request): 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: @@ -2150,12 +2158,6 @@ async def test_client_batch_check_multiple_request_fail(self, mock_request): } """ - # 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", @@ -2171,6 +2173,22 @@ async def test_client_batch_check_multiple_request_fail(self, mock_request): 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: diff --git a/test/sync/client/client_test.py b/test/sync/client/client_test.py index 33548c9..8d0c3ff 100644 --- a/test/sync/client/client_test.py +++ b/test/sync/client/client_test.py @@ -2044,12 +2044,6 @@ def test_client_batch_check_multiple_request(self, mock_request): 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", @@ -2065,6 +2059,20 @@ def test_client_batch_check_multiple_request(self, mock_request): 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: @@ -2153,12 +2161,6 @@ def test_client_batch_check_multiple_request_fail(self, mock_request): } """ - # 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", @@ -2174,6 +2176,22 @@ def test_client_batch_check_multiple_request_fail(self, mock_request): 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: diff --git a/test/sync/open_fga_api_test.py b/test/sync/open_fga_api_test.py index 3fcb73a..d571c33 100644 --- a/test/sync/open_fga_api_test.py +++ b/test/sync/open_fga_api_test.py @@ -1589,14 +1589,14 @@ async def test_429_error_retry_configured_with_http_date( "message": "Rate Limit exceeded" } """ - retry_after_in_sec = 5 - five_seconds_from_now = ( + 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(http_resp=mock_http_response), @@ -1604,7 +1604,7 @@ async def test_429_error_retry_configured_with_http_date( ] retry = openfga_sdk.configuration.RetryParams( - max_retry=1, min_wait_in_ms=10, max_wait_in_sec=1 + max_retry=1, min_wait_in_ms=10, max_wait_in_sec=15 ) configuration = self.configuration configuration.store_id = store_id @@ -1625,7 +1625,11 @@ async def test_429_error_retry_configured_with_http_date( 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")