Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
streams: Report an invalid subscription color instead of erroring.
Aman Agrawal675c8cf9927e
diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.pyindex 41d5df2c903..70ec9465fae 100644--- a/zerver/tests/test_subs.py+++ b/zerver/tests/test_subs.py@@ -3583,6 +3583,24 @@ class SubscriptionRestApiTest(ZulipTestCase):)self.assert_json_error(result, "Unknown subscription property: invalid")+ def test_api_invalid_color_value(self) -> None:+ """+ An invalid color is reported to the client, rather than raising+ an uncaught pydantic ValidationError.+ """+ user = self.example_user("hamlet")++ self.login_user(user)+ subs = gather_subscriptions(user)[0]++ for invalid_color in ["true", "3ffrff"]:+ result = self.api_patch(+ user,+ "/api/v1/users/me/subscriptions/{}".format(subs[0]["stream_id"]),+ {"property": "color", "value": invalid_color},+ )+ self.assert_json_error(result, "color is not a valid hex color code")+def test_api_invalid_stream_id(self) -> None:"""Trying to set an invalid stream id returns a JSON error.diff --git a/zerver/tests/test_subscription_settings.py b/zerver/tests/test_subscription_settings.pyindex cccc0ee7a2d..cb3a0ccf8f7 100644--- a/zerver/tests/test_subscription_settings.py+++ b/zerver/tests/test_subscription_settings.py@@ -59,9 +59,7 @@ class SubscriptionPropertiesTest(ZulipTestCase):).decode()},)- self.assert_json_error(- result, "Invalid subscription_data[0]: Value error, color is not a valid hex color code"- )+ self.assert_json_error(result, "color is not a valid hex color code")def test_set_color_missing_stream_id(self) -> None:"""@@ -395,9 +393,7 @@ class SubscriptionPropertiesTest(ZulipTestCase):).decode()},)- self.assert_json_error(- result, "Invalid subscription_data[0]: Value error, color is not a valid hex color code"- )+ self.assert_json_error(result, "color is not a valid hex color code")def test_json_subscription_property_invalid_stream(self) -> None:test_user = self.example_user("hamlet")diff --git a/zerver/views/streams.py b/zerver/views/streams.pyindex 8b4ae7dd680..2ff92aa639a 100644--- a/zerver/views/streams.py+++ b/zerver/views/streams.py@@ -1363,7 +1363,11 @@ class SubscriptionPropertyChangeRequest(BaseModel):}if self.property == "color":- self.value = check_color("color", self.value)+ try:+ self.value = check_color("color", self.value)+ except ValueError as e:+ # Raise JsonableError on validation failure to avoid 500.+ raise JsonableError(str(e))elif self.property in boolean_properties:if not isinstance(self.value, bool):raise JsonableError(_("{property} is not a boolean").format(property=self.property))