Last active
June 28, 2019 07:29
-
-
Save pakal/04ad1343550ecd357dab to your computer and use it in GitHub Desktop.
Shows non-ajax-friendly 302 responses in many workflows, for django-allauth module app.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This file demonstrates writing tests using the unittest module. These will pass | |
when you run "manage.py test". | |
Replace this with more appropriate tests for your application. | |
""" | |
from django.test import TestCase | |
import json | |
AJAX_HEADERS = dict(HTTP_X_REQUESTED_WITH='XMLHttpRequest') | |
class SimpleTest(TestCase): | |
@staticmethod | |
def _to_json(resp): | |
return json.loads(resp.content.decode('utf8')) | |
def test_allauth_ajax_workflow(self): | |
# signup error case # | |
resp = self.client.post('/accounts/signup/', | |
dict(email="test@testage.fr", | |
password1="mypwd"), | |
**AJAX_HEADERS) | |
self.assertEqual(resp.status_code, 400) | |
data = self._to_json(resp) | |
assert "must be a minimum of" in data["form_errors"]["password1"][0] | |
# signup success case # | |
resp = self.client.post('/accounts/signup/', | |
dict(email="test@testage.fr", | |
password1="mylongpwd"), | |
**AJAX_HEADERS) | |
self.assertEqual(resp.status_code, 200) | |
data = self._to_json(resp) | |
assert data["location"] # wanted redirect after success | |
# already signed in, can't sign up # | |
resp = self.client.post('/accounts/signup/', | |
dict(email="test@testage.fr", | |
password1="mylongpwd"), | |
**AJAX_HEADERS) | |
self.assertEqual(resp.status_code, 302) # ALREADY SIGNED IN | |
assert "/profile" in data["location"] | |
# already signed in, can't login # | |
resp = self.client.post('/accounts/login/', | |
dict(login="test@testage.fr", | |
password="mylongpwd"), | |
**AJAX_HEADERS) | |
self.assertEqual(resp.status_code, 302) # ALREADY SIGNED IN | |
assert "/profile" in data["location"] | |
# we log out # | |
resp = self.client.post('/accounts/logout/', | |
**AJAX_HEADERS) | |
self.assertEqual(resp.status_code, 302) # SUCCESS... | |
# we log out again # | |
resp = self.client.post('/accounts/logout/', | |
**AJAX_HEADERS) | |
self.assertEqual(resp.status_code, 302) # SUCCESS again... | |
# login error # | |
resp = self.client.post('/accounts/login/', | |
dict(login="test@testage.fr", | |
password="badpwd"), | |
**AJAX_HEADERS) | |
self.assertEqual(resp.status_code, 400) # ERROR | |
data = self._to_json(resp) | |
assert "not correct" in data["form_errors"]["__all__"][0] | |
# now we can log in for real # | |
resp = self.client.post('/accounts/login/', | |
dict(login="test@testage.fr", | |
password="mylongpwd"), | |
**AJAX_HEADERS) | |
self.assertEqual(resp.status_code, 200) # SUCCESS | |
data = self._to_json(resp) | |
assert "/profile" in data["location"] | |
###print(resp.items(), "\n", resp.content) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment