From 60075e596fc2369b1807ea3171066346eca1ae31 Mon Sep 17 00:00:00 2001 From: Luke Faraone Date: Thu, 21 Mar 2013 11:58:30 -0700 Subject: [PATCH] stream_exists_backend now returns a 404 if the stream is not found. Update tests and compose.js to handle the 404. (imported from commit d9ba4fe59c34bd14d9198e3365a845888fa04f03) --- zephyr/static/js/compose.js | 10 ++++++---- zephyr/tests.py | 5 ++++- zephyr/views.py | 5 +++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/zephyr/static/js/compose.js b/zephyr/static/js/compose.js index 6862c8aef8..d46bda64d1 100644 --- a/zephyr/static/js/compose.js +++ b/zephyr/static/js/compose.js @@ -365,16 +365,18 @@ exports.check_stream_existence = function (stream_name) { data: {'stream': stream_name}, async: false, success: function (data) { - if (!data.exists) { - result = "does-not-exist"; - } else if (data.subscribed) { + if (data.subscribed) { result = "subscribed"; } else { result = "not-subscribed"; } }, error: function (xhr) { - result = "error"; + if (xhr.status === 404) { + result = "does-not-exist"; + } else { + result = "error"; + } } }); return result; diff --git a/zephyr/tests.py b/zephyr/tests.py index 794e7dcc4f..97b0bc4ef9 100644 --- a/zephyr/tests.py +++ b/zephyr/tests.py @@ -929,10 +929,13 @@ class SubscriptionAPITest(AuthedTestCase): """ result = self.client.post("/json/subscriptions/exists", {"stream": stream}) - self.assert_json_success(result) json = simplejson.loads(result.content) self.assertIn("exists", json) self.assertEqual(json["exists"], exists) + if exists: + self.assert_json_success(result) + else: + self.assertEquals(result.status_code, 404) if not subscribed is None: self.assertIn("subscribed", json) self.assertEqual(json["subscribed"], subscribed) diff --git a/zephyr/views.py b/zephyr/views.py index 1af1fdfd32..2a3ffb11e9 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -37,7 +37,7 @@ from zephyr.decorator import require_post, \ get_user_profile_by_user_id from zephyr.lib.query import last_n from zephyr.lib.avatar import gravatar_hash -from zephyr.lib.response import json_success, json_error +from zephyr.lib.response import json_success, json_error, json_response from zephyr.lib.timestamp import timestamp_to_datetime, datetime_to_timestamp from zephyr.lib.cache import cache_with_key @@ -1022,7 +1022,8 @@ def json_stream_exists(request, user_profile, stream=POST): result["subscribed"] = Subscription.objects.filter(user_profile=user_profile, recipient=recipient, active=True).exists() - return json_success(result) + return json_success(result) # results are ignored for HEAD requests + return json_response(data=result, status=404) def get_subscription_or_die(stream_name, user_profile): stream = get_stream(stream_name, user_profile.realm)