From 50d229fe11d0da46dfa38e7943074d2cdf70d02b Mon Sep 17 00:00:00 2001 From: Keegan McAllister Date: Wed, 2 Jan 2013 15:43:49 -0500 Subject: [PATCH] Accept '{}' to mean 'no narrowing', for old mobile clients (imported from commit 879f6ef846f2a8980a0574372324d8dc8d22c8ed) --- zephyr/tests.py | 18 +++++++++++++++++- zephyr/views.py | 28 ++++++++++++++++------------ 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/zephyr/tests.py b/zephyr/tests.py index 6d3fd6542d..5b0ed932ad 100644 --- a/zephyr/tests.py +++ b/zephyr/tests.py @@ -28,6 +28,9 @@ def find_key_by_email(address): if address in message.to: return key_regex.search(message.body).groups()[0] +def message_ids(result): + return set(message['id'] for message in result['messages']) + class AuthedTestCase(TestCase): def login(self, email, password=None): if password is None: @@ -652,13 +655,26 @@ class GetOldMessagesTest(AuthedTestCase): other_params = [("anchor", 0), ("num_before", 0), ("num_after", 0)] bad_types = (False, 0, '', '{malformed json,', - '{}', '{foo: 3}', '[1,2]', '[["x","y","z"]]') + '{foo: 3}', '[1,2]', '[["x","y","z"]]') for type in bad_types: post_params = dict(other_params + [("narrow", type)]) result = self.client.post("/json/get_old_messages", post_params) self.assert_json_error(result, "Bad value for 'narrow': %s" % (type,)) + def test_old_empty_narrow(self): + """ + '{}' is accepted to mean 'no narrow', for use by old mobile clients. + """ + self.login("hamlet@humbughq.com") + all_result = self.post_with_params({}) + narrow_result = self.post_with_params({'narrow': '{}'}) + + for r in (all_result, narrow_result): + self.check_well_formed_messages_response(r) + + self.assertEqual(message_ids(all_result), message_ids(narrow_result)) + def test_bad_narrow_operator(self): """ Unrecognized narrow operators are rejected. diff --git a/zephyr/views.py b/zephyr/views.py index 9f89e42764..c5adf5a417 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -63,17 +63,6 @@ def json_to_list(json): raise ValueError("argument is not a list") return data -def json_to_list_of_string_pairs(json): - data = json_to_list(json) - for elem in data: - if not isinstance(elem, list): - raise ValueError("element is not a list") - if (len(elem) != 2 - or any(not isinstance(x, str) and not isinstance(x, unicode) - for x in elem)): - raise ValueError("element is not a string pair") - return data - def get_stream(stream_name, realm): try: return Stream.objects.get(name__iexact=stream_name, realm=realm) @@ -347,11 +336,26 @@ class NarrowBuilder(object): return (Q(content__icontains=operand) | Q(subject__icontains=operand)) +def narrow_parameter(json): + # FIXME: A hack to support old mobile clients + if json == '{}': + return None + + data = json_to_list(json) + for elem in data: + if not isinstance(elem, list): + raise ValueError("element is not a list") + if (len(elem) != 2 + or any(not isinstance(x, str) and not isinstance(x, unicode) + for x in elem)): + raise ValueError("element is not a string pair") + return data + @has_request_variables def get_old_messages_backend(request, anchor = POST(converter=to_non_negative_int), num_before = POST(converter=to_non_negative_int), num_after = POST(converter=to_non_negative_int), - narrow = POST('narrow', converter=json_to_list_of_string_pairs, default=None), + narrow = POST('narrow', converter=narrow_parameter, default=None), user_profile=None, apply_markdown=True): query = Message.objects.select_related().filter(usermessage__user_profile = user_profile).order_by('id')