Debian Codegraph @

tests/apps/headless/account/test_reset_password_by_code.py

raw
1 from http import HTTPStatus
2 from unittest.mock import ANY
3
4 import pytest
5
6 from allauth.account import app_settings
7 from allauth.account.internal.flows.login import AUTHENTICATION_METHODS_SESSION_KEY
8 from allauth.account.models import EmailAddress
9
10
11 @pytest.fixture(autouse=True)
12 def prbc_settings(settings_impacting_urls):
13 with settings_impacting_urls(ACCOUNT_PASSWORD_RESET_BY_CODE_ENABLED=True):
14 yield
15
16
17 def test_validating_codes_by_get_is_limited(client, user, headless_reverse):
18 resp = client.post(
19 headless_reverse("headless:account:request_password_reset"),
20 data={
21 "email": user.email,
22 },
23 content_type="application/json",
24 )
25 assert resp.status_code == HTTPStatus.UNAUTHORIZED
26 for i in range(app_settings.PASSWORD_RESET_BY_CODE_MAX_ATTEMPTS + 1):
27 resp = client.get(
28 headless_reverse("headless:account:reset_password"),
29 HTTP_X_PASSWORD_RESET_KEY=f"attempt{i}",
30 )
31 if i < app_settings.PASSWORD_RESET_BY_CODE_MAX_ATTEMPTS:
32 expected_status = HTTPStatus.BAD_REQUEST
33 else:
34 expected_status = HTTPStatus.CONFLICT
35 assert resp.status_code == expected_status
36
37
38 def test_validating_codes_by_get_limitation_carriers_over_to_post(
39 client, user, headless_reverse
40 ):
41 resp = client.post(
42 headless_reverse("headless:account:request_password_reset"),
43 data={
44 "email": user.email,
45 },
46 content_type="application/json",
47 )
48 assert resp.status_code == HTTPStatus.UNAUTHORIZED
49 for i in range(app_settings.PASSWORD_RESET_BY_CODE_MAX_ATTEMPTS):
50 resp = client.get(
51 headless_reverse("headless:account:reset_password"),
52 HTTP_X_PASSWORD_RESET_KEY=f"attempt{i}",
53 )
54 assert resp.status_code == HTTPStatus.BAD_REQUEST
55 resp = client.post(
56 headless_reverse("headless:account:reset_password"),
57 data={
58 "key": "wrong",
59 "password": "a",
60 },
61 content_type="application/json",
62 )
63 assert resp.status_code == HTTPStatus.CONFLICT
64
65
66 @pytest.mark.parametrize("login_on_password_reset", [False, True])
67 def test_password_reset_flow(
68 client,
69 user,
70 mailoutbox,
71 password_factory,
72 settings,
73 headless_reverse,
74 get_last_password_reset_code,
75 login_on_password_reset,
76 ):
77 settings.ACCOUNT_EMAIL_NOTIFICATIONS = True
78 settings.ACCOUNT_LOGIN_ON_PASSWORD_RESET = login_on_password_reset
79
80 resp = client.post(
81 headless_reverse("headless:account:request_password_reset"),
82 data={
83 "email": user.email,
84 },
85 content_type="application/json",
86 )
87 assert resp.status_code == HTTPStatus.UNAUTHORIZED
88 code = get_last_password_reset_code(client, mailoutbox)
89 password = password_factory()
90
91 # Get token info
92 resp = client.get(
93 headless_reverse("headless:account:reset_password"),
94 content_type="application/json",
95 HTTP_X_PASSWORD_RESET_KEY=code,
96 )
97 assert resp.status_code == HTTPStatus.OK
98
99 # Too simple password
100 resp = client.post(
101 headless_reverse("headless:account:reset_password"),
102 data={
103 "key": code,
104 "password": "a",
105 },
106 content_type="application/json",
107 )
108 assert resp.status_code == HTTPStatus.BAD_REQUEST
109 assert resp.json() == {
110 "status": HTTPStatus.BAD_REQUEST,
111 "errors": [
112 {
113 "code": "password_too_short",
114 "message": "This password is too short. It must contain at least 6 characters.",
115 "param": "password",
116 }
117 ],
118 }
119
120 assert len(mailoutbox) == 1
121
122 # Success
123 resp = client.post(
124 headless_reverse("headless:account:reset_password"),
125 data={
126 "key": code,
127 "password": password,
128 },
129 content_type="application/json",
130 )
131 assert (
132 resp.status_code == HTTPStatus.OK
133 if login_on_password_reset
134 else HTTPStatus.UNAUTHORIZED
135 )
136 if login_on_password_reset:
137 assert client.headless_session()[AUTHENTICATION_METHODS_SESSION_KEY] == [
138 {"method": "password_reset", "at": ANY, "email": user.email}
139 ]
140 else:
141 assert AUTHENTICATION_METHODS_SESSION_KEY not in client.headless_session()
142
143 user.refresh_from_db()
144 assert user.check_password(password)
145 assert len(mailoutbox) == 2 # The security notification
146
147 # Try once more, shouldn't be allowed.
148 resp = client.post(
149 headless_reverse("headless:account:reset_password"),
150 data={
151 "key": code,
152 "password": password,
153 },
154 content_type="application/json",
155 )
156 assert resp.status_code == HTTPStatus.CONFLICT
157
158
159 def test_indirect_email_verification_on_get(
160 db, client, mailoutbox, get_last_password_reset_code, user_factory, headless_reverse
161 ):
162 user = user_factory(email_verified=False)
163 address = EmailAddress.objects.get(user=user, email=user.email, verified=False)
164 resp = client.post(
165 headless_reverse("headless:account:request_password_reset"),
166 data={
167 "email": address.email,
168 },
169 content_type="application/json",
170 )
171 assert resp.status_code == HTTPStatus.UNAUTHORIZED
172 resp = client.get(
173 headless_reverse("headless:account:reset_password"),
174 HTTP_X_PASSWORD_RESET_KEY=get_last_password_reset_code(client, mailoutbox),
175 )
176 assert resp.status_code == HTTPStatus.OK
177 address.refresh_from_db()
178 assert address.verified
179
180
181 def test_indirect_email_verification_on_post(
182 db,
183 client,
184 mailoutbox,
185 get_last_password_reset_code,
186 user_factory,
187 headless_reverse,
188 password_factory,
189 ):
190 user = user_factory(email_verified=False)
191 address = EmailAddress.objects.get(user=user, email=user.email, verified=False)
192 resp = client.post(
193 headless_reverse("headless:account:request_password_reset"),
194 data={
195 "email": address.email,
196 },
197 content_type="application/json",
198 )
199 assert resp.status_code == HTTPStatus.UNAUTHORIZED
200 resp = client.post(
201 headless_reverse("headless:account:reset_password"),
202 {
203 "key": get_last_password_reset_code(client, mailoutbox),
204 "password": password_factory(),
205 },
206 content_type="application/json",
207 )
208 assert resp.status_code == HTTPStatus.UNAUTHORIZED
209 address.refresh_from_db()
210 assert address.verified