简体   繁体   English

在django中测试csrf令牌

[英]Testing csrf token in django

I'm looking to test if csrf tokens are working in my django site. 我想测试csrf令牌是否在我的django站点中工作。 The issue is that csrf_token returns a token value rather that the custom value of 'csrftoken'. 问题是csrf_token返回一个标记值,而不是自定义值'csrftoken'。 Is there a way to set the value of the csrf for testing? 有没有办法设置csrf的值进行测试? 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)

Is there a particular reason you're testing something that Django's own tests already cover in a fuller way? 有没有特别的原因你正在测试Django自己的测试已经覆盖的更全面的东西?

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? 或者换句话说,您使用CSRF令牌进行特定/非标准的操作是否需要对其进行测试?

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 如果您只是按照文档使用它,节省一些时间并将精力用于测试您自己的代码,而不是Django的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM