From e909ceae9b3e72b72e0a2baaa92bba9714f18cd2 Mon Sep 17 00:00:00 2001 From: Ramiro Morales Date: Sat, 1 Jun 2013 14:24:46 -0300 Subject: Made django.test.testcases not depend on staticfiles contrib app. Do this by introducing a django.contrib.staticfiles.testing.StaticLiveServerCase unittest TestCase subclass. Fixes #20739. --- tests/staticfiles_tests/test_liveserver.py | 101 +++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 tests/staticfiles_tests/test_liveserver.py (limited to 'tests/staticfiles_tests/test_liveserver.py') diff --git a/tests/staticfiles_tests/test_liveserver.py b/tests/staticfiles_tests/test_liveserver.py new file mode 100644 index 0000000000..80333aae1f --- /dev/null +++ b/tests/staticfiles_tests/test_liveserver.py @@ -0,0 +1,101 @@ +""" +A subset of the tests in tests/servers/tests exercicing +django.contrib.staticfiles.testing.StaticLiveServerCase instead of +django.test.LiveServerTestCase. +""" + +import os +try: + from urllib.request import urlopen +except ImportError: # Python 2 + from urllib2 import urlopen + +from django.core.exceptions import ImproperlyConfigured +from django.test.utils import override_settings +from django.utils._os import upath + +from django.contrib.staticfiles.testing import StaticLiveServerCase + + +TEST_ROOT = os.path.dirname(upath(__file__)) +TEST_SETTINGS = { + 'MEDIA_URL': '/media/', + 'STATIC_URL': '/static/', + 'MEDIA_ROOT': os.path.join(TEST_ROOT, 'project', 'site_media', 'media'), + 'STATIC_ROOT': os.path.join(TEST_ROOT, 'project', 'site_media', 'static'), +} + + +class LiveServerBase(StaticLiveServerCase): + + available_apps = [] + + @classmethod + def setUpClass(cls): + # Override settings + cls.settings_override = override_settings(**TEST_SETTINGS) + cls.settings_override.enable() + super(LiveServerBase, cls).setUpClass() + + @classmethod + def tearDownClass(cls): + # Restore original settings + cls.settings_override.disable() + super(LiveServerBase, cls).tearDownClass() + + +class StaticLiveServerChecks(LiveServerBase): + + @classmethod + def setUpClass(cls): + # Backup original environment variable + address_predefined = 'DJANGO_LIVE_TEST_SERVER_ADDRESS' in os.environ + old_address = os.environ.get('DJANGO_LIVE_TEST_SERVER_ADDRESS') + + # If contrib.staticfiles isn't configured properly, the exception + # should bubble up to the main thread. + old_STATIC_URL = TEST_SETTINGS['STATIC_URL'] + TEST_SETTINGS['STATIC_URL'] = None + cls.raises_exception('localhost:8081', ImproperlyConfigured) + TEST_SETTINGS['STATIC_URL'] = old_STATIC_URL + + # Restore original environment variable + if address_predefined: + os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = old_address + else: + del os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] + + @classmethod + def tearDownClass(cls): + # skip it, as setUpClass doesn't call its parent either + pass + + @classmethod + def raises_exception(cls, address, exception): + os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = address + try: + super(StaticLiveServerChecks, cls).setUpClass() + raise Exception("The line above should have raised an exception") + except exception: + pass + finally: + super(StaticLiveServerChecks, cls).tearDownClass() + + def test_test_test(self): + # Intentionally empty method so that the test is picked up by the + # test runner and the overridden setUpClass() method is executed. + pass + + +class StaticLiveServerView(LiveServerBase): + + def urlopen(self, url): + return urlopen(self.live_server_url + url) + + def test_collectstatic_emulation(self): + """ + Test that StaticLiveServerCase use of staticfiles' serve() allows it to + discover app's static assets without having to collectstatic first. + """ + f = self.urlopen('/static/test/file.txt') + self.assertEqual(f.read().rstrip(b'\r\n'), b'In app media directory.') -- cgit v1.3