2017-09-13 06:08:38 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-04-13 13:00:22 +00:00
|
|
|
##########################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# ---------------------------------------------------------------------- #
|
2020-01-01 02:53:08 +00:00
|
|
|
# Copyright (c) 2008-2020 OpenLP Developers #
|
2019-04-13 13:00:22 +00:00
|
|
|
# ---------------------------------------------------------------------- #
|
|
|
|
# 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, either version 3 of the License, or #
|
|
|
|
# (at your option) any later version. #
|
|
|
|
# #
|
|
|
|
# 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, see <https://www.gnu.org/licenses/>. #
|
|
|
|
##########################################################################
|
2017-09-13 06:08:38 +00:00
|
|
|
"""
|
|
|
|
Package to test the openlp.core.version package.
|
|
|
|
"""
|
|
|
|
import sys
|
|
|
|
from datetime import date
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
|
|
|
|
from requests.exceptions import ConnectionError
|
|
|
|
|
|
|
|
from openlp.core.version import VersionWorker, check_for_update, get_version, update_check_date
|
|
|
|
|
|
|
|
|
|
|
|
def test_worker_init():
|
2018-06-26 19:44:54 +00:00
|
|
|
"""
|
|
|
|
Test the VersionWorker constructor
|
|
|
|
"""
|
2017-09-13 06:08:38 +00:00
|
|
|
# GIVEN: A last check date and a current version
|
|
|
|
last_check_date = '1970-01-01'
|
|
|
|
current_version = '2.0'
|
|
|
|
|
|
|
|
# WHEN: A worker is created
|
|
|
|
worker = VersionWorker(last_check_date, current_version)
|
|
|
|
|
|
|
|
# THEN: The correct attributes should have been set
|
|
|
|
assert worker.last_check_date == last_check_date
|
|
|
|
assert worker.current_version == current_version
|
|
|
|
|
|
|
|
|
|
|
|
@patch('openlp.core.version.platform')
|
|
|
|
@patch('openlp.core.version.requests')
|
|
|
|
def test_worker_start(mock_requests, mock_platform):
|
2018-06-26 19:44:54 +00:00
|
|
|
"""
|
|
|
|
Test the VersionWorkder.start() method
|
|
|
|
"""
|
2017-09-13 06:08:38 +00:00
|
|
|
# GIVEN: A last check date, current version, and an instance of worker
|
|
|
|
last_check_date = '1970-01-01'
|
|
|
|
current_version = {'full': '2.0', 'version': '2.0', 'build': None}
|
|
|
|
mock_platform.system.return_value = 'Linux'
|
|
|
|
mock_platform.release.return_value = '4.12.0-1-amd64'
|
2018-06-26 19:44:54 +00:00
|
|
|
mock_requests.get.return_value = MagicMock(text='2.4.6', status_code=200)
|
2017-09-13 06:08:38 +00:00
|
|
|
worker = VersionWorker(last_check_date, current_version)
|
|
|
|
|
|
|
|
# WHEN: The worker is run
|
|
|
|
with patch.object(worker, 'new_version') as mock_new_version, \
|
|
|
|
patch.object(worker, 'quit') as mock_quit:
|
|
|
|
worker.start()
|
|
|
|
|
|
|
|
# THEN: The check completes and the signal is emitted
|
2017-12-29 10:38:51 +00:00
|
|
|
expected_download_url = 'https://www.openlp.org/files/version.txt'
|
2017-09-13 06:08:38 +00:00
|
|
|
expected_headers = {'User-Agent': 'OpenLP/2.0 Linux/4.12.0-1-amd64; '}
|
|
|
|
mock_requests.get.assert_called_once_with(expected_download_url, headers=expected_headers)
|
|
|
|
mock_new_version.emit.assert_called_once_with('2.4.6')
|
|
|
|
mock_quit.emit.assert_called_once_with()
|
|
|
|
|
|
|
|
|
2018-06-26 19:44:54 +00:00
|
|
|
@patch('openlp.core.version.platform')
|
|
|
|
@patch('openlp.core.version.requests')
|
|
|
|
def test_worker_start_fail(mock_requests, mock_platform):
|
|
|
|
"""
|
|
|
|
Test the Version Workder.start() method with no response
|
|
|
|
"""
|
|
|
|
# GIVEN: A last check date, current version, and an instance of worker
|
|
|
|
last_check_date = '1970-01-01'
|
|
|
|
current_version = {'full': '2.0', 'version': '2.0', 'build': None}
|
|
|
|
mock_platform.system.return_value = 'Linux'
|
|
|
|
mock_platform.release.return_value = '4.12.0-1-amd64'
|
|
|
|
mock_requests.get.return_value = MagicMock(text='2.4.6', status_code=500)
|
|
|
|
worker = VersionWorker(last_check_date, current_version)
|
|
|
|
|
|
|
|
# WHEN: The worker is run
|
|
|
|
with patch.object(worker, 'new_version') as mock_new_version, \
|
|
|
|
patch.object(worker, 'quit') as mock_quit:
|
|
|
|
worker.start()
|
|
|
|
|
|
|
|
# THEN: The check completes and the signal is emitted
|
|
|
|
expected_download_url = 'https://www.openlp.org/files/version.txt'
|
|
|
|
expected_headers = {'User-Agent': 'OpenLP/2.0 Linux/4.12.0-1-amd64; '}
|
|
|
|
mock_requests.get.assert_called_once_with(expected_download_url, headers=expected_headers)
|
|
|
|
mock_new_version.emit.assert_not_called()
|
|
|
|
mock_quit.emit.assert_called_once_with()
|
|
|
|
|
|
|
|
|
2017-09-13 06:08:38 +00:00
|
|
|
@patch('openlp.core.version.platform')
|
|
|
|
@patch('openlp.core.version.requests')
|
|
|
|
def test_worker_start_dev_version(mock_requests, mock_platform):
|
2018-06-26 19:44:54 +00:00
|
|
|
"""
|
|
|
|
Test the VersionWorkder.start() method for dev versions
|
|
|
|
"""
|
2017-09-13 06:08:38 +00:00
|
|
|
# GIVEN: A last check date, current version, and an instance of worker
|
|
|
|
last_check_date = '1970-01-01'
|
|
|
|
current_version = {'full': '2.1.3', 'version': '2.1.3', 'build': None}
|
|
|
|
mock_platform.system.return_value = 'Linux'
|
|
|
|
mock_platform.release.return_value = '4.12.0-1-amd64'
|
2018-06-26 19:44:54 +00:00
|
|
|
mock_requests.get.return_value = MagicMock(text='2.4.6', status_code=200)
|
2017-09-13 06:08:38 +00:00
|
|
|
worker = VersionWorker(last_check_date, current_version)
|
|
|
|
|
|
|
|
# WHEN: The worker is run
|
|
|
|
with patch.object(worker, 'new_version') as mock_new_version, \
|
|
|
|
patch.object(worker, 'quit') as mock_quit:
|
|
|
|
worker.start()
|
|
|
|
|
|
|
|
# THEN: The check completes and the signal is emitted
|
2017-12-29 10:38:51 +00:00
|
|
|
expected_download_url = 'https://www.openlp.org/files/dev_version.txt'
|
2017-09-13 06:08:38 +00:00
|
|
|
expected_headers = {'User-Agent': 'OpenLP/2.1.3 Linux/4.12.0-1-amd64; '}
|
|
|
|
mock_requests.get.assert_called_once_with(expected_download_url, headers=expected_headers)
|
|
|
|
mock_new_version.emit.assert_called_once_with('2.4.6')
|
|
|
|
mock_quit.emit.assert_called_once_with()
|
|
|
|
|
|
|
|
|
|
|
|
@patch('openlp.core.version.platform')
|
|
|
|
@patch('openlp.core.version.requests')
|
|
|
|
def test_worker_start_nightly_version(mock_requests, mock_platform):
|
2018-06-26 19:44:54 +00:00
|
|
|
"""
|
|
|
|
Test the VersionWorkder.start() method for nightlies
|
|
|
|
"""
|
2017-09-13 06:08:38 +00:00
|
|
|
# GIVEN: A last check date, current version, and an instance of worker
|
|
|
|
last_check_date = '1970-01-01'
|
|
|
|
current_version = {'full': '2.1-bzr2345', 'version': '2.1', 'build': '2345'}
|
|
|
|
mock_platform.system.return_value = 'Linux'
|
|
|
|
mock_platform.release.return_value = '4.12.0-1-amd64'
|
2018-06-26 19:44:54 +00:00
|
|
|
mock_requests.get.return_value = MagicMock(text='2.4.6', status_code=200)
|
2017-09-13 06:08:38 +00:00
|
|
|
worker = VersionWorker(last_check_date, current_version)
|
|
|
|
|
|
|
|
# WHEN: The worker is run
|
|
|
|
with patch.object(worker, 'new_version') as mock_new_version, \
|
|
|
|
patch.object(worker, 'quit') as mock_quit:
|
|
|
|
worker.start()
|
|
|
|
|
|
|
|
# THEN: The check completes and the signal is emitted
|
2017-12-29 10:38:51 +00:00
|
|
|
expected_download_url = 'https://www.openlp.org/files/nightly_version.txt'
|
2017-09-13 06:08:38 +00:00
|
|
|
expected_headers = {'User-Agent': 'OpenLP/2.1-bzr2345 Linux/4.12.0-1-amd64; '}
|
|
|
|
mock_requests.get.assert_called_once_with(expected_download_url, headers=expected_headers)
|
|
|
|
mock_new_version.emit.assert_called_once_with('2.4.6')
|
|
|
|
mock_quit.emit.assert_called_once_with()
|
|
|
|
|
|
|
|
|
2017-12-29 10:38:51 +00:00
|
|
|
@patch('openlp.core.version.platform')
|
|
|
|
@patch('openlp.core.version.requests')
|
|
|
|
def test_worker_empty_response(mock_requests, mock_platform):
|
2018-06-26 19:44:54 +00:00
|
|
|
"""
|
|
|
|
Test the VersionWorkder.start() method for empty responses
|
|
|
|
"""
|
2017-12-29 10:38:51 +00:00
|
|
|
# GIVEN: A last check date, current version, and an instance of worker
|
|
|
|
last_check_date = '1970-01-01'
|
|
|
|
current_version = {'full': '2.1-bzr2345', 'version': '2.1', 'build': '2345'}
|
|
|
|
mock_platform.system.return_value = 'Linux'
|
|
|
|
mock_platform.release.return_value = '4.12.0-1-amd64'
|
|
|
|
mock_requests.get.return_value = MagicMock(text='\n')
|
|
|
|
worker = VersionWorker(last_check_date, current_version)
|
|
|
|
|
|
|
|
# WHEN: The worker is run
|
|
|
|
with patch.object(worker, 'new_version') as mock_new_version, \
|
|
|
|
patch.object(worker, 'quit') as mock_quit:
|
|
|
|
worker.start()
|
|
|
|
|
|
|
|
# THEN: The check completes and the signal is emitted
|
|
|
|
expected_download_url = 'https://www.openlp.org/files/nightly_version.txt'
|
|
|
|
expected_headers = {'User-Agent': 'OpenLP/2.1-bzr2345 Linux/4.12.0-1-amd64; '}
|
|
|
|
mock_requests.get.assert_called_once_with(expected_download_url, headers=expected_headers)
|
|
|
|
assert mock_new_version.emit.call_count == 0
|
|
|
|
mock_quit.emit.assert_called_once_with()
|
|
|
|
|
|
|
|
|
2017-09-13 06:08:38 +00:00
|
|
|
@patch('openlp.core.version.platform')
|
|
|
|
@patch('openlp.core.version.requests')
|
|
|
|
def test_worker_start_connection_error(mock_requests, mock_platform):
|
2018-06-26 19:44:54 +00:00
|
|
|
"""
|
|
|
|
Test the VersionWorkder.start() method when a ConnectionError happens
|
|
|
|
"""
|
2017-09-13 06:08:38 +00:00
|
|
|
# GIVEN: A last check date, current version, and an instance of worker
|
|
|
|
last_check_date = '1970-01-01'
|
|
|
|
current_version = {'full': '2.0', 'version': '2.0', 'build': None}
|
|
|
|
mock_platform.system.return_value = 'Linux'
|
|
|
|
mock_platform.release.return_value = '4.12.0-1-amd64'
|
|
|
|
mock_requests.get.side_effect = ConnectionError('Could not connect')
|
|
|
|
worker = VersionWorker(last_check_date, current_version)
|
|
|
|
|
|
|
|
# WHEN: The worker is run
|
|
|
|
with patch.object(worker, 'no_internet') as mocked_no_internet, \
|
|
|
|
patch.object(worker, 'quit') as mocked_quit:
|
|
|
|
worker.start()
|
|
|
|
|
|
|
|
# THEN: The check completes and the signal is emitted
|
2017-12-29 10:38:51 +00:00
|
|
|
expected_download_url = 'https://www.openlp.org/files/version.txt'
|
2017-09-13 06:08:38 +00:00
|
|
|
expected_headers = {'User-Agent': 'OpenLP/2.0 Linux/4.12.0-1-amd64; '}
|
|
|
|
mock_requests.get.assert_called_with(expected_download_url, headers=expected_headers)
|
|
|
|
assert mock_requests.get.call_count == 3
|
|
|
|
mocked_no_internet.emit.assert_called_once_with()
|
|
|
|
mocked_quit.emit.assert_called_once_with()
|
|
|
|
|
|
|
|
|
2020-02-09 20:24:05 +00:00
|
|
|
def test_update_check_date(mock_settings):
|
2018-06-26 19:44:54 +00:00
|
|
|
"""
|
|
|
|
Test that the update_check_date() function writes the correct date
|
|
|
|
"""
|
2017-09-13 06:08:38 +00:00
|
|
|
# GIVEN: A mocked Settings object
|
|
|
|
# WHEN: update_check_date() is called
|
|
|
|
update_check_date()
|
|
|
|
|
|
|
|
# THEN: The correct date should have been saved
|
2020-02-09 20:24:05 +00:00
|
|
|
mock_settings.setValue.assert_called_once_with('core/last version test', date.today().strftime('%Y-%m-%d'))
|
2017-09-13 06:08:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
@patch('openlp.core.version.run_thread')
|
2020-02-09 20:24:05 +00:00
|
|
|
def test_check_for_update(mocked_run_thread, mock_settings):
|
2018-06-26 19:44:54 +00:00
|
|
|
"""
|
|
|
|
Test the check_for_update() function
|
|
|
|
"""
|
2017-09-13 06:08:38 +00:00
|
|
|
# GIVEN: A mocked settings object
|
2020-02-09 20:24:05 +00:00
|
|
|
mock_settings.value.return_value = '1970-01-01'
|
2017-09-13 06:08:38 +00:00
|
|
|
|
|
|
|
# WHEN: check_for_update() is called
|
|
|
|
check_for_update(MagicMock())
|
|
|
|
|
|
|
|
# THEN: The right things should have been called and a thread set in motion
|
|
|
|
assert mocked_run_thread.call_count == 1
|
|
|
|
|
|
|
|
|
|
|
|
@patch('openlp.core.version.run_thread')
|
2020-02-09 20:24:05 +00:00
|
|
|
def test_check_for_update_skipped(mocked_run_thread, mock_settings):
|
2018-06-26 19:44:54 +00:00
|
|
|
"""
|
|
|
|
Test that the check_for_update() function skips running if it already ran today
|
|
|
|
"""
|
2017-09-13 06:08:38 +00:00
|
|
|
# GIVEN: A mocked settings object
|
2020-02-09 20:24:05 +00:00
|
|
|
mock_settings.value.return_value = date.today().strftime('%Y-%m-%d')
|
2017-09-13 06:08:38 +00:00
|
|
|
|
|
|
|
# WHEN: check_for_update() is called
|
|
|
|
check_for_update(MagicMock())
|
|
|
|
|
|
|
|
# THEN: The right things should have been called and a thread set in motion
|
|
|
|
assert mocked_run_thread.call_count == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_version_dev_version():
|
2018-06-26 19:44:54 +00:00
|
|
|
"""
|
|
|
|
Test the get_version() function
|
|
|
|
"""
|
2017-09-13 06:08:38 +00:00
|
|
|
# GIVEN: We're in dev mode
|
|
|
|
with patch.object(sys, 'argv', ['--dev-version']), \
|
|
|
|
patch('openlp.core.version.APPLICATION_VERSION', None):
|
|
|
|
# WHEN: get_version() is run
|
|
|
|
version = get_version()
|
|
|
|
|
|
|
|
# THEN: version is something
|
|
|
|
assert version
|