Search code examples
pythondjangocookiesdjango-rest-frameworkdjango-testing

Django get_signed_cookie on manually signed cookie


I'm using get_signed_cookie in one of my views, and it works. The problem is in testing it, i'm manually signing my cookie in my test using Django's django.core.signing module with the same salt as my actual code (i'm using a salt that's in my settings).

When i do non-automated tests, all is well. I've compared the cookie that is set in my test with the cookie that is set by Django itself and they're the same structure:

In my test (I'm manually signing the string 'test_cookie_value' in my test):

{'my_cookie': 'test_cookie_value:s5SA5skGmc4As-Weyia6Tlwq_V8'}

Django:

{'my_cookie': 'zTmwe4ATUEtEEAVs:1YRl0a:Lg3KHfpL93HoUijkZlNphZNURu4'}

But when my view tries to get_signed_cookie("my_cookie", False, salt="my_salt"), False is returned.

I think this is strange, and while i'm trying to obey the testing goat (first time TDD'er here) i'm tempted to skip testing this..

I hope someone can give me some guidance, i'm stuck ignoring the goat noises in the background for now.


Solution

  • I have found my solution. It was rather straightforward actually.

    I tried to set my cookie on my request for the test, which made sense since the cookie should be sent in the request. A better way of testing it is to just GET a resource first (which sets te cookie, which would happen in a normal web-situation as well) and then POST with the set cookie. This worked just fine.