MCPcopy Create free account
hub / github.com/tiny-pilot/tinypilot / CheckAuthTest

Class CheckAuthTest

app/session_test.py:16–228  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

14
15
16class CheckAuthTest(unittest.TestCase):
17
18 @mock.patch.object(video_service, 'restart', do_nothing)
19 @mock.patch.object(session, '_get_credentials_last_changed')
20 @mock.patch.object(session, 'get_username')
21 @mock.patch.object(db.users.db_connection, 'get')
22 def test_grants_full_access_if_auth_requirement_is_off(
23 self, mock_get_db, mock_username, mock_credentials):
24 with tempfile.NamedTemporaryFile() as temp_file:
25 mock_get_db.return_value = db.store.create_or_open(temp_file.name)
26
27 mock_username.return_value = None
28 mock_credentials.return_value = None
29
30 # The session is valid and satisfies all possible roles.
31 self.assertTrue(session.is_auth_valid())
32 self.assertTrue(
33 session.is_auth_valid(satisfies_role=auth.Role.OPERATOR))
34 self.assertTrue(
35 session.is_auth_valid(satisfies_role=auth.Role.ADMIN))
36
37 @mock.patch.object(video_service, 'restart', do_nothing)
38 @mock.patch.object(session, '_get_credentials_last_changed')
39 @mock.patch.object(session, 'get_username')
40 @mock.patch.object(db.users.db_connection, 'get')
41 def test_grants_access_for_authenticated_admin(self, mock_get_db,
42 mock_username,
43 mock_credentials):
44 with tempfile.NamedTemporaryFile() as temp_file:
45 mock_get_db.return_value = db.store.create_or_open(temp_file.name)
46 auth.register('admin', 'p4ssw0rd', auth.Role.ADMIN)
47 admin = auth.get_account('admin')
48
49 mock_username.return_value = 'admin'
50 mock_credentials.return_value = admin.credentials_last_changed
51
52 # The session is valid and satisfies all possible roles.
53 self.assertTrue(session.is_auth_valid())
54 self.assertTrue(
55 session.is_auth_valid(satisfies_role=auth.Role.OPERATOR))
56 self.assertTrue(
57 session.is_auth_valid(satisfies_role=auth.Role.ADMIN))
58
59 @mock.patch.object(video_service, 'restart', do_nothing)
60 @mock.patch.object(session, '_get_credentials_last_changed')
61 @mock.patch.object(session, 'get_username')
62 @mock.patch.object(db.users.db_connection, 'get')
63 def test_grants_access_for_authenticated_operator(self, mock_get_db,
64 mock_username,
65 mock_credentials):
66 with tempfile.NamedTemporaryFile() as temp_file:
67 mock_get_db.return_value = db.store.create_or_open(temp_file.name)
68 auth.register('admin', 'p4ssw0rd', auth.Role.ADMIN)
69 auth.register('operator', 'p4ssw0rd', auth.Role.OPERATOR)
70 operator = auth.get_account('operator')
71
72 mock_username.return_value = 'operator'
73 mock_credentials.return_value = operator.credentials_last_changed

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected