Debian Codegraph @

tests/apps/headless/account/test_signup.py

raw
1 from http import HTTPStatus
2 from unittest.mock import ANY, patch
3
4 from django.contrib.auth.models import User
5
6 import pytest
7
8 from allauth.account.models import EmailAddress, EmailConfirmationHMAC
9 from allauth.account.signals import user_logged_in
10 from allauth.headless.base.response import AuthenticationResponse
11 from allauth.headless.constants import Flow
12
13
14 def test_signup(
15 db,
16 client,
17 email_factory,
18 password_factory,
19 settings,
20 headless_reverse,
21 headless_client,
22 ):
23 def on_user_logged_in(**kwargs):
24 response = kwargs["response"]
25 assert isinstance(response, AuthenticationResponse)
26
27 user_logged_in.connect(on_user_logged_in)
28 try:
29 resp = client.post(
30 headless_reverse("headless:account:signup"),
31 data={
32 "username": "wizard",
33 "email": email_factory(),
34 "password": password_factory(),
35 },
36 content_type="application/json",
37 )
38 assert resp.status_code == HTTPStatus.OK
39 assert User.objects.filter(username="wizard").exists()
40 finally:
41 user_logged_in.disconnect(on_user_logged_in)
42
43
44 def test_signup_with_email_verification(
45 db,
46 client,
47 email_factory,
48 password_factory,
49 settings,
50 headless_reverse,
51 headless_client,
52 ):
53 settings.ACCOUNT_EMAIL_VERIFICATION = "mandatory"
54 settings.ACCOUNT_USERNAME_REQUIRED = False
55 settings.ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATION = True
56 email = email_factory()
57 resp = client.post(
58 headless_reverse("headless:account:signup"),
59 data={
60 "email": email,
61 "password": password_factory(),
62 },
63 content_type="application/json",
64 )
65 assert resp.status_code == HTTPStatus.UNAUTHORIZED
66 assert User.objects.filter(email=email).exists()
67 data = resp.json()
68 flow = next(f for f in data["data"]["flows"] if f.get("is_pending"))
69 assert flow["id"] == "verify_email"
70 addr = EmailAddress.objects.get(email=email)
71 key = EmailConfirmationHMAC(addr).key
72 resp = client.get(
73 headless_reverse("headless:account:verify_email"),
74 HTTP_X_EMAIL_VERIFICATION_KEY=key,
75 )
76 assert resp.status_code == HTTPStatus.OK
77 assert resp.json() == {
78 "data": {
79 "email": email,
80 "user": ANY,
81 },
82 "meta": {"is_authenticating": True},
83 "status": HTTPStatus.OK,
84 }
85 resp = client.post(
86 headless_reverse("headless:account:verify_email"),
87 data={"key": key},
88 content_type="application/json",
89 )
90 assert resp.status_code == HTTPStatus.OK
91 data = resp.json()
92 assert data["meta"]["is_authenticated"]
93 addr.refresh_from_db()
94 assert addr.verified
95
96
97 @pytest.mark.parametrize("email_verification_by_code_enabled", (False, True))
98 def test_signup_prevent_enumeration(
99 db,
100 client,
101 email_factory,
102 password_factory,
103 settings,
104 headless_reverse,
105 headless_client,
106 user,
107 mailoutbox,
108 email_verification_by_code_enabled,
109 ):
110 settings.ACCOUNT_EMAIL_VERIFICATION_BY_CODE_ENABLED = (
111 email_verification_by_code_enabled
112 )
113 settings.ACCOUNT_EMAIL_VERIFICATION = "mandatory"
114 settings.ACCOUNT_USERNAME_REQUIRED = False
115 settings.ACCOUNT_PREVENT_ENUMERATION = True
116 resp = client.post(
117 headless_reverse("headless:account:signup"),
118 data={
119 "email": user.email,
120 "password": password_factory(),
121 },
122 content_type="application/json",
123 )
124 assert len(mailoutbox) == 1
125 assert "an account using that email address already exists" in mailoutbox[0].body
126 assert resp.status_code == HTTPStatus.UNAUTHORIZED
127 data = resp.json()
128 assert [f for f in data["data"]["flows"] if f["id"] == Flow.VERIFY_EMAIL][0][
129 "is_pending"
130 ]
131 resp = client.get(headless_reverse("headless:account:current_session"))
132 data = resp.json()
133 assert [f for f in data["data"]["flows"] if f["id"] == Flow.VERIFY_EMAIL][0][
134 "is_pending"
135 ]
136 resp = client.post(
137 headless_reverse("headless:account:verify_email"),
138 data={"key": "wrong"},
139 content_type="application/json",
140 )
141 assert resp.status_code == HTTPStatus.BAD_REQUEST
142
143
144 def test_signup_rate_limit(
145 db,
146 client,
147 email_factory,
148 password_factory,
149 settings,
150 headless_reverse,
151 enable_cache,
152 headless_client,
153 ):
154 settings.ACCOUNT_RATE_LIMITS = {"signup": "1/m/ip"}
155 for attempt in range(2):
156 resp = client.post(
157 headless_reverse("headless:account:signup"),
158 data={
159 "username": f"wizard{attempt}",
160 "email": email_factory(),
161 "password": password_factory(),
162 },
163 content_type="application/json",
164 )
165 expected_status = HTTPStatus.TOO_MANY_REQUESTS if attempt else HTTPStatus.OK
166 assert resp.status_code == expected_status
167 assert resp.json()["status"] == expected_status
168
169
170 def test_signup_closed(
171 db,
172 client,
173 email_factory,
174 password_factory,
175 settings,
176 headless_reverse,
177 headless_client,
178 ):
179 with patch(
180 "allauth.account.adapter.DefaultAccountAdapter.is_open_for_signup"
181 ) as iofs:
182 iofs.return_value = False
183 resp = client.post(
184 headless_reverse("headless:account:signup"),
185 data={
186 "username": "wizard",
187 "email": email_factory(),
188 "password": password_factory(),
189 },
190 content_type="application/json",
191 )
192 assert resp.status_code == HTTPStatus.FORBIDDEN
193 assert not User.objects.filter(username="wizard").exists()
194
195
196 def test_signup_while_logged_in(
197 db,
198 auth_client,
199 email_factory,
200 password_factory,
201 settings,
202 headless_reverse,
203 headless_client,
204 ):
205 resp = auth_client.post(
206 headless_reverse("headless:account:signup"),
207 data={
208 "username": "wizard",
209 "email": email_factory(),
210 "password": password_factory(),
211 },
212 content_type="application/json",
213 )
214 assert resp.status_code == HTTPStatus.CONFLICT
215 assert resp.json() == {"status": HTTPStatus.CONFLICT}
216
217
218 def test_signup_without_password(
219 db,
220 client,
221 email_factory,
222 password_factory,
223 headless_reverse,
224 headless_client,
225 settings_impacting_urls,
226 ):
227 with settings_impacting_urls(
228 ACCOUNT_LOGIN_BY_CODE_ENABLED=True,
229 ACCOUNT_EMAIL_VERIFICATION="mandatory",
230 ACCOUNT_EMAIL_VERIFICATION_BY_CODE_ENABLED=True,
231 ACCOUNT_SIGNUP_FIELDS=["email*", "password1"],
232 ):
233 email = email_factory()
234 resp = client.post(
235 headless_reverse("headless:account:signup"),
236 data={
237 "username": "wizard",
238 "email": email,
239 },
240 content_type="application/json",
241 )
242 assert resp.status_code == HTTPStatus.UNAUTHORIZED
243 user = User.objects.get(email=email)
244 assert not user.check_password("")