Debian Codegraph @

tests/apps/socialaccount/test_signup.py

raw
1 from http import HTTPStatus
2 from unittest.mock import ANY, patch
3
4 from django.conf import settings
5 from django.contrib.auth import get_user_model
6 from django.contrib.auth.models import AnonymousUser
7 from django.urls import reverse
8
9 import pytest
10 from pytest_django.asserts import assertFormError, assertRedirects
11
12 from allauth.account import app_settings as account_settings
13 from allauth.account.authentication import AUTHENTICATION_METHODS_SESSION_KEY
14 from allauth.account.models import EmailAddress
15 from allauth.account.utils import user_email, user_username
16 from allauth.core import context
17 from allauth.socialaccount.helpers import complete_social_login
18 from allauth.socialaccount.models import SocialAccount
19 from allauth.socialaccount.views import signup
20
21
22 @pytest.fixture
23 def setup_sociallogin_flow(request_factory):
24 def f(client, sociallogin):
25 request = request_factory.get("/")
26 request.user = AnonymousUser()
27
28 with context.request_context(request):
29 resp = complete_social_login(request, sociallogin)
30 session = client.session
31 for k, v in request.session.items():
32 session[k] = v
33 session.save()
34 return resp
35
36 return f
37
38
39 @pytest.fixture
40 def email_address_clash(
41 request_factory, sociallogin_factory, twitter_provider_settings
42 ):
43 def _email_address_clash(username, email):
44 User = get_user_model()
45 # Some existig user
46 exi_user = User()
47 user_username(exi_user, "test")
48 exi_user_email = "[email protected]"
49 user_email(exi_user, exi_user_email)
50 exi_user.save()
51 EmailAddress.objects.create(
52 user=exi_user, email=exi_user_email, verified=True, primary=True
53 )
54
55 # A social user being signed up...
56 sociallogin = sociallogin_factory(
57 provider="twitter", username=username, email=email
58 )
59
60 # Signing up, should pop up the social signup form
61 request = request_factory.get("/accounts/twitter/login/callback/")
62 request.user = AnonymousUser()
63 with context.request_context(request):
64 resp = complete_social_login(request, sociallogin)
65 return request, resp
66
67 return _email_address_clash
68
69
70 def test_email_address_created(
71 settings, db, client, setup_sociallogin_flow, sociallogin_factory
72 ):
73 settings.SOCIALACCOUNT_AUTO_SIGNUP = True
74 settings.ACCOUNT_SIGNUP_FORM_CLASS = None
75 settings.ACCOUNT_EMAIL_VERIFICATION = account_settings.EmailVerificationMethod.NONE
76
77 sociallogin = sociallogin_factory(
78 email="[email protected]", email_verified=False, username="test"
79 )
80 setup_sociallogin_flow(client, sociallogin)
81
82 user = get_user_model().objects.get(
83 **{account_settings.USER_MODEL_USERNAME_FIELD: "test"}
84 )
85 assert SocialAccount.objects.filter(user=user, uid=sociallogin.account.uid).exists()
86 assert EmailAddress.objects.filter(user=user, email=user_email(user)).exists()
87
88
89 def test_email_address_clash_username_required(
90 db, client, settings, email_address_clash
91 ):
92 """Test clash on both username and email"""
93 settings.ACCOUNT_EMAIL_REQUIRED = True
94 settings.ACCOUNT_UNIQUE_EMAIL = True
95 settings.ACCOUNT_USERNAME_REQUIRED = True
96 settings.ACCOUNT_LOGIN_METHODS = {"email"}
97 settings.SOCIALACCOUNT_AUTO_SIGNUP = True
98 request, resp = email_address_clash("test", "[email protected]")
99 assert resp["location"] == reverse("socialaccount_signup")
100
101 # POST different username/email to social signup form
102 request.method = "POST"
103 request.POST = {"username": "other", "email": "[email protected]"}
104 with context.request_context(request):
105 resp = signup(request)
106 assert resp["location"] == "/accounts/profile/"
107 user = get_user_model().objects.get(
108 **{account_settings.USER_MODEL_EMAIL_FIELD: "[email protected]"}
109 )
110 assert user_username(user) == "other"
111
112
113 def test_email_address_clash_username_not_required(db, settings, email_address_clash):
114 """Test clash while username is not required"""
115 settings.ACCOUNT_EMAIL_REQUIRED = True
116 settings.ACCOUNT_UNIQUE_EMAIL = True
117 settings.ACCOUNT_USERNAME_REQUIRED = False
118 settings.ACCOUNT_LOGIN_METHODS = {"email"}
119 settings.SOCIALACCOUNT_AUTO_SIGNUP = True
120 request, resp = email_address_clash("test", "[email protected]")
121 assert resp["location"] == reverse("socialaccount_signup")
122
123 # POST email to social signup form (username not present)
124 request.method = "POST"
125 request.POST = {"email": "[email protected]"}
126 with context.request_context(request):
127 resp = signup(request)
128 assert resp["location"] == "/accounts/profile/"
129 user = get_user_model().objects.get(
130 **{account_settings.USER_MODEL_EMAIL_FIELD: "[email protected]"}
131 )
132 assert user_username(user) != "test"
133
134
135 def test_email_address_clash_username_auto_signup(db, settings, email_address_clash):
136 settings.ACCOUNT_EMAIL_REQUIRED = True
137 settings.ACCOUNT_UNIQUE_EMAIL = True
138 settings.ACCOUNT_USERNAME_REQUIRED = False
139 settings.ACCOUNT_LOGIN_METHODS = {"email"}
140 settings.SOCIALACCOUNT_AUTO_SIGNUP = True
141 # Clash on username, but auto signup still works
142 request, resp = email_address_clash("test", "[email protected]")
143 assert resp["location"] == "/accounts/profile/"
144 user = get_user_model().objects.get(
145 **{account_settings.USER_MODEL_EMAIL_FIELD: "[email protected]"}
146 )
147 assert user_username(user) != "test"
148
149
150 def test_populate_username_in_blacklist(
151 db, settings, request_factory, sociallogin_factory, twitter_provider_settings
152 ):
153 settings.ACCOUNT_EMAIL_REQUIRED = True
154 settings.ACCOUNT_USERNAME_BLACKLIST = ["username", "username1", "username2"]
155 settings.ACCOUNT_UNIQUE_EMAIL = True
156 settings.ACCOUNT_USERNAME_REQUIRED = True
157 settings.ACCOUNT_LOGIN_METHODS = {"email"}
158 settings.SOCIALACCOUNT_AUTO_SIGNUP = True
159 request = request_factory.get("/accounts/twitter/login/callback/")
160 request.user = AnonymousUser()
161 sociallogin = sociallogin_factory(
162 provider="twitter", username="username", email="[email protected]"
163 )
164 with context.request_context(request):
165 complete_social_login(request, sociallogin)
166
167 assert request.user.username not in account_settings.USERNAME_BLACKLIST
168
169
170 def test_verified_email_change_at_signup(
171 db, client, settings, sociallogin_factory, setup_sociallogin_flow
172 ):
173 """
174 Test scenario for when the user changes email at social signup. Current
175 behavior is that both the unverified and verified email are added, and
176 that the user is allowed to pass because he did provide a verified one.
177 """
178 settings.ACCOUNT_EMAIL_REQUIRED = True
179 settings.ACCOUNT_EMAIL_VERIFICATION = "mandatory"
180 settings.ACCOUNT_UNIQUE_EMAIL = True
181 settings.ACCOUNT_USERNAME_REQUIRED = False
182 settings.ACCOUNT_LOGIN_METHODS = {"email"}
183 settings.SOCIALACCOUNT_AUTO_SIGNUP = False
184
185 sociallogin = sociallogin_factory(email="[email protected]")
186 setup_sociallogin_flow(client, sociallogin)
187 resp = client.get(reverse("socialaccount_signup"))
188 form = resp.context["form"]
189 assert form["email"].value() == "[email protected]"
190 resp = client.post(
191 reverse("socialaccount_signup"),
192 data={"email": "[email protected]"},
193 )
194 assertRedirects(resp, "/accounts/profile/", fetch_redirect_response=False)
195 user = get_user_model().objects.all()[0]
196 assert user_email(user) == "[email protected]"
197 assert EmailAddress.objects.filter(
198 user=user,
199 email="[email protected]",
200 verified=True,
201 primary=True,
202 ).exists()
203 assert EmailAddress.objects.filter(
204 user=user,
205 email="[email protected]",
206 verified=False,
207 primary=False,
208 ).exists()
209
210
211 def test_unverified_email_change_at_signup(
212 db, client, settings, sociallogin_factory, setup_sociallogin_flow
213 ):
214 """
215 Test scenario for when the user changes email at social signup, while
216 his provider did not provide a verified email. In that case, email
217 verification will kick in. Here, both email addresses are added as
218 well.
219 """
220 settings.ACCOUNT_EMAIL_REQUIRED = True
221 settings.ACCOUNT_EMAIL_VERIFICATION = "mandatory"
222 settings.ACCOUNT_UNIQUE_EMAIL = True
223 settings.ACCOUNT_USERNAME_REQUIRED = False
224 settings.ACCOUNT_LOGIN_METHODS = {"email"}
225 settings.SOCIALACCOUNT_AUTO_SIGNUP = False
226
227 User = get_user_model()
228 sociallogin = sociallogin_factory(
229 email="[email protected]", email_verified=False
230 )
231 setup_sociallogin_flow(client, sociallogin)
232 resp = client.get(reverse("socialaccount_signup"))
233 form = resp.context["form"]
234 assert form["email"].value() == "[email protected]"
235 resp = client.post(
236 reverse("socialaccount_signup"),
237 data={"email": "[email protected]"},
238 )
239
240 assertRedirects(resp, reverse("account_email_verification_sent"))
241 user = User.objects.all()[0]
242 assert user_email(user) == "[email protected]"
243 assert EmailAddress.objects.filter(
244 user=user,
245 email="[email protected]",
246 verified=False,
247 primary=False,
248 ).exists()
249 assert EmailAddress.objects.filter(
250 user=user,
251 email="[email protected]",
252 verified=False,
253 primary=True,
254 ).exists()
255
256
257 def test_unique_email_validation_signup(
258 db, client, sociallogin_factory, settings, setup_sociallogin_flow
259 ):
260 settings.ACCOUNT_PREVENT_ENUMERATION = False
261 settings.ACCOUNT_EMAIL_REQUIRED = True
262 settings.ACCOUNT_EMAIL_VERIFICATION = "mandatory"
263 settings.ACCOUNT_UNIQUE_EMAIL = True
264 settings.ACCOUNT_USERNAME_REQUIRED = False
265 settings.ACCOUNT_LOGIN_METHODS = {"email"}
266 settings.SOCIALACCOUNT_AUTO_SIGNUP = False
267 User = get_user_model()
268 email = "[email protected]"
269 user = User.objects.create(email=email)
270 EmailAddress.objects.create(email=email, user=user, verified=True)
271 sociallogin = sociallogin_factory(email="[email protected]")
272 setup_sociallogin_flow(client, sociallogin)
273 resp = client.get(reverse("socialaccount_signup"))
274 form = resp.context["form"]
275 assert form["email"].value() == email
276 resp = client.post(reverse("socialaccount_signup"), data={"email": email})
277 assertFormError(
278 resp.context["form"],
279 "email",
280 "An account already exists with this email address."
281 " Please sign in to that account first, then connect"
282 " your Unittest Server account.",
283 )
284
285
286 def test_social_account_taken_at_signup(
287 db, client, sociallogin_factory, settings, setup_sociallogin_flow
288 ):
289 """
290 Test scenario for when the user signs up with a social account
291 and uses email address in that social account. But upon seeing the
292 verification screen, they realize that email address is somehow
293 unusable for them, and so backs up and enters a different email
294 address (and is forced to choose a new username) while providing
295 the same social account token which is owned by their first attempt.
296 """
297 settings.ACCOUNT_EMAIL_REQUIRED = True
298 settings.ACCOUNT_EMAIL_VERIFICATION = "mandatory"
299 settings.ACCOUNT_UNIQUE_EMAIL = True
300 settings.ACCOUNT_USERNAME_REQUIRED = True
301 settings.ACCOUNT_LOGIN_METHODS = {"email"}
302 settings.SOCIALACCOUNT_AUTO_SIGNUP = False
303
304 User = get_user_model()
305 sociallogin = sociallogin_factory(email="[email protected]", email_verified=False)
306 setup_sociallogin_flow(client, sociallogin)
307 resp = client.get(reverse("socialaccount_signup"))
308 form = resp.context["form"]
309 assert form["email"].value() == "[email protected]"
310 resp = client.post(
311 reverse("socialaccount_signup"),
312 data={"username": "me1", "email": "[email protected]"},
313 )
314 assert resp.status_code == HTTPStatus.FOUND
315 assert User.objects.count() == 1
316 assert SocialAccount.objects.count() == 1
317
318 resp = client.get(reverse("socialaccount_signup"))
319 assertRedirects(resp, reverse("account_login"))
320
321
322 def test_email_address_required_missing_from_sociallogin(
323 db, settings, sociallogin_factory, client, setup_sociallogin_flow
324 ):
325 """Tests that when the email address is missing from the sociallogin email
326 verification kicks in.
327 """
328 settings.ACCOUNT_EMAIL_REQUIRED = True
329 settings.ACCOUNT_UNIQUE_EMAIL = True
330 settings.ACCOUNT_USERNAME_REQUIRED = False
331 settings.ACCOUNT_LOGIN_METHODS = {"email"}
332 settings.ACCOUNT_EMAIL_VERIFICATION = "mandatory"
333 settings.SOCIALACCOUNT_AUTO_SIGNUP = True
334
335 sociallogin = sociallogin_factory(with_email=False)
336 resp = setup_sociallogin_flow(client, sociallogin)
337 assert resp["location"] == reverse("socialaccount_signup")
338
339 resp = client.post(reverse("socialaccount_signup"), {"email": "[email protected]"})
340 assert resp["location"] == reverse("account_email_verification_sent")
341
342
343 def test_email_address_conflict_at_social_signup_form(
344 db,
345 settings,
346 user_factory,
347 sociallogin_factory,
348 client,
349 setup_sociallogin_flow,
350 mailoutbox,
351 ):
352 """Tests that when an already existing email is given at the social signup
353 form, enumeration preventation kicks in.
354 """
355 settings.ACCOUNT_EMAIL_REQUIRED = True
356 settings.ACCOUNT_UNIQUE_EMAIL = True
357 settings.ACCOUNT_USERNAME_REQUIRED = False
358 settings.ACCOUNT_LOGIN_METHODS = {"email"}
359 settings.ACCOUNT_EMAIL_VERIFICATION = "mandatory"
360 settings.SOCIALACCOUNT_AUTO_SIGNUP = True
361
362 user = user_factory()
363 sociallogin = sociallogin_factory(with_email=False)
364
365 resp = setup_sociallogin_flow(client, sociallogin)
366 # Auto signup does not kick in as the `sociallogin` does not have an email.
367 assert resp["location"] == reverse("socialaccount_signup")
368
369 # Here, we input the already existing email.
370 resp = client.post(reverse("socialaccount_signup"), {"email": user.email})
371 assert mailoutbox[0].subject == "[example.com] Account Already Exists"
372 assert resp["location"] == reverse("account_email_verification_sent")
373
374
375 def test_email_address_conflict_during_auto_signup(
376 db,
377 settings,
378 user_factory,
379 sociallogin_factory,
380 client,
381 mailoutbox,
382 setup_sociallogin_flow,
383 ):
384 """Tests that when an already existing email is received from the provider,
385 enumeration preventation kicks in.
386 """
387 settings.ACCOUNT_EMAIL_REQUIRED = True
388 settings.ACCOUNT_UNIQUE_EMAIL = True
389 settings.ACCOUNT_USERNAME_REQUIRED = False
390 settings.ACCOUNT_LOGIN_METHODS = {"email"}
391 settings.ACCOUNT_EMAIL_VERIFICATION = "mandatory"
392 settings.SOCIALACCOUNT_AUTO_SIGNUP = True
393
394 user = user_factory()
395 sociallogin = sociallogin_factory(email=user.email, with_email=True)
396 resp = setup_sociallogin_flow(client, sociallogin)
397 assert resp["location"] == reverse("account_email_verification_sent")
398 assert mailoutbox[0].subject == "[example.com] Account Already Exists"
399
400
401 def test_email_address_conflict_removes_conflicting_email(
402 db,
403 settings,
404 user_factory,
405 sociallogin_factory,
406 client,
407 mailoutbox,
408 setup_sociallogin_flow,
409 ):
410 """Tests that when an already existing email is given at the social signup
411 form, enumeration preventation kicks in.
412 """
413 settings.ACCOUNT_EMAIL_REQUIRED = True
414 settings.ACCOUNT_UNIQUE_EMAIL = True
415 settings.ACCOUNT_USERNAME_REQUIRED = False
416 settings.ACCOUNT_LOGIN_METHODS = {"email"}
417 settings.ACCOUNT_EMAIL_VERIFICATION = "optional"
418 settings.SOCIALACCOUNT_AUTO_SIGNUP = True
419 settings.SOCIALACCOUNT_EMAIL_AUTHENTICATION = False
420
421 user = user_factory(email_verified=False)
422 sociallogin = sociallogin_factory(email=user.email, email_verified=False)
423
424 resp = setup_sociallogin_flow(client, sociallogin)
425
426 # Auto signup does not kick in as the `sociallogin` has a conflicting email.
427 assert resp["location"] == reverse("socialaccount_signup")
428
429 # Here, we input the already existing email.
430 resp = client.post(reverse("socialaccount_signup"), {"email": "[email protected]"})
431 assert mailoutbox[0].subject == "[example.com] Please Confirm Your Email Address"
432 assert resp["location"] == settings.LOGIN_REDIRECT_URL
433 assert EmailAddress.objects.filter(email=user.email).count() == 1
434
435
436 def test_signup_closed(
437 settings,
438 db,
439 client,
440 setup_sociallogin_flow,
441 sociallogin_factory,
442 ):
443 sociallogin = sociallogin_factory(
444 email="[email protected]", email_verified=False, username="test"
445 )
446 with patch(
447 "allauth.socialaccount.adapter.DefaultSocialAccountAdapter.is_open_for_signup"
448 ) as iofs:
449 iofs.return_value = False
450 resp = setup_sociallogin_flow(client, sociallogin)
451 assert b"Sign Up Closed" in resp.content
452 assert not get_user_model().objects.exists()
453
454
455 def test_authentication_records(client, db):
456 resp = client.post(reverse("dummy_login"))
457 assert resp.status_code == HTTPStatus.FOUND
458 resp = client.post(
459 resp["location"],
460 {"id": "123", "email": "[email protected]", "email_verified": True},
461 )
462 assert resp.status_code == HTTPStatus.FOUND
463 assert resp["location"] == settings.LOGIN_REDIRECT_URL
464 assert client.session[AUTHENTICATION_METHODS_SESSION_KEY] == [
465 {
466 "at": ANY,
467 "method": "socialaccount",
468 "provider": "dummy",
469 "uid": "123",
470 }
471 ]