Debian Codegraph @

tests/apps/account/test_phone.py

raw
1 from http import HTTPStatus
2 from unittest.mock import ANY
3
4 from django.conf import settings
5 from django.test import Client
6 from django.urls import reverse
7
8 import pytest
9 from pytest_django.asserts import assertTemplateUsed
10
11 from allauth.account.adapter import get_adapter
12 from allauth.account.authentication import AUTHENTICATION_METHODS_SESSION_KEY
13
14
15 @pytest.fixture
16 def phone_only_settings(settings_impacting_urls):
17 with settings_impacting_urls(
18 ACCOUNT_LOGIN_METHODS=("phone",), ACCOUNT_SIGNUP_FIELDS=["phone*"]
19 ):
20 yield
21
22
23 def test_signup(db, client, phone, sms_outbox, phone_only_settings):
24 assert len(sms_outbox) == 0
25 resp = client.post(reverse("account_signup"), data={"phone": phone})
26 assert resp.status_code == HTTPStatus.FOUND
27 assert len(sms_outbox) == 1
28 assert resp["location"] == reverse("account_verify_phone")
29 resp = client.get(resp["location"])
30 assert resp.status_code == HTTPStatus.OK
31 resp = client.post(
32 reverse("account_verify_phone"), data={"code": sms_outbox[-1]["code"]}
33 )
34 assert resp.status_code == HTTPStatus.FOUND
35 adapter = get_adapter()
36 user = adapter.get_user_by_phone(phone)
37 phone2, phone_verified = adapter.get_phone(user)
38 assert phone_verified
39 assert phone == phone2
40 assert not user.check_password("")
41
42
43 def test_signup_invalid_attempts(db, client, phone, sms_outbox, phone_only_settings):
44 assert len(sms_outbox) == 0
45 resp = client.post(reverse("account_signup"), data={"phone": phone})
46 assert resp.status_code == HTTPStatus.FOUND
47 adapter = get_adapter()
48 user = adapter.get_user_by_phone(phone)
49 _, phone_verified = adapter.get_phone(user)
50 assert not phone_verified
51 assert len(sms_outbox) == 1
52 assert resp["location"] == reverse("account_verify_phone")
53 resp = client.get(resp["location"])
54 assert resp.status_code == HTTPStatus.OK
55 for i in range(3):
56 resp = client.post(reverse("account_verify_phone"), data={"code": "wrong"})
57 assert resp.status_code == (HTTPStatus.OK if i < 2 else HTTPStatus.FOUND)
58
59
60 def test_login_sends_code(
61 user_with_phone, client, phone_only_settings, phone, sms_outbox
62 ):
63 resp = client.post(reverse("account_login"), data={"login": phone})
64 assert resp.status_code == HTTPStatus.FOUND
65 assert resp["location"] == reverse("account_confirm_login_code")
66 assert len(sms_outbox) == 1
67
68
69 def test_login_with_verified_phone_and_password(
70 client, settings_impacting_urls, phone, user_with_phone, user_password
71 ):
72 with settings_impacting_urls(
73 ACCOUNT_SIGNUP_FIELDS=["phone*", "password1*"],
74 ACCOUNT_LOGIN_METHODS=["phone"],
75 ):
76 resp = client.post(
77 reverse("account_login"), data={"login": phone, "password": user_password}
78 )
79 assert resp.status_code == HTTPStatus.FOUND
80 assert resp["location"] == settings.LOGIN_REDIRECT_URL
81
82
83 def test_login_with_unverified_phone_and_password(
84 client, settings_impacting_urls, phone, password_factory, user_factory, sms_outbox
85 ):
86 with settings_impacting_urls(
87 ACCOUNT_SIGNUP_FIELDS=["phone*", "password1*"],
88 ACCOUNT_LOGIN_METHODS=["phone"],
89 ):
90 password = password_factory()
91 user = user_factory(phone=phone, password=password, phone_verified=False)
92 resp = client.post(
93 reverse("account_login"), data={"login": phone, "password": password}
94 )
95 assert resp.status_code == HTTPStatus.FOUND
96 assert resp["location"] == reverse("account_verify_phone")
97 code = sms_outbox[-1]["code"]
98 resp = client.post(reverse("account_verify_phone"), data={"code": code})
99 assert resp["location"] == settings.LOGIN_REDIRECT_URL
100 phone_verified = get_adapter().get_phone(user)
101 assert phone_verified == (phone, True)
102
103
104 def test_change_phone(
105 auth_client, user, phone_only_settings, phone_factory, sms_outbox
106 ):
107 new_phone = phone_factory()
108 resp = auth_client.get(reverse("account_change_phone"))
109 assert resp.status_code == HTTPStatus.OK
110 assertTemplateUsed(resp, "account/phone_change.html")
111
112 resp = auth_client.post(reverse("account_change_phone"), {"phone": new_phone})
113 assert resp.status_code == HTTPStatus.FOUND
114 assert resp["location"] == reverse("account_verify_phone")
115
116 code = sms_outbox[-1]["code"]
117 resp = auth_client.get(reverse("account_verify_phone"))
118 assert resp.status_code == HTTPStatus.OK
119 assertTemplateUsed(resp, "account/confirm_phone_verification_code.html")
120
121 resp = auth_client.post(reverse("account_verify_phone"), {"code": code})
122 assert resp.status_code == HTTPStatus.FOUND
123 assert resp["location"] == reverse("account_change_phone")
124
125 phone_verified = get_adapter().get_phone(user)
126 assert phone_verified == (new_phone, True)
127
128 resp = auth_client.get(reverse("account_verify_phone"))
129 assert resp.status_code == HTTPStatus.FOUND
130 assert resp["location"] == reverse("account_change_phone")
131
132
133 def test_change_phone_to_already_existing(
134 auth_client, user, phone_only_settings, phone_factory, sms_outbox, user_factory
135 ):
136 other_phone = phone_factory()
137 user_factory(phone=other_phone)
138
139 resp = auth_client.post(reverse("account_change_phone"), {"phone": other_phone})
140 assert resp.status_code == HTTPStatus.FOUND
141 assert resp["location"] == reverse("account_verify_phone")
142
143 code = sms_outbox[-1]["code"]
144 resp = auth_client.get(reverse("account_verify_phone"))
145 assert resp.status_code == HTTPStatus.OK
146 assertTemplateUsed(resp, "account/confirm_phone_verification_code.html")
147
148 resp = auth_client.post(reverse("account_verify_phone"), {"code": code})
149 assert resp.status_code == HTTPStatus.OK
150 assert resp.context["form"].errors == {
151 "code": ["A user is already registered with this phone number."]
152 }
153
154
155 def test_login_by_code_enumeration_prevention(
156 db, phone_only_settings, client, phone_factory, sms_outbox
157 ):
158 resp = client.post(
159 reverse("account_request_login_code"), data={"phone": phone_factory()}
160 )
161 assert resp.status_code == HTTPStatus.FOUND
162 assert resp["location"] == reverse("account_confirm_login_code")
163 assert "code" not in sms_outbox[-1]
164 assert "user_id" not in sms_outbox[-1]
165
166
167 def test_reauthentication(
168 settings,
169 auth_client,
170 user_with_phone,
171 phone_factory,
172 settings_impacting_urls,
173 user_password,
174 ):
175 with settings_impacting_urls(
176 ACCOUNT_REAUTHENTICATION_REQUIRED=True,
177 ACCOUNT_LOGIN_METHODS=("phone",),
178 ACCOUNT_SIGNUP_FIELDS=["phone*", "password1*"],
179 ):
180 new_phone = phone_factory()
181 resp = auth_client.post(
182 reverse("account_change_phone"),
183 {"phone": new_phone},
184 )
185 assert resp["location"].startswith(reverse("account_reauthenticate"))
186
187 resp = auth_client.get(reverse("account_reauthenticate"))
188 assertTemplateUsed(resp, "account/reauthenticate.html")
189 resp = auth_client.post(
190 reverse("account_reauthenticate"), data={"password": user_password}
191 )
192 assert resp.status_code == HTTPStatus.FOUND
193
194 methods = auth_client.session[AUTHENTICATION_METHODS_SESSION_KEY]
195 assert methods[-1] == {"method": "password", "at": ANY, "reauthenticated": True}
196
197 resp = auth_client.post(
198 reverse("account_change_phone"),
199 {"phone": new_phone},
200 )
201 assert resp["location"].startswith(reverse("account_verify_phone"))
202
203
204 def test_signup_conflict(db, phone, sms_outbox, phone_only_settings):
205 assert len(sms_outbox) == 0
206 client = Client()
207 resp = client.post(reverse("account_signup"), data={"phone": phone})
208 assert resp.status_code == HTTPStatus.FOUND
209 assert len(sms_outbox) == 1
210
211 client = Client()
212 resp = client.post(reverse("account_signup"), data={"phone": phone})
213 assert resp.status_code == HTTPStatus.FOUND
214 assert len(sms_outbox) == 2
215 assert sms_outbox == [
216 {"code": ANY, "phone": phone, "user_id": 1},
217 {"phone": phone, "reason": "exists"},
218 ]
219
220
221 def test_change_to_already_existing_phone(
222 client,
223 db,
224 settings,
225 phone_factory,
226 sms_outbox,
227 messagesoutbox,
228 user_with_phone,
229 phone_only_settings,
230 ):
231 settings.ACCOUNT_PHONE_VERIFICATION_SUPPORTS_RESEND = True
232 settings.ACCOUNT_PHONE_VERIFICATION_SUPPORTS_CHANGE = True
233 settings.ACCOUNT_PREVENT_ENUMERATION = True
234 new_phone = phone_factory()
235 resp = client.post(
236 reverse("account_signup"),
237 {"phone": new_phone},
238 )
239 # A user signed up.
240 new_user = get_adapter().get_user_by_phone(new_phone)
241 assert resp.status_code == HTTPStatus.FOUND
242 assert resp["location"] == reverse("account_verify_phone")
243 assert len(sms_outbox) == 1
244 assert sms_outbox[0] == {"code": ANY, "phone": new_phone, "user_id": new_user.pk}
245 # assert len(messagesoutbox) == 1
246 # assert (
247 # messagesoutbox[-1]["message_template"]
248 # == "account/messages/email_confirmation_sent.txt"
249 # )
250
251 # Change to a conflicting phone number
252 existing_phone_verified = get_adapter().get_phone(user_with_phone)
253 resp = client.post(
254 reverse("account_verify_phone"),
255 {"action": "change", "phone": existing_phone_verified[0]},
256 )
257 assert resp.status_code == HTTPStatus.FOUND
258 assert resp["location"] == reverse("account_verify_phone")
259 assert len(sms_outbox) == 2
260 assert sms_outbox[1] == {"phone": existing_phone_verified[0], "reason": "exists"}
261 # assert len(messagesoutbox) == 2
262 # assert (
263 # messagesoutbox[-1]["message_template"]
264 # == "account/messages/email_confirmation_sent.txt"
265 # )
266 assert get_adapter().get_phone(new_user)[0] == new_phone
267
268 # Change back to new phone
269 new_phone2 = phone_factory()
270 resp = client.post(
271 reverse("account_verify_phone"),
272 {"action": "change", "phone": new_phone2},
273 )
274 assert len(sms_outbox) == 3
275 assert resp.status_code == HTTPStatus.FOUND
276 assert resp["location"] == reverse("account_verify_phone")
277 assert sms_outbox[2] == {"code": ANY, "phone": new_phone2, "user_id": ANY}
278 # assert len(messagesoutbox) == 3
279 # assert (
280 # messagesoutbox[-1]["message_template"]
281 # == "account/messages/email_confirmation_sent.txt"
282 # )
283 assert get_adapter().get_phone(new_user)[0] == new_phone2
284
285 resp = client.post(
286 reverse("account_verify_phone"),
287 data={"code": sms_outbox[-1]["code"]},
288 )
289 assert resp.status_code == HTTPStatus.FOUND
290 assert get_adapter().get_phone(new_user) == (new_phone2, True)
291
292
293 def test_signup_invalid_phone(db, client, sms_outbox, phone_only_settings):
294 assert len(sms_outbox) == 0
295 resp = client.post(reverse("account_signup"), data={"phone": "notaphone"})
296 assert resp.status_code == HTTPStatus.OK
297 assert resp.context["form"].errors == {
298 "phone": ["Enter a phone number including country code (e.g. +1 for the US)."]
299 }