Debian Codegraph @

tests/test_camera.py

raw
1 # SPDX-License-Identifier: LGPL-2.1-or-later
2 #
3 # This file is formatted with Python Black
4
5 import tests.xdp_utils as xdp
6
7 import dbus
8 import pytest
9
10
11 @pytest.fixture
12 def required_templates():
13 return {
14 "access": {},
15 "lockdown": {},
16 }
17
18
19 class TestCamera:
20 def set_permissions(self, dbus_con, appid, permissions):
21 perm_store_intf = xdp.get_permission_store_iface(dbus_con)
22 perm_store_intf.SetPermission(
23 "devices",
24 True,
25 "camera",
26 appid,
27 permissions,
28 )
29
30 def test_version(self, portals, dbus_con):
31 xdp.check_version(dbus_con, "Camera", 1)
32
33 def test_access(self, portals, dbus_con, xdp_app_info):
34 app_id = xdp_app_info.app_id
35 camera_intf = xdp.get_portal_iface(dbus_con, "Camera")
36 mock_intf = xdp.get_mock_iface(dbus_con)
37
38 request = xdp.Request(dbus_con, camera_intf)
39 response = request.call(
40 "AccessCamera",
41 options={},
42 )
43
44 assert response
45 assert response.response == 0
46
47 # Check the impl portal was called with the right args
48 method_calls = mock_intf.GetMethodCalls("AccessDialog")
49 assert len(method_calls) == 1
50 _, args = method_calls[-1]
51 assert args[1] == app_id
52
53 @pytest.mark.parametrize("template_params", ({"access": {"response": 1}},))
54 def test_access_cancel(self, portals, dbus_con, xdp_app_info):
55 app_id = xdp_app_info.app_id
56 camera_intf = xdp.get_portal_iface(dbus_con, "Camera")
57 mock_intf = xdp.get_mock_iface(dbus_con)
58
59 request = xdp.Request(dbus_con, camera_intf)
60 response = request.call(
61 "AccessCamera",
62 options={},
63 )
64
65 assert response
66 assert response.response == 1
67
68 # Check the impl portal was called with the right args
69 method_calls = mock_intf.GetMethodCalls("AccessDialog")
70 assert len(method_calls) == 1
71 _, args = method_calls[-1]
72 assert args[1] == app_id
73
74 @pytest.mark.parametrize("template_params", ({"access": {"expect-close": True}},))
75 def test_access_close(self, portals, dbus_con, xdp_app_info):
76 app_id = xdp_app_info.app_id
77 camera_intf = xdp.get_portal_iface(dbus_con, "Camera")
78 mock_intf = xdp.get_mock_iface(dbus_con)
79
80 request = xdp.Request(dbus_con, camera_intf)
81 # technically racy, and valgrind is really slow
82 request.schedule_close(3000 if xdp.is_valgrind() else 500)
83 request.call(
84 "AccessCamera",
85 options={},
86 )
87
88 # Only true if the impl.Request was closed too
89 assert request.closed
90
91 # Check the impl portal was called with the right args
92 method_calls = mock_intf.GetMethodCalls("AccessDialog")
93 assert len(method_calls) == 1
94 _, args = method_calls[-1]
95 assert args[1] == app_id
96
97 @pytest.mark.parametrize(
98 "template_params", ({"lockdown": {"disable-camera": True}},)
99 )
100 def test_access_lockdown(self, portals, dbus_con):
101 camera_intf = xdp.get_portal_iface(dbus_con, "Camera")
102 mock_intf = xdp.get_mock_iface(dbus_con)
103
104 request = xdp.Request(dbus_con, camera_intf)
105 with pytest.raises(dbus.exceptions.DBusException) as excinfo:
106 request.call(
107 "AccessCamera",
108 options={},
109 )
110 assert (
111 excinfo.value.get_dbus_name() == "org.freedesktop.portal.Error.NotAllowed"
112 )
113
114 # Check the impl portal was called with the right args
115 method_calls = mock_intf.GetMethodCalls("AccessDialog")
116 assert len(method_calls) == 0
117
118 def test_access_denied(self, portals, dbus_con, xdp_app_info):
119 app_id = xdp_app_info.app_id
120 camera_intf = xdp.get_portal_iface(dbus_con, "Camera")
121 mock_intf = xdp.get_mock_iface(dbus_con)
122
123 self.set_permissions(dbus_con, app_id, ["no"])
124
125 request = xdp.Request(dbus_con, camera_intf)
126 response = request.call(
127 "AccessCamera",
128 options={},
129 )
130
131 assert response
132 assert response.response == 1
133
134 # Check the impl portal was called with the right args
135 method_calls = mock_intf.GetMethodCalls("AccessDialog")
136 assert len(method_calls) == 0