Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix mocks in tests for remote_session_assistant (BugFix) #896

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions checkbox-ng/plainbox/impl/secure/test_sudo_broker.py
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())
10 changes: 7 additions & 3 deletions checkbox-ng/plainbox/impl/session/test_remote_assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,19 @@ def not_allowed(self, *args):
not_allowed(self_mock)

@mock.patch.object(SessionAssistant, "__init__")
@mock.patch("plainbox.impl.secure.sudo_broker.is_passwordless_sudo")
@mock.patch("plainbox.impl.session.remote_assistant.is_passwordless_sudo")
def test__reset_sa(self, is_passwordless_sudo_mock, init_mock):
init_mock.return_value = None
# RSA constructor calls _reset_sa, which in turns creates a new SA
rsa = remote_assistant.RemoteSessionAssistant(lambda: None)
self.assertEqual(init_mock.call_count, 1)

@mock.patch("plainbox.impl.session.remote_assistant.guess_normal_user")
@mock.patch("fnmatch.filter")
def test_start_session_with_launcher(self, mock_filter):
def test_start_session_with_launcher(self, mock_filter, mock_gnu):
# the real tp is referenced later on by it's second field
mock_filter.return_value = [("tp", "tp")]
mock_gnu.return_value = "user"
extra_cfg = dict()
extra_cfg["launcher"] = "test_launcher"
rsa = mock.Mock()
Expand All @@ -82,10 +84,12 @@ def test_start_session_with_launcher(self, mock_filter):
)
self.assertEqual(tps[0][0][1], "tp")

@mock.patch("plainbox.impl.session.remote_assistant.guess_normal_user")
@mock.patch("fnmatch.filter")
def test_start_session_without_launcher(self, mock_filter):
def test_start_session_without_launcher(self, mock_filter, mock_gnu):
# the real tp is referenced later on by it's second field
mock_filter.return_value = [("tp", "tp")]
mock_gnu.return_value = "user"
extra_cfg = dict()
extra_cfg["launcher"] = "test_launcher"
rsa = mock.Mock()
Expand Down
Loading