Debian Codegraph @

tests/test_openuri.py

raw
1 # SPDX-License-Identifier: LGPL-2.1-or-later
2 #
3 # This file is formatted with Python Black
4
5 import tests as xdp
6
7 import dbus
8 import pytest
9 import os
10 import tempfile
11 from pathlib import Path
12 from typing import Any
13
14
15 defaults_list = b"""[Default Applications]
16 x-scheme-handler/http=furrfix.desktop;
17 text/plain=furrfix.desktop
18 """
19
20 furrfix_desktop = b"""[Desktop Entry]
21 Version=1.0
22 Name=Furrfix
23 GenericName=Not a Web Browser
24 Comment=Don't Browse the Web
25 Exec=true %u
26 Icon=furrfix
27 Terminal=false
28 Type=Application
29 MimeType=text/plain;text/html;text/xml;application/xhtml+xml;application/vnd.mozilla.xul+xml;text/mml;x-scheme-handler/http;x-scheme-handler/https;x-scheme-handler/xdg-desktop-portal-test;
30 StartupNotify=true
31 Categories=Network;WebBrowser;
32 Keywords=web;browser;internet;
33 """
34
35 mimeinfo_cache = b"""[MIME Cache]
36 application/vnd.mozilla.xul+xml=furrfix.desktop;
37 application/xhtml+xml=furrfix.desktop;
38 text/plain=furrfix.desktop;
39 text/html=furrfix.desktop;
40 text/mml=furrfix.desktop;
41 text/xml=furrfix.desktop;
42 x-scheme-handler/http=furrfix.desktop;
43 x-scheme-handler/https=furrfix.desktop;
44 x-scheme-handler/xdg-desktop-portal-test=furrfix.desktop;
45 """
46
47
48 @pytest.fixture
49 def xdg_data_home_files():
50 return {
51 "applications/defaults.list": defaults_list,
52 "applications/furrfix.desktop": furrfix_desktop,
53 "applications/mimeinfo.cache": mimeinfo_cache,
54 }
55
56
57 @pytest.fixture
58 def required_templates():
59 return {
60 "appchooser": {},
61 "lockdown": {},
62 }
63
64
65 class TestOpenURI:
66 def set_permissions(self, dbus_con, type, permissions):
67 perm_store_intf = xdp.get_permission_store_iface(dbus_con)
68 perm_store_intf.SetPermission(
69 "desktop-used-apps",
70 True,
71 "inhibit",
72 type,
73 permissions,
74 )
75
76 def enable_paranoid_mode(self, dbus_con, type):
77 # turn on paranoid mode to ensure we get a backend call
78 perm_store_intf = xdp.get_permission_store_iface(dbus_con)
79 perm_store_intf.SetValue(
80 "desktop-used-apps",
81 True,
82 type,
83 dbus.Dictionary(
84 {
85 "always-ask": True,
86 },
87 signature="sv",
88 ),
89 )
90
91 def test_version(self, portals, dbus_con):
92 xdp.check_version(dbus_con, "OpenURI", 5)
93
94 def test_http1(self, portals, dbus_con, app_id):
95 openuri_intf = xdp.get_portal_iface(dbus_con, "OpenURI")
96 mock_intf = xdp.get_mock_iface(dbus_con)
97
98 scheme_handler = "x-scheme-handler/http"
99 self.enable_paranoid_mode(dbus_con, scheme_handler)
100
101 uri = "http://www.flatpak.org"
102 writable = False
103 activation_token = "token"
104
105 request = xdp.Request(dbus_con, openuri_intf)
106 options = {
107 "writable": writable,
108 "activation_token": activation_token,
109 }
110 response = request.call(
111 "OpenURI",
112 parent_window="",
113 uri=uri,
114 options=options,
115 )
116
117 assert response
118 assert response.response == 0
119
120 # Check the impl portal was called with the right args
121 method_calls = mock_intf.GetMethodCalls("ChooseApplication")
122 assert len(method_calls) > 0
123 _, args = method_calls[-1]
124 assert args[1] == app_id
125 assert args[2] == "" # parent window
126 assert "furrfix" in args[3]
127 assert args[4]["uri"] == uri
128 assert args[4]["content_type"] == scheme_handler
129 assert args[4]["activation_token"] == activation_token
130
131 def test_http2(self, portals, dbus_con):
132 openuri_intf = xdp.get_portal_iface(dbus_con, "OpenURI")
133 mock_intf = xdp.get_mock_iface(dbus_con)
134
135 scheme_handler = "x-scheme-handler/http"
136 self.set_permissions(dbus_con, scheme_handler, ["furrfix", "3", "3"])
137
138 uri = "http://www.flatpak.org"
139 writable = False
140 activation_token = "token"
141
142 request = xdp.Request(dbus_con, openuri_intf)
143 options = {
144 "writable": writable,
145 "activation_token": activation_token,
146 }
147 response = request.call(
148 "OpenURI",
149 parent_window="",
150 uri=uri,
151 options=options,
152 )
153
154 assert response
155 assert response.response == 0
156
157 # Check the impl portal was not called because the choice thresold
158 # has been reached
159 method_calls = mock_intf.GetMethodCalls("ChooseApplication")
160 assert len(method_calls) == 0
161
162 def test_file(self, portals, dbus_con, app_id):
163 openuri_intf = xdp.get_portal_iface(dbus_con, "OpenURI")
164 mock_intf = xdp.get_mock_iface(dbus_con)
165
166 scheme_handler = "text/plain"
167 self.enable_paranoid_mode(dbus_con, scheme_handler)
168
169 fd, _ = tempfile.mkstemp(prefix="openuri_mock_file_", dir=Path.home())
170 os.write(fd, b"openuri_mock_file")
171
172 writable = False
173 activation_token = "token"
174
175 request = xdp.Request(dbus_con, openuri_intf)
176 options = {
177 "writable": writable,
178 "activation_token": activation_token,
179 }
180 response = request.call(
181 "OpenFile",
182 parent_window="",
183 fd=fd,
184 options=options,
185 )
186
187 assert response
188 assert response.response == 0
189
190 # Check the impl portal was called with the right args
191 method_calls = mock_intf.GetMethodCalls("ChooseApplication")
192 assert len(method_calls) > 0
193 _, args = method_calls[-1]
194 assert args[1] == app_id
195 assert args[2] == "" # parent window
196 assert "furrfix" in args[3]
197 assert args[4]["content_type"] == scheme_handler
198 assert args[4]["activation_token"] == activation_token
199
200 path = args[4]["uri"]
201 assert path.startswith("file:///")
202
203 with open(path[7:]) as file:
204 openuri_file_contents = file.read()
205 assert openuri_file_contents == "openuri_mock_file"
206
207 @pytest.mark.parametrize("template_params", ({"appchooser": {"response": 1}},))
208 def test_cancel(self, portals, dbus_con):
209 openuri_intf = xdp.get_portal_iface(dbus_con, "OpenURI")
210
211 scheme_handler = "x-scheme-handler/http"
212 self.enable_paranoid_mode(dbus_con, scheme_handler)
213
214 uri = "http://www.flatpak.org"
215
216 request = xdp.Request(dbus_con, openuri_intf)
217 options: Any = {}
218 response = request.call(
219 "OpenURI",
220 parent_window="",
221 uri=uri,
222 options=options,
223 )
224
225 assert response
226 assert response.response == 1
227
228 @pytest.mark.parametrize(
229 "template_params", ({"appchooser": {"expect-close": True}},)
230 )
231 def test_close(self, portals, dbus_con, app_id):
232 openuri_intf = xdp.get_portal_iface(dbus_con, "OpenURI")
233 mock_intf = xdp.get_mock_iface(dbus_con)
234
235 scheme_handler = "x-scheme-handler/http"
236 self.enable_paranoid_mode(dbus_con, scheme_handler)
237
238 uri = "http://www.flatpak.org"
239 writable = False
240 activation_token = "token"
241
242 request = xdp.Request(dbus_con, openuri_intf)
243 request.schedule_close(1000)
244 options = {
245 "writable": writable,
246 "activation_token": activation_token,
247 }
248 request.call(
249 "OpenURI",
250 parent_window="",
251 uri=uri,
252 options=options,
253 )
254
255 # Only true if the impl.Request was closed too
256 assert request.closed
257
258 # Check the impl portal was called with the right args
259 method_calls = mock_intf.GetMethodCalls("ChooseApplication")
260 assert len(method_calls) > 0
261 _, args = method_calls[-1]
262 assert args[1] == app_id
263 assert args[2] == "" # parent window
264 assert "furrfix" in args[3]
265 assert args[4]["uri"] == uri
266 assert args[4]["content_type"] == scheme_handler
267 assert args[4]["activation_token"] == activation_token
268
269 @pytest.mark.parametrize(
270 "template_params", ({"lockdown": {"disable-application-handlers": True}},)
271 )
272 def test_lockdown(self, portals, dbus_con, app_id):
273 openuri_intf = xdp.get_portal_iface(dbus_con, "OpenURI")
274
275 scheme_handler = "x-scheme-handler/http"
276 self.enable_paranoid_mode(dbus_con, scheme_handler)
277
278 uri = "http://www.flatpak.org"
279 writable = False
280 activation_token = "token"
281
282 request = xdp.Request(dbus_con, openuri_intf)
283 options = {
284 "writable": writable,
285 "activation_token": activation_token,
286 }
287 with pytest.raises(dbus.exceptions.DBusException) as excinfo:
288 request.call(
289 "OpenURI",
290 parent_window="",
291 uri=uri,
292 options=options,
293 )
294 assert (
295 excinfo.value.get_dbus_name() == "org.freedesktop.portal.Error.NotAllowed"
296 )
297
298 def test_dir(self, portals, dbus_con, app_id):
299 openuri_intf = xdp.get_portal_iface(dbus_con, "OpenURI")
300 mock_intf = xdp.get_mock_iface(dbus_con)
301
302 scheme_handler = "inode/directory"
303 self.enable_paranoid_mode(dbus_con, scheme_handler)
304
305 fd, file_path = tempfile.mkstemp(prefix="openuri_mock_file_", dir=Path.home())
306 os.write(fd, b"openuri_mock_file")
307
308 activation_token = "token"
309
310 request = xdp.Request(dbus_con, openuri_intf)
311 options = {
312 "activation_token": activation_token,
313 }
314 response = request.call(
315 "OpenDirectory",
316 parent_window="",
317 fd=fd,
318 options=options,
319 )
320
321 assert response
322 assert response.response == 0
323
324 # Check the appchooser portal got called to open the containing dir
325 method_calls = mock_intf.GetMethodCalls("ChooseApplication")
326 assert len(method_calls) > 0
327 _, args = method_calls[-1]
328 assert args[1] == app_id
329 assert args[2] == "" # parent window
330 assert args[4]["content_type"] == scheme_handler
331 assert args[4]["activation_token"] == activation_token
332
333 path = args[4]["uri"]
334 assert path.startswith("file:///")
335
336 assert Path(path[7:]) == Path(file_path).parent
337
338 def test_scheme_supported(self, portals, dbus_con):
339 openuri_intf = xdp.get_portal_iface(dbus_con, "OpenURI")
340
341 supported = openuri_intf.SchemeSupported("https", {})
342 assert supported
343
344 supported = openuri_intf.SchemeSupported("bogusnonexistanthandler", {})
345 assert not supported
346
347 with pytest.raises(dbus.exceptions.DBusException) as excinfo:
348 openuri_intf.SchemeSupported("", {})
349 assert (
350 excinfo.value.get_dbus_name()
351 == "org.freedesktop.portal.Error.InvalidArgument"
352 )