Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ Default: None

The path for the MaxMind GeoIP data.

TINYLINK_VALIDATION_ENABLED
+++++++++++++++++++++

Default: False

The validation of the long url , disabled by default

Usage
-----

Expand Down
7 changes: 7 additions & 0 deletions tinylinks/detaults.py
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
from django.conf import settings

DEFAULT_ALLOWED_URL_SCHEMES = ("http", "https", "ftp")

try:
TINYLINK_VALIDATION_ENABLED = settings.TINYLINK_VALIDATION_ENABLED
except AttributeError:
TINYLINK_VALIDATION_ENABLED = False
6 changes: 5 additions & 1 deletion tinylinks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from urllib3 import PoolManager
from urllib3.exceptions import HTTPError, MaxRetryError, TimeoutError


User = get_user_model()


Expand Down Expand Up @@ -40,12 +41,15 @@ def get_url_response(pool, link, url):
return response


def validate_long_url(link):
def validate_long_url(link, force_validation=False):
"""
Function to validate a URL. The validator uses urllib3 to test the URL's
availability.

"""
from tinylinks.detaults import TINYLINK_VALIDATION_ENABLED
if not TINYLINK_VALIDATION_ENABLED and not force_validation:
return link
http = PoolManager()
response = get_url_response(http, link, link.long_url)
if response and response.status == 200:
Expand Down
20 changes: 19 additions & 1 deletion tinylinks/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def test_validate_long_url_server_code_with_broken_link(self, mock_fn):
def test_validate_long_url_with_error(self, mock_fn):
response = Mock()
mock_fn.return_value = response
validate_long_url(self.link)
validate_long_url(self.link, True)
self.assertEqual(self.link.validation_error, "URL not accessible.")

def test_can_not_be_validated(self):
Expand All @@ -481,6 +481,24 @@ def test_can_be_validated(self, mock_fn):
)
self.assertEqual(True, self.link.can_be_validated())

@patch("tinylinks.models.get_url_response")
@patch("tinylinks.detaults.TINYLINK_VALIDATION_ENABLED", True)
def test_validate_long_url_enabled(self, mock_fn):
""" test validate long url is enabled """
response = Mock()
mock_fn.return_value = response
link = validate_long_url(self.link)
self.assertNotEqual(link.validation_error, "")

@patch("tinylinks.models.get_url_response")
@patch("tinylinks.detaults.TINYLINK_VALIDATION_ENABLED", False)
def test_validate_long_url_disabled(self, mock_fn):
""" test validate long url is disabled """
response = Mock()
mock_fn.return_value = response
link = validate_long_url(self.link)
self.assertEqual(link.validation_error, "")


class TinyLinkFormTests(TestCase):
def setUp(self):
Expand Down