Debian Codegraph @

tests/apps/headless/mfa/test_views.py

raw
1 from http import HTTPStatus
2
3 from allauth.account.models import EmailAddress, get_emailconfirmation_model
4 from allauth.headless.constants import Flow
5
6
7 def test_auth_unverified_email_and_mfa(
8 client,
9 user_factory,
10 password_factory,
11 settings,
12 totp_validation_bypass,
13 headless_reverse,
14 headless_client,
15 ):
16 settings.ACCOUNT_LOGIN_METHODS = {"email"}
17 settings.ACCOUNT_EMAIL_VERIFICATION = "mandatory"
18 settings.ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATION = True
19 password = password_factory()
20 user = user_factory(email_verified=False, password=password, with_totp=True)
21 resp = client.post(
22 headless_reverse("headless:account:login"),
23 data={
24 "email": user.email,
25 "password": password,
26 },
27 content_type="application/json",
28 )
29 assert resp.status_code == HTTPStatus.UNAUTHORIZED
30 data = resp.json()
31 assert [f for f in data["data"]["flows"] if f["id"] == Flow.VERIFY_EMAIL][0][
32 "is_pending"
33 ]
34 emailaddress = EmailAddress.objects.filter(user=user, verified=False).get()
35 key = get_emailconfirmation_model().create(emailaddress).key
36 resp = client.post(
37 headless_reverse("headless:account:verify_email"),
38 data={"key": key},
39 content_type="application/json",
40 )
41 assert resp.status_code == HTTPStatus.UNAUTHORIZED
42 flows = [
43 {"id": "login"},
44 {"id": "login_by_code"},
45 {"id": "signup"},
46 ]
47 if headless_client == "browser":
48 flows.append(
49 {
50 "id": "provider_redirect",
51 "providers": ["dummy", "unittest-server", "other-server"],
52 }
53 )
54 flows.append(
55 {
56 "id": "provider_token",
57 "providers": ["dummy", "unittest-server", "other-server"],
58 }
59 )
60 flows.append({"id": "mfa_login_webauthn"})
61 flows.append(
62 {
63 "id": "mfa_authenticate",
64 "is_pending": True,
65 "types": ["totp"],
66 }
67 )
68
69 assert resp.json() == {
70 "data": {"flows": flows},
71 "meta": {"is_authenticated": False},
72 "status": HTTPStatus.UNAUTHORIZED,
73 }
74 resp = client.post(
75 headless_reverse("headless:mfa:authenticate"),
76 data={"code": "bad"},
77 content_type="application/json",
78 )
79 assert resp.status_code == HTTPStatus.BAD_REQUEST
80 assert resp.json() == {
81 "status": HTTPStatus.BAD_REQUEST,
82 "errors": [
83 {"message": "Incorrect code.", "code": "incorrect_code", "param": "code"}
84 ],
85 }
86
87 with totp_validation_bypass():
88 resp = client.post(
89 headless_reverse("headless:mfa:authenticate"),
90 data={"code": "bad"},
91 content_type="application/json",
92 )
93 assert resp.status_code == HTTPStatus.OK
94
95
96 def test_dangling_mfa_is_logged_out(
97 client,
98 user_with_totp,
99 password_factory,
100 settings,
101 totp_validation_bypass,
102 headless_reverse,
103 headless_client,
104 user_password,
105 ):
106 settings.ACCOUNT_LOGIN_METHODS = {"email"}
107 resp = client.post(
108 headless_reverse("headless:account:login"),
109 data={
110 "email": user_with_totp.email,
111 "password": user_password,
112 },
113 content_type="application/json",
114 )
115 assert resp.status_code == HTTPStatus.UNAUTHORIZED
116 data = resp.json()
117 flow = [f for f in data["data"]["flows"] if f["id"] == Flow.MFA_AUTHENTICATE][0]
118 assert flow["is_pending"]
119 assert flow["types"] == ["totp"]
120 resp = client.delete(headless_reverse("headless:account:current_session"))
121 data = resp.json()
122 assert resp.status_code == HTTPStatus.UNAUTHORIZED
123 assert all(not f.get("is_pending") for f in data["data"]["flows"])