Debian Codegraph @

tests/test_tf_plugin.py

raw
1 """
2 test_tf_plugin
3 ~~~~~~~~~~~~~~~~~
4
5 tf_plugin tests
6
7 :copyright: (c) 2022-2025 by J. Christopher Wagner (jwag).
8 :license: MIT, see LICENSE for more details.
9 """
10
11 import json
12 import pytest
13
14 from tests.test_utils import (
15 get_form_action,
16 get_session,
17 get_existing_session,
18 logout,
19 setup_tf_sms,
20 )
21
22 from tests.test_two_factor import tf_in_session
23 from tests.test_webauthn import HackWebauthnUtil, wan_signin, reg_2_keys
24
25 pytest.importorskip("webauthn")
26
27
28 @pytest.mark.webauthn(webauthn_util_cls=HackWebauthnUtil)
29 @pytest.mark.two_factor()
30 def test_tf_select(app, client, get_message):
31 # Test basic select mechanism when more than one 2FA has been setup
32 wankeys = reg_2_keys(client) # add a webauthn 2FA key (authenticates)
33 sms_sender = setup_tf_sms(client)
34 logout(client)
35
36 # since we have 2 2FA methods configured - we should get the tf-select form
37 # also - test that we correctly propagate 'next' all the way through
38 response = client.post(
39 "/login?next=/profile",
40 data=dict(email="[email protected]", password="password"),
41 follow_redirects=True,
42 )
43 assert b"Select Two-Factor Method" in response.data
44 tf_select_url = get_form_action(response)
45 response = client.post(
46 tf_select_url, data=dict(which="webauthn"), follow_redirects=True
47 )
48 assert b"Use a Passkey as a Second Factor" in response.data
49 wan_signin_url = get_form_action(response)
50 assert "/wan-signin?next=/profile" == wan_signin_url
51
52 response = wan_signin(
53 client, "[email protected]", wankeys["secondary"]["signin"], wan_signin_url
54 )
55 assert not tf_in_session(get_existing_session(client))
56 assert b"Profile Page" in response.data
57
58 # now do other 2FA
59 logout(client)
60 response = client.post(
61 "/login",
62 data=dict(email="[email protected]", password="password"),
63 follow_redirects=True,
64 )
65 assert b"Select Two-Factor Method" in response.data
66 response = client.post("/tf-select", data=dict(which="sms"), follow_redirects=True)
67 assert b"Please enter your authentication code generated via: SMS" in response.data
68 code = sms_sender.messages[0].split()[-1]
69 response = client.post("/tf-validate", data=dict(code=code), follow_redirects=True)
70 assert b"Your code has been confirmed" in response.data
71
72 assert not tf_in_session(get_session(response))
73
74 # verify actually logged in
75 response = client.get("/profile", follow_redirects=False)
76 assert response.status_code == 200
77 assert not tf_in_session(get_existing_session(client))
78
79
80 @pytest.mark.webauthn(webauthn_util_cls=HackWebauthnUtil)
81 @pytest.mark.two_factor()
82 def test_tf_select_json(app, client, get_message):
83 # Test basic select mechanism when more than one 2FA has been setup
84 headers = {"Accept": "application/json", "Content-Type": "application/json"}
85 wankeys = reg_2_keys(client) # add a webauthn 2FA key (authenticates)
86 setup_tf_sms(client)
87 logout(client)
88
89 # since we have 2 2FA methods configured - we should get the tf-select form
90 response = client.post(
91 "/login", json=dict(email="[email protected]", password="password")
92 )
93 assert response.json["response"]["tf_required"]
94 choices = response.json["response"]["tf_setup_methods"]
95 assert all(k in choices for k in ["sms", "webauthn"])
96
97 # should get same answer for GET on /tf-select
98 response = client.get("/tf-select", headers=headers)
99 choices = response.json["response"]["tf_setup_methods"]
100 assert all(k in choices for k in ["sms", "webauthn"])
101
102 # use webauthn as the second factor
103 response = client.post("/tf-select", json=dict(which="webauthn"))
104 signin_url = response.json["response"]["tf_signin_url"]
105 response = client.post(signin_url, headers=headers)
106 response_url = f'wan-signin/{response.json["response"]["wan_state"]}'
107 response = client.post(
108 response_url,
109 json=dict(credential=json.dumps(wankeys["secondary"]["signin"])),
110 )
111 assert response.status_code == 200
112 assert not tf_in_session(get_existing_session(client))
113
114 response = client.get("/profile", follow_redirects=False)
115 assert response.status_code == 200
116
117
118 @pytest.mark.two_factor()
119 @pytest.mark.settings(url_prefix="/api")
120 def test_tf_select_auth(app, client, get_message):
121 # /tf-select is an unauthenticated endpoint - make sure only allowable in correct
122 # state.
123 response = client.get("/api/tf-select", follow_redirects=False)
124 assert "/api/login" in response.location