Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static files not served in StaticLiveServerTestCase #204

Closed
pupeno opened this issue Nov 15, 2018 · 3 comments
Closed

Static files not served in StaticLiveServerTestCase #204

pupeno opened this issue Nov 15, 2018 · 3 comments

Comments

@pupeno
Copy link

pupeno commented Nov 15, 2018

First, I'm aware of issue #66 but the problem I'm experiencing doesn't get fixed by running collectstatic.

I'm doing all of these experiments on a minimal Django 2.1.3 project with a fresh app, running on Python 3.7 and using whitenoise 4.1.1:

Django==2.1.3
psycopg2==2.7.6.1
pytz==2018.7
selenium==3.141.0
urllib3==1.24.1
whitenoise==4.1.1

The starting settings are:

DEBUG=True
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")

and at that point the tests look good. The test itself looks like this:

from django.contrib.auth.models import User
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.keys import Keys


class TestImportCRMData(StaticLiveServerTestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.webdriver = webdriver.Chrome()
        cls.webdriver.implicitly_wait(10)

    @classmethod
    def tearDownClass(cls):
        cls.webdriver.close()
        cls.webdriver.quit()
        super().tearDownClass()

    def setUp(self):
        self.admin = User.objects.create_superuser(username="username", password="password",
                                                   email="example@example.com")

    def test_navigate_to_import_crm_data(self):
        self.webdriver.get(f"{self.live_server_url}/admin")
        self.webdriver.find_element_by_id("id_username").send_keys("username")
        self.webdriver.find_element_by_id("id_password").send_keys("password")
        self.webdriver.find_element_by_id("id_password").send_keys(Keys.RETURN)
        self.webdriver.find_element_by_link_text("Users").click()

which should run in any application with the stock Django admin. When I add the app:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    # ...
]

and:

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

and I run the test, I get

Server Error (500)

on the browser and

UserWarning: No directory at: C:\Users\pupeno\Temporary\untitled\static\
  warnings.warn(u'No directory at: {}'.format(root))

on the terminal. Even with DEBUG set to True. If I run collectstatic , then the test run, but all the static file URL return a 404.

I tried setting DEBUG to False and the behavior was exactly the same.

@evansd
Copy link
Owner

evansd commented Nov 15, 2018

If you just use LiveServerTestCase instead of StaticLiveServerTestCase it should all work.

@pupeno
Copy link
Author

pupeno commented Nov 16, 2018

That works and makes sense. Would you like me to add this information to the docs?

@evansd
Copy link
Owner

evansd commented Nov 16, 2018

Great, glad to hear it! Thanks for the offer, but I need to do some work on the docs anyway so I should just add a note on this myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants