Debian Codegraph @

tests/apps/headless/mfa/test_trust.py

raw
1 from http import HTTPStatus
2
3 import pytest
4
5 from allauth.headless.constants import Client, Flow
6
7
8 @pytest.mark.parametrize("trust", [False, True])
9 def test_auth_unverified_email_and_mfa(
10 client,
11 user_with_totp,
12 user_password,
13 settings_impacting_urls,
14 totp_validation_bypass,
15 headless_reverse,
16 headless_client,
17 trust,
18 ):
19 with settings_impacting_urls(
20 ACCOUNT_LOGIN_METHODS={"email"},
21 ACCOUNT_EMAIL_VERIFICATION="mandatory",
22 ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATION=True,
23 MFA_TRUST_ENABLED=True,
24 ):
25 # Login
26 resp = client.post(
27 headless_reverse("headless:account:login"),
28 data={
29 "email": user_with_totp.email,
30 "password": user_password,
31 },
32 content_type="application/json",
33 )
34
35 # TOTP is the next stage
36 assert resp.status_code == HTTPStatus.UNAUTHORIZED
37 data = resp.json()
38 assert [f for f in data["data"]["flows"] if f["id"] == Flow.MFA_AUTHENTICATE][
39 0
40 ]["is_pending"]
41 with totp_validation_bypass():
42 resp = client.post(
43 headless_reverse("headless:mfa:authenticate"),
44 data={"code": "bad"},
45 content_type="application/json",
46 )
47
48 if headless_client == Client.APP:
49 # App client does not support trust
50 assert resp.status_code == HTTPStatus.OK
51 else:
52 # Trust stage is pending
53 assert resp.status_code == HTTPStatus.UNAUTHORIZED
54 data = resp.json()
55 assert [f for f in data["data"]["flows"] if f["id"] == Flow.MFA_TRUST][0][
56 "is_pending"
57 ]
58
59 # Indicate trust Y/N
60 resp = client.post(
61 headless_reverse("headless:mfa:trust"),
62 data={"trust": trust},
63 content_type="application/json",
64 )
65
66 # Logout
67 assert resp.status_code == HTTPStatus.OK
68 resp = client.delete(
69 headless_reverse("headless:account:current_session"),
70 )
71 assert resp.status_code == HTTPStatus.UNAUTHORIZED
72
73 # Login
74 resp = client.post(
75 headless_reverse("headless:account:login"),
76 data={
77 "email": user_with_totp.email,
78 "password": user_password,
79 },
80 content_type="application/json",
81 )
82
83 # Trust used?
84 assert resp.status_code == (
85 HTTPStatus.OK if trust else HTTPStatus.UNAUTHORIZED
86 )