openlp/tests/functional/openlp_plugins/remotes/test_router.py

262 lines
12 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2013-12-24 08:56:50 +00:00
# Copyright (c) 2008-2014 Raoul Snyman #
# Portions copyright (c) 2008-2014 Tim Bentley, Gerald Britton, Jonathan #
# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, #
# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. #
# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, #
# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, #
# Frode Woldsund, Martin Zibricky, Patrick Zimmermann #
# --------------------------------------------------------------------------- #
# 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 #
###############################################################################
2013-03-29 08:25:33 +00:00
"""
This module contains tests for the lib submodule of the Remotes plugin.
"""
import os
import urllib.request
2013-03-29 08:25:33 +00:00
from unittest import TestCase
from tempfile import mkstemp
from PyQt4 import QtGui
2013-03-29 08:25:33 +00:00
2013-11-07 20:13:15 +00:00
from openlp.core.lib import Registry
2013-10-13 20:36:42 +00:00
from openlp.core.common import Settings
2013-09-14 21:00:58 +00:00
from openlp.plugins.remotes.lib.httpserver import HttpRouter
from urllib.parse import urlparse
2013-12-18 21:54:56 +00:00
from tests.functional import MagicMock, patch, mock_open
2013-03-29 08:25:33 +00:00
__default_settings__ = {
2013-08-31 18:17:38 +00:00
'remotes/twelve hour': True,
'remotes/port': 4316,
'remotes/https port': 4317,
'remotes/https enabled': False,
'remotes/user id': 'openlp',
'remotes/password': 'password',
'remotes/authentication enabled': False,
'remotes/ip address': '0.0.0.0'
2013-03-29 08:25:33 +00:00
}
TEST_PATH = os.path.abspath(os.path.dirname(__file__))
2013-03-29 08:25:33 +00:00
2013-12-18 21:54:56 +00:00
2013-03-29 09:06:43 +00:00
class TestRouter(TestCase):
2013-03-29 08:25:33 +00:00
"""
Test the functions in the :mod:`lib` module.
"""
def setUp(self):
"""
Create the UI
"""
self.fd, self.ini_file = mkstemp('.ini')
2013-03-29 08:25:33 +00:00
Settings().set_filename(self.ini_file)
self.application = QtGui.QApplication.instance()
Settings().extend_default_settings(__default_settings__)
2013-03-29 09:06:43 +00:00
self.router = HttpRouter()
2013-03-29 08:25:33 +00:00
def tearDown(self):
"""
Delete all the C++ objects at the end so that we don't have a segfault
"""
del self.application
os.close(self.fd)
2013-03-29 08:25:33 +00:00
os.unlink(self.ini_file)
2013-09-14 21:00:58 +00:00
def password_encrypter_test(self):
2013-03-29 09:32:46 +00:00
"""
2013-09-14 21:00:58 +00:00
Test hash userid and password function
2013-03-29 09:32:46 +00:00
"""
# GIVEN: A default configuration
2013-09-14 21:00:58 +00:00
Settings().setValue('remotes/user id', 'openlp')
Settings().setValue('remotes/password', 'password')
2013-03-29 09:32:46 +00:00
# WHEN: called with the defined userid
2013-09-14 21:00:58 +00:00
router = HttpRouter()
router.initialise()
test_value = 'b3BlbmxwOnBhc3N3b3Jk'
print(router.auth)
2013-03-29 09:32:46 +00:00
# THEN: the function should return the correct password
2013-09-14 21:00:58 +00:00
self.assertEqual(router.auth, test_value,
2013-12-18 21:54:56 +00:00
'The result for make_sha_hash should return the correct encrypted password')
2013-03-29 09:32:46 +00:00
2013-03-29 08:25:33 +00:00
def process_http_request_test(self):
"""
2013-03-29 09:06:43 +00:00
Test the router control functionality
2013-03-29 08:25:33 +00:00
"""
2013-03-29 09:06:43 +00:00
# GIVEN: A testing set of Routes
2013-09-14 21:00:58 +00:00
router = HttpRouter()
2013-03-29 09:06:43 +00:00
mocked_function = MagicMock()
test_route = [
2013-09-14 21:00:58 +00:00
(r'^/stage/api/poll$', {'function': mocked_function, 'secure': False}),
2013-03-29 09:06:43 +00:00
]
2013-09-14 21:00:58 +00:00
router.routes = test_route
2013-03-29 09:06:43 +00:00
# WHEN: called with a poll route
2013-09-14 21:00:58 +00:00
function, args = router.process_http_request('/stage/api/poll', None)
2013-03-29 08:25:33 +00:00
2013-03-29 09:06:43 +00:00
# THEN: the function should have been called only once
2013-12-18 21:54:56 +00:00
self.assertEqual(mocked_function, function['function'], 'The mocked function should match defined value.')
self.assertFalse(function['secure'], 'The mocked function should not require any security.')
def get_content_type_test(self):
"""
2013-11-08 17:43:36 +00:00
Test the get_content_type logic
"""
# GIVEN: a set of files and their corresponding types
2013-12-30 08:35:05 +00:00
headers = [['test.html', 'text/html'], ['test.css', 'text/css'],
2013-10-28 15:16:22 +00:00
['test.js', 'application/javascript'], ['test.jpg', 'image/jpeg'],
['test.gif', 'image/gif'], ['test.ico', 'image/x-icon'],
['test.png', 'image/png'], ['test.whatever', 'text/plain'],
2013-11-08 17:43:36 +00:00
['test', 'text/plain'], ['', 'text/plain'],
2013-12-18 21:54:56 +00:00
[os.path.join(TEST_PATH, 'test.html'), 'text/html']]
# WHEN: calling each file type
for header in headers:
2013-11-08 17:43:36 +00:00
ext, content_type = self.router.get_content_type(header[0])
2013-12-18 21:54:56 +00:00
# THEN: all types should match
2013-11-08 17:43:36 +00:00
self.assertEqual(content_type, header[1], 'Mismatch of content type')
2013-10-28 02:33:28 +00:00
def serve_file_without_params_test(self):
"""
Test the serve_file method without params
"""
# GIVEN: mocked environment
self.router.send_response = MagicMock()
self.router.send_header = MagicMock()
self.router.end_headers = MagicMock()
self.router.wfile = MagicMock()
self.router.html_dir = os.path.normpath('test/dir')
self.router.template_vars = MagicMock()
2013-12-18 21:54:56 +00:00
# WHEN: call serve_file with no file_name
self.router.serve_file()
2013-12-18 21:54:56 +00:00
# THEN: it should return a 404
self.router.send_response.assert_called_once_with(404)
self.router.send_header.assert_called_once_with('Content-type','text/html')
2013-12-18 21:54:56 +00:00
self.assertEqual(self.router.end_headers.call_count, 1, 'end_headers called once')
def serve_file_with_valid_params_test(self):
"""
Test the serve_file method with an existing file
"""
# GIVEN: mocked environment
self.router.send_response = MagicMock()
self.router.send_header = MagicMock()
self.router.end_headers = MagicMock()
self.router.wfile = MagicMock()
self.router.html_dir = os.path.normpath('test/dir')
self.router.template_vars = MagicMock()
with patch('openlp.core.lib.os.path.exists') as mocked_exists, \
2013-12-30 08:35:05 +00:00
patch('builtins.open', mock_open(read_data='123')):
mocked_exists.return_value = True
2013-12-18 21:54:56 +00:00
# WHEN: call serve_file with an existing html file
self.router.serve_file(os.path.normpath('test/dir/test.html'))
# THEN: it should return a 200 and the file
self.router.send_response.assert_called_once_with(200)
2013-12-18 21:54:56 +00:00
self.router.send_header.assert_called_once_with('Content-type', 'text/html')
self.assertEqual(self.router.end_headers.call_count, 1, 'end_headers called once')
2013-10-28 02:33:28 +00:00
def serve_thumbnail_without_params_test(self):
"""
Test the serve_thumbnail routine without params
"""
self.router.send_response = MagicMock()
self.router.send_header = MagicMock()
self.router.end_headers = MagicMock()
self.router.wfile = MagicMock()
self.router.serve_thumbnail()
self.router.send_response.assert_called_once_with(404)
2013-12-30 08:35:05 +00:00
self.assertEqual(self.router.send_response.call_count, 1, 'Send response called once')
self.assertEqual(self.router.end_headers.call_count, 1, 'end_headers called once')
2013-10-28 02:33:28 +00:00
def serve_thumbnail_with_invalid_params_test(self):
"""
Test the serve_thumbnail routine with invalid params
"""
# GIVEN: Mocked send_header, send_response, end_headers and wfile
self.router.send_response = MagicMock()
self.router.send_header = MagicMock()
self.router.end_headers = MagicMock()
self.router.wfile = MagicMock()
2013-12-30 08:35:05 +00:00
2013-10-28 02:33:28 +00:00
# WHEN: pass a bad controller
2013-12-30 08:35:05 +00:00
self.router.serve_thumbnail('badcontroller', 'tecnologia 1.pptx/slide1.png')
2013-10-28 02:33:28 +00:00
# THEN: a 404 should be returned
2013-12-30 08:35:05 +00:00
self.assertEqual(len(self.router.send_header.mock_calls), 1, 'One header')
self.assertEqual(len(self.router.send_response.mock_calls), 1, 'One response')
self.assertEqual(len(self.router.wfile.mock_calls), 1, 'Once call to write to the socket')
2013-10-28 02:33:28 +00:00
self.router.send_response.assert_called_once_with(404)
2013-12-30 08:35:05 +00:00
2013-10-28 02:33:28 +00:00
# WHEN: pass a bad filename
self.router.send_response.reset_mock()
2013-12-30 08:35:05 +00:00
self.router.serve_thumbnail('presentations', 'tecnologia 1.pptx/badfilename.png')
2013-10-28 02:33:28 +00:00
# THEN: return a 404
self.router.send_response.assert_called_once_with(404)
2013-12-30 08:35:05 +00:00
2013-10-28 02:33:28 +00:00
# WHEN: a dangerous URL is passed
self.router.send_response.reset_mock()
2013-12-30 08:35:05 +00:00
self.router.serve_thumbnail('presentations', '../tecnologia 1.pptx/slide1.png')
2013-10-28 02:33:28 +00:00
# THEN: return a 404
self.router.send_response.assert_called_once_with(404)
def serve_thumbnail_with_valid_params_test(self):
"""
Test the serve_thumbnail routine with valid params
"""
# GIVEN: Mocked send_header, send_response, end_headers and wfile
self.router.send_response = MagicMock()
self.router.send_header = MagicMock()
self.router.end_headers = MagicMock()
self.router.wfile = MagicMock()
2013-11-07 20:13:15 +00:00
mocked_image_manager = MagicMock()
Registry.create()
2013-12-30 08:35:05 +00:00
Registry().register('image_manager', mocked_image_manager)
file_name = 'another%20test/slide1.png'
full_path = os.path.normpath(os.path.join('thumbnails',file_name))
width = 120
height = 90
2013-10-28 02:33:28 +00:00
with patch('openlp.core.lib.os.path.exists') as mocked_exists, \
patch('builtins.open', mock_open(read_data='123')), \
2013-12-30 08:35:05 +00:00
patch('openlp.plugins.remotes.lib.httprouter.AppLocation') as mocked_location, \
patch('openlp.plugins.remotes.lib.httprouter.image_to_byte') as mocked_image_to_byte:
2013-10-28 02:33:28 +00:00
mocked_exists.return_value = True
mocked_image_to_byte.return_value = '123'
2013-10-28 02:33:28 +00:00
mocked_location.get_section_data_path.return_value = ''
2013-12-30 08:35:05 +00:00
2013-10-28 02:33:28 +00:00
# WHEN: pass good controller and filename
2013-12-30 08:35:05 +00:00
result = self.router.serve_thumbnail('presentations', '{0}x{1}'.format(width, height), file_name)
2013-10-28 02:33:28 +00:00
# THEN: a file should be returned
2013-12-30 08:35:05 +00:00
self.assertEqual(self.router.send_header.call_count, 1, 'One header')
self.assertEqual(self.router.send_response.call_count, 1, 'Send response called once')
self.assertEqual(self.router.end_headers.call_count, 1, 'end_headers called once')
mocked_exists.assert_called_with(urllib.parse.unquote(full_path))
self.assertEqual(mocked_image_to_byte.call_count, 1, 'Called once')
2013-12-30 08:35:05 +00:00
mocked_image_manager.assert_called_any(os.path.normpath('thumbnails\\another test'),
'slide1.png', None, '120x90')
mocked_image_manager.assert_called_any(os.path.normpath('thumbnails\\another test'), 'slide1.png', '120x90')