update_storage: Validate storage type correctly.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2020-06-20 18:23:55 -07:00 committed by Tim Abbott
parent 717e349834
commit c584462b32
2 changed files with 6 additions and 4 deletions

View File

@ -299,7 +299,7 @@ class TestServiceBotStateHandler(ZulipTestCase):
'storage': ujson.dumps({'foo': [1, 2, 3]}),
}
result = self.client_put('/json/bot_storage', params)
self.assert_json_error(result, "Value type is <class 'list'>, but should be str.")
self.assert_json_error(result, "storage contains a value that is not a string")
# Remove some entries.
keys_to_remove = ['key 1', 'key 2']

View File

@ -16,11 +16,13 @@ from zerver.models import UserProfile
@has_request_variables
def update_storage(request: HttpRequest, user_profile: UserProfile,
storage: Dict[str, str]=REQ(validator=check_dict([]))) -> HttpResponse:
def update_storage(
request: HttpRequest, user_profile: UserProfile,
storage: Dict[str, str]=REQ(validator=check_dict([], value_validator=check_string)),
) -> HttpResponse:
try:
set_bot_storage(user_profile, list(storage.items()))
except StateError as e:
except StateError as e: # nocoverage
return json_error(str(e))
return json_success()