diff --git a/zephyr/tests.py b/zephyr/tests.py index 3348c1bee2..d9b661289d 100644 --- a/zephyr/tests.py +++ b/zephyr/tests.py @@ -609,25 +609,25 @@ class SubscriptionPropertiesTest(AuthedTestCase): def test_get_stream_color(self): """ A GET request to - /json/subscriptions/property?property=color returns a - list of (stream, color) pairs, both of which are strings. + /json/subscriptions/property?property=color+stream_name=foo returns + the color for stream foo. """ test_email = "hamlet@humbughq.com" self.login(test_email) + subs = gather_subscriptions(self.get_user_profile(test_email)) result = self.client.get("/json/subscriptions/property", - {"property": "color"}) + {"property": "color", + "stream_name": subs[0]['name']}) self.assert_json_success(result) json = simplejson.loads(result.content) - self.assertIn("stream_colors", json) - subs = gather_subscriptions(self.get_user_profile(test_email)) - for stream, color in json["stream_colors"]: - self.assertIsInstance(color, basestring) - self.assertIsInstance(stream, basestring) - self.assertIn({'name': stream, 'in_home_view': True, 'color': color, 'invite_only': False}, subs) - subs.remove({'name': stream, 'in_home_view': True, 'color': color, 'invite_only': False}) - self.assertFalse(subs) + self.assertIn("stream_name", json) + self.assertIn("value", json) + self.assertIsInstance(json["stream_name"], basestring) + self.assertIsInstance(json["value"], basestring) + self.assertEqual(json["stream_name"], subs[0]["name"]) + self.assertEqual(json["value"], subs[0]["color"]) def test_set_stream_color(self): """ @@ -651,10 +651,13 @@ class SubscriptionPropertiesTest(AuthedTestCase): self.assert_json_success(result) new_subs = gather_subscriptions(self.get_user_profile(test_email)) - self.assertIn({'name': stream_name, 'in_home_view': True, 'color': new_color, 'invite_only': invite_only}, new_subs) + sub = {'name': stream_name, 'in_home_view': True, 'color': new_color, + 'invite_only': invite_only} + self.assertIn(sub, new_subs) - old_subs.remove({'name': stream_name, 'in_home_view': True, 'color': old_color, 'invite_only': invite_only}) - new_subs.remove({'name': stream_name, 'in_home_view': True, 'color': new_color, 'invite_only': invite_only}) + new_subs.remove(sub) + sub['color'] = old_color + old_subs.remove(sub) self.assertEqual(old_subs, new_subs) def test_set_color_missing_stream_name(self): @@ -675,9 +678,10 @@ class SubscriptionPropertiesTest(AuthedTestCase): """ test_email = "hamlet@humbughq.com" self.login(test_email) + subs = gather_subscriptions(self.get_user_profile(test_email)) result = self.client.post("/json/subscriptions/property", {"property": "color", - "stream_name": "test"}) + "stream_name": subs[0]["name"]}) self.assert_json_error(result, "Missing 'value' argument") @@ -685,12 +689,15 @@ class SubscriptionPropertiesTest(AuthedTestCase): """ Trying to set an invalid property returns a JSON error. """ - self.login("hamlet@humbughq.com") + test_email = "hamlet@humbughq.com" + self.login(test_email) + subs = gather_subscriptions(self.get_user_profile(test_email)) result = self.client.post("/json/subscriptions/property", - {"property": "bad"}) + {"property": "bad", + "stream_name": subs[0]["name"]}) self.assert_json_error(result, - "Unknown property or invalid verb for bad") + "Unknown subscription property: bad") class SubscriptionAPITest(AuthedTestCase): fixtures = ['messages.json'] diff --git a/zephyr/views.py b/zephyr/views.py index 97102c8189..9997e0ee8b 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -1141,91 +1141,33 @@ def get_subscription_or_die(stream_name, user_profile): return subscription -def set_in_home_view(user_profile, stream_name, value): - subscription = get_subscription_or_die(stream_name, user_profile)[0] - - subscription.in_home_view = value - subscription.save(update_fields=["in_home_view"]) - -class SubscriptionProperties(object): - """ - A class for managing GET and POST requests for subscription properties. The - name for a request handler is _. - - Requests must have already been authenticated before being processed here. - - Requests that set or change subscription properties should typically log the - change through log_event. - """ - - def __call__(self, request, user_profile, property): - property_method = getattr(self, "%s_%s" % (request.method.lower(), property), None) - if not property_method: - return json_error("Unknown property or invalid verb for %s" % (property,)) - - return property_method(request, user_profile) - - def request_property(self, request_dict, property): - try: - return request_dict[property].strip() - except KeyError: - raise RequestVariableMissingError(property) - - def get_color(self, request, user_profile): - return json_success({"stream_colors": get_stream_colors(user_profile)}) - - def post_color(self, request, user_profile): - stream_name = self.request_property(request.POST, "stream_name") - color = self.request_property(request.POST, "value") - - set_stream_color(user_profile, stream_name, color) - log_subscription_property_change(user_profile.email, stream_name, - "color", color) - return json_success() - - def post_in_home_view(self, request, user_profile): - stream_name = self.request_property(request.POST, "stream_name") - value = self.request_property(request.POST, "value").lower() - - if value == "true": - value = True - elif value == "false": - value = False - else: - raise JsonableError("Invalid value for `in_home_view`.") - - set_in_home_view(user_profile, stream_name, value) - - return json_success() - -subscription_properties = SubscriptionProperties() - -def make_property_call(request, query_dict, user_profile): - try: - property = query_dict["property"].strip() - except KeyError: - return json_error("Missing property") - - return subscription_properties(request, user_profile, property.lower()) - -def make_get_property_call(request, user_profile): - return make_property_call(request, request.GET, user_profile) - -def make_post_property_call(request, user_profile): - return make_property_call(request, request.POST, user_profile) - @authenticated_json_view -def json_subscription_property(request, user_profile): +@has_request_variables +def json_subscription_property(request, user_profile, stream_name=REQ, + property=REQ): """ This is the entry point to accessing or changing subscription - properties. Authentication happens here. - - Add a handler for a new subscription property in SubscriptionProperties. + properties. """ + property_converters = dict(color=lambda x: x, + in_home_view=json_to_bool) + if property not in property_converters: + return json_error("Unknown subscription property: %s" % (property,)) + + sub = get_subscription_or_die(stream_name, user_profile)[0] if request.method == "GET": - return make_get_property_call(request, user_profile) + return json_success({'stream_name': stream_name, + 'value': getattr(sub, property)}) elif request.method == "POST": - return make_post_property_call(request, user_profile) + @has_request_variables + def do_set_property(request, + value=POST(converter=property_converters[property])): + setattr(sub, property, value) + sub.save(update_fields=[property]) + log_subscription_property_change(user_profile.email, stream_name, + property, value) + do_set_property(request) + return json_success() else: return json_error("Invalid verb")