summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2021-02-11 05:43:36 -0800
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-02-12 11:53:37 +0100
commit65a620948c38c6ac6c7d93e79f70590331bcb2d0 (patch)
treed64e1607c68e07f8448d86793ee393a62962d868
parentbfc429d993a728ea2c644c45da991947b72b5354 (diff)
downloaddjango-65a620948c38c6ac6c7d93e79f70590331bcb2d0.tar.gz
[3.2.x] Fixed #32437 -- Fixed cleaning up ALLOWED_HOSTS in LiveServerTestCase on setUpClass() failure.
Backport of 694deff82f86e025561dfa724425f67e2ff7cbb7 from master
-rw-r--r--django/test/testcases.py5
-rw-r--r--tests/servers/tests.py37
2 files changed, 40 insertions, 2 deletions
diff --git a/django/test/testcases.py b/django/test/testcases.py
index 114ca85012..47c3377151 100644
--- a/django/test/testcases.py
+++ b/django/test/testcases.py
@@ -1567,11 +1567,12 @@ class LiveServerTestCase(TransactionTestCase):
for conn in cls.server_thread.connections_override.values():
conn.dec_thread_sharing()
+ cls._live_server_modified_settings.disable()
+ super().tearDownClass()
+
@classmethod
def tearDownClass(cls):
cls._tearDownClassInternal()
- cls._live_server_modified_settings.disable()
- super().tearDownClass()
class SerializeMixin:
diff --git a/tests/servers/tests.py b/tests/servers/tests.py
index a586b82aeb..16abc440fb 100644
--- a/tests/servers/tests.py
+++ b/tests/servers/tests.py
@@ -9,6 +9,7 @@ from urllib.error import HTTPError
from urllib.parse import urlencode
from urllib.request import urlopen
+from django.conf import settings
from django.core.servers.basehttp import WSGIServer
from django.test import LiveServerTestCase, override_settings
from django.test.testcases import LiveServerThread, QuietWSGIRequestHandler
@@ -39,6 +40,42 @@ class LiveServerBase(LiveServerTestCase):
return urlopen(self.live_server_url + url)
+class FailingLiveServerThread(LiveServerThread):
+ def _create_server(self):
+ raise RuntimeError('Error creating server.')
+
+
+class LiveServerTestCaseSetupTest(LiveServerBase):
+ server_thread_class = FailingLiveServerThread
+
+ @classmethod
+ def check_allowed_hosts(cls, expected):
+ if settings.ALLOWED_HOSTS != expected:
+ raise RuntimeError(f'{settings.ALLOWED_HOSTS} != {expected}')
+
+ @classmethod
+ def setUpClass(cls):
+ cls.check_allowed_hosts(['testserver'])
+ try:
+ super().setUpClass()
+ except RuntimeError:
+ # LiveServerTestCase's change to ALLOWED_HOSTS should be reverted.
+ cls.check_allowed_hosts(['testserver'])
+ else:
+ raise RuntimeError('Server did not fail.')
+ cls.set_up_called = True
+
+ @classmethod
+ def tearDownClass(cls):
+ # Make tearDownClass() a no-op because setUpClass() was already cleaned
+ # up, and because the error inside setUpClass() was handled, which will
+ # cause tearDownClass() to be called when it normally wouldn't.
+ pass
+
+ def test_set_up_class(self):
+ self.assertIs(self.set_up_called, True)
+
+
class LiveServerAddress(LiveServerBase):
@classmethod