Source code for django_landing_simple.test.test_django_server_landing_simple

"""
Test if the Django server is running and properly configured with SSL settings
"""

import unittest
import requests
from requests.exceptions import SSLError

# Import base test case for automatic config loading
from django_landing_simple.test.base_test_case import ConfigTestCase

[docs] class test_django_server_landing_simple(ConfigTestCase): """ Class for the test cases to test if the Django Server is running including SSL tests """
[docs] def setUp(self): # Get the url of the Django server to be tested from config self.url = self.config_global.get('settings_django_server', 'url')
[docs] def test_server_is_running(self): """ Checks if the Django server is running by making a GET request to the specified URL. It expects a 200 status code in response, which indicates the server is running correctly. """ try: response = requests.get(self.url, timeout=5) self.assertEqual(response.status_code, 200, f"Expected status code 200, got {response.status_code}") except requests.exceptions.ConnectionError as e: self.fail(f"Connection error: {e}") except requests.exceptions.Timeout as e: self.fail(f"Timeout error: {e}")
[docs] def test_ssl_configuration(self): """ Attempts to connect to the server using HTTPS to ensure SSL is properly configured. If there's an SSL error, the test will fail. """ try: response = requests.get(self.url, timeout=5) self.assertEqual(response.status_code, 200, f"Expected status code 200, got {response.status_code}") except SSLError as e: self.fail(f"SSL error: {e}") except requests.exceptions.ConnectionError as e: self.fail(f"Connection error: {e}") except requests.exceptions.Timeout as e: self.fail(f"Timeout error: {e}")
[docs] def test_ssl_eof_error(self): """ Test for SSL EOFError: EOF occurred in violation of protocol (_ssl.c:2427). """ try: response = requests.get(self.url, timeout=5) self.assertEqual(response.status_code, 200, f"Expected status code 200, got {response.status_code}") except SSLError as e: if isinstance(e, ssl.SSLEOFError): self.fail(f"SSLEOFError occurred: {e}") else: self.fail(f"SSL error occurred: {e}")
[docs] def test_ssl_certificate_verification(self): """ Ensures that SSL certificate verification works correctly. If the certificate is invalid or untrusted, an SSLError will be raised. """ try: response = requests.get(self.url, verify=True, timeout=5) self.assertEqual(response.status_code, 200, f"Expected status code 200, got {response.status_code}") except SSLError as e: self.fail(f"SSL verification error: {e}")
if __name__ == "__main__": unittest.main()