Debian Codegraph @

tests/apps/headless/account/test_login.py

raw
1 from http import HTTPStatus
2 from unittest.mock import ANY
3
4 import pytest
5
6 from allauth.account.signals import user_logged_in
7 from allauth.headless.base.response import AuthenticationResponse
8
9
10 def test_auth_password_input_error(headless_reverse, client):
11 resp = client.post(
12 headless_reverse("headless:account:login"),
13 data={},
14 content_type="application/json",
15 )
16 assert resp.status_code == HTTPStatus.BAD_REQUEST
17 assert resp.json() == {
18 "status": HTTPStatus.BAD_REQUEST,
19 "errors": [
20 {
21 "message": "This field is required.",
22 "code": "required",
23 "param": "username",
24 },
25 {
26 "message": "This field is required.",
27 "code": "required",
28 "param": "password",
29 },
30 ],
31 }
32
33
34 def test_auth_password_bad_password(headless_reverse, client, user, settings):
35 settings.ACCOUNT_LOGIN_METHODS = {"email"}
36 resp = client.post(
37 headless_reverse("headless:account:login"),
38 data={
39 "email": user.email,
40 "password": "wrong",
41 },
42 content_type="application/json",
43 )
44 assert resp.status_code == HTTPStatus.BAD_REQUEST
45 assert resp.json() == {
46 "status": HTTPStatus.BAD_REQUEST,
47 "errors": [
48 {
49 "param": "password",
50 "message": "The email address and/or password you specified are not correct.",
51 "code": "email_password_mismatch",
52 }
53 ],
54 }
55
56
57 def test_auth_password_success(
58 client, user, user_password, settings, headless_reverse, headless_client
59 ):
60 settings.ACCOUNT_LOGIN_METHODS = {"email", "username"}
61 login_resp = client.post(
62 headless_reverse("headless:account:login"),
63 data={
64 "email": user.email,
65 "password": user_password,
66 },
67 content_type="application/json",
68 )
69 assert login_resp.status_code == HTTPStatus.OK
70 session_resp = client.get(
71 headless_reverse("headless:account:current_session"),
72 content_type="application/json",
73 )
74 assert session_resp.status_code == HTTPStatus.OK
75 for resp in [login_resp, session_resp]:
76 extra_meta = {}
77 if headless_client == "app" and resp == login_resp:
78 # The session is created on first login, and hence the token is
79 # exposed only at that moment.
80 extra_meta["session_token"] = ANY
81 assert resp.json() == {
82 "status": HTTPStatus.OK,
83 "data": {
84 "user": {
85 "id": user.pk,
86 "display": str(user),
87 "email": user.email,
88 "username": user.username,
89 "has_usable_password": True,
90 },
91 "methods": [
92 {
93 "at": ANY,
94 "email": user.email,
95 "method": "password",
96 }
97 ],
98 },
99 "meta": {"is_authenticated": True, **extra_meta},
100 }
101
102
103 @pytest.mark.parametrize(
104 "is_active,status_code", [(False, HTTPStatus.UNAUTHORIZED), (True, HTTPStatus.OK)]
105 )
106 def test_auth_password_user_inactive(
107 client, user, user_password, settings, status_code, is_active, headless_reverse
108 ):
109 user.is_active = is_active
110 user.save(update_fields=["is_active"])
111 resp = client.post(
112 headless_reverse("headless:account:login"),
113 data={
114 "username": user.username,
115 "password": user_password,
116 },
117 content_type="application/json",
118 )
119 assert resp.status_code == status_code
120
121
122 def test_login_failed_rate_limit(
123 client,
124 user,
125 settings,
126 headless_reverse,
127 headless_client,
128 enable_cache,
129 ):
130 settings.ACCOUNT_RATE_LIMITS = {"login_failed": "1/m/ip"}
131 settings.ACCOUNT_LOGIN_METHODS = {"email"}
132 for attempt in range(2):
133 resp = client.post(
134 headless_reverse("headless:account:login"),
135 data={
136 "email": user.email,
137 "password": "wrong",
138 },
139 content_type="application/json",
140 )
141 assert resp.status_code == HTTPStatus.BAD_REQUEST
142 assert resp.json()["errors"] == [
143 (
144 {
145 "code": "email_password_mismatch",
146 "message": "The email address and/or password you specified are not correct.",
147 "param": "password",
148 }
149 if attempt == 0
150 else {
151 "message": "Too many failed login attempts. Try again later.",
152 "code": "too_many_login_attempts",
153 }
154 )
155 ]
156
157
158 def test_login_rate_limit(
159 client,
160 user,
161 user_password,
162 settings,
163 headless_reverse,
164 headless_client,
165 enable_cache,
166 ):
167 settings.ACCOUNT_RATE_LIMITS = {"login": "1/m/ip"}
168 settings.ACCOUNT_LOGIN_METHODS = {"email"}
169 for attempt in range(2):
170 resp = client.post(
171 headless_reverse("headless:account:login"),
172 data={
173 "email": user.email,
174 "password": user_password,
175 },
176 content_type="application/json",
177 )
178 expected_status = HTTPStatus.TOO_MANY_REQUESTS if attempt else HTTPStatus.OK
179 assert resp.status_code == expected_status
180
181
182 def test_login_already_logged_in(
183 auth_client, user, user_password, settings, headless_reverse
184 ):
185 settings.ACCOUNT_LOGIN_METHODS = {"email"}
186 resp = auth_client.post(
187 headless_reverse("headless:account:login"),
188 data={
189 "email": user.email,
190 "password": user_password,
191 },
192 content_type="application/json",
193 )
194 assert resp.status_code == HTTPStatus.CONFLICT
195
196
197 def test_custom_post_login_response(
198 settings, client, headless_reverse, user, user_password
199 ):
200 settings.ACCOUNT_LOGIN_METHODS = {"email"}
201
202 def on_user_logged_in(**kwargs):
203 response = kwargs["response"]
204 assert isinstance(response, AuthenticationResponse)
205
206 user_logged_in.connect(on_user_logged_in)
207 try:
208 login_resp = client.post(
209 headless_reverse("headless:account:login"),
210 data={
211 "email": user.email,
212 "password": user_password,
213 },
214 content_type="application/json",
215 )
216 assert login_resp.status_code == HTTPStatus.OK
217 finally:
218 user_logged_in.disconnect(on_user_logged_in)