openlp/tests/functional/openlp_core/common/test_httputils.py

244 lines
11 KiB
Python
Raw Normal View History

2016-12-20 21:21:17 +00:00
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2016-12-31 11:01:36 +00:00
# Copyright (c) 2008-2017 OpenLP Developers #
2016-12-20 21:21:17 +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; 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 #
###############################################################################
"""
Functional tests to test the AppLocation class and related methods.
"""
2016-12-21 09:41:57 +00:00
import os
2016-12-21 09:47:33 +00:00
import tempfile
2016-12-20 21:21:17 +00:00
from unittest import TestCase
from unittest.mock import MagicMock, patch
2016-12-20 21:21:17 +00:00
2017-09-19 16:48:34 +00:00
from openlp.core.common.httputils import get_user_agent, get_web_page, get_url_file_size, url_get_file
2017-09-05 20:48:55 +00:00
from openlp.core.common.path import Path
2016-12-20 21:21:17 +00:00
2016-12-21 09:47:33 +00:00
from tests.helpers.testmixin import TestMixin
2016-12-20 21:21:17 +00:00
2016-12-21 09:47:33 +00:00
class TestHttpUtils(TestCase, TestMixin):
2016-12-20 21:21:17 +00:00
"""
2016-12-21 09:47:33 +00:00
A test suite to test out various http helper functions.
2016-12-20 21:21:17 +00:00
"""
2016-12-21 09:47:33 +00:00
def setUp(self):
self.tempfile = os.path.join(tempfile.gettempdir(), 'testfile')
def tearDown(self):
if os.path.isfile(self.tempfile):
os.remove(self.tempfile)
2016-12-20 21:21:17 +00:00
def test_get_user_agent_linux(self):
"""
Test that getting a user agent on Linux returns a user agent suitable for Linux
"""
with patch('openlp.core.common.httputils.sys') as mocked_sys:
# GIVEN: The system is Linux
mocked_sys.platform = 'linux2'
# WHEN: We call get_user_agent()
user_agent = get_user_agent()
# THEN: The user agent is a Linux (or ChromeOS) user agent
result = 'Linux' in user_agent or 'CrOS' in user_agent
2017-12-09 16:11:45 +00:00
assert result, 'The user agent should be a valid Linux user agent'
2016-12-20 21:21:17 +00:00
def test_get_user_agent_windows(self):
"""
Test that getting a user agent on Windows returns a user agent suitable for Windows
"""
with patch('openlp.core.common.httputils.sys') as mocked_sys:
2017-09-20 16:55:21 +00:00
# GIVEN: The system is Windows
2016-12-20 21:21:17 +00:00
mocked_sys.platform = 'win32'
# WHEN: We call get_user_agent()
user_agent = get_user_agent()
# THEN: The user agent is a Linux (or ChromeOS) user agent
2017-12-09 16:11:45 +00:00
assert 'Windows' in user_agent, 'The user agent should be a valid Windows user agent'
2016-12-20 21:21:17 +00:00
def test_get_user_agent_macos(self):
"""
Test that getting a user agent on OS X returns a user agent suitable for OS X
"""
with patch('openlp.core.common.httputils.sys') as mocked_sys:
2017-09-20 16:55:21 +00:00
# GIVEN: The system is macOS
2016-12-20 21:21:17 +00:00
mocked_sys.platform = 'darwin'
# WHEN: We call get_user_agent()
user_agent = get_user_agent()
# THEN: The user agent is a Linux (or ChromeOS) user agent
2017-12-09 16:11:45 +00:00
assert 'Mac OS X' in user_agent, 'The user agent should be a valid OS X user agent'
2016-12-20 21:21:17 +00:00
def test_get_user_agent_default(self):
"""
Test that getting a user agent on a non-Linux/Windows/OS X platform returns the default user agent
"""
with patch('openlp.core.common.httputils.sys') as mocked_sys:
2017-09-20 16:55:21 +00:00
# GIVEN: The system is something else
2016-12-20 21:21:17 +00:00
mocked_sys.platform = 'freebsd'
# WHEN: We call get_user_agent()
user_agent = get_user_agent()
# THEN: The user agent is a Linux (or ChromeOS) user agent
2017-12-09 16:11:45 +00:00
assert 'NetBSD'in user_agent, 'The user agent should be the default user agent'
2016-12-20 21:21:17 +00:00
def test_get_web_page_no_url(self):
"""
Test that sending a URL of None to the get_web_page method returns None
"""
# GIVEN: A None url
test_url = None
# WHEN: We try to get the test URL
result = get_web_page(test_url)
# THEN: None should be returned
2017-12-09 16:11:45 +00:00
assert result is None, 'The return value of get_web_page should be None'
2016-12-20 21:21:17 +00:00
2017-09-20 16:55:21 +00:00
@patch('openlp.core.common.httputils.requests')
@patch('openlp.core.common.httputils.get_user_agent')
@patch('openlp.core.common.httputils.Registry')
def test_get_web_page(self, MockRegistry, mocked_get_user_agent, mocked_requests):
2016-12-20 21:21:17 +00:00
"""
Test that the get_web_page method works correctly
"""
2017-09-20 16:55:21 +00:00
# GIVEN: Mocked out objects and a fake URL
mocked_requests.get.return_value = MagicMock(text='text')
mocked_get_user_agent.return_value = 'user_agent'
fake_url = 'this://is.a.fake/url'
# WHEN: The get_web_page() method is called
returned_page = get_web_page(fake_url)
# THEN: The correct methods are called with the correct arguments and a web page is returned
mocked_requests.get.assert_called_once_with(fake_url, headers={'User-Agent': 'user_agent'},
proxies=None, timeout=30.0)
mocked_get_user_agent.assert_called_once_with()
assert MockRegistry.call_count == 0, 'The Registry() object should have never been called'
assert returned_page == 'text', 'The returned page should be the mock object'
@patch('openlp.core.common.httputils.requests')
@patch('openlp.core.common.httputils.get_user_agent')
def test_get_web_page_with_header(self, mocked_get_user_agent, mocked_requests):
2016-12-20 21:21:17 +00:00
"""
Test that adding a header to the call to get_web_page() adds the header to the request
"""
2017-09-20 16:55:21 +00:00
# GIVEN: Mocked out objects, a fake URL and a fake header
mocked_requests.get.return_value = MagicMock(text='text')
mocked_get_user_agent.return_value = 'user_agent'
fake_url = 'this://is.a.fake/url'
fake_headers = {'Fake-Header': 'fake value'}
# WHEN: The get_web_page() method is called
returned_page = get_web_page(fake_url, headers=fake_headers)
# THEN: The correct methods are called with the correct arguments and a web page is returned
expected_headers = dict(fake_headers)
expected_headers.update({'User-Agent': 'user_agent'})
mocked_requests.get.assert_called_once_with(fake_url, headers=expected_headers,
proxies=None, timeout=30.0)
mocked_get_user_agent.assert_called_with()
assert returned_page == 'text', 'The returned page should be the mock object'
@patch('openlp.core.common.httputils.requests')
@patch('openlp.core.common.httputils.get_user_agent')
def test_get_web_page_with_user_agent_in_headers(self, mocked_get_user_agent, mocked_requests):
2016-12-20 21:21:17 +00:00
"""
Test that adding a user agent in the header when calling get_web_page() adds that user agent to the request
"""
2017-09-20 16:55:21 +00:00
# GIVEN: Mocked out objects, a fake URL and a fake header
mocked_requests.get.return_value = MagicMock(text='text')
fake_url = 'this://is.a.fake/url'
user_agent_headers = {'User-Agent': 'OpenLP/2.2.0'}
# WHEN: The get_web_page() method is called
returned_page = get_web_page(fake_url, headers=user_agent_headers)
# THEN: The correct methods are called with the correct arguments and a web page is returned
mocked_requests.get.assert_called_once_with(fake_url, headers=user_agent_headers,
proxies=None, timeout=30.0)
assert mocked_get_user_agent.call_count == 0, 'get_user_agent() should not have been called'
assert returned_page == 'text', 'The returned page should be "test"'
@patch('openlp.core.common.httputils.requests')
@patch('openlp.core.common.httputils.get_user_agent')
@patch('openlp.core.common.httputils.Registry')
def test_get_web_page_update_openlp(self, MockRegistry, mocked_get_user_agent, mocked_requests):
2016-12-20 21:21:17 +00:00
"""
Test that passing "update_openlp" as true to get_web_page calls Registry().get('app').process_events()
"""
2017-09-20 16:55:21 +00:00
# GIVEN: Mocked out objects, a fake URL
mocked_requests.get.return_value = MagicMock(text='text')
mocked_get_user_agent.return_value = 'user_agent'
mocked_registry_object = MagicMock()
mocked_application_object = MagicMock()
mocked_registry_object.get.return_value = mocked_application_object
MockRegistry.return_value = mocked_registry_object
fake_url = 'this://is.a.fake/url'
# WHEN: The get_web_page() method is called
returned_page = get_web_page(fake_url, update_openlp=True)
# THEN: The correct methods are called with the correct arguments and a web page is returned
mocked_requests.get.assert_called_once_with(fake_url, headers={'User-Agent': 'user_agent'},
proxies=None, timeout=30.0)
mocked_get_user_agent.assert_called_once_with()
mocked_registry_object.get.assert_called_with('application')
mocked_application_object.process_events.assert_called_with()
assert returned_page == 'text', 'The returned page should be the mock object'
@patch('openlp.core.common.httputils.requests')
def test_get_url_file_size(self, mocked_requests):
2016-12-20 21:21:17 +00:00
"""
2017-09-20 16:55:21 +00:00
Test that calling "get_url_file_size" works correctly
2016-12-20 21:21:17 +00:00
"""
2017-09-20 16:55:21 +00:00
# GIVEN: Mocked out objects, a fake URL
mocked_requests.head.return_value = MagicMock(headers={'Content-Length': 100})
fake_url = 'this://is.a.fake/url'
# WHEN: The get_url_file_size() method is called
file_size = get_url_file_size(fake_url)
# THEN: The correct methods are called with the correct arguments and a web page is returned
mocked_requests.head.assert_called_once_with(fake_url, allow_redirects=True, timeout=30.0)
assert file_size == 100
@patch('openlp.core.common.httputils.requests')
def test_socket_timeout(self, mocked_requests):
2016-12-21 09:41:57 +00:00
"""
Test socket timeout gets caught
"""
# GIVEN: Mocked urlopen to fake a network disconnect in the middle of a download
mocked_requests.get.side_effect = OSError
2016-12-21 09:41:57 +00:00
# WHEN: Attempt to retrieve a file
url_get_file(MagicMock(), url='http://localhost/test', file_path=Path(self.tempfile))
2016-12-21 09:41:57 +00:00
# THEN: socket.timeout should have been caught
# NOTE: Test is if $tmpdir/tempfile is still there, then test fails since ftw deletes bad downloaded files
assert not os.path.exists(self.tempfile), 'tempfile should have been deleted'