From f76e030ac97b1c5feae18a2c320e81c3aed5ba12 Mon Sep 17 00:00:00 2001 From: Tim Bentley Date: Sat, 4 Mar 2017 17:32:47 +0000 Subject: [PATCH] more cleanups --- openlp/plugins/remotes/remoteplugin.py | 1 - .../openlp_plugins/remotes/test_deploy.py | 115 ++++++++++++++++++ .../openlp_plugins/remotes/test_deploy.py | 50 +++++++- 3 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 tests/functional/openlp_plugins/remotes/test_deploy.py rename openlp/plugins/remotes/lib/__init__.py => tests/interfaces/openlp_plugins/remotes/test_deploy.py (52%) diff --git a/openlp/plugins/remotes/remoteplugin.py b/openlp/plugins/remotes/remoteplugin.py index d9304241c..ef27bfd0b 100644 --- a/openlp/plugins/remotes/remoteplugin.py +++ b/openlp/plugins/remotes/remoteplugin.py @@ -27,7 +27,6 @@ from openlp.core.api.http import register_endpoint from openlp.core.common import AppLocation, OpenLPMixin, check_directory_exists from openlp.core.common.httputils import get_web_page from openlp.core.lib import Plugin, StringContent, translate, build_icon -from openlp.plugins.remotes.lib import RemotesTab from openlp.plugins.remotes.endpoint import remote_endpoint log = logging.getLogger(__name__) 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..e6893b591 --- /dev/null +++ b/tests/functional/openlp_plugins/remotes/test_deploy.py @@ -0,0 +1,115 @@ +# -*- coding: utf-8 -*- +# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4 + +############################################################################### +# OpenLP - Open Source Lyrics Projection # +# --------------------------------------------------------------------------- # +# Copyright (c) 2008-2016 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 tempfile import mkdtemp +from unittest import TestCase + +from openlp.plugins.remotes.deploy import check_for_previous_deployment, deploy_zipfile + +from tests.functional import patch + + +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() + + @patch('openlp.plugins.remotes.deploy.open') + def test_deploy_zipfile(self, mocked_open): + """ + 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", "deploy.zip") + # WHEN: I process the zipfile + deploy_zipfile(zip_file, self.app_root) + + # THEN test the see if marker is present and has not been created + self.assertEqual(mocked_open.call_count, 46, "We should write 46 files") diff --git a/openlp/plugins/remotes/lib/__init__.py b/tests/interfaces/openlp_plugins/remotes/test_deploy.py similarity index 52% rename from openlp/plugins/remotes/lib/__init__.py rename to tests/interfaces/openlp_plugins/remotes/test_deploy.py index c458210a3..8737c6a82 100644 --- a/openlp/plugins/remotes/lib/__init__.py +++ b/tests/interfaces/openlp_plugins/remotes/test_deploy.py @@ -20,4 +20,52 @@ # Temple Place, Suite 330, Boston, MA 02111-1307 USA # ############################################################################### -from .remotestab import RemotesTab +import os +import shutil + +from tempfile import mkdtemp +from unittest import TestCase + +from openlp.core.common.httputils import url_get_file, get_web_page + +from tests.functional import MagicMock + + +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) + + def test_download_and_check_size(self): + """ + Remote Deploy tests - Test hosted sites file matches the config file + """ + # GIVEN: a hosted configuration file + user_agent = 'OpenLP/2.4.4' + web = 'https://get.openlp.org/webclient/' + web_config = get_web_page('{host}{name}'.format(host=web, name='download.cfg'), + header=('User-Agent', user_agent)) + sha = web_config.read().decode('utf-8').split()[0] + callback = MagicMock() + callback.was_cancelled = False + f = os.path.join(self.app_root, 'sites.zip') + # WHEN: I download the sites file + # THEN: the file will download and match the sha256 from the config file + url_get_file(callback, web + 'site.zip', f, sha256=sha) + +