openlp/tests/functional/openlp_core/api/test_websockets.py

143 lines
6.9 KiB
Python
Raw Normal View History

2016-06-12 16:22:48 +00:00
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2019-02-14 15:09:09 +00:00
# Copyright (c) 2008-2019 OpenLP Developers #
2016-06-12 16:22:48 +00:00
# --------------------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
"""
2016-06-14 20:43:25 +00:00
Functional tests to test the Http Server Class.
2016-06-12 16:22:48 +00:00
"""
from unittest import TestCase
2017-08-12 19:22:09 +00:00
from unittest.mock import MagicMock, patch
2016-06-12 16:22:48 +00:00
2017-12-28 08:22:55 +00:00
from openlp.core.api.poll import Poller
from openlp.core.api.websockets import WebSocketServer
2017-10-07 07:05:07 +00:00
from openlp.core.common.registry import Registry
from openlp.core.common.settings import Settings
2016-06-12 16:22:48 +00:00
from tests.helpers.testmixin import TestMixin
2018-10-02 04:39:42 +00:00
2016-06-12 16:22:48 +00:00
__default_settings__ = {
2016-07-09 11:21:25 +00:00
'api/twelve hour': True,
'api/port': 4316,
'api/user id': 'openlp',
'api/password': 'password',
'api/authentication enabled': False,
'api/ip address': '0.0.0.0',
2017-08-12 19:34:56 +00:00
'api/thumbnails': True,
'songs/chord notation': True
2016-06-12 16:22:48 +00:00
}
2016-06-14 20:43:25 +00:00
class TestWSServer(TestCase, TestMixin):
2016-06-12 16:22:48 +00:00
"""
2016-06-14 20:43:25 +00:00
A test suite to test starting the websocket server
2016-06-12 16:22:48 +00:00
"""
def setUp(self):
"""
Create the UI
"""
self.build_settings()
Settings().extend_default_settings(__default_settings__)
Registry().create()
2016-06-28 21:39:16 +00:00
self.poll = Poller()
2016-06-12 16:22:48 +00:00
def tearDown(self):
"""
Delete all the C++ objects at the end so that we don't have a segfault
"""
self.destroy_settings()
2016-06-14 20:56:50 +00:00
@patch('openlp.core.api.websockets.WebSocketWorker')
2018-01-04 06:01:35 +00:00
@patch('openlp.core.api.websockets.run_thread')
def test_serverstart(self, mocked_run_thread, MockWebSocketWorker):
2016-06-14 20:43:25 +00:00
"""
2017-12-02 09:31:13 +00:00
Test the starting of the WebSockets Server with the disabled flag set on
2016-06-14 20:43:25 +00:00
"""
# GIVEN: A new httpserver
# WHEN: I start the server
2018-01-07 05:24:55 +00:00
Registry().set_flag('no_web_server', False)
2017-09-19 16:48:34 +00:00
WebSocketServer()
2016-06-14 20:43:25 +00:00
# THEN: the api environment should have been created
2018-01-04 06:01:35 +00:00
assert mocked_run_thread.call_count == 1, 'The qthread should have been called once'
assert MockWebSocketWorker.call_count == 1, 'The http thread should have been called once'
2016-06-14 20:43:25 +00:00
2017-12-02 09:31:13 +00:00
@patch('openlp.core.api.websockets.WebSocketWorker')
2018-01-04 06:01:35 +00:00
@patch('openlp.core.api.websockets.run_thread')
def test_serverstart_not_required(self, mocked_run_thread, MockWebSocketWorker):
2017-12-02 09:31:13 +00:00
"""
Test the starting of the WebSockets Server with the disabled flag set off
"""
# GIVEN: A new httpserver and the server is not required
# WHEN: I start the server
2018-01-07 05:24:55 +00:00
Registry().set_flag('no_web_server', True)
2017-12-02 09:31:13 +00:00
WebSocketServer()
# THEN: the api environment should have been created
2018-01-04 06:01:35 +00:00
assert mocked_run_thread.call_count == 0, 'The qthread should not have been called'
assert MockWebSocketWorker.call_count == 0, 'The http thread should not have been called'
2017-12-02 09:31:13 +00:00
2016-06-12 16:22:48 +00:00
def test_main_poll(self):
"""
Test the main_poll function returns the correct JSON
"""
# WHEN: the live controller has 5 slides
mocked_live_controller = MagicMock()
mocked_live_controller.slide_count = 5
Registry().register('live_controller', mocked_live_controller)
# THEN: the live json should be generated
main_json = self.poll.main_poll()
2017-12-09 15:21:59 +00:00
assert b'{"results": {"slide_count": 5}}' == main_json, 'The return value should match the defined json'
2016-06-12 16:22:48 +00:00
def test_poll(self):
"""
Test the poll function returns the correct JSON
"""
2017-09-19 16:48:34 +00:00
# GIVEN: the system is configured with a set of data
2016-06-12 16:22:48 +00:00
mocked_service_manager = MagicMock()
mocked_service_manager.service_id = 21
mocked_live_controller = MagicMock()
mocked_live_controller.selected_row = 5
mocked_live_controller.service_item = MagicMock()
mocked_live_controller.service_item.unique_identifier = '23-34-45'
mocked_live_controller.blank_screen.isChecked.return_value = True
mocked_live_controller.theme_screen.isChecked.return_value = False
mocked_live_controller.desktop_screen.isChecked.return_value = False
Registry().register('live_controller', mocked_live_controller)
Registry().register('service_manager', mocked_service_manager)
2017-09-19 16:48:34 +00:00
# WHEN: The poller polls
with patch.object(self.poll, 'is_stage_active') as mocked_is_stage_active, \
patch.object(self.poll, 'is_live_active') as mocked_is_live_active, \
patch.object(self.poll, 'is_chords_active') as mocked_is_chords_active:
mocked_is_stage_active.return_value = True
mocked_is_live_active.return_value = True
mocked_is_chords_active.return_value = True
poll_json = self.poll.poll()
2016-06-12 16:22:48 +00:00
# THEN: the live json should be generated and match expected results
2017-12-09 15:32:05 +00:00
assert poll_json['results']['blank'] is True, 'The blank return value should be True'
2017-12-09 15:28:09 +00:00
assert poll_json['results']['theme'] is False, 'The theme return value should be False'
assert poll_json['results']['display'] is False, 'The display return value should be False'
assert poll_json['results']['isSecure'] is False, 'The isSecure return value should be False'
assert poll_json['results']['isAuthorised'] is False, 'The isAuthorised return value should be False'
2017-12-09 15:34:16 +00:00
assert poll_json['results']['twelve'] is True, 'The twelve return value should be True'
2017-12-09 15:21:59 +00:00
assert poll_json['results']['version'] == 3, 'The version return value should be 3'
assert poll_json['results']['slide'] == 5, 'The slide return value should be 5'
assert poll_json['results']['service'] == 21, 'The version return value should be 21'
assert poll_json['results']['item'] == '23-34-45', 'The item return value should match 23-34-45'