Skip to content

Commit

Permalink
test incorrect json
Browse files Browse the repository at this point in the history
  • Loading branch information
kp-cat committed Aug 30, 2023
1 parent fdd567e commit b7fa936
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 21 deletions.
37 changes: 18 additions & 19 deletions configcatclient/configcatclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 35 additions & 2 deletions configcatclienttests/test_configcatclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()))
Expand Down

0 comments on commit b7fa936

Please sign in to comment.