diff --git a/openlp/plugins/remotes/deploy.py b/openlp/plugins/remotes/deploy.py new file mode 100644 index 000000000..c1447b7a0 --- /dev/null +++ b/openlp/plugins/remotes/deploy.py @@ -0,0 +1,71 @@ +# -*- coding: utf-8 -*- +# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4 + +############################################################################### +# OpenLP - Open Source Lyrics Projection # +# --------------------------------------------------------------------------- # +# Copyright (c) 2008-2017 OpenLP Developers # +# --------------------------------------------------------------------------- # +# This program is free software; you can redistribute it and/or modify it # +# under the terms of the GNU General Public License as published by the Free # +# Software Foundation; version 2 of the License. # +# # +# This program 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 this program; if not, write to the Free Software Foundation, Inc., 59 # +# Temple Place, Suite 330, Boston, MA 02111-1307 USA # +############################################################################### + +import os +import zipfile +import urllib.error + +from openlp.core.common import AppLocation, Registry +from openlp.core.common.httputils import url_get_file, get_web_page + + +def deploy_zipfile(app_root, zip_name): + """ + Process the downloaded zip file and add to the correct directory + + :param zip_name: the zip file to be processed + :param app_root: the directory where the zip get expanded to + + :return: None + """ + zip_file = os.path.join(app_root, zip_name) + web_zip = zipfile.ZipFile(zip_file) + web_zip.extractall(app_root) + + +def check_for_previous_deployment(app_root, create=False): + marker_file = os.path.join(app_root, "marker.txt") + if os.path.isfile(marker_file): + return True + else: + if create: + os.mknod(marker_file) + return False + + +def download_sha256(): + user_agent = 'OpenLP/' + Registry().get('application').applicationVersion() + try: + web_config = get_web_page('{host}{name}'.format(host='https://get.openlp.org/webclient/', name='download.cfg'), + header=('User-Agent', user_agent)) + except (urllib.error.URLError, ConnectionError) as err: + return False + file_bits = web_config.read().decode('utf-8').split() + return file_bits[0], file_bits[2] + + +def download_and_check(callback=None): + sha256, version = download_sha256() + if url_get_file(callback, '{host}{name}'.format(host='https://get.openlp.org/webclient/', name='site.zip'), + os.path.join(AppLocation.get_section_data_path('remotes'), 'site.zip'), + sha256=sha256): + deploy_zipfile(AppLocation.get_section_data_path('remotes'), 'site.zip') diff --git a/tests/functional/openlp_plugins/presentations/test_impresscontroller.py b/tests/functional/openlp_plugins/presentations/test_impresscontroller.py index e93c98d39..d383b16e4 100644 --- a/tests/functional/openlp_plugins/presentations/test_impresscontroller.py +++ b/tests/functional/openlp_plugins/presentations/test_impresscontroller.py @@ -23,7 +23,7 @@ Functional tests to test the Impress class and related methods. """ from unittest import TestCase -from unittest.mock import patch, MagicMock +from unittest.mock import MagicMock import os import shutil from tempfile import mkdtemp diff --git a/tests/functional/openlp_plugins/remotes/test_deploy.py b/tests/functional/openlp_plugins/remotes/test_deploy.py new file mode 100644 index 000000000..1df0406f5 --- /dev/null +++ b/tests/functional/openlp_plugins/remotes/test_deploy.py @@ -0,0 +1,116 @@ +# -*- coding: utf-8 -*- +# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4 + +############################################################################### +# OpenLP - Open Source Lyrics Projection # +# --------------------------------------------------------------------------- # +# Copyright (c) 2008-2017 OpenLP Developers # +# --------------------------------------------------------------------------- # +# This program is free software; you can redistribute it and/or modify it # +# under the terms of the GNU General Public License as published by the Free # +# Software Foundation; version 2 of the License. # +# # +# This program 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 this program; if not, write to the Free Software Foundation, Inc., 59 # +# Temple Place, Suite 330, Boston, MA 02111-1307 USA # +############################################################################### + +import os +import shutil + +from unittest.mock import patch + +from tempfile import mkdtemp +from unittest import TestCase + +from openlp.plugins.remotes.deploy import check_for_previous_deployment, deploy_zipfile + + +TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', 'resources')) + + +class TestRemoteDeploy(TestCase): + """ + Test the Remote plugin deploy functions + """ + + def setUp(self): + """ + Setup for tests + """ + self.app_root = mkdtemp() + + def tearDown(self): + """ + Clean up after tests + """ + shutil.rmtree(self.app_root) + + @patch('openlp.plugins.remotes.deploy.os.path.isfile') + @patch('openlp.plugins.remotes.deploy.os.mknod') + def test_check_for_previous_deployment_false(self, mocked_mknod, mocked_isfile): + """ + Remote Deploy tests - Test when the marker file is missing + """ + # GIVEN: A new setup with no marker file + # WHEN: I check for a deployment which does not create the marker file + mocked_isfile.return_value = False + processed = check_for_previous_deployment(self.app_root) + + # THEN test the see if marker has not been created + self.assertFalse(processed, 'should return False as marker does not exist') + mocked_isfile.assert_called_once_with(os.path.join(self.app_root, 'marker.txt')) + mocked_mknod.assert_not_called() + + @patch('openlp.plugins.remotes.deploy.os.path.isfile') + @patch('openlp.plugins.remotes.deploy.os.mknod') + def test_check_for_previous_deployment_true(self, mocked_mknod, mocked_isfile): + """ + Remote Deploy tests - Test when the marker file is missing + """ + # GIVEN: A new setup with not market file + # WHEN: I check for a deployment which does create the marker file + mocked_isfile.return_value = False + processed = check_for_previous_deployment(self.app_root, True) + + # THEN test the see if marker has been created + marker_file = os.path.join(self.app_root, 'marker.txt') + self.assertFalse(processed, 'should return False as marker does not exist') + mocked_isfile.assert_called_once_with(marker_file) + mocked_mknod.assert_called_once_with(marker_file) + + @patch('openlp.plugins.remotes.deploy.os.path.isfile') + @patch('openlp.plugins.remotes.deploy.os.mknod') + def test_check_for_previous_deployment_true(self, mocked_mknod, mocked_isfile): + """ + Remote Deploy tests - Test when the marker file is present + """ + # GIVEN: A new setup with not market file + # WHEN: I check for a deployment which does not create the marker file + mocked_isfile.return_value = True + processed = check_for_previous_deployment(self.app_root, True) + + # THEN test the see if marker is present and has not been created + marker_file = os.path.join(self.app_root, 'marker.txt') + self.assertTrue(processed, 'should return True as marker does exist') + mocked_isfile.assert_called_once_with(marker_file) + mocked_mknod.assert_not_called() + + def test_deploy_zipfile(self): + """ + Remote Deploy tests - test the dummy zip file is processed correctly + """ + # GIVEN: A new downloaded zip file + zip_file = os.path.join(TEST_PATH, 'remotes', 'site.zip') + app_root = os.path.join(self.app_root, 'site.zip') + shutil.copyfile(zip_file, app_root) + # WHEN: I process the zipfile + deploy_zipfile(self.app_root, 'site.zip') + + # THEN test if www directory has been created + self.assertTrue(os.path.isdir(os.path.join(self.app_root, 'www')), 'We should have a www directory') diff --git a/tests/interfaces/openlp_plugins/remotes/__init__.py b/tests/interfaces/openlp_plugins/remotes/__init__.py new file mode 100644 index 000000000..ea62548f4 --- /dev/null +++ b/tests/interfaces/openlp_plugins/remotes/__init__.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4 + +############################################################################### +# OpenLP - Open Source Lyrics Projection # +# --------------------------------------------------------------------------- # +# Copyright (c) 2008-2017 OpenLP Developers # +# --------------------------------------------------------------------------- # +# This program is free software; you can redistribute it and/or modify it # +# under the terms of the GNU General Public License as published by the Free # +# Software Foundation; version 2 of the License. # +# # +# This program 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 this program; if not, write to the Free Software Foundation, Inc., 59 # +# Temple Place, Suite 330, Boston, MA 02111-1307 USA # +############################################################################### diff --git a/tests/interfaces/openlp_core_api/test_deploy.py b/tests/interfaces/openlp_plugins/remotes/test_deploy.py similarity index 95% rename from tests/interfaces/openlp_core_api/test_deploy.py rename to tests/interfaces/openlp_plugins/remotes/test_deploy.py index 680208a07..874ac21d1 100644 --- a/tests/interfaces/openlp_core_api/test_deploy.py +++ b/tests/interfaces/openlp_plugins/remotes/test_deploy.py @@ -25,12 +25,13 @@ import shutil from tempfile import mkdtemp from unittest import TestCase +from unittest.mock import MagicMock from openlp.core.common import Registry from openlp.core.common.httputils import url_get_file -from openlp.core.api.deploy import download_sha256, download_and_check +from openlp.plugins.remotes.deploy import download_sha256, download_and_check + -from tests.functional import MagicMock from tests.helpers.testmixin import TestMixin @@ -65,7 +66,7 @@ class TestRemoteDeploy(TestCase, TestMixin): """ # GIVEN: a hosted configuration file web = 'https://get.openlp.org/webclient/' - sha = download_sha256() + sha, version = download_sha256() callback = MagicMock() callback.was_cancelled = False f = os.path.join(self.app_root, 'sites.zip')