question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ChannelsLiveServerTestCase session cookie creation error

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:23 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
andrewgodwincommented, Jun 13, 2018

Fixed it, it was ALLOWED_HOSTS not being set.

0reactions
krademcommented, Jun 13, 2018

Great job, thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error when loading cookies into a Python request session
You can create a session. The session class handles cookies between requests. s = requests.Session() ...
Read more >
Session Cookie Error - Wiley Online Library
Question: Why am I receiving a Session Cookie Error? Answer: This error occurs when we are unable to send a cookie to your...
Read more >
An invalid domain [.xxx.xxxxxl] was specified for this cookie
Exception occured during SESSION cookie creation. ... the SP side after IDP authentication, the browser gets error 500 and
Read more >
IT20994: DATAPOWER DOES NOT SET A PROPER ... - IBM
In a failover scenario the HTTP client is left with a DPJSESSIONID cookie of the failed server even though requests now flow to...
Read more >
Invalid session cookie - Oracle Communities
SessionBean or creating a new one with ... I did this to cope with invalid session cookie error that seemed to be because...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found