Opened 13 years ago

Closed 13 years ago

#16106 closed Bug (duplicate)

Test client session not set for unauthenticated requests

Reported by: Alexandru Plugaru Owned by: nobody
Component: Testing framework Version: 1.3
Severity: Normal Keywords: testclient
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In https://docs.djangoproject.com/en/1.3/topics/testing/#django.test.client.Client.session it says that I can do something like this in my tests:

def test_something(self):
    session = self.client.session
    session['somekey'] = 'test'
    session.save()

However if the client is not logged-in, self.client.session returns a dict which will result in an AttributeError.

Change History (2)

comment:1 by Alexandru Plugaru, 13 years ago

Summary: Test client session for unauthenticated usersTest client session not set for unauthenticated requests

I found a workaround for this bug. Just access a view that sets the session for you. For example:

self.client.get('/admin/')
session = self.client.session
session['some_var'] = 'value'
session.save()

...
do stuff

comment:2 by Preston Timmons, 13 years ago

Resolution: duplicate
Status: newclosed

Thanks for the report. This issue is documented in #15740 and #10899, which has a patch to fix this. Closing this one as a duplicate.

Note: See TracTickets for help on using tickets.
Back to Top