-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix mocks in tests for remote_session_assistant (BugFix) (#896)
* fix mocks in tests for remote_session_assistant * add unittests for is_passwordless_sudo
- Loading branch information
Showing
2 changed files
with
55 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# This file is part of Checkbox. | ||
# | ||
# Copyright 2023 Canonical Ltd. | ||
# | ||
# Checkbox is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License version 3, | ||
# as published by the Free Software Foundation. | ||
# | ||
# Checkbox is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
from subprocess import CalledProcessError | ||
from unittest import TestCase, mock | ||
|
||
from plainbox.impl.secure.sudo_broker import is_passwordless_sudo | ||
|
||
|
||
class IsPasswordlessSudoTests(TestCase): | ||
@mock.patch("os.geteuid", return_value=0) | ||
@mock.patch("plainbox.impl.secure.sudo_broker.check_call") | ||
def test_root_happy(self, mock_check_call, mock_getuid): | ||
mock_check_call.return_value = 0 | ||
self.assertTrue(is_passwordless_sudo()) | ||
|
||
@mock.patch("os.geteuid", return_value=0) | ||
@mock.patch("plainbox.impl.secure.sudo_broker.check_call") | ||
def test_root_raising(self, mock_check_call, mock_getuid): | ||
mock_check_call.side_effect = OSError | ||
|
||
with self.assertRaises(SystemExit): | ||
is_passwordless_sudo() | ||
|
||
@mock.patch("os.geteuid", return_value=1000) | ||
@mock.patch("plainbox.impl.secure.sudo_broker.check_call") | ||
def test_non_root_happy(self, mock_check_call, mock_getuid): | ||
mock_check_call.return_value = 0 | ||
self.assertTrue(is_passwordless_sudo()) | ||
|
||
@mock.patch("os.geteuid", return_value=1000) | ||
@mock.patch("plainbox.impl.secure.sudo_broker.check_call") | ||
def test_non_root_raising(self, mock_check_call, mock_getuid): | ||
mock_check_call.side_effect = CalledProcessError(1, "oops") | ||
self.assertFalse(is_passwordless_sudo()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters