Debian Codegraph @

tests/apps/headless/account/test_phone.py

raw
1 from http import HTTPStatus
2
3 from django.contrib.auth import get_user_model
4
5 import pytest
6
7 from allauth.account.adapter import get_adapter as get_account_adapter
8
9
10 def test_change_phone_to_same(
11 auth_client, user_with_phone, phone, settings_impacting_urls, headless_reverse
12 ):
13 with settings_impacting_urls(
14 ACCOUNT_SIGNUP_FIELDS=["phone*"],
15 ACCOUNT_LOGIN_METHODS=("phone",),
16 ):
17 resp = auth_client.post(
18 headless_reverse("headless:account:manage_phone"),
19 data={
20 "phone": phone,
21 },
22 content_type="application/json",
23 )
24 assert resp.status_code == HTTPStatus.BAD_REQUEST
25 assert resp.json() == {
26 "errors": [
27 {
28 "code": "same_as_current",
29 "message": "The new value must be different from the current one.",
30 "param": "phone",
31 }
32 ],
33 "status": HTTPStatus.BAD_REQUEST,
34 }
35
36
37 def test_change_phone(
38 auth_client,
39 user_with_phone,
40 phone,
41 settings_impacting_urls,
42 headless_reverse,
43 phone_factory,
44 sms_outbox,
45 ):
46 with settings_impacting_urls(
47 ACCOUNT_SIGNUP_FIELDS=["phone*"],
48 ACCOUNT_LOGIN_METHODS=("phone",),
49 ):
50 new_phone = phone_factory()
51 resp = auth_client.post(
52 headless_reverse("headless:account:manage_phone"),
53 data={
54 "phone": new_phone,
55 },
56 content_type="application/json",
57 )
58 assert resp.status_code == HTTPStatus.ACCEPTED
59 assert resp.json() == {
60 "data": [
61 {
62 "phone": new_phone,
63 "verified": False,
64 },
65 ],
66 "status": HTTPStatus.ACCEPTED,
67 }
68
69 resp = auth_client.post(
70 headless_reverse("headless:account:verify_phone"),
71 data={
72 "code": sms_outbox[-1]["code"],
73 },
74 content_type="application/json",
75 )
76 assert resp.status_code == HTTPStatus.OK
77
78 resp = auth_client.post(
79 headless_reverse("headless:account:verify_phone"),
80 data={
81 "code": sms_outbox[-1]["code"],
82 },
83 content_type="application/json",
84 )
85 assert resp.status_code == HTTPStatus.CONFLICT
86
87 resp = auth_client.get(
88 headless_reverse("headless:account:manage_phone"),
89 content_type="application/json",
90 )
91 assert resp.json() == {
92 "data": [
93 {
94 "phone": new_phone,
95 "verified": True,
96 },
97 ],
98 "status": HTTPStatus.OK,
99 }
100
101
102 def test_login(
103 client,
104 user_with_phone,
105 phone,
106 user_password,
107 headless_reverse,
108 settings_impacting_urls,
109 ):
110 with settings_impacting_urls(
111 ACCOUNT_SIGNUP_FIELDS=["phone*", "password1*"],
112 ACCOUNT_LOGIN_METHODS=("phone",),
113 ):
114 resp = client.post(
115 headless_reverse("headless:account:login"),
116 data={"phone": phone, "password": user_password},
117 content_type="application/json",
118 )
119 assert resp.status_code == HTTPStatus.OK
120
121
122 def test_login_by_code(
123 client,
124 user_with_phone,
125 phone,
126 headless_reverse,
127 settings_impacting_urls,
128 sms_outbox,
129 ):
130 with settings_impacting_urls(
131 ACCOUNT_SIGNUP_FIELDS=["phone*"],
132 ACCOUNT_LOGIN_METHODS=("phone",),
133 ):
134 resp = client.post(
135 headless_reverse("headless:account:request_login_code"),
136 data={
137 "phone": phone,
138 },
139 content_type="application/json",
140 )
141 assert resp.status_code == HTTPStatus.UNAUTHORIZED
142 code = sms_outbox[-1]["code"]
143 resp = client.post(
144 headless_reverse("headless:account:confirm_login_code"),
145 data={"code": code},
146 content_type="application/json",
147 )
148 assert resp.status_code == HTTPStatus.OK
149 data = resp.json()
150 assert data["meta"]["is_authenticated"]
151
152
153 def test_signup(
154 db,
155 client,
156 settings_impacting_urls,
157 phone,
158 headless_reverse,
159 headless_client,
160 password_factory,
161 sms_outbox,
162 ):
163 with settings_impacting_urls(
164 ACCOUNT_SIGNUP_FIELDS=["phone*", "password1*"],
165 ACCOUNT_LOGIN_METHODS=("phone",),
166 ):
167 resp = client.post(
168 headless_reverse("headless:account:signup"),
169 data={
170 "phone": phone,
171 "password": password_factory(),
172 },
173 content_type="application/json",
174 )
175 assert resp.status_code == HTTPStatus.UNAUTHORIZED
176 pending_flow = [
177 flow for flow in resp.json()["data"]["flows"] if flow.get("is_pending")
178 ][0]
179 assert pending_flow["id"] == "verify_phone"
180 assert len(sms_outbox) == 1
181 code = sms_outbox[-1]["code"]
182 resp = client.post(
183 headless_reverse("headless:account:verify_phone"),
184 data={
185 "code": code,
186 },
187 content_type="application/json",
188 )
189 assert resp.json()["status"] == HTTPStatus.OK
190
191
192 def test_reauthentication(
193 auth_client,
194 user_with_phone,
195 phone,
196 settings_impacting_urls,
197 headless_reverse,
198 phone_factory,
199 sms_outbox,
200 ):
201 with settings_impacting_urls(
202 ACCOUNT_REAUTHENTICATION_REQUIRED=True,
203 ACCOUNT_SIGNUP_FIELDS=["phone*"],
204 ACCOUNT_LOGIN_METHODS=("phone",),
205 ):
206 new_phone = phone_factory()
207 resp = auth_client.post(
208 headless_reverse("headless:account:manage_phone"),
209 data={
210 "phone": new_phone,
211 },
212 content_type="application/json",
213 )
214 assert resp.status_code == HTTPStatus.UNAUTHORIZED
215 assert resp.json()["data"]["flows"] == [{"id": "reauthenticate"}]
216
217
218 def test_change_phone_to_conflicting(
219 auth_client,
220 user_with_phone,
221 phone,
222 settings_impacting_urls,
223 headless_reverse,
224 user_factory,
225 phone_factory,
226 sms_outbox,
227 ):
228 other_phone = phone_factory()
229 user_factory(phone=other_phone)
230 with settings_impacting_urls(
231 ACCOUNT_SIGNUP_FIELDS=["phone*"],
232 ACCOUNT_LOGIN_METHODS=("phone",),
233 ):
234 resp = auth_client.post(
235 headless_reverse("headless:account:manage_phone"),
236 data={
237 "phone": other_phone,
238 },
239 content_type="application/json",
240 )
241 assert resp.status_code == HTTPStatus.ACCEPTED
242 assert resp.json() == {
243 "data": [{"phone": other_phone, "verified": False}],
244 "status": HTTPStatus.ACCEPTED,
245 }
246 resp = auth_client.post(
247 headless_reverse("headless:account:verify_phone"),
248 data={
249 "code": sms_outbox[-1]["code"],
250 },
251 content_type="application/json",
252 )
253 assert resp.status_code == HTTPStatus.BAD_REQUEST
254 assert resp.json() == {
255 "errors": [
256 {
257 "code": "phone_taken",
258 "message": "A user is already registered with this phone number.",
259 "param": "code",
260 }
261 ],
262 "status": HTTPStatus.BAD_REQUEST,
263 }
264
265
266 def test_change_at_signup(
267 db,
268 client,
269 settings_impacting_urls,
270 phone,
271 headless_reverse,
272 headless_client,
273 password_factory,
274 sms_outbox,
275 phone_factory,
276 ):
277 with settings_impacting_urls(
278 ACCOUNT_SIGNUP_FIELDS=["phone*", "password1*"],
279 ACCOUNT_LOGIN_METHODS=("phone",),
280 ACCOUNT_PHONE_VERIFICATION_SUPPORTS_CHANGE=True,
281 ):
282 resp = client.post(
283 headless_reverse("headless:account:signup"),
284 data={
285 "phone": phone,
286 "password": password_factory(),
287 },
288 content_type="application/json",
289 )
290 user = get_user_model().objects.last()
291 assert resp.status_code == HTTPStatus.UNAUTHORIZED
292 assert len(sms_outbox) == 1
293
294 new_phone = phone_factory()
295 resp = client.post(
296 headless_reverse("headless:account:manage_phone"),
297 data={
298 "phone": new_phone,
299 },
300 content_type="application/json",
301 )
302 assert resp.status_code == HTTPStatus.ACCEPTED
303 assert resp.json() == {
304 "data": [
305 {
306 "phone": new_phone,
307 "verified": False,
308 },
309 ],
310 "status": HTTPStatus.ACCEPTED,
311 }
312
313 assert len(sms_outbox) == 2
314 code = sms_outbox[-1]["code"]
315 resp = client.post(
316 headless_reverse("headless:account:verify_phone"),
317 data={
318 "code": code,
319 },
320 content_type="application/json",
321 )
322 assert resp.json()["status"] == HTTPStatus.OK
323 assert get_account_adapter().get_phone(user) == (new_phone, True)
324
325
326 @pytest.mark.parametrize("rate_limit_enabled", [(False,), (True,)])
327 def test_resend_at_signup(
328 db,
329 client,
330 settings_impacting_urls,
331 phone,
332 headless_reverse,
333 headless_client,
334 password_factory,
335 sms_outbox,
336 phone_factory,
337 rate_limit_enabled,
338 request,
339 ):
340 if rate_limit_enabled:
341 request.getfixturevalue("enable_cache")
342 with settings_impacting_urls(
343 ACCOUNT_SIGNUP_FIELDS=["phone*", "password1*"],
344 ACCOUNT_LOGIN_METHODS=("phone",),
345 ACCOUNT_PHONE_VERIFICATION_SUPPORTS_RESEND=True,
346 ):
347 resp = client.post(
348 headless_reverse("headless:account:signup"),
349 data={
350 "phone": phone,
351 "password": password_factory(),
352 },
353 content_type="application/json",
354 )
355 assert resp.status_code == HTTPStatus.UNAUTHORIZED
356 assert len(sms_outbox) == 1
357
358 resp = client.post(
359 headless_reverse("headless:account:resend_phone_verification_code"),
360 )
361 if rate_limit_enabled:
362 assert resp.status_code == HTTPStatus.TOO_MANY_REQUESTS
363 else:
364 assert resp.status_code == HTTPStatus.OK
365 assert len(sms_outbox) == 2
366
367 old_code = sms_outbox[0]["code"]
368 new_code = sms_outbox[1]["code"]
369 assert old_code != new_code
370
371
372 def test_manage_phone_anon_no_500(
373 client, db, settings_impacting_urls, headless_reverse
374 ):
375 with settings_impacting_urls(
376 ACCOUNT_SIGNUP_FIELDS=["phone*"],
377 ACCOUNT_LOGIN_METHODS=("phone",),
378 ):
379 resp = client.get(
380 headless_reverse("headless:account:manage_phone"),
381 )
382 assert resp.status_code == HTTPStatus.UNAUTHORIZED