| 1 |
from http import HTTPStatus |
| 2 |
|
| 3 |
|
| 4 |
def test_reauthenticate( |
| 5 |
auth_client, user, user_password, headless_reverse, headless_client |
| 6 |
): |
| 7 |
resp = auth_client.get( |
| 8 |
headless_reverse("headless:account:current_session"), |
| 9 |
content_type="application/json", |
| 10 |
) |
| 11 |
assert resp.status_code == HTTPStatus.OK |
| 12 |
data = resp.json() |
| 13 |
method_count = len(data["data"]["methods"]) |
| 14 |
|
| 15 |
resp = auth_client.post( |
| 16 |
headless_reverse("headless:account:reauthenticate"), |
| 17 |
data={ |
| 18 |
"password": user_password, |
| 19 |
}, |
| 20 |
content_type="application/json", |
| 21 |
) |
| 22 |
assert resp.status_code == HTTPStatus.OK |
| 23 |
|
| 24 |
resp = auth_client.get( |
| 25 |
headless_reverse("headless:account:current_session"), |
| 26 |
content_type="application/json", |
| 27 |
) |
| 28 |
assert resp.status_code == HTTPStatus.OK |
| 29 |
data = resp.json() |
| 30 |
assert len(data["data"]["methods"]) == method_count + 1 |
| 31 |
last_method = data["data"]["methods"][-1] |
| 32 |
assert last_method["method"] == "password" |
| 33 |
|
| 34 |
|
| 35 |
def test_reauthenticate_rate_limit( |
| 36 |
auth_client, |
| 37 |
user, |
| 38 |
user_password, |
| 39 |
headless_reverse, |
| 40 |
headless_client, |
| 41 |
settings, |
| 42 |
enable_cache, |
| 43 |
): |
| 44 |
settings.ACCOUNT_RATE_LIMITS = {"reauthenticate": "1/m/ip"} |
| 45 |
for attempt in range(4): |
| 46 |
resp = auth_client.post( |
| 47 |
headless_reverse("headless:account:reauthenticate"), |
| 48 |
data={ |
| 49 |
"password": user_password, |
| 50 |
}, |
| 51 |
content_type="application/json", |
| 52 |
) |
| 53 |
expected_status = HTTPStatus.TOO_MANY_REQUESTS if attempt else HTTPStatus.OK |
| 54 |
assert resp.status_code == expected_status |
| 55 |
assert resp.json()["status"] == expected_status |