From 97d65864623b308021d9a1a628e962e8d1403998 Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Sun, 17 Jul 2016 21:12:00 +0200 Subject: [PATCH] Fixed another part of the permission denied error --- openlp/core/ui/servicemanager.py | 2 +- .../openlp_core_ui/test_servicemanager.py | 31 ++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/openlp/core/ui/servicemanager.py b/openlp/core/ui/servicemanager.py index 7c01b4191..578217ea6 100644 --- a/openlp/core/ui/servicemanager.py +++ b/openlp/core/ui/servicemanager.py @@ -598,7 +598,7 @@ class ServiceManager(OpenLPMixin, RegistryMixin, QtWidgets.QWidget, Ui_ServiceMa if success: try: shutil.copy(temp_file_name, path_file_name) - except shutil.Error: + except (shutil.Error, PermissionError): return self.save_file_as() except OSError as ose: QtWidgets.QMessageBox.critical(self, translate('OpenLP.ServiceManager', 'Error Saving File'), diff --git a/tests/functional/openlp_core_ui/test_servicemanager.py b/tests/functional/openlp_core_ui/test_servicemanager.py index 1697eb1f6..5a337d786 100644 --- a/tests/functional/openlp_core_ui/test_servicemanager.py +++ b/tests/functional/openlp_core_ui/test_servicemanager.py @@ -625,7 +625,7 @@ class TestServiceManager(TestCase): @patch(u'openlp.core.ui.servicemanager.shutil.copy') @patch(u'openlp.core.ui.servicemanager.zipfile') @patch(u'openlp.core.ui.servicemanager.ServiceManager.save_file_as') - def test_save_raises_permission_error(self, mocked_save_file_as, mocked_zipfile, mocked_shutil_copy): + def test_save_file_raises_permission_error(self, mocked_save_file_as, mocked_zipfile, mocked_shutil_copy): """ Test that when a PermissionError is raised when trying to save a file, it is handled correctly """ @@ -639,6 +639,35 @@ class TestServiceManager(TestCase): service_manager._save_lite = False service_manager.service_items = [] service_manager.service_theme = 'Default' + service_manager.service_manager_list = MagicMock() + mocked_save_file_as.return_value = True + mocked_zipfile.ZipFile.return_value = MagicMock() + mocked_shutil_copy.side_effect = PermissionError + + # WHEN: The service is saved and a PermissionError is thrown + result = service_manager.save_file() + + # THEN: The "save_as" method is called to save the service + self.assertTrue(result) + mocked_save_file_as.assert_called_with() + + @patch(u'openlp.core.ui.servicemanager.shutil.copy') + @patch(u'openlp.core.ui.servicemanager.zipfile') + @patch(u'openlp.core.ui.servicemanager.ServiceManager.save_file_as') + def test_save_local_file_raises_permission_error(self, mocked_save_file_as, mocked_zipfile, mocked_shutil_copy): + """ + Test that when a PermissionError is raised when trying to save a local file, it is handled correctly + """ + # GIVEN: A service manager, a service to save + mocked_main_window = MagicMock() + mocked_main_window.service_manager_settings_section = 'servicemanager' + Registry().register('main_window', mocked_main_window) + Registry().register('application', MagicMock()) + service_manager = ServiceManager(None) + service_manager._file_name = os.path.join('temp', 'filename.osz') + service_manager._save_lite = False + service_manager.service_items = [] + service_manager.service_theme = 'Default' mocked_save_file_as.return_value = True mocked_zipfile.ZipFile.return_value = MagicMock() mocked_shutil_copy.side_effect = PermissionError