diff --git a/README.rst b/README.rst index 5590560..07cadf6 100644 --- a/README.rst +++ b/README.rst @@ -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 ----- diff --git a/tinylinks/detaults.py b/tinylinks/detaults.py index 0fccd93..75bb723 100644 --- a/tinylinks/detaults.py +++ b/tinylinks/detaults.py @@ -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 diff --git a/tinylinks/models.py b/tinylinks/models.py index 44da01f..1fa6d08 100644 --- a/tinylinks/models.py +++ b/tinylinks/models.py @@ -11,6 +11,7 @@ from urllib3 import PoolManager from urllib3.exceptions import HTTPError, MaxRetryError, TimeoutError + User = get_user_model() @@ -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: diff --git a/tinylinks/tests/tests.py b/tinylinks/tests/tests.py index 4ee2500..18c2572 100644 --- a/tinylinks/tests/tests.py +++ b/tinylinks/tests/tests.py @@ -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): @@ -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):