Skip to content

Commit

Permalink
Handle malformed user_profiles Message context with fallback to def…
Browse files Browse the repository at this point in the history
…ault user config
  • Loading branch information
NeonDaniel committed Dec 20, 2023
1 parent 2faeec2 commit 82522fb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 6 additions & 2 deletions neon_utils/user_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,13 @@ def get_user_prefs(message: Message = None) -> dict:
return default_user_config

for profile in message.context.get(profile_key):
if profile["user"]["username"] == username:
return dict(dict_update_keys(profile, default_user_config))
try:
if profile["user"]["username"] == username:
return dict(dict_update_keys(profile, default_user_config))
except KeyError:
LOG.error(f"Malformed profile in message context: {profile}")
LOG.warning(f"No preferences found for {username} in {message.context}")
default_user_config['user']['username'] = username
return default_user_config


Expand Down
9 changes: 9 additions & 0 deletions tests/user_util_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ def test_get_user_prefs(self):
self.assertIn("address", user_2["user"])
self.assertEqual(test_message_2.context['user_profiles'][1], user_2)

missing_profile = get_user_prefs(Message("", {},
{"username": "test",
"user_profiles": [{}]}))
self.assertEqual(missing_profile["user"]["username"], "test")
missing_profile_2 = get_user_prefs(Message("", {},
{"username": "test2",
"user_profiles": [{}]}))
self.assertEqual(missing_profile_2["user"]["username"], "test2")

def wrapper(message, valid_dict):
self.assertEqual(get_user_prefs(), valid_dict)

Expand Down

0 comments on commit 82522fb

Please sign in to comment.