From c05fb01cbfadaf9c18ec317543c16036508d586c Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Mon, 11 Mar 2019 11:36:00 -0700 Subject: [PATCH] stream: Fix validator for stream colors. Apparently, our new validator for stream color having a valid format incorrectly handled colors that had duplicate characters in them. (This is caused in part by the spectrum.js logic automatically converting #ffff00 to #ff0, which our validator rejected). Given that we had old stream colors in the #ff0 format in our database anyway for legacy, there's no benefit to banning these colors. In the future, we could imagine standardizing the format, but doing so will require also changing the frontend to submit colors only in the 6-character format. Fixes an issue reported in https://github.com/zulip/zulip/issues/11845#issuecomment-471417073 --- zerver/lib/validator.py | 2 +- zerver/tests/test_decorators.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/zerver/lib/validator.py b/zerver/lib/validator.py index 210578fcc7..4d113b466f 100644 --- a/zerver/lib/validator.py +++ b/zerver/lib/validator.py @@ -105,7 +105,7 @@ def check_bool(var_name: str, val: object) -> Optional[str]: def check_color(var_name: str, val: object) -> Optional[str]: if not isinstance(val, str): return _('%s is not a string') % (var_name,) - valid_color_pattern = re.compile(r'^#(?:[a-fA-F0-9]{6})$') + valid_color_pattern = re.compile(r'^#([a-fA-F0-9]{3,6})$') matched_results = valid_color_pattern.match(val) if not matched_results: return _('%s is not a valid hex color code') % (var_name,) diff --git a/zerver/tests/test_decorators.py b/zerver/tests/test_decorators.py index a609f3c2ef..ad9d60db62 100644 --- a/zerver/tests/test_decorators.py +++ b/zerver/tests/test_decorators.py @@ -772,7 +772,7 @@ class ValidatorTestCase(TestCase): self.assertEqual(check_float('x', x), 'x is not a float') def test_check_color(self) -> None: - x = ['#000099', '#80ffaa', '#80FFAA', '#abcd12'] # valid + x = ['#000099', '#80ffaa', '#80FFAA', '#abcd12', '#ffff00', '#ff0', '#f00'] # valid y = ['000099', '#80f_aa', '#80fraa', '#abcd1234', 'blue'] # invalid z = 5 # invalid