title: Test CSRF Verification with Django Rest Framework
body: <p>I'm using Django Rest Framework 3 and would like to test the CSRF verification.</p>
  
  <p>First, I initialize the DRF <code>APIClient</code>:</p>
  
  <pre><code>client = APIClient(enforce_csrf_checks=True)
  </code></pre>
  
  <p>Then I set a password on a user so I can login and get a session:</p>
  
  <pre><code>superuser.set_password('1234')
  superuser.save()
  client.login(email=superuser.email, password='1234')
  </code></pre>
  
  <p>Now we need a CSRF token. For that I simply create a request and retrieve the token from the cookies.</p>
  
  <pre><code>response = client.request()
  csrftoken = client.cookies['csrftoken'].value
  </code></pre>
  
  <p>When inspecting the code, this seems to work, I get back a valid looking CSRF token. I then do the POST request, passing in the <code>csrfmiddlewartoken</code> parameter:</p>
  
  <pre><code>data = {'name': 'My fancy test report', 'csrfmiddlewaretoken': csrftoken}
  response = client.post(API_BASE + '/reports', data=data, format='json')
  assert response.status_code == status.HTTP_201_CREATED, response.content
  </code></pre>
  
  <p>The problem is, this fails:</p>
  
  <pre><code>tests/api/test_api.py:156: in test_csrf_success
      assert response.status_code == status.HTTP_201_CREATED, response.content
  E   AssertionError: {"detail":"CSRF Failed: CSRF token missing or incorrect."}
  E   assert 403 == 201
  E    +  where 403 = &lt;rest_framework.response.Response object at 0x7f7bd6453bd0&gt;.status_code
  E    +  and   201 = status.HTTP_201_CREATED
  </code></pre>
  
  <p>What's the correct way to test CSRF verification with DRF?</p>