emulating an existing session in tests

27 views
Skip to first unread message

omat

unread,
Aug 20, 2007, 5:13:36 AM8/20/07
to Django users
There is an error that occurs occasionally and I suspect that it may
be user specific (i.e. for users with certain credentials).

I need to run tests in the user's context, thus, I need to emulate the
existing session of the user to test this.

I have the session id of the user that faced the error from the
traceback that was mailed to me.

I have tried modifying the cookies and session attributes of the test
client instance but I couldn't succeed.

How can I achieve this?

Thanks,
oMat

Malcolm Tredinnick

unread,
Aug 20, 2007, 5:19:17 AM8/20/07
to django...@googlegroups.com

The session id is just a key used to look up the session data from the
Sessions table. The data is stored in that table in pickled form (see
contrib.sessions for the details). So you don't just need the session
id. You also would need to pull out the appropriate session data and
store that in the test database that is created. Part of your test setup
would need to be putting data into the sessions table.

Regards,
Malcolm

--
Two wrongs are only the beginning.
http://www.pointy-stick.com/blog/

omat

unread,
Aug 20, 2007, 5:57:39 AM8/20/07
to Django users
Thanks for the reply...

I used the existing database instead of the test database to hook to
the user's session like this:

from django.test.client import Client
from django.contrib.sessions.models import Session

client = Client()
client.session = Session.objects.get(session_key = key_from_traceback)


but did not work.

Will it work, if I build a cookie back from the session and initialize
the Client() with it?

On 20 Ağustos, 12:19, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:

Malcolm Tredinnick

unread,
Aug 20, 2007, 6:14:44 AM8/20/07
to django...@googlegroups.com
On Mon, 2007-08-20 at 09:57 +0000, omat wrote:
> Thanks for the reply...
>
> I used the existing database instead of the test database to hook to
> the user's session like this:
>
> from django.test.client import Client
> from django.contrib.sessions.models import Session
>
> client = Client()
> client.session = Session.objects.get(session_key = key_from_traceback)
>
>
> but did not work.

If you're using the test framework, it doesn't talk to the production
database. It creates it's own testing database (read the testing docs
for more information). So your above query is talking to an empty table
unless you have populated it yourself.

Regards,
Malcolm

--
No one is listening until you make a mistake.
http://www.pointy-stick.com/blog/

omat

unread,
Aug 20, 2007, 6:52:16 AM8/20/07
to Django users
I am using the api via shell (i.e.: python manage.py shell) as
follows:

>>> from django.contrib.sessions.middleware import SessionWrapper
>>> from django.test.client import Client
>>> c = Client()
>>> s = SessionWrapper('8a3dd9235b8e718c3f7e890106180528')
>>> c.session = s._session
>>> c.session
{'_auth_user_id': 840, '_auth_user_backend':
'django.contrib.auth.backends.ModelBackend'}
>>> c.get('/messages/')
<django.http.HttpResponseForbidden object at 0xb775ffec>

The last line says the request is not considered as being form an
authenticated user because HttpResponseForbidden is returned when an
anonymous user tries to access /messages/.

I am supplying the data in the session, so I think it must be ok. Am I
thinking wrong?


Thanks,
oMat

On 20 Ağustos, 13:14, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:

omat

unread,
Aug 20, 2007, 7:47:41 AM8/20/07
to Django users
If I am not missing something, it looks like I have solved the
problem.

This is hack:

>>> from django.http import SimpleCookie


>>> from django.test.client import Client
>>> c = Client()

>>> cookies = SimpleCookie()
>>> cookies['sessionid'] = '8a3dd9235b8e718c3f7e890106180528'
>>> c.cookies = cookies
>>> c.get('/messages/')
<django.http.HttpResponse object at 0xb77aeb0c>


Thanks...

Reply all
Reply to author
Forward
0 new messages