| 1 |
import copy |
| 2 |
from http import HTTPStatus |
| 3 |
from unittest.mock import ANY |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
|
| 8 |
@pytest.mark.parametrize( |
| 9 |
"has_password,request_data,response_data,status_code", |
| 10 |
[ |
| 11 |
# Wrong current password |
| 12 |
( |
| 13 |
True, |
| 14 |
{"current_password": "wrong", "new_password": "{password_factory}"}, |
| 15 |
{ |
| 16 |
"status": HTTPStatus.BAD_REQUEST, |
| 17 |
"errors": [ |
| 18 |
{ |
| 19 |
"param": "current_password", |
| 20 |
"message": "Please type your current password.", |
| 21 |
"code": "enter_current_password", |
| 22 |
} |
| 23 |
], |
| 24 |
}, |
| 25 |
HTTPStatus.BAD_REQUEST, |
| 26 |
), |
| 27 |
# Happy flow, regular password change |
| 28 |
( |
| 29 |
True, |
| 30 |
{ |
| 31 |
"current_password": "{user_password}", |
| 32 |
"new_password": "{password_factory}", |
| 33 |
}, |
| 34 |
{ |
| 35 |
"status": HTTPStatus.OK, |
| 36 |
"meta": {"is_authenticated": True}, |
| 37 |
"data": { |
| 38 |
"user": ANY, |
| 39 |
"methods": [], |
| 40 |
}, |
| 41 |
}, |
| 42 |
HTTPStatus.OK, |
| 43 |
), |
| 44 |
# New password does not match constraints |
| 45 |
( |
| 46 |
True, |
| 47 |
{ |
| 48 |
"current_password": "{user_password}", |
| 49 |
"new_password": "a", |
| 50 |
}, |
| 51 |
{ |
| 52 |
"status": HTTPStatus.BAD_REQUEST, |
| 53 |
"errors": [ |
| 54 |
{ |
| 55 |
"param": "new_password", |
| 56 |
"code": "password_too_short", |
| 57 |
"message": "This password is too short. It must contain at least 6 characters.", |
| 58 |
} |
| 59 |
], |
| 60 |
}, |
| 61 |
HTTPStatus.BAD_REQUEST, |
| 62 |
), |
| 63 |
# New password not empty |
| 64 |
( |
| 65 |
True, |
| 66 |
{ |
| 67 |
"current_password": "{user_password}", |
| 68 |
"new_password": "", |
| 69 |
}, |
| 70 |
{ |
| 71 |
"status": HTTPStatus.BAD_REQUEST, |
| 72 |
"errors": [ |
| 73 |
{ |
| 74 |
"param": "new_password", |
| 75 |
"code": "required", |
| 76 |
"message": "This field is required.", |
| 77 |
} |
| 78 |
], |
| 79 |
}, |
| 80 |
HTTPStatus.BAD_REQUEST, |
| 81 |
), |
| 82 |
# Current password not blank |
| 83 |
( |
| 84 |
True, |
| 85 |
{ |
| 86 |
"current_password": "", |
| 87 |
"new_password": "{password_factory}", |
| 88 |
}, |
| 89 |
{ |
| 90 |
"status": HTTPStatus.BAD_REQUEST, |
| 91 |
"errors": [ |
| 92 |
{ |
| 93 |
"param": "current_password", |
| 94 |
"message": "This field is required.", |
| 95 |
"code": "required", |
| 96 |
} |
| 97 |
], |
| 98 |
}, |
| 99 |
HTTPStatus.BAD_REQUEST, |
| 100 |
), |
| 101 |
# Current password missing |
| 102 |
( |
| 103 |
True, |
| 104 |
{ |
| 105 |
"new_password": "{password_factory}", |
| 106 |
}, |
| 107 |
{ |
| 108 |
"status": HTTPStatus.BAD_REQUEST, |
| 109 |
"errors": [ |
| 110 |
{ |
| 111 |
"param": "current_password", |
| 112 |
"message": "This field is required.", |
| 113 |
"code": "required", |
| 114 |
} |
| 115 |
], |
| 116 |
}, |
| 117 |
HTTPStatus.BAD_REQUEST, |
| 118 |
), |
| 119 |
# Current password not set, happy flow |
| 120 |
( |
| 121 |
False, |
| 122 |
{ |
| 123 |
"current_password": "", |
| 124 |
"new_password": "{password_factory}", |
| 125 |
}, |
| 126 |
{ |
| 127 |
"status": HTTPStatus.OK, |
| 128 |
"meta": {"is_authenticated": True}, |
| 129 |
"data": { |
| 130 |
"user": ANY, |
| 131 |
"methods": [], |
| 132 |
}, |
| 133 |
}, |
| 134 |
HTTPStatus.OK, |
| 135 |
), |
| 136 |
# Current password not set, current_password absent |
| 137 |
( |
| 138 |
False, |
| 139 |
{ |
| 140 |
"new_password": "{password_factory}", |
| 141 |
}, |
| 142 |
{ |
| 143 |
"status": HTTPStatus.OK, |
| 144 |
"meta": {"is_authenticated": True}, |
| 145 |
"data": { |
| 146 |
"user": ANY, |
| 147 |
"methods": [], |
| 148 |
}, |
| 149 |
}, |
| 150 |
HTTPStatus.OK, |
| 151 |
), |
| 152 |
], |
| 153 |
) |
| 154 |
def test_change_password( |
| 155 |
auth_client, |
| 156 |
user, |
| 157 |
request_data, |
| 158 |
response_data, |
| 159 |
status_code, |
| 160 |
has_password, |
| 161 |
user_password, |
| 162 |
password_factory, |
| 163 |
settings, |
| 164 |
mailoutbox, |
| 165 |
headless_reverse, |
| 166 |
headless_client, |
| 167 |
): |
| 168 |
request_data = copy.deepcopy(request_data) |
| 169 |
response_data = copy.deepcopy(response_data) |
| 170 |
settings.ACCOUNT_EMAIL_NOTIFICATIONS = True |
| 171 |
if not has_password: |
| 172 |
user.set_unusable_password() |
| 173 |
user.save(update_fields=["password"]) |
| 174 |
auth_client.force_login(user) |
| 175 |
if request_data.get("current_password") == "{user_password}": |
| 176 |
request_data["current_password"] = user_password |
| 177 |
if request_data.get("new_password") == "{password_factory}": |
| 178 |
request_data["new_password"] = password_factory() |
| 179 |
resp = auth_client.post( |
| 180 |
headless_reverse("headless:account:change_password"), |
| 181 |
data=request_data, |
| 182 |
content_type="application/json", |
| 183 |
) |
| 184 |
assert resp.status_code == status_code |
| 185 |
resp_json = resp.json() |
| 186 |
if headless_client == "app" and resp.status_code == HTTPStatus.OK: |
| 187 |
response_data["meta"]["session_token"] = ANY |
| 188 |
assert resp_json == response_data |
| 189 |
user.refresh_from_db() |
| 190 |
if resp.status_code == HTTPStatus.OK: |
| 191 |
assert user.check_password(request_data["new_password"]) |
| 192 |
assert len(mailoutbox) == 1 |
| 193 |
else: |
| 194 |
assert user.check_password(user_password) |
| 195 |
assert len(mailoutbox) == 0 |
| 196 |
|
| 197 |
|
| 198 |
def test_change_password_rate_limit( |
| 199 |
enable_cache, |
| 200 |
auth_client, |
| 201 |
user, |
| 202 |
user_password, |
| 203 |
password_factory, |
| 204 |
settings, |
| 205 |
headless_reverse, |
| 206 |
): |
| 207 |
settings.ACCOUNT_RATE_LIMITS = {"change_password": "1/m/ip"} |
| 208 |
for attempt in range(2): |
| 209 |
new_password = password_factory() |
| 210 |
resp = auth_client.post( |
| 211 |
headless_reverse("headless:account:change_password"), |
| 212 |
data={ |
| 213 |
"current_password": user_password, |
| 214 |
"new_password": new_password, |
| 215 |
}, |
| 216 |
content_type="application/json", |
| 217 |
) |
| 218 |
user_password = new_password |
| 219 |
expected_status = ( |
| 220 |
HTTPStatus.OK if attempt == 0 else HTTPStatus.TOO_MANY_REQUESTS |
| 221 |
) |
| 222 |
assert resp.status_code == expected_status |
| 223 |
assert resp.json()["status"] == expected_status |