Debian Codegraph @

tests/apps/headless/mfa/test_totp.py

raw
1 from http import HTTPStatus
2
3 import pytest
4
5 from allauth.account.internal.flows.login import AUTHENTICATION_METHODS_SESSION_KEY
6 from allauth.mfa.models import Authenticator
7
8
9 @pytest.mark.parametrize("email_verified", [False, True])
10 def test_get_totp_not_active(auth_client, user, headless_reverse, email_verified):
11 resp = auth_client.get(headless_reverse("headless:mfa:manage_totp"))
12 if email_verified:
13 assert resp.status_code == HTTPStatus.NOT_FOUND
14 data = resp.json()
15 assert len(data["meta"]["secret"]) == 32
16 assert len(data["meta"]["totp_url"]) == 145
17 else:
18 assert resp.status_code == HTTPStatus.CONFLICT
19 assert resp.json() == {
20 "status": HTTPStatus.CONFLICT,
21 "errors": [
22 {
23 "message": "You cannot activate two-factor authentication until you have verified your email address.",
24 "code": "unverified_email",
25 }
26 ],
27 }
28
29
30 def test_get_totp(
31 auth_client,
32 user_with_totp,
33 headless_reverse,
34 ):
35 resp = auth_client.get(headless_reverse("headless:mfa:manage_totp"))
36 assert resp.status_code == HTTPStatus.OK
37 data = resp.json()
38 assert data["data"]["type"] == "totp"
39 assert isinstance(data["data"]["created_at"], float)
40
41
42 def test_deactivate_totp(
43 auth_client,
44 user_with_totp,
45 headless_reverse,
46 reauthentication_bypass,
47 ):
48 with reauthentication_bypass():
49 resp = auth_client.delete(headless_reverse("headless:mfa:manage_totp"))
50 assert resp.status_code == HTTPStatus.OK
51 assert not Authenticator.objects.filter(user=user_with_totp).exists()
52
53
54 def test_deactivate_totp_with_reauthenticate(
55 auth_client,
56 user_with_totp,
57 headless_reverse,
58 totp_validation_bypass,
59 ):
60 # Let's ensure the session require reauthentication by having an old `at`.
61 session = auth_client.headless_session()
62 session[AUTHENTICATION_METHODS_SESSION_KEY] = [{"at": 0}]
63 session.save()
64
65 # Attempt to deactivate, should not be allowed.
66 resp = auth_client.delete(headless_reverse("headless:mfa:manage_totp"))
67 assert resp.status_code == HTTPStatus.UNAUTHORIZED
68 assert resp.json()["data"]["flows"] == [
69 {"id": "reauthenticate"},
70 {"id": "mfa_reauthenticate", "types": ["totp"]},
71 ]
72 assert Authenticator.objects.filter(user=user_with_totp).exists()
73
74 # Now, reauthenticate...
75 with totp_validation_bypass():
76 resp = auth_client.post(
77 headless_reverse("headless:mfa:reauthenticate"),
78 data={"code": "42"},
79 content_type="application/json",
80 )
81 assert resp.status_code == HTTPStatus.OK
82
83 # ... and try again.
84 resp = auth_client.delete(headless_reverse("headless:mfa:manage_totp"))
85 assert resp.status_code == HTTPStatus.OK
86
87 # Success
88 assert not Authenticator.objects.filter(user=user_with_totp).exists()
89
90
91 @pytest.mark.parametrize("email_verified", [False, True])
92 def test_activate_totp(
93 auth_client,
94 user,
95 headless_reverse,
96 reauthentication_bypass,
97 settings,
98 totp_validation_bypass,
99 email_verified,
100 ):
101 with reauthentication_bypass():
102 with totp_validation_bypass():
103 resp = auth_client.post(
104 headless_reverse("headless:mfa:manage_totp"),
105 data={"code": "42"},
106 content_type="application/json",
107 )
108 if email_verified:
109 assert resp.status_code == HTTPStatus.OK
110 assert Authenticator.objects.filter(
111 user=user, type=Authenticator.Type.TOTP
112 ).exists()
113 data = resp.json()
114 assert data["data"]["type"] == "totp"
115 assert isinstance(data["data"]["created_at"], float)
116 assert data["data"]["last_used_at"] is None
117 else:
118 assert resp.status_code == HTTPStatus.BAD_REQUEST