| 1 |
from http import HTTPStatus |
| 2 |
|
| 3 |
from django.urls import reverse |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
|
| 8 |
def test_password_reset_flow( |
| 9 |
client, user, mailoutbox, password_factory, settings, headless_reverse |
| 10 |
): |
| 11 |
settings.ACCOUNT_EMAIL_NOTIFICATIONS = True |
| 12 |
|
| 13 |
resp = client.post( |
| 14 |
headless_reverse("headless:account:request_password_reset"), |
| 15 |
data={ |
| 16 |
"email": user.email, |
| 17 |
}, |
| 18 |
content_type="application/json", |
| 19 |
) |
| 20 |
assert resp.status_code == HTTPStatus.OK |
| 21 |
assert len(mailoutbox) == 1 |
| 22 |
body = mailoutbox[0].body |
| 23 |
# Extract URL for `password_reset_from_key` view |
| 24 |
url = body[body.find("/password/reset/") :].split()[0] |
| 25 |
key = url.split("/")[-2] |
| 26 |
password = password_factory() |
| 27 |
|
| 28 |
# Too simple password |
| 29 |
resp = client.post( |
| 30 |
headless_reverse("headless:account:reset_password"), |
| 31 |
data={ |
| 32 |
"key": key, |
| 33 |
"password": "a", |
| 34 |
}, |
| 35 |
content_type="application/json", |
| 36 |
) |
| 37 |
assert resp.status_code == HTTPStatus.BAD_REQUEST |
| 38 |
assert resp.json() == { |
| 39 |
"status": HTTPStatus.BAD_REQUEST, |
| 40 |
"errors": [ |
| 41 |
{ |
| 42 |
"code": "password_too_short", |
| 43 |
"message": "This password is too short. It must contain at least 6 characters.", |
| 44 |
"param": "password", |
| 45 |
} |
| 46 |
], |
| 47 |
} |
| 48 |
|
| 49 |
assert len(mailoutbox) == 1 |
| 50 |
|
| 51 |
# Success |
| 52 |
resp = client.post( |
| 53 |
headless_reverse("headless:account:reset_password"), |
| 54 |
data={ |
| 55 |
"key": key, |
| 56 |
"password": password, |
| 57 |
}, |
| 58 |
content_type="application/json", |
| 59 |
) |
| 60 |
assert resp.status_code == HTTPStatus.UNAUTHORIZED |
| 61 |
|
| 62 |
user.refresh_from_db() |
| 63 |
assert user.check_password(password) |
| 64 |
assert len(mailoutbox) == 2 # The security notification |
| 65 |
|
| 66 |
|
| 67 |
@pytest.mark.parametrize("method", ["get", "post"]) |
| 68 |
def test_password_reset_flow_wrong_key( |
| 69 |
client, password_factory, headless_reverse, method |
| 70 |
): |
| 71 |
password = password_factory() |
| 72 |
|
| 73 |
if method == "get": |
| 74 |
resp = client.get( |
| 75 |
headless_reverse("headless:account:reset_password"), |
| 76 |
HTTP_X_PASSWORD_RESET_KEY="wrong", |
| 77 |
) |
| 78 |
else: |
| 79 |
resp = client.post( |
| 80 |
headless_reverse("headless:account:reset_password"), |
| 81 |
data={ |
| 82 |
"key": "wrong", |
| 83 |
"password": password, |
| 84 |
}, |
| 85 |
content_type="application/json", |
| 86 |
) |
| 87 |
assert resp.status_code == HTTPStatus.BAD_REQUEST |
| 88 |
assert resp.json() == { |
| 89 |
"status": HTTPStatus.BAD_REQUEST, |
| 90 |
"errors": [ |
| 91 |
{ |
| 92 |
"param": "key", |
| 93 |
"code": "invalid_password_reset", |
| 94 |
"message": "The password reset token was invalid.", |
| 95 |
} |
| 96 |
], |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
def test_password_reset_flow_unknown_user( |
| 101 |
client, db, mailoutbox, password_factory, settings, headless_reverse |
| 102 |
): |
| 103 |
resp = client.post( |
| 104 |
headless_reverse("headless:account:request_password_reset"), |
| 105 |
data={ |
| 106 |
"email": "[email protected]", |
| 107 |
}, |
| 108 |
content_type="application/json", |
| 109 |
) |
| 110 |
assert resp.status_code == HTTPStatus.OK |
| 111 |
assert len(mailoutbox) == 1 |
| 112 |
body = mailoutbox[0].body |
| 113 |
if getattr(settings, "HEADLESS_ONLY", False): |
| 114 |
assert settings.HEADLESS_FRONTEND_URLS["account_signup"] in body |
| 115 |
else: |
| 116 |
assert reverse("account_signup") in body |
| 117 |
|
| 118 |
|
| 119 |
def test_reset_password_rate_limit( |
| 120 |
auth_client, user, headless_reverse, settings, enable_cache |
| 121 |
): |
| 122 |
settings.ACCOUNT_RATE_LIMITS = {"reset_password": "1/m/ip"} |
| 123 |
for attempt in range(2): |
| 124 |
resp = auth_client.post( |
| 125 |
headless_reverse("headless:account:request_password_reset"), |
| 126 |
data={"email": user.email}, |
| 127 |
content_type="application/json", |
| 128 |
) |
| 129 |
expected_status = ( |
| 130 |
HTTPStatus.OK if attempt == 0 else HTTPStatus.TOO_MANY_REQUESTS |
| 131 |
) |
| 132 |
assert resp.status_code == expected_status |
| 133 |
assert resp.json()["status"] == expected_status |
| 134 |
|
| 135 |
|
| 136 |
def test_password_reset_key_rate_limit( |
| 137 |
client, |
| 138 |
user, |
| 139 |
settings, |
| 140 |
headless_reverse, |
| 141 |
password_reset_key_generator, |
| 142 |
enable_cache, |
| 143 |
): |
| 144 |
settings.ACCOUNT_RATE_LIMITS = {"reset_password_from_key": "1/m/ip"} |
| 145 |
for attempt in range(2): |
| 146 |
resp = client.post( |
| 147 |
headless_reverse("headless:account:reset_password"), |
| 148 |
data={ |
| 149 |
"key": password_reset_key_generator(user), |
| 150 |
"password": "a", # too short |
| 151 |
}, |
| 152 |
content_type="application/json", |
| 153 |
) |
| 154 |
expected_status = ( |
| 155 |
HTTPStatus.TOO_MANY_REQUESTS if attempt else HTTPStatus.BAD_REQUEST |
| 156 |
) |
| 157 |
assert resp.status_code == expected_status |
| 158 |
assert resp.json()["status"] == expected_status |