2019-07-01 22:36:14 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# ---------------------------------------------------------------------- #
|
2022-02-06 09:10:17 +00:00
|
|
|
# Copyright (c) 2008-2022 OpenLP Developers #
|
2019-07-01 22:36:14 +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, either version 3 of the License, or #
|
|
|
|
# (at your option) any later version. #
|
|
|
|
# #
|
|
|
|
# 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, see <https://www.gnu.org/licenses/>. #
|
|
|
|
##########################################################################
|
|
|
|
from unittest.mock import MagicMock, call, patch
|
|
|
|
|
|
|
|
from openlp.core.api.zeroconf import ZeroconfWorker, start_zeroconf
|
|
|
|
|
|
|
|
|
2020-02-27 22:23:40 +00:00
|
|
|
def test_zeroconf_worker_constructor():
|
2019-07-01 22:36:14 +00:00
|
|
|
"""Test creating the Zeroconf worker object"""
|
2020-02-27 22:23:40 +00:00
|
|
|
# GIVEN: A ZeroconfWorker class
|
2019-07-01 22:36:14 +00:00
|
|
|
# WHEN: An instance of the ZeroconfWorker is created
|
2020-02-27 22:23:40 +00:00
|
|
|
worker = ZeroconfWorker(['127.0.0.1'], 8000, 8001)
|
2019-07-01 22:36:14 +00:00
|
|
|
|
|
|
|
# THEN: The inet_aton function should have been called and the attrs should be set
|
2020-02-27 22:23:40 +00:00
|
|
|
assert worker.addresses == ['127.0.0.1']
|
2019-07-01 22:36:14 +00:00
|
|
|
assert worker.http_port == 8000
|
|
|
|
assert worker.ws_port == 8001
|
|
|
|
|
|
|
|
|
|
|
|
@patch('openlp.core.api.zeroconf.ServiceInfo')
|
|
|
|
@patch('openlp.core.api.zeroconf.Zeroconf')
|
|
|
|
def test_zeroconf_worker_start(MockedZeroconf, MockedServiceInfo):
|
|
|
|
"""Test the start() method of ZeroconfWorker"""
|
|
|
|
# GIVEN: A few mocks and a ZeroconfWorker instance with a mocked can_run method
|
|
|
|
mocked_http_info = MagicMock()
|
|
|
|
mocked_ws_info = MagicMock()
|
|
|
|
mocked_zc = MagicMock()
|
|
|
|
MockedServiceInfo.side_effect = [mocked_http_info, mocked_ws_info]
|
|
|
|
MockedZeroconf.return_value = mocked_zc
|
2020-02-27 22:23:40 +00:00
|
|
|
worker = ZeroconfWorker(['127.0.0.1'], 8000, 8001)
|
2019-07-01 22:36:14 +00:00
|
|
|
|
|
|
|
# WHEN: The start() method is called
|
|
|
|
with patch.object(worker, 'can_run') as mocked_can_run:
|
|
|
|
mocked_can_run.side_effect = [True, False]
|
|
|
|
worker.start()
|
|
|
|
|
|
|
|
# THEN: The correct calls are made
|
|
|
|
assert MockedServiceInfo.call_args_list == [
|
2020-02-27 22:23:40 +00:00
|
|
|
call('_http._tcp.local.', 'OpenLP._http._tcp.local.', addresses=[b'\x7f\x00\x00\x01'], port=8000,
|
|
|
|
properties={}),
|
|
|
|
call('_ws._tcp.local.', 'OpenLP._ws._tcp.local.', addresses=[b'\x7f\x00\x00\x01'], port=8001,
|
|
|
|
properties={})
|
2019-07-01 22:36:14 +00:00
|
|
|
]
|
|
|
|
assert MockedZeroconf.call_count == 1
|
|
|
|
assert mocked_zc.register_service.call_args_list == [call(mocked_http_info), call(mocked_ws_info)]
|
|
|
|
assert mocked_can_run.call_count == 2
|
2020-02-27 22:23:40 +00:00
|
|
|
mocked_zc.unregister_all_services.assert_called_once_with()
|
|
|
|
mocked_zc.close.assert_called_once_with()
|
2019-07-01 22:36:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_zeroconf_worker_stop():
|
|
|
|
"""Test that the ZeroconfWorker.stop() method correctly stops the service"""
|
|
|
|
# GIVEN: A worker object with _can_run set to True
|
2020-02-27 22:23:40 +00:00
|
|
|
worker = ZeroconfWorker(['127.0.0.1'], 8000, 8001)
|
2019-07-01 22:36:14 +00:00
|
|
|
worker._can_run = True
|
|
|
|
|
|
|
|
# WHEN: stop() is called
|
|
|
|
worker.stop()
|
|
|
|
|
|
|
|
# THEN: _can_run should be False
|
2019-11-26 19:32:16 +00:00
|
|
|
assert worker.can_run() is False
|
2019-07-01 22:36:14 +00:00
|
|
|
|
|
|
|
|
2019-07-03 03:05:01 +00:00
|
|
|
@patch('openlp.core.api.zeroconf.get_network_interfaces')
|
2019-07-01 22:36:14 +00:00
|
|
|
@patch('openlp.core.api.zeroconf.Registry')
|
|
|
|
@patch('openlp.core.api.zeroconf.ZeroconfWorker')
|
|
|
|
@patch('openlp.core.api.zeroconf.run_thread')
|
2020-01-29 06:50:09 +00:00
|
|
|
def test_start_zeroconf(mocked_run_thread, MockedZeroconfWorker, MockedRegistry, mocked_get_network_interfaces):
|
2019-07-01 22:36:14 +00:00
|
|
|
"""Test the start_zeroconf() function"""
|
|
|
|
# GIVEN: A whole bunch of stuff that's mocked out
|
2019-07-03 03:05:01 +00:00
|
|
|
mocked_get_network_interfaces.return_value = {
|
|
|
|
'eth0': {
|
|
|
|
'ip': '192.168.1.191',
|
|
|
|
'broadcast': '192.168.1.255',
|
|
|
|
'netmask': '255.255.255.0',
|
|
|
|
'prefix': 24,
|
|
|
|
'localnet': '192.168.1.0'
|
|
|
|
}
|
|
|
|
}
|
2019-07-01 22:36:14 +00:00
|
|
|
MockedRegistry.return_value.get_flag.return_value = False
|
2020-01-29 06:50:09 +00:00
|
|
|
MockedRegistry.settings.return_value.value.side_effect = [8000, 8001]
|
2019-07-01 22:36:14 +00:00
|
|
|
mocked_worker = MagicMock()
|
|
|
|
MockedZeroconfWorker.return_value = mocked_worker
|
|
|
|
|
|
|
|
# WHEN: start_zeroconf() is called
|
|
|
|
start_zeroconf()
|
|
|
|
|
|
|
|
# THEN: A worker is added to the list of threads
|
2020-02-27 22:23:40 +00:00
|
|
|
mocked_run_thread.assert_called_once_with(mocked_worker, 'api_zeroconf')
|