2013-03-29 08:25:33 +00:00
|
|
|
"""
|
|
|
|
This module contains tests for the lib submodule of the Remotes plugin.
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
|
|
|
|
from unittest import TestCase
|
|
|
|
from tempfile import mkstemp
|
2013-03-29 20:58:06 +00:00
|
|
|
from mock import MagicMock
|
2013-03-29 08:25:33 +00:00
|
|
|
|
|
|
|
from openlp.core.lib import Settings
|
2013-04-08 16:53:11 +00:00
|
|
|
from openlp.plugins.remotes.lib.httpserver import HttpRouter, fetch_password, make_sha_hash
|
2013-03-29 08:25:33 +00:00
|
|
|
from PyQt4 import QtGui
|
|
|
|
|
|
|
|
__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
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
"""
|
2013-08-31 18:17:38 +00:00
|
|
|
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.unlink(self.ini_file)
|
|
|
|
|
2013-03-29 09:32:46 +00:00
|
|
|
def fetch_password_unknown_test(self):
|
|
|
|
"""
|
|
|
|
Test the fetch password code with an unknown userid
|
|
|
|
"""
|
|
|
|
# GIVEN: A default configuration
|
|
|
|
# WHEN: called with the defined userid
|
2013-08-31 18:17:38 +00:00
|
|
|
password = fetch_password('itwinkle')
|
2013-03-29 09:32:46 +00:00
|
|
|
|
|
|
|
# THEN: the function should return None
|
2013-08-31 18:17:38 +00:00
|
|
|
self.assertEqual(password, None, 'The result for fetch_password should be None')
|
2013-03-29 09:32:46 +00:00
|
|
|
|
|
|
|
def fetch_password_known_test(self):
|
|
|
|
"""
|
|
|
|
Test the fetch password code with the defined userid
|
|
|
|
"""
|
|
|
|
# GIVEN: A default configuration
|
|
|
|
# WHEN: called with the defined userid
|
2013-08-31 18:17:38 +00:00
|
|
|
password = fetch_password('openlp')
|
|
|
|
required_password = make_sha_hash('password')
|
2013-03-29 09:32:46 +00:00
|
|
|
|
|
|
|
# THEN: the function should return the correct password
|
2013-08-31 18:17:38 +00:00
|
|
|
self.assertEqual(password, required_password, 'The result for fetch_password should be the defined password')
|
2013-03-29 09:32:46 +00:00
|
|
|
|
|
|
|
def sha_password_encrypter_test(self):
|
|
|
|
"""
|
|
|
|
Test hash password function
|
|
|
|
"""
|
|
|
|
# GIVEN: A default configuration
|
|
|
|
# WHEN: called with the defined userid
|
2013-08-31 18:17:38 +00:00
|
|
|
required_password = make_sha_hash('password')
|
|
|
|
test_value = '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8'
|
2013-03-29 09:32:46 +00:00
|
|
|
|
|
|
|
# THEN: the function should return the correct password
|
|
|
|
self.assertEqual(required_password, test_value,
|
2013-08-31 18:17:38 +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
|
|
|
|
mocked_function = MagicMock()
|
|
|
|
test_route = [
|
|
|
|
(r'^/stage/api/poll$', mocked_function),
|
|
|
|
]
|
|
|
|
self.router.routes = test_route
|
|
|
|
|
|
|
|
# WHEN: called with a poll route
|
2013-08-31 18:17:38 +00:00
|
|
|
self.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
|
|
|
|
assert mocked_function.call_count == 1, \
|
2013-08-31 18:17:38 +00:00
|
|
|
'The mocked function should have been matched and called once.'
|