diff --git a/config/clients/python/template/test/api_test.py.mustache b/config/clients/python/template/test/api_test.py.mustache index 69d2842d2..a7d424552 100644 --- a/config/clients/python/template/test/api_test.py.mustache +++ b/config/clients/python/template/test/api_test.py.mustache @@ -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( @@ -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 @@ -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") diff --git a/config/clients/python/template/test/client/client_test.py.mustache b/config/clients/python/template/test/client/client_test.py.mustache index b58da3bcb..ddf35e84e 100644 --- a/config/clients/python/template/test/client/client_test.py.mustache +++ b/config/clients/python/template/test/client/client_test.py.mustache @@ -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", @@ -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: @@ -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", @@ -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: diff --git a/config/clients/python/template/test/sync/api_test.py.mustache b/config/clients/python/template/test/sync/api_test.py.mustache index 2ef3f4f5f..7030f61b6 100644 --- a/config/clients/python/template/test/sync/api_test.py.mustache +++ b/config/clients/python/template/test/sync/api_test.py.mustache @@ -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( @@ -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 @@ -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") diff --git a/config/clients/python/template/test/sync/client/client_test.py.mustache b/config/clients/python/template/test/sync/client/client_test.py.mustache index 0f4e62b54..44f64261a 100644 --- a/config/clients/python/template/test/sync/client/client_test.py.mustache +++ b/config/clients/python/template/test/sync/client/client_test.py.mustache @@ -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", @@ -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: @@ -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", @@ -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: