|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch, MagicMock |
| 3 | +import os |
| 4 | +from lufus.browse_freely import open_url_non_root |
| 5 | + |
| 6 | +class TestBrowseFreely(unittest.TestCase): |
| 7 | + def setUp(self): |
| 8 | + self.url = "https://github.com/Hogjects/Lufus" |
| 9 | + |
| 10 | + @patch("os.geteuid") |
| 11 | + @patch("webbrowser.open") |
| 12 | + def test_open_url_non_root_as_regular_user(self, mock_webbrowser, mock_geteuid): |
| 13 | + # Simulate regular user (UID 1000) |
| 14 | + mock_geteuid.return_value = 1000 |
| 15 | + |
| 16 | + open_url_non_root(self.url) |
| 17 | + |
| 18 | + # Should fallback to standard webbrowser.open |
| 19 | + mock_webbrowser.assert_called_once_with(self.url) |
| 20 | + |
| 21 | + @patch("os.geteuid") |
| 22 | + @patch("os.environ.get") |
| 23 | + @patch("subprocess.Popen") |
| 24 | + @patch("pwd.getpwuid") |
| 25 | + def test_open_url_as_root_via_pkexec(self, mock_getpwuid, mock_popen, mock_env_get, mock_geteuid): |
| 26 | + # Simulate root (UID 0) |
| 27 | + mock_geteuid.return_value = 0 |
| 28 | + |
| 29 | + # Mock environment variables |
| 30 | + def side_effect(key, default=None): |
| 31 | + env = { |
| 32 | + "PKEXEC_UID": "1000", |
| 33 | + "DISPLAY": ":0", |
| 34 | + "XDG_RUNTIME_DIR": "/run/user/1000" |
| 35 | + } |
| 36 | + return env.get(key, default) |
| 37 | + mock_env_get.side_effect = side_effect |
| 38 | + |
| 39 | + # Mock pwd info |
| 40 | + mock_user = MagicMock() |
| 41 | + mock_user.pw_name = "raphael" |
| 42 | + mock_getpwuid.return_value = mock_user |
| 43 | + |
| 44 | + open_url_non_root(self.url) |
| 45 | + |
| 46 | + # Verify subprocess.Popen was called with runuser |
| 47 | + args, kwargs = mock_popen.call_args |
| 48 | + cmd = args[0] |
| 49 | + self.assertEqual(cmd[0], "runuser") |
| 50 | + self.assertEqual(cmd[2], "raphael") |
| 51 | + self.assertIn("xdg-open", cmd) |
| 52 | + self.assertIn(self.url, cmd) |
| 53 | + |
| 54 | + # Verify env passing |
| 55 | + env = kwargs.get("env", {}) |
| 56 | + self.assertEqual(env.get("DISPLAY"), ":0") |
| 57 | + self.assertEqual(env.get("XDG_RUNTIME_DIR"), "/run/user/1000") |
| 58 | + |
| 59 | + @patch("os.geteuid") |
| 60 | + @patch("os.environ.get") |
| 61 | + @patch("subprocess.Popen") |
| 62 | + def test_open_url_as_root_via_sudo(self, mock_popen, mock_env_get, mock_geteuid): |
| 63 | + # Simulate root (UID 0) |
| 64 | + mock_geteuid.return_value = 0 |
| 65 | + |
| 66 | + # Mock environment variables (no PKEXEC_UID, but SUDO_USER) |
| 67 | + def side_effect(key, default=None): |
| 68 | + env = { |
| 69 | + "SUDO_USER": "raphael", |
| 70 | + "DISPLAY": ":0" |
| 71 | + } |
| 72 | + return env.get(key, default) |
| 73 | + mock_env_get.side_effect = side_effect |
| 74 | + |
| 75 | + open_url_non_root(self.url) |
| 76 | + |
| 77 | + # Verify subprocess.Popen was called with runuser |
| 78 | + args, _ = mock_popen.call_args |
| 79 | + cmd = args[0] |
| 80 | + self.assertEqual(cmd[0], "runuser") |
| 81 | + self.assertEqual(cmd[2], "raphael") |
| 82 | + self.assertIn(self.url, cmd) |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + unittest.main() |
0 commit comments