I have troubles setting cookie in Django test.
class Test_views(TestCase):
def test_set_cookie(self):
session = self.client.session
session['mycookie'] = 'testcookie'
session.save()
response = self.c.get(reverse('homepage'))
...
I print the cookies in the Views to be sure:
views.py
...
def homepage(request):
print(request.session.keys())
...
And indeed, the cookie mycookie
doesn't exist.
Is it a bug?
class TestDataMixin:
@classmethod
def setUpTestData(cls):
cls.u1 = User.objects.create_user(username='testclient', password='password')
cls.staff = User.objects.create_user(username='staff', password='password', is_staff=True)
class SessionTests(TestDataMixin, TestCase):
def test_session_initiale(self): # NOT WORKING
session = self.client.session
session['session_var'] = 'foo'
session.save()
response = self.client.get('/check_session/')
self.assertEqual(response.content, b'foo')
self.assertEqual(response.content, b'foo')
AssertionError: b'NO' != b'foo'
I was using the wrong SESSION_ENGINE in settings:
SESSION_ENGINE = 'django.contrib.sessions.backends.signed_cookies'
I forgot that I've done that...