""" This module contains tests for the lib submodule of the Remotes plugin. """ import os from unittest import TestCase from tempfile import mkstemp from mock import MagicMock from openlp.core.lib import Settings from openlp.plugins.remotes.lib.httpserver import HttpRouter from PyQt4 import QtGui __default_settings__ = { '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' } class TestRouter(TestCase): """ Test the functions in the :mod:`lib` module. """ def setUp(self): """ Create the UI """ fd, self.ini_file = mkstemp('.ini') Settings().set_filename(self.ini_file) self.application = QtGui.QApplication.instance() Settings().extend_default_settings(__default_settings__) self.router = HttpRouter() 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) def password_encrypter_test(self): """ Test hash userid and password function """ # GIVEN: A default configuration Settings().setValue('remotes/user id', 'openlp') Settings().setValue('remotes/password', 'password') # WHEN: called with the defined userid router = HttpRouter() router.initialise() test_value = 'b3BlbmxwOnBhc3N3b3Jk' print(router.auth) # 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') def process_http_request_test(self): """ Test the router control functionality """ # GIVEN: A testing set of Routes router = HttpRouter() mocked_function = MagicMock() test_route = [ (r'^/stage/api/poll$', {'function': mocked_function, 'secure': False}), ] router.routes = test_route # WHEN: called with a poll route 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.'