4
votes

I'm looking to test if csrf tokens are working in my django site. The issue is that csrf_token returns a token value rather that the custom value of 'csrftoken'. Is there a way to set the value of the csrf for testing? This is the code that I am working with:

token = 'csrftoken'
client = Client(enforce_csrf_checks=True)
client.login(username='user', password='pass')
client.get("/my/web/page/")
csrf_token = client.cookies[token].value
assetEqual(token, csrf_token)
1

1 Answers

4
votes

Is there a particular reason you're testing something that Django's own tests already cover in a fuller way?

Or, put another way, is there something specific/non-standard that you're doing with the CSRF token that means you need to test it?

If you're just using it as per the docs, save yourself some time and put the effort into testing your own code, not Django's