From 595b60e684f7557a3fae1f1c2711e409117a647a Mon Sep 17 00:00:00 2001 From: "Kevin R. Barnes" Date: Mon, 25 Mar 2024 09:17:48 -0500 Subject: [PATCH 1/3] fix KeyError by providing empty dict as default value --- feature_gate/clients/posthog_api_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/feature_gate/clients/posthog_api_client.py b/feature_gate/clients/posthog_api_client.py index 1394fa2..3b890f9 100644 --- a/feature_gate/clients/posthog_api_client.py +++ b/feature_gate/clients/posthog_api_client.py @@ -77,7 +77,7 @@ def create_feature(self, name, description, deleted=False, active=False): return self._map_single_response("POST", path, response) def fetch_feature(self, key): - features = self.list_features()["data"] + features = self.list_features().get("data", {}) for entry in features: if "key" in entry and entry["key"] == key: return entry From ad083023cdbc59930faf8632584a99b7e1c5465d Mon Sep 17 00:00:00 2001 From: "Kevin R. Barnes" Date: Mon, 25 Mar 2024 21:07:02 -0500 Subject: [PATCH 2/3] change default value to a list --- feature_gate/clients/posthog_api_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/feature_gate/clients/posthog_api_client.py b/feature_gate/clients/posthog_api_client.py index 3b890f9..109656e 100644 --- a/feature_gate/clients/posthog_api_client.py +++ b/feature_gate/clients/posthog_api_client.py @@ -77,7 +77,7 @@ def create_feature(self, name, description, deleted=False, active=False): return self._map_single_response("POST", path, response) def fetch_feature(self, key): - features = self.list_features().get("data", {}) + features = self.list_features().get("data", []) for entry in features: if "key" in entry and entry["key"] == key: return entry From 8c2feac4e6c5184cd0a2dfc412d5e9ccbd947b65 Mon Sep 17 00:00:00 2001 From: "Kevin R. Barnes" Date: Mon, 25 Mar 2024 21:25:29 -0500 Subject: [PATCH 3/3] add scenario to test for 500 error returned by posthog api --- tests/feature_gate/adapters/posthog_test.py | 11 ++++++++++- tests/fixtures/posthog_api_client/mocks.py | 8 ++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/feature_gate/adapters/posthog_test.py b/tests/feature_gate/adapters/posthog_test.py index 743d5f2..fd82c4c 100644 --- a/tests/feature_gate/adapters/posthog_test.py +++ b/tests/feature_gate/adapters/posthog_test.py @@ -4,7 +4,7 @@ from feature_gate.adapters.posthog import PosthogAdapter from feature_gate.client import Client, FeatureNotFound from feature_gate.feature import Feature -from tests.fixtures.posthog_api_client.mocks import build_feature_from_mocks, mock_add_feature_funnel, mock_disable_feature_funnel, mock_enable_feature_funnel, mock_features_when_empty, mock_features_when_funnel, mock_funnel_is_disabled, mock_funnel_is_enabled, mock_remove_feature_funnel +from tests.fixtures.posthog_api_client.mocks import build_feature_from_mocks, mock_add_feature_funnel, mock_disable_feature_funnel, mock_enable_feature_funnel, mock_features_when_empty, mock_features_when_error_returned, mock_features_when_funnel, mock_funnel_is_disabled, mock_funnel_is_enabled, mock_remove_feature_funnel from unittest.mock import patch def configured_client(): @@ -87,6 +87,15 @@ def test_is_enabled_raises_an_error_when_the_feature_does_not_exist(): except FeatureNotFound as e: assert str(e) == "Feature funnel_test not found" +def test_is_enabled_raises_an_error_when_the_api_response_returns_an_error_status(): + client = configured_client() + feature = build_feature_from_mocks() + with patch.object(requests, 'get', return_value=mock_features_when_error_returned()): + try: + resp = client.is_enabled(feature.key) + except FeatureNotFound as e: + assert str(e) == "Feature funnel_test not found" + def test_enable_returns_true_when_feature_exists(): client = configured_client() feature = build_feature_from_mocks() diff --git a/tests/fixtures/posthog_api_client/mocks.py b/tests/fixtures/posthog_api_client/mocks.py index 8994ee6..65aaac7 100644 --- a/tests/fixtures/posthog_api_client/mocks.py +++ b/tests/fixtures/posthog_api_client/mocks.py @@ -81,3 +81,11 @@ def mock_funnel_is_disabled(): return_value=load_response('funnel_is_disabled') ) ) + +def mock_features_when_error_returned(): + return Mock( + status_code=500, + json=Mock( + return_value=load_response('get_features_when_empty') + ) + )