~thelinuxguy/openlp/bible-improvements

« back to all changes in this revision

Viewing changes to tests/functional/openlp_core/api/test_deploy.py

  • Committer: Tim Bentley
  • Author(s): Raoul Snyman
  • Date: 2018-01-13 08:17:04 UTC
  • mfrom: (2807.1.5 bug-1742910)
  • Revision ID: tim.bentley@gmail.com-20180113081704-jao5jtaa33qsrizu
Fix bug #1742910 by moving the threads to the application object instead of the main window object.

Add this to your merge proposal:
--------------------------------------------------------------------------------
lp:~raoul-snyman/openlp/bug-1742910 (revision 2810)
https://ci.openlp.io/job/Branch-01-Pull/2418/                          [SUCCESS]
https://ci.openlp.io/job/Branch-02a-Linux-Tests/2319/                  [SUCCESS]
https://ci.openlp.io/job/Branch-02b-macOS-Tests/114/                ...

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
###############################################################################
22
22
from tempfile import mkdtemp
23
23
from unittest import TestCase
24
 
 
25
 
from openlp.core.api.deploy import deploy_zipfile
26
 
from openlp.core.common.path import Path, copyfile
27
 
 
28
 
TEST_PATH = (Path(__file__).parent / '..' / '..' / '..' / 'resources').resolve()
 
24
from unittest.mock import MagicMock, patch
 
25
 
 
26
from openlp.core.api.deploy import deploy_zipfile, download_sha256, download_and_check
 
27
from openlp.core.common.path import Path
 
28
 
 
29
CONFIG_FILE = '2c266badff1e3d140664c50fd1460a2b332b24d5ad8c267fa62e506b5eb6d894  deploy/site.zip\n2017_06_27'
29
30
 
30
31
 
31
32
class TestRemoteDeploy(TestCase):
45
46
        """
46
47
        self.app_root_path.rmtree()
47
48
 
48
 
    def test_deploy_zipfile(self):
 
49
    @patch('openlp.core.api.deploy.ZipFile')
 
50
    def test_deploy_zipfile(self, MockZipFile):
49
51
        """
50
52
        Remote Deploy tests - test the dummy zip file is processed correctly
51
53
        """
52
54
        # GIVEN: A new downloaded zip file
53
 
        zip_path = TEST_PATH / 'remotes' / 'site.zip'
54
 
        app_root_path = self.app_root_path / 'site.zip'
55
 
        copyfile(zip_path, app_root_path)
56
 
 
57
 
        # WHEN: I process the zipfile
58
 
        deploy_zipfile(self.app_root_path, 'site.zip')
59
 
 
60
 
        # THEN: test if www directory has been created
61
 
        assert (self.app_root_path / 'www').is_dir(), 'We should have a www directory'
 
55
        mocked_zipfile = MagicMock()
 
56
        MockZipFile.return_value = mocked_zipfile
 
57
        root_path = Path('/tmp/remotes')
 
58
 
 
59
        # WHEN: deploy_zipfile() is called
 
60
        deploy_zipfile(root_path, 'site.zip')
 
61
 
 
62
        # THEN: the zip file should have been extracted to the right location
 
63
        MockZipFile.assert_called_once_with('/tmp/remotes/site.zip')
 
64
        mocked_zipfile.extractall.assert_called_once_with('/tmp/remotes')
 
65
 
 
66
    @patch('openlp.core.api.deploy.Registry')
 
67
    @patch('openlp.core.api.deploy.get_web_page')
 
68
    def test_download_sha256_connection_error(self, mocked_get_web_page, MockRegistry):
 
69
        """
 
70
        Test that if a ConnectionError occurs while downloading a sha256 False is returned
 
71
        """
 
72
        # GIVEN: A bunch of mocks
 
73
        MockRegistry.return_value.get.return_value.applicationVersion.return_value = '1.0'
 
74
        mocked_get_web_page.side_effect = ConnectionError()
 
75
 
 
76
        # WHEN: download_sha256() is called
 
77
        result = download_sha256()
 
78
 
 
79
        # THEN: The result should be False
 
80
        assert result is False, 'download_sha256() should return False when encountering ConnectionError'
 
81
 
 
82
    @patch('openlp.core.api.deploy.Registry')
 
83
    @patch('openlp.core.api.deploy.get_web_page')
 
84
    def test_download_sha256_no_config(self, mocked_get_web_page, MockRegistry):
 
85
        """
 
86
        Test that if there's no config when downloading a sha256 None is returned
 
87
        """
 
88
        # GIVEN: A bunch of mocks
 
89
        MockRegistry.return_value.get.return_value.applicationVersion.return_value = '1.0'
 
90
        mocked_get_web_page.return_value = None
 
91
 
 
92
        # WHEN: download_sha256() is called
 
93
        result = download_sha256()
 
94
 
 
95
        # THEN: The result should be Nonw
 
96
        assert result is None, 'download_sha256() should return None when there is a problem downloading the page'
 
97
 
 
98
    @patch('openlp.core.api.deploy.Registry')
 
99
    @patch('openlp.core.api.deploy.get_web_page')
 
100
    def test_download_sha256(self, mocked_get_web_page, MockRegistry):
 
101
        """
 
102
        Test that the sha256 and the version are returned
 
103
        """
 
104
        # GIVEN: A bunch of mocks
 
105
        MockRegistry.return_value.get.return_value.applicationVersion.return_value = '1.0'
 
106
        mocked_get_web_page.return_value = CONFIG_FILE
 
107
 
 
108
        # WHEN: download_sha256() is called
 
109
        result = download_sha256()
 
110
 
 
111
        # THEN: The result should be Nonw
 
112
        assert result == ('2c266badff1e3d140664c50fd1460a2b332b24d5ad8c267fa62e506b5eb6d894', '2017_06_27'), \
 
113
            'download_sha256() should return a tuple of sha256 and version'
 
114
 
 
115
    @patch('openlp.core.api.deploy.Registry')
 
116
    @patch('openlp.core.api.deploy.download_sha256')
 
117
    @patch('openlp.core.api.deploy.get_url_file_size')
 
118
    @patch('openlp.core.api.deploy.download_file')
 
119
    @patch('openlp.core.api.deploy.AppLocation.get_section_data_path')
 
120
    @patch('openlp.core.api.deploy.deploy_zipfile')
 
121
    def test_download_and_check(self, mocked_deploy_zipfile, mocked_get_data_path, mocked_download_file,
 
122
                                mocked_get_url_file_size, mocked_download_sha256, MockRegistry):
 
123
        # GIVEN: A bunch of mocks
 
124
        mocked_get_data_path.return_value = Path('/tmp/remotes')
 
125
        mocked_download_file.return_value = True
 
126
        mocked_get_url_file_size.return_value = 5
 
127
        mocked_download_sha256.return_value = ('asdfgh', '0.1')
 
128
        MockRegistry.return_value.get.return_value.applicationVersion.return_value = '1.0'
 
129
        mocked_callback = MagicMock()
 
130
 
 
131
        # WHEN: download_and_check() is called
 
132
        download_and_check(mocked_callback)
 
133
 
 
134
        # THEN: The correct things should have been done
 
135
        mocked_download_sha256.assert_called_once_with()
 
136
        mocked_get_url_file_size.assert_called_once_with('https://get.openlp.org/webclient/site.zip')
 
137
        mocked_callback.setRange.assert_called_once_with(0, 5)
 
138
        mocked_download_file.assert_called_once_with(mocked_callback, 'https://get.openlp.org/webclient/site.zip',
 
139
                                                     Path('/tmp/remotes/site.zip'), sha256='asdfgh')
 
140
        mocked_deploy_zipfile.assert_called_once_with(Path('/tmp/remotes'), 'site.zip')