Source code for django_webapps_fullstack.test.test_django_server
"""Test if the Django server is running and properly configured with SSL settings"""importsslimportunittestimportrequestsfromrequests.exceptionsimportSSLError# Import base test case for automatic cleanupfromdjango_webapps_fullstack.test.base_test_caseimportReadOnlyDjangoTestCase
[docs]classtest_django_server(ReadOnlyDjangoTestCase):""" Class for the test cases to test if the Django Server is running including SSL tests """
[docs]defsetUp(self):""" Set up test - this test doesn't need Selenium, only the URL from config. Override parent setUp to skip Selenium WebDriver creation. """# Get URL from configuration (inherited from base class setUpClass)self.url=self.config_global.get('settings_django_server','url')
[docs]deftearDown(self):""" Clean up test - no Selenium driver to quit. """pass
[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}")