django-webtest / django-webtest

django-webtest provides integration of Ian Bicking's WebTest (http://docs.pylonsproject.org/projects/webtest/) with django's testing framework.
https://pypi.python.org/pypi/django-webtest/
MIT License
329 stars 68 forks source link

Sessions key missing in subsequent user's request #130

Open je-bugshell opened 9 months ago

je-bugshell commented 9 months ago

django-allauth just introduced a userssessions app. They added a check for a request.session.session_key in their model manager which assumes that session_key exists. But writing a simple test like this breaks it. (the second self.app.get() raises a ValueError() because the session_key is missing):

def test_basic_two_requests_fail_because_second_has_no_session_key(self):
    test_user = MyUser.objects.create(username='test_user')
    second_user = MyUser.objects.create(username='second_user')
    self.app.get('/', user=test_user)
    self.app.get('/', user=second_user)

To be clear, this issue is not specific to django-allauth, but was discovered due to this new feature.

This issue can replicated by adding django-allauth's new usersessions app, include 'django.contrib.sessions', in the INSTALLED_APPS and 'django.contrib.sessions.middleware.SessionMiddleware', and 'allauth.usersessions.middleware.UserSessionsMiddleware' to the middlewares.

gawel commented 9 months ago

Hi,

django-webtest is supposed to handle sessions just fine. see https://github.com/django-webtest/django-webtest/blob/6370c1afe034da416b03b2f88b7c71b9a49122c6/django_webtest_tests/testapp_tests/tests.py#L466

provide at least your INSTALLED_APPS / MIDDLEWARE. The session stuff need to be before the allauth stuff

also provide a traceback. even if it's simple for you to reproduce the problem, I'm not sure I'll have time for that.

Even better, add a test in a PR to reproduce the problem, if possible

Thanks

je-bugshell commented 9 months ago

I've opened a PR https://github.com/django-webtest/django-webtest/pull/131 that demonstrates this bug.