forked from kalkin/qubes-desktop-linux-manager
-
Notifications
You must be signed in to change notification settings - Fork 49
devices widget: open file manager on dispvm block attach #300
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
Open
Sahilll10
wants to merge
1
commit into
QubesOS:main
Choose a base branch
from
Sahilll10:feature/auto-open-filemanager-dispvm-block-attach
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
Empty file.
This file contains hidden or 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,158 @@ | ||
| # -*- encoding: utf8 -*- | ||
| # | ||
| # The Qubes OS Project, http://www.qubes-os.org | ||
| # | ||
| # Copyright (C) 2026 Sahil Kumar | ||
| # | ||
| # This program is free software; you can redistribute it and/or modify | ||
| # it under the terms of the GNU Lesser General Public License as published by | ||
| # the Free Software Foundation; either version 2.1 of the License, or | ||
| # (at your option) any later version. | ||
| # pylint: disable=redefined-outer-name | ||
| import asyncio | ||
| import pytest | ||
| from unittest.mock import patch, Mock | ||
| from qubesadmin import exc | ||
| from qubesadmin.tests.mock_app import MockQubesComplete | ||
| from qui.devices.actionable_widgets import ( | ||
| AttachDisposableWidget, | ||
| DetachAndAttachDisposableWidget, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_qapp(): | ||
| app = MockQubesComplete() | ||
| return app | ||
|
|
||
|
|
||
| def make_mock_device(device_class="block"): | ||
| device = Mock() | ||
| device.device_class = device_class | ||
| device.interfaces = "" | ||
| device.is_valid_for_vm = Mock(return_value=True) | ||
| device.attach_to_vm = Mock() | ||
| device.detach_from_vm = Mock() | ||
| return device | ||
|
|
||
|
|
||
| def make_mock_vm(qubes_app): | ||
| vm = Mock() | ||
| vm.icon_name = "appvm-green" | ||
| vm.name = "test-vm" | ||
| vm.vm_object = qubes_app._qubes["test-vm"] # pylint: disable=protected-access | ||
| return vm | ||
|
|
||
|
|
||
| class TestAttachDisposableWidget: | ||
|
|
||
| def test_block_device_opens_file_manager(self, mock_qapp): | ||
| """run_service is called for block device attach to dispvm""" | ||
| mock_vm = make_mock_vm(mock_qapp) | ||
| mock_device = make_mock_device("block") | ||
| mock_dispvm = Mock() | ||
| mock_dispvm.name = "disp123" | ||
| mock_dispvm.log = Mock() | ||
| mock_dispvm.devices_denied = "" | ||
|
|
||
| with patch("qubesadmin.vm.DispVM.from_appvm", return_value=mock_dispvm): | ||
| widget = AttachDisposableWidget(mock_vm, mock_device) | ||
| asyncio.run(widget.widget_action()) | ||
|
|
||
| mock_dispvm.run_service.assert_called_once_with( | ||
| "qubes.StartApp+qubes-open-file-manager", | ||
| wait=False, | ||
| ) | ||
|
|
||
| def test_non_block_device_no_file_manager(self, mock_qapp): | ||
| """run_service is NOT called for non-block device""" | ||
| mock_vm = make_mock_vm(mock_qapp) | ||
| mock_device = make_mock_device("usb") | ||
| mock_dispvm = Mock() | ||
| mock_dispvm.name = "disp123" | ||
| mock_dispvm.devices_denied = "" | ||
|
|
||
| with patch("qubesadmin.vm.DispVM.from_appvm", return_value=mock_dispvm): | ||
| widget = AttachDisposableWidget(mock_vm, mock_device) | ||
| asyncio.run(widget.widget_action()) | ||
|
|
||
| mock_dispvm.run_service.assert_not_called() | ||
|
|
||
| def test_file_manager_exception_logged(self, mock_qapp): | ||
| """QubesException from run_service is logged via dispvm.log""" | ||
| mock_vm = make_mock_vm(mock_qapp) | ||
| mock_device = make_mock_device("block") | ||
| mock_dispvm = Mock() | ||
| mock_dispvm.name = "disp123" | ||
| mock_dispvm.log = Mock() | ||
| mock_dispvm.devices_denied = "" | ||
| mock_dispvm.run_service.side_effect = exc.QubesException("failed") | ||
|
|
||
| with patch("qubesadmin.vm.DispVM.from_appvm", return_value=mock_dispvm): | ||
| widget = AttachDisposableWidget(mock_vm, mock_device) | ||
| asyncio.run(widget.widget_action()) | ||
|
|
||
| mock_dispvm.log.exception.assert_called_once() | ||
|
|
||
| def test_feature_flag_disables_file_manager(self, mock_qapp): | ||
| """run_service is NOT called when feature flag is disabled""" | ||
| mock_vm = make_mock_vm(mock_qapp) | ||
| mock_vm.vm_object.features["device-open-file-manager-on-attach"] = "" | ||
| mock_device = make_mock_device("block") | ||
| mock_dispvm = Mock() | ||
| mock_dispvm.name = "disp123" | ||
| mock_dispvm.devices_denied = "" | ||
| with patch("qubesadmin.vm.DispVM.from_appvm", return_value=mock_dispvm): | ||
| widget = AttachDisposableWidget(mock_vm, mock_device) | ||
| asyncio.run(widget.widget_action()) | ||
| mock_dispvm.run_service.assert_not_called() | ||
|
|
||
|
|
||
| class TestDetachAndAttachDisposableWidget: | ||
|
|
||
| def test_block_device_opens_file_manager(self, mock_qapp): | ||
| """run_service is called for block device on detach+attach""" | ||
| mock_vm = make_mock_vm(mock_qapp) | ||
| mock_device = make_mock_device("block") | ||
| mock_dispvm = Mock() | ||
| mock_dispvm.name = "disp456" | ||
| mock_dispvm.log = Mock() | ||
| mock_dispvm.devices_denied = "" | ||
|
|
||
| with patch("qubesadmin.vm.DispVM.from_appvm", return_value=mock_dispvm): | ||
| widget = DetachAndAttachDisposableWidget(mock_vm, mock_device) | ||
| asyncio.run(widget.widget_action()) | ||
|
|
||
| mock_dispvm.run_service.assert_called_once_with( | ||
| "qubes.StartApp+qubes-open-file-manager", | ||
| wait=False, | ||
| ) | ||
|
|
||
| def test_file_manager_exception_logged(self, mock_qapp): | ||
| """QubesException from run_service is logged via dispvm.log""" | ||
| mock_vm = make_mock_vm(mock_qapp) | ||
| mock_device = make_mock_device("block") | ||
| mock_dispvm = Mock() | ||
| mock_dispvm.name = "disp456" | ||
| mock_dispvm.log = Mock() | ||
| mock_dispvm.devices_denied = "" | ||
| mock_dispvm.run_service.side_effect = exc.QubesException("failed") | ||
|
|
||
| with patch("qubesadmin.vm.DispVM.from_appvm", return_value=mock_dispvm): | ||
| widget = DetachAndAttachDisposableWidget(mock_vm, mock_device) | ||
| asyncio.run(widget.widget_action()) | ||
|
|
||
| mock_dispvm.log.exception.assert_called_once() | ||
|
|
||
| def test_feature_flag_disables_file_manager(self, mock_qapp): | ||
| """run_service is NOT called when feature flag is disabled""" | ||
| mock_vm = make_mock_vm(mock_qapp) | ||
| mock_vm.vm_object.features["device-open-file-manager-on-attach"] = "" | ||
| mock_device = make_mock_device("block") | ||
| mock_dispvm = Mock() | ||
| mock_dispvm.name = "disp456" | ||
| mock_dispvm.devices_denied = "" | ||
| with patch("qubesadmin.vm.DispVM.from_appvm", return_value=mock_dispvm): | ||
| widget = DetachAndAttachDisposableWidget(mock_vm, mock_device) | ||
| asyncio.run(widget.widget_action()) | ||
| mock_dispvm.run_service.assert_not_called() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, I don't think this is 100% wrong - it definitely works - but a more future-resilient way is using the existing mocks and overwriting only the method you want to verify. E.g. here you can use any of the existing mock vms in the MockQubesComplete object and later when you want to check that run_service was called, just patch the run_service object. That approach should be more resilient to later changes in the code, while your approach means that potentially if some part of the code changes and e.g. asks for more VM properties, this mock has to be re-done.