channels: ChannelsLiveServerTestCase session cookie creation error
My functional tests use following code snippets to log user in:
def create_session_cookie(self, username, password):
# First, create a new test user
User = get_user_model()
user = User.objects.create_user(username=username, password=password)
# Then create the authenticated session using the new user credentials
session = SessionStore()
session[SESSION_KEY] = user.pk
session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0]
session[HASH_SESSION_KEY] = user.get_session_auth_hash()
session.save()
# Finally, create the cookie dictionary
cookie = {
'name': settings.SESSION_COOKIE_NAME,
'value': session.session_key,
'secure': False,
'path': '/',
}
return cookie
def create_cookie_and_go_to_page(self, email):
session_cookie = self.create_session_cookie(
username=email, password='top_secret'
)
# visit some url in your domain to setup Selenium.
# (404 pages load the quickest)
self.browser.get(self.server_url + '/404.html')
# add the newly created session cookie to selenium webdriver.
self.browser.add_cookie(session_cookie)
# refresh to exchange cookies with the server.
self.browser.refresh()
# This time user should present as logged in.
self.browser.get(self.server_url)
and I use it as:
class DjangoTest(StaticLiveServerTestCase):
def test_django(self):
self.create_cookie_and_go_to_page('email@example.com')
Also I used to test my channels pages (installed with pip install git+git://github.com/django/channels.git@master#egg=channels
) like this:
class ChannelsTest(ChannelsLiveServerTestCase):
def test_channels(self):
self.create_cookie_and_go_to_page('email@example.com')
, but with ChannelsLiveServerTestCase
something has gone wrong in recent commits to master. It simply shows Bad Request (400) page after self.browser.get(self.server_url + '/404.html')
call. If I revert to channels==2.0.2 from PyPI then it goes well, but css and js are broken with that version. I tried to locate error on a specific commit or dependency version, but without success.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 23 (11 by maintainers)
Fixed it, it was
ALLOWED_HOSTS
not being set.