From e354b255e637fbf0bbd8a33bfeb1bfe9fd83bf1a Mon Sep 17 00:00:00 2001 From: Shreyas Satish Date: Fri, 4 Aug 2017 13:25:14 +0530 Subject: [PATCH] added tests for invalid_urls, fixed minor bug with string pattern --- baseframe/forms/validators.py | 5 +++-- tests/fixtures.py | 8 +++++++- tests/test_validators.py | 14 ++++++++------ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/baseframe/forms/validators.py b/baseframe/forms/validators.py index 2122b55a..7ea55d94 100644 --- a/baseframe/forms/validators.py +++ b/baseframe/forms/validators.py @@ -238,8 +238,9 @@ def check_url(self, invalid_urls, url, text=None): # For text patterns, do a substring search. For regex patterns (assumed so if not text), # do a regex search. Test with the final URL from the response, after redirects, # but report errors using the URL the user provided - if (isinstance(pattern, basestring) and pattern in rurl) or (pattern.search(rurl) is not None): - return message.format(url=url, text=text) + if (isinstance(pattern, basestring) and pattern in rurl) or ( + isinstance(pattern, re._pattern_type) and pattern.search(rurl) is not None): + return message.format(url=url, text=text) # All good. The URL works and isn't invalid, so save to cache and return without an error message asset_cache.set(cache_key, {'url': rurl, 'code': code}, timeout=86400) return diff --git a/tests/fixtures.py b/tests/fixtures.py index b2d9216c..07ec82dc 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -1,3 +1,4 @@ +import re import unittest from flask import Flask from baseframe import baseframe @@ -29,9 +30,14 @@ def __init__(self, url=None, content=None): self.content = content +reject_list = [ + (['example.com', re.compile(r'example.in')], u'This URL is not allowed') +] + class TestUrlForm(forms.Form): url = forms.URLField(__("URL"), - validators=[forms.validators.DataRequired(), forms.validators.Length(max=255), forms.validators.ValidUrl()], + validators=[forms.validators.DataRequired(), forms.validators.Length(max=255), + forms.validators.ValidUrl(invalid_urls=reject_list)], filters=[forms.filters.strip()]) class TestAllUrlsForm(forms.Form): diff --git a/tests/test_validators.py b/tests/test_validators.py index e8b327ac..60de26ad 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -32,12 +32,14 @@ def test_inaccessible_url(self): self.form.process(url=url) self.assertEqual(self.form.validate(), False) - # def test_blacklisted_url(self): - # with self.app.test_request_context('/'): - # url = 'https://example.com/' - # self.form.process(url=url) - # import IPython; IPython.embed() - # self.assertEqual(self.form.validate(), False) + def test_disallowed_url(self): + with self.app.test_request_context('/'): + url = 'https://example.com/' + self.form.process(url=url) + self.assertEqual(self.form.validate(), False) + url = 'https://example.in/' + self.form.process(url=url) + self.assertEqual(self.form.validate(), False) def test_html_snippet_valid_urls(self): url1 = 'https://hasgeek.com/'