Skip to content

Commit

Permalink
added tests for invalid_urls, fixed minor bug with string pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyas-satish committed Aug 4, 2017
1 parent 16f2237 commit e354b25
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
5 changes: 3 additions & 2 deletions baseframe/forms/validators.py
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion tests/fixtures.py
@@ -1,3 +1,4 @@
import re
import unittest
from flask import Flask
from baseframe import baseframe
Expand Down Expand Up @@ -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):
Expand Down
14 changes: 8 additions & 6 deletions tests/test_validators.py
Expand Up @@ -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/'
Expand Down

0 comments on commit e354b25

Please sign in to comment.