diff --git a/tests/functional/__init__.py b/tests/functional/__init__.py index 3f471cf61..f3b85b9b3 100644 --- a/tests/functional/__init__.py +++ b/tests/functional/__init__.py @@ -11,9 +11,9 @@ import sys from PyQt4 import QtGui if sys.version_info[1] >= 3: - from unittest.mock import patch, MagicMock + from unittest.mock import MagicMock, patch, mock_open else: - from mock import patch, MagicMock + from mock import MagicMock, patch, mock_open # Only one QApplication can be created. Use QtGui.QApplication.instance() when you need to "create" a QApplication. application = QtGui.QApplication([]) diff --git a/tests/functional/openlp_plugins/remotes/test_router.py b/tests/functional/openlp_plugins/remotes/test_router.py index 4d7da2b91..f0af48afd 100644 --- a/tests/functional/openlp_plugins/remotes/test_router.py +++ b/tests/functional/openlp_plugins/remotes/test_router.py @@ -37,8 +37,7 @@ from PyQt4 import QtGui from openlp.core.common import Settings from openlp.plugins.remotes.lib.httpserver import HttpRouter -from tests.functional import MagicMock, patch -from mock import mock_open +from tests.functional import MagicMock, patch, mock_open __default_settings__ = { 'remotes/twelve hour': True, @@ -53,6 +52,7 @@ __default_settings__ = { TEST_PATH = os.path.abspath(os.path.dirname(__file__)) + class TestRouter(TestCase): """ Test the functions in the :mod:`lib` module. @@ -91,7 +91,7 @@ class TestRouter(TestCase): # THEN: the function should return the correct password self.assertEqual(router.auth, test_value, - 'The result for make_sha_hash should return the correct encrypted password') + 'The result for make_sha_hash should return the correct encrypted password') def process_http_request_test(self): """ @@ -109,10 +109,8 @@ class TestRouter(TestCase): function, args = router.process_http_request('/stage/api/poll', None) # THEN: the function should have been called only once - assert function['function'] == mocked_function, \ - 'The mocked function should match defined value.' - assert function['secure'] == False, \ - 'The mocked function should not require any security.' + 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): """ @@ -124,10 +122,12 @@ class TestRouter(TestCase): ['test.gif', 'image/gif'], ['test.ico', 'image/x-icon'], ['test.png', 'image/png'], ['test.whatever', 'text/plain'], ['test', 'text/plain'], ['', 'text/plain'], - [os.path.join(TEST_PATH,'test.html'), 'text/html']] + [os.path.join(TEST_PATH, 'test.html'), 'text/html']] + # WHEN: calling each file type for header in headers: ext, content_type = self.router.get_content_type(header[0]) + # THEN: all types should match self.assertEqual(content_type, header[1], 'Mismatch of content type') @@ -142,13 +142,14 @@ class TestRouter(TestCase): self.router.wfile = MagicMock() self.router.html_dir = os.path.normpath('test/dir') self.router.template_vars = MagicMock() + # WHEN: call serve_file with no file_name self.router.serve_file() + # 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') - self.assertEqual(self.router.end_headers.call_count, 1, - 'end_headers called once') + self.assertEqual(self.router.end_headers.call_count, 1, 'end_headers called once') def serve_file_with_valid_params_test(self): """ @@ -162,13 +163,13 @@ class TestRouter(TestCase): 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, \ - patch('builtins.open', mock_open(read_data='123')): + patch('builtins.open', mock_open(read_data='123')): mocked_exists.return_value = True + # 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) - 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') + 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')