My site allows individuals to contribute content in the absence of being logged in by creating a User based on the current session_key
I would like to setup a test for my view, but it seems that it is not possible to modify the request.session:
I'd like to do this:
from django.contrib.sessions.models import Session
s = Session()
s.expire_date = '2010-12-05'
s.session_key = 'my_session_key'
s.save()
self.client.session = s
response = self.client.get('/myview/')
But I get the error:
AttributeError: can't set attribute
Thoughts on how to modify the client session before making get requests? I have seen this and it doesn't seem to work
You can modify the session with a middleware.
The client object of the django testing framework makes possible to touch the session. Look at http://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#django.test.client.Client.session for details
Be careful :
To modify the session and then save it, it must be stored in a variable first (because a new SessionStore is created every time this property is accessed)
I think something like this below should work
as per the docs you can add values to the session in the test client via eg
https://docs.djangoproject.com/en/dev/topics/testing/tools/#django.test.Client.session
This will let you test views that require data in the session to function correctly.
So for this question:
This is how I did it (inspired by a solution in http://blog.mediaonfire.com/?p=36).
After that, you may create your tests as:
etc...
You can create a custom view which inserts dummy data such as the session.
The view with the corresponding url: /dummy/:
Than in test script just call
self.client.get('/dummy/')
I also use this dummy view to initilize the dummy data in the session when testing by hand.
As Andrew Austin already mentioned, it doesn't work because of this bug: https://code.djangoproject.com/ticket/11475
What you can do though is this:
After creating and logging in a user with User.objects.create_user() and self.client.login(), as in the code above, sessions should work.