Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
madjid-asa committed Dec 4, 2024
1 parent 7c448f8 commit cc702a1
Showing 1 changed file with 120 additions and 0 deletions.
120 changes: 120 additions & 0 deletions lemarche/utils/tests_validators.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import socket
from unittest.mock import patch

import requests
from django.core.exceptions import ValidationError
from django.test import TestCase

Expand Down Expand Up @@ -55,3 +59,119 @@ def test_naf_validator(self):
NAF_NOT_OK = ["1234", "12345", "ABCDE"]
for item in NAF_NOT_OK:
self.assertRaises(ValidationError, validator, item)


class StrictURLValidatorTests(TestCase):
"""
Test suite for the `StrictURLValidator` class.
Covers syntax validation, DNS resolution, and HTTP response checks.
"""

def setUp(self):
"""Set up a reusable instance of the validator."""
self.validator = OptionalSchemeURLValidator()

@patch("socket.gethostbyname")
@patch("requests.get")
def test_valid_url(self, mock_requests_get, mock_socket_gethostbyname):
"""
Test: Valid URL with proper DNS resolution and HTTP server response.
Expected: No exception raised.
"""
mock_socket_gethostbyname.return_value = "127.0.0.1"
mock_requests_get.return_value.status_code = 200

try:
self.validator("http://example.com")
except ValidationError:
self.fail("StrictURLValidator raised ValidationError for a valid URL.")

def test_invalid_syntax(self):
"""
Test: URL with invalid syntax (e.g., missing scheme or malformed domain).
Expected: ValidationError with a user-friendly message.
"""
with self.assertRaises(ValidationError) as context:
self.validator("http://en cours")
self.assertIn("Saisissez une URL valide.", str(context.exception))

@patch("socket.gethostbyname")
def test_dns_failure(self, mock_socket_gethostbyname):
"""
Test: URL with a domain that cannot be resolved (DNS failure).
Expected: ValidationError with a message indicating the domain is invalid.
"""
mock_socket_gethostbyname.side_effect = socket.gaierror

with self.assertRaises(ValidationError) as context:
self.validator("http://invalid-domain.com")
self.assertIn("Le site web associé à cette adresse n'existe pas", str(context.exception))

@patch("socket.gethostbyname")
@patch("requests.get")
def test_http_error(self, mock_requests_get, mock_socket_gethostbyname):
"""
Test: URL with a valid domain but the HTTP server returns an error response.
Expected: ValidationError with a message indicating a server issue.
"""
mock_socket_gethostbyname.return_value = "127.0.0.1"
mock_requests_get.return_value.status_code = 500

with self.assertRaises(ValidationError) as context:
self.validator("http://example.com")
self.assertIn("Le site web semble rencontrer un problème", str(context.exception))

@patch("socket.gethostbyname")
@patch("requests.get")
def test_http_timeout(self, mock_requests_get, mock_socket_gethostbyname):
"""
Test: URL with a valid domain but the HTTP server takes too long to respond (timeout).
Expected: ValidationError with a message indicating a timeout.
"""
mock_socket_gethostbyname.return_value = "127.0.0.1"
mock_requests_get.side_effect = requests.Timeout

with self.assertRaises(ValidationError) as context:
self.validator("http://example.com")
self.assertIn("Une erreur est survenue en essayant de vérifier cette adresse", str(context.exception))

def test_real_world_integration(self):
"""
Integration Test: Real-world validation using actual DNS and HTTP requests.
Note: Slower and depends on external resources being available.
"""
try:
self.validator("http://example.com") # Known valid domain
except ValidationError:
self.fail("StrictURLValidator raised ValidationError for a valid real-world URL.")

with self.assertRaises(ValidationError) as context:
self.validator("http://invalid-domain-123456789.com")
self.assertIn("Le site web associé à cette adresse n'existe pas", str(context.exception))

@patch("requests.get")
def test_localhost_url(self, mock_requests_get):
"""
Test: Localhost URL to verify compatibility with development or internal environments.
Expected: No exception raised.
"""
mock_requests_get.return_value.status_code = 200

try:
self.validator("http://localhost")
except ValidationError:
self.fail("StrictURLValidator raised ValidationError for a valid localhost URL.")

def test_missing_scheme(self):
"""
Test: URL without a scheme (e.g., 'example.com').
Expected: Automatically prepend 'https://' and validate successfully.
"""
with patch("socket.gethostbyname") as mock_socket_gethostbyname, patch("requests.get") as mock_requests_get:
mock_socket_gethostbyname.return_value = "127.0.0.1"
mock_requests_get.return_value.status_code = 200

try:
self.validator("example.com") # Should be treated as https://example.com
except ValidationError:
self.fail("StrictURLValidator raised ValidationError for a valid URL without scheme.")

0 comments on commit cc702a1

Please sign in to comment.