From b7fa9366f7d980b28127866b92cd12d845f6bdd9 Mon Sep 17 00:00:00 2001 From: kp-cat <52385411+kp-cat@users.noreply.github.com> Date: Wed, 30 Aug 2023 09:52:23 +0200 Subject: [PATCH] test incorrect json --- configcatclient/configcatclient.py | 37 ++++++++++---------- configcatclienttests/test_configcatclient.py | 37 ++++++++++++++++++-- 2 files changed, 53 insertions(+), 21 deletions(-) diff --git a/configcatclient/configcatclient.py b/configcatclient/configcatclient.py index 3382037..1527a6f 100644 --- a/configcatclient/configcatclient.py +++ b/configcatclient/configcatclient.py @@ -197,25 +197,24 @@ def get_key_and_value(self, variation_id): return None settings = config.get(FEATURE_FLAGS, {}) - for key, value in list(settings.items()): - try: - if variation_id == value.get(VARIATION_ID): - return KeyValue(key, get_value(value)) - - targeting_rules = value.get(TARGETING_RULES, []) - for targeting_rule in targeting_rules: - served_value = targeting_rule.get(SERVED_VALUE) - if served_value is not None and variation_id == served_value.get(VARIATION_ID): - return KeyValue(key, get_value(served_value)) - - rollout_percentage_items = targeting_rule.get(PERCENTAGE_OPTIONS, []) - for rollout_percentage_item in rollout_percentage_items: - if variation_id == rollout_percentage_item.get(VARIATION_ID): - return KeyValue(key, get_value(rollout_percentage_item)) - except ValueError as e: - self.log.error("Value for variation ID '%s' for the key '%s' is invalid (%s)", variation_id, key, - str(e), event_id=2010) - return None + try: + for key, value in list(settings.items()): + if variation_id == value.get(VARIATION_ID): + return KeyValue(key, get_value(value)) + + targeting_rules = value.get(TARGETING_RULES, []) + for targeting_rule in targeting_rules: + served_value = targeting_rule.get(SERVED_VALUE) + if served_value is not None and variation_id == served_value.get(VARIATION_ID): + return KeyValue(key, get_value(served_value)) + + rollout_percentage_items = targeting_rule.get(PERCENTAGE_OPTIONS, []) + for rollout_percentage_item in rollout_percentage_items: + if variation_id == rollout_percentage_item.get(VARIATION_ID): + return KeyValue(key, get_value(rollout_percentage_item)) + except Exception: + self.log.exception('Error occurred in the `' + __name__ + '` method. Returning None.', event_id=1002) + return None self.log.error('Could not find the setting for the specified variation ID: \'%s\'.', variation_id, event_id=2011) return None diff --git a/configcatclienttests/test_configcatclient.py b/configcatclienttests/test_configcatclient.py index 8d23bf3..ac98ca4 100644 --- a/configcatclienttests/test_configcatclient.py +++ b/configcatclienttests/test_configcatclient.py @@ -8,13 +8,14 @@ from configcatclient import ConfigCatClientException from configcatclient.configcatclient import ConfigCatClient +from configcatclient.configentry import ConfigEntry from configcatclient.constants import VALUE, COMPARATOR, COMPARISON_ATTRIBUTE, SERVED_VALUE, STRING_VALUE, CONDITIONS from configcatclient.user import User from configcatclient.configcatoptions import ConfigCatOptions, Hooks from configcatclient.pollingmode import PollingMode -from configcatclient.utils import get_utc_now +from configcatclient.utils import get_utc_now, get_utc_now_seconds_since_epoch from configcatclienttests.mocks import ConfigCacheMock, TEST_OBJECT, TEST_SDK_KEY, TEST_SDK_KEY1, TEST_SDK_KEY2, \ - HookCallbacks + HookCallbacks, TEST_JSON_FORMAT, SingleValueConfigCache # Python2/Python3 support try: @@ -108,6 +109,38 @@ def test_invalidation(self): self.assertEqual(True, client.get_value('testBoolKey', False)) client.close() + def test_incorrect_json(self): + config_json_string = r'''{ + "f": { + "testKey": { + "t": 0, + "r": [ { + "c": [ { "t": { "a": "Custom1", "c": 19, "d": "wrong_utc_timestamp" } } ], + "s": { "v": { "b": true } } + } ], + "v": { "b": false } + } + } + }''' + config_cache = SingleValueConfigCache(ConfigEntry( + config=json.loads(config_json_string), + etag='test-etag', + config_json_string=config_json_string, + fetch_time=get_utc_now_seconds_since_epoch()).serialize() + ) + + hook_callbacks = HookCallbacks() + hooks = Hooks( + on_error=hook_callbacks.on_error + ) + client = ConfigCatClient.get(TEST_SDK_KEY, ConfigCatOptions(polling_mode=PollingMode.manual_poll(), + config_cache=config_cache, + hooks=hooks)) + self.assertEqual(False, client.get_value('testKey', False, User('1234', custom={'Custom1': 1681118000.56}))) + self.assertEqual(1, hook_callbacks.error_call_count) + self.assertTrue(hook_callbacks.error.startswith("Failed to evaluate setting 'testKey'.")) + client.close() + def test_get_all_keys(self): client = ConfigCatClient.get(TEST_SDK_KEY, ConfigCatOptions(polling_mode=PollingMode.manual_poll(), config_cache=ConfigCacheMock()))