Debian Codegraph @

tests/TestGuiBasic.py

raw
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 # Copyright (c) 2008-2026 Andrew Ziem.
3 #
4 # This work is licensed under the terms of the GNU GPL, version 3 or
5 # later. See the COPYING file in the top-level directory.
6
7 """
8 Test cases for GuiBasic module.
9 """
10
11 import unittest
12 from unittest import mock
13
14 from bleachbit.GtkShim import is_gtk_available
15
16 from tests import common
17
18 HAVE_GTK = is_gtk_available()
19 if HAVE_GTK:
20 from bleachbit import GuiBasic
21 from bleachbit.GuiBasic import open_url
22
23
24 @unittest.skipUnless(HAVE_GTK, 'requires GTK+ module and a display environment')
25 class GuiBasicTestCase(common.BleachbitTestCase):
26 """Test case for module GuiBasic"""
27
28 def test_open_url_rejects_disallowed_scheme(self):
29 """open_url() must refuse any scheme other than http(s)"""
30 urls = ('file:///etc/passwd',
31 'javascript:alert(1)',
32 'myapp://action',
33 'HTTP:evil') # missing slashes: not a valid http(s) URL
34 with mock.patch('webbrowser.open') as mock_open, \
35 mock.patch.object(GuiBasic.Gtk, 'show_uri_on_window') as mock_show_uri, \
36 mock.patch.object(GuiBasic.Gtk, 'show_uri') as mock_show_uri_old, \
37 mock.patch.object(GuiBasic, 'logger') as mock_logger:
38 for url in urls:
39 with self.subTest(url=url):
40 mock_logger.reset_mock()
41 open_url(url, prompt=False)
42 mock_open.assert_not_called()
43 mock_show_uri.assert_not_called()
44 mock_show_uri_old.assert_not_called()
45 mock_logger.error.assert_called_once()
46
47 def test_open_url_accepts_http_https(self):
48 """open_url() proceeds past the scheme check for http(s) URLs"""
49 with mock.patch('webbrowser.open'), \
50 mock.patch.object(GuiBasic.Gtk, 'show_uri_on_window'), \
51 mock.patch.object(GuiBasic.Gtk, 'show_uri'), \
52 mock.patch.object(GuiBasic, 'logger') as mock_logger:
53 open_url('https://example.com', prompt=False)
54 mock_logger.error.assert_not_called()