| 1 |
import json |
| 2 |
from http import HTTPStatus |
| 3 |
|
| 4 |
from django.conf import settings |
| 5 |
from django.urls import reverse |
| 6 |
|
| 7 |
import pytest |
| 8 |
from pytest_django.asserts import assertRedirects |
| 9 |
|
| 10 |
from allauth.account import app_settings |
| 11 |
|
| 12 |
|
| 13 |
@pytest.mark.parametrize( |
| 14 |
"headers,ajax_expected", |
| 15 |
[ |
| 16 |
({}, False), |
| 17 |
({"HTTP_X_REQUESTED_WITH": "XMLHttpRequest"}, True), |
| 18 |
({"HTTP_ACCEPT": "application/json"}, True), |
| 19 |
], |
| 20 |
) |
| 21 |
def test_ajax_headers(db, client, headers, ajax_expected): |
| 22 |
resp = client.post( |
| 23 |
reverse("account_signup"), |
| 24 |
{ |
| 25 |
"username": "johndoe", |
| 26 |
"email": "[email protected]", |
| 27 |
"email2": "[email protected]", |
| 28 |
"password1": "johndoe", |
| 29 |
"password2": "johndoe", |
| 30 |
}, |
| 31 |
**headers, |
| 32 |
) |
| 33 |
if ajax_expected: |
| 34 |
assert resp.status_code == HTTPStatus.OK |
| 35 |
assert resp.json()["location"] == settings.LOGIN_REDIRECT_URL |
| 36 |
assert resp.json()["location"] == settings.LOGIN_REDIRECT_URL |
| 37 |
else: |
| 38 |
assert resp.status_code == HTTPStatus.FOUND |
| 39 |
assertRedirects( |
| 40 |
resp, settings.LOGIN_REDIRECT_URL, fetch_redirect_response=False |
| 41 |
) |
| 42 |
|
| 43 |
|
| 44 |
def test_ajax_password_reset(client, user, mailoutbox): |
| 45 |
resp = client.post( |
| 46 |
reverse("account_reset_password"), |
| 47 |
data={"email": user.email}, |
| 48 |
HTTP_X_REQUESTED_WITH="XMLHttpRequest", |
| 49 |
) |
| 50 |
assert len(mailoutbox) == 1 |
| 51 |
assert mailoutbox[0].to == [user.email] |
| 52 |
assert resp["content-type"] == "application/json" |
| 53 |
|
| 54 |
|
| 55 |
def test_ajax_login_fail(client, db): |
| 56 |
resp = client.post( |
| 57 |
reverse("account_login"), |
| 58 |
{}, |
| 59 |
HTTP_X_REQUESTED_WITH="XMLHttpRequest", |
| 60 |
) |
| 61 |
assert resp.status_code == HTTPStatus.BAD_REQUEST |
| 62 |
json.loads(resp.content.decode("utf8")) |
| 63 |
# TODO: Actually test something |
| 64 |
|
| 65 |
|
| 66 |
def test_ajax_login_success(settings, user, user_password, client): |
| 67 |
settings.ACCOUNT_EMAIL_VERIFICATION = app_settings.EmailVerificationMethod.OPTIONAL |
| 68 |
resp = client.post( |
| 69 |
reverse("account_login"), |
| 70 |
{"login": user.username, "password": user_password}, |
| 71 |
HTTP_X_REQUESTED_WITH="XMLHttpRequest", |
| 72 |
) |
| 73 |
assert resp.status_code == HTTPStatus.OK |
| 74 |
data = json.loads(resp.content.decode("utf8")) |
| 75 |
assert data["location"] == "/accounts/profile/" |