0

This is part of my views.py

views,py

.
.
def get(self, request, *args, **kwargs):
    if request.session['from_product'] = True:
        blah blah
.
.

For passing this if statement, I tried to store session data in test code like this:

class OrderViewFromProductWithLoginTest(OrderSetupTestCase):

    def setUp(self):
        self.login()
        self.client.session['from_product'] = True
        self.client.session.save()

But it doesn't work.

This is how I verified:

from django.contrib.sessions.models import Session
session = Session.objects.first()
sessoin.get_decode()
>> {'_auth_user_backend': 'django.contrib.auth.backends.ModelBackend',
 '_auth_user_hash': 'ffd87a41ff1e77abcbd78724f4b5b5133c67a08f',
 '_auth_user_id': '1'}

How can I implement this?

user3595632
  • 5,380
  • 10
  • 55
  • 111
  • Look at this answer. That guy solved it pretty nice. [How do I modify the session in the Django test framework](http://stackoverflow.com/a/7722483/2886270) – Adilet Maratov Oct 26 '16 at 04:01
  • @AdiletMaratov This is for older versions of Django. However OP didn't mention used Django version, but I'm pretty sure it's > 1.7. – arogachev Oct 26 '16 at 04:06
  • I think your problem is that you did not assign it to separate variable. See [official docs with example](https://docs.djangoproject.com/en/dev/topics/testing/tools/#django.test.Client.session). – arogachev Oct 26 '16 at 04:07

0 Answers0