From f2c04576bd369df96439c81f46dcaa748b54b8e4 Mon Sep 17 00:00:00 2001 From: Harshit Bansal Date: Fri, 21 Jul 2017 23:08:26 +0000 Subject: [PATCH] models: Fix the URL validation code in `RealmFilter` model. --- .../0094_realm_filter_url_validator.py | 22 +++++++++++++++++++ zerver/models.py | 2 +- zerver/tests/test_realm_filters.py | 5 +++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 zerver/migrations/0094_realm_filter_url_validator.py diff --git a/zerver/migrations/0094_realm_filter_url_validator.py b/zerver/migrations/0094_realm_filter_url_validator.py new file mode 100644 index 0000000000..b9af3bc83f --- /dev/null +++ b/zerver/migrations/0094_realm_filter_url_validator.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.2 on 2017-07-22 13:44 +from __future__ import unicode_literals + +import django.core.validators +from django.db import migrations, models +import zerver.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('zerver', '0093_subscription_event_log_backfill'), + ] + + operations = [ + migrations.AlterField( + model_name='realmfilter', + name='url_format_string', + field=models.TextField(validators=[django.core.validators.URLValidator(), zerver.models.filter_format_validator]), + ), + ] diff --git a/zerver/models.py b/zerver/models.py index 6fcc7a8d0e..20727fb84b 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -468,7 +468,7 @@ def filter_format_validator(value): class RealmFilter(models.Model): realm = models.ForeignKey(Realm, on_delete=CASCADE) # type: Realm pattern = models.TextField(validators=[filter_pattern_validator]) # type: Text - url_format_string = models.TextField(validators=[URLValidator, filter_format_validator]) # type: Text + url_format_string = models.TextField(validators=[URLValidator(), filter_format_validator]) # type: Text class Meta(object): unique_together = ("realm", "pattern") diff --git a/zerver/tests/test_realm_filters.py b/zerver/tests/test_realm_filters.py index 8225f5df61..5f73c84c0a 100644 --- a/zerver/tests/test_realm_filters.py +++ b/zerver/tests/test_realm_filters.py @@ -43,6 +43,11 @@ class RealmFilterTest(ZulipTestCase): data['pattern'] = 'ZUL-(?P\d+)' data['url_format_string'] = '$fgfg' result = self.client_post("/json/realm/filters", info=data) + self.assert_json_error(result, 'Enter a valid URL.') + + data['pattern'] = 'ZUL-(?P\d+)' + data['url_format_string'] = 'https://realm.com/my_realm_filter/' + result = self.client_post("/json/realm/filters", info=data) self.assert_json_error(result, 'URL format string must be in the following format: `https://example.com/%(\\w+)s`') data['url_format_string'] = 'https://realm.com/my_realm_filter/%(id)s'