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"""importunittestimportrequestsfromrequests.exceptionsimportSSLError# Import base test case for automatic config loadingfromdjango_landing_simple.test.base_test_caseimportConfigTestCase
[docs]classtest_django_server_landing_simple(ConfigTestCase):""" Class for the test cases to test if the Django Server is running including SSL tests """
[docs]defsetUp(self):# Get the url of the Django server to be tested from configself.url=self.config_global.get('settings_django_server','url')
[docs]deftest_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}")exceptrequests.exceptions.ConnectionErrorase:self.fail(f"Connection error: {e}")exceptrequests.exceptions.Timeoutase:self.fail(f"Timeout error: {e}")
[docs]deftest_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}")exceptSSLErrorase:self.fail(f"SSL error: {e}")exceptrequests.exceptions.ConnectionErrorase:self.fail(f"Connection error: {e}")exceptrequests.exceptions.Timeoutase:self.fail(f"Timeout error: {e}")
[docs]deftest_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}")exceptSSLErrorase:ifisinstance(e,ssl.SSLEOFError):self.fail(f"SSLEOFError occurred: {e}")else:self.fail(f"SSL error occurred: {e}")
[docs]deftest_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}")exceptSSLErrorase:self.fail(f"SSL verification error: {e}")