-
Martin Burchell authored
Now if a user attempts to log in from a login page with a stale CSRF token, they will be redirected to the dashboard or, if they have logged out, to a fresh login page.
Martin Burchell authoredNow if a user attempts to log in from a login page with a stale CSRF token, they will be redirected to the dashboard or, if they have logged out, to a fresh login page.
login_tests.py 1.16 KiB
from __future__ import unicode_literals, absolute_import
import pytest
from django.core.urlresolvers import reverse
from django.test import Client
from django.utils.six.moves.urllib.parse import urlsplit
from users.models import User
@pytest.mark.django_db
def test_user_directed_to_login_page_when_csrf_error():
username = 'william'
password = 'passw0rd'
User.objects.create_user(username, 'william@example.com', password)
client = Client(enforce_csrf_checks=True)
data = {'username': username,
'password': password,
'csrfmiddlewaretoken': 'notavalidtoken'}
response = client.post(reverse('login'),
data=data, follow=True)
assert hasattr(response, 'redirect_chain')
assert len(response.redirect_chain) > 0, "Response didn't redirect"
assert response.redirect_chain[0][1] == 302
url, _ = response.redirect_chain[-1]
scheme, netloc, path, query, fragment = urlsplit(url)
assert path == reverse('login')
url, _ = response.redirect_chain[-2]
scheme, netloc, path, query, fragment = urlsplit(url)
assert path == reverse('dashboard')
assert response.status_code == 200