Fix cache for http authentication

This commit is contained in:
Tim Bentley 2015-02-14 09:12:35 +00:00
parent 6f76c164e0
commit 58f10d3b6b
4 changed files with 28 additions and 7 deletions

View File

@ -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()

View File

@ -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('<html><body>Sorry, an error occurred </body></html>', 'UTF-8'))

View File

@ -215,6 +215,7 @@ class RemoteTab(SettingsTab):
ip_address == 0.0.0.0: return the IP address of the first valid interface
else: return ip_address
"""
ip_address = ZERO_URL
if ip_address == ZERO_URL:
interfaces = QtNetwork.QNetworkInterface.allInterfaces()
for interface in interfaces:

View File

@ -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,11 +215,16 @@ 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')
@ -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, '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')
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')