diff --git a/openlp/core/lib/settingstab.py b/openlp/core/lib/settingstab.py index 4874d98b7..5b33b2f51 100644 --- a/openlp/core/lib/settingstab.py +++ b/openlp/core/lib/settingstab.py @@ -51,6 +51,12 @@ class SettingsTab(QtGui.QWidget, RegistryProperties): self.tab_visited = False if icon_path: self.icon_path = icon_path + self._setup() + + def _setup(self): + """ + Run some initial setup. This method is separate from __init__ in order to mock it out in tests. + """ self.setupUi() self.retranslateUi() self.initialise() diff --git a/openlp/plugins/remotes/lib/httprouter.py b/openlp/plugins/remotes/lib/httprouter.py index ae50eab40..d12898514 100644 --- a/openlp/plugins/remotes/lib/httprouter.py +++ b/openlp/plugins/remotes/lib/httprouter.py @@ -117,7 +117,7 @@ from urllib.parse import urlparse, parse_qs from mako.template import Template from PyQt4 import QtCore -from openlp.core.common import Registry, RegistryProperties, AppLocation, Settings, translate +from openlp.core.common import RegistryProperties, AppLocation, Settings, translate, UiStrings from openlp.core.lib import PluginStatus, StringContent, image_to_byte, ItemCapabilities, create_thumb log = logging.getLogger(__name__) @@ -232,12 +232,18 @@ class HttpRouter(RegistryProperties): return func, args return None, None + def set_cache_headers(self): + self.send_header("Cache-Control", "no-cache, no-store, must-revalidate") + self.send_header("Pragma", "no-cache") + self.send_header("Expires", "0") + def do_http_success(self): """ Create a success http header. """ self.send_response(200) self.send_header('Content-type', 'text/html') + self.set_cache_headers() self.end_headers() def do_json_header(self): @@ -246,6 +252,7 @@ class HttpRouter(RegistryProperties): """ self.send_response(200) self.send_header('Content-type', 'application/json') + self.set_cache_headers() self.end_headers() def do_http_error(self): @@ -254,6 +261,7 @@ class HttpRouter(RegistryProperties): """ self.send_response(404) self.send_header('Content-type', 'text/html') + self.set_cache_headers() self.end_headers() def do_authorisation(self): @@ -261,8 +269,10 @@ class HttpRouter(RegistryProperties): Create a needs authorisation http header. """ self.send_response(401) - self.send_header('WWW-Authenticate', 'Basic realm=\"Test\"') + header = 'Basic realm=\"{}\"'.format(UiStrings().OLPV2) + self.send_header('WWW-Authenticate', header) self.send_header('Content-type', 'text/html') + self.set_cache_headers() self.end_headers() def do_not_found(self): @@ -271,6 +281,7 @@ class HttpRouter(RegistryProperties): """ self.send_response(404) self.send_header('Content-type', 'text/html') + self.set_cache_headers() self.end_headers() self.wfile.write(bytes('Sorry, an error occurred ', 'UTF-8')) diff --git a/tests/functional/openlp_plugins/remotes/test_router.py b/tests/functional/openlp_plugins/remotes/test_router.py index 65dff62f3..70a2033a4 100644 --- a/tests/functional/openlp_plugins/remotes/test_router.py +++ b/tests/functional/openlp_plugins/remotes/test_router.py @@ -25,7 +25,6 @@ This module contains tests for the lib submodule of the Remotes plugin. import os import urllib.request from unittest import TestCase -from PyQt4 import QtCore from openlp.core.common import Settings, Registry from openlp.core.ui import ServiceManager from openlp.plugins.remotes.lib.httpserver import HttpRouter @@ -128,7 +127,7 @@ class TestRouter(TestCase, TestMixin): # THEN: the function should have been called only once self.router.send_response.assert_called_once_with(401) - self.assertEqual(self.router.send_header.call_count, 2, 'The header should have been called twice.') + self.assertEqual(self.router.send_header.call_count, 5, 'The header should have been called five times.') def get_content_type_test(self): """ @@ -187,7 +186,6 @@ class TestRouter(TestCase, TestMixin): # 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') def serve_file_with_valid_params_test(self): @@ -217,14 +215,19 @@ class TestRouter(TestCase, TestMixin): """ Test the serve_thumbnail routine without params """ + # GIVEN: mocked environment self.router.send_response = MagicMock() self.router.send_header = MagicMock() self.router.end_headers = MagicMock() self.router.wfile = MagicMock() + + # WHEN: I request a thumbnail self.router.serve_thumbnail() + + # THEN: The headers should be set correctly self.router.send_response.assert_called_once_with(404) - 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') + self.assertEqual(self.router.send_response.call_count, 1, 'Send response should be called once') + self.assertEqual(self.router.end_headers.call_count, 1, 'end_headers should be called once') def serve_thumbnail_with_invalid_params_test(self): """ @@ -240,7 +243,7 @@ class TestRouter(TestCase, TestMixin): self.router.serve_thumbnail('badcontroller', 'tecnologia 1.pptx/slide1.png') # THEN: a 404 should be returned - self.assertEqual(len(self.router.send_header.mock_calls), 1, 'One header') + self.assertEqual(len(self.router.send_header.mock_calls), 4, 'header should be called 4 times') 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') self.router.send_response.assert_called_once_with(404) @@ -284,7 +287,7 @@ class TestRouter(TestCase, TestMixin): mocked_location.get_section_data_path.return_value = '' # WHEN: pass good controller and filename - result = self.router.serve_thumbnail('presentations', '{0}x{1}'.format(width, height), file_name) + self.router.serve_thumbnail('presentations', '{0}x{1}'.format(width, height), file_name) # THEN: a file should be returned self.assertEqual(self.router.send_header.call_count, 1, 'One header')