2017-08-11 11:04:33 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
|
|
|
|
2019-04-13 13:00:22 +00:00
|
|
|
##########################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
|
|
# Copyright (c) 2008-2019 OpenLP Developers #
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
|
|
# 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/>. #
|
|
|
|
##########################################################################
|
2017-08-11 11:04:33 +00:00
|
|
|
"""
|
2017-11-10 11:59:38 +00:00
|
|
|
Package to test the openlp.core.projectors.pjlink command routing.
|
2017-08-11 11:04:33 +00:00
|
|
|
"""
|
|
|
|
|
2019-04-21 01:04:59 +00:00
|
|
|
from unittest import TestCase, skip
|
2019-05-04 05:25:07 +00:00
|
|
|
from unittest.mock import call, patch
|
2017-08-11 11:04:33 +00:00
|
|
|
|
2017-11-10 11:59:38 +00:00
|
|
|
import openlp.core.projectors.pjlink
|
2018-10-02 04:39:42 +00:00
|
|
|
from openlp.core.projectors.constants import E_AUTHENTICATION, E_PARAMETER, E_PROJECTOR, E_UNAVAILABLE, E_UNDEFINED, \
|
|
|
|
PJLINK_ERRORS, PJLINK_PREFIX, STATUS_MSG
|
2017-11-16 23:53:53 +00:00
|
|
|
from openlp.core.projectors.db import Projector
|
|
|
|
from openlp.core.projectors.pjlink import PJLink
|
2017-12-25 08:44:30 +00:00
|
|
|
from tests.resources.projector.data import TEST1_DATA
|
2017-08-11 11:04:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestPJLinkRouting(TestCase):
|
|
|
|
"""
|
|
|
|
Tests for the PJLink module command routing
|
|
|
|
"""
|
2019-04-21 01:04:59 +00:00
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Setup test environment
|
|
|
|
"""
|
|
|
|
self.pjlink = PJLink(Projector(**TEST1_DATA), no_poll=True)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""
|
|
|
|
Reset test environment
|
|
|
|
"""
|
|
|
|
del(self.pjlink)
|
|
|
|
|
2019-05-04 05:25:07 +00:00
|
|
|
@patch.object(openlp.core.projectors.pjlinkcommands, 'process_command')
|
2018-04-20 06:04:43 +00:00
|
|
|
@patch.object(openlp.core.projectors.pjlink, 'log')
|
2019-05-04 05:25:07 +00:00
|
|
|
def test_projector_get_data_invalid_version(self, mock_log, mock_process_cmd):
|
2017-12-25 08:44:30 +00:00
|
|
|
"""
|
2019-05-04 05:25:07 +00:00
|
|
|
Test projector received valid command invalid version
|
2017-12-25 08:44:30 +00:00
|
|
|
"""
|
|
|
|
# GIVEN: Test object
|
2019-05-04 05:25:07 +00:00
|
|
|
log_warning_text = [call('({ip}) get_data() Command reply version does not match '
|
|
|
|
'a valid command version'.format(ip=self.pjlink.name)),
|
|
|
|
call('({ip}) _send_command(): Nothing to send - returning'.format(ip=self.pjlink.name))]
|
|
|
|
log_debug_text = [call('({ip}) get_data(buffer="{pre}XCLSS=X"'.format(ip=self.pjlink.name, pre=PJLINK_PREFIX)),
|
|
|
|
call('({ip}) get_data(): Checking new data "{pre}XCLSS=X"'.format(ip=self.pjlink.name,
|
|
|
|
pre=PJLINK_PREFIX)),
|
|
|
|
call('({ip}) get_data() header="{pre}XCLSS" data="X"'.format(ip=self.pjlink.name,
|
|
|
|
pre=PJLINK_PREFIX)),
|
|
|
|
call('({ip}) get_data() version="X" cmd="CLSS" data="X"'.format(ip=self.pjlink.name)),
|
|
|
|
call('({ip}) Cleaning buffer - msg = "get_data() Command reply version does '
|
|
|
|
'not match a valid command version"'.format(ip=self.pjlink.name)),
|
|
|
|
call('({ip}) Finished cleaning buffer - 0 bytes dropped'.format(ip=self.pjlink.name))]
|
2018-04-20 06:04:43 +00:00
|
|
|
# WHEN: get_data called with an unknown command
|
2019-05-04 05:25:07 +00:00
|
|
|
self.pjlink.get_data(buff='{prefix}XCLSS=X'.format(prefix=PJLINK_PREFIX))
|
2018-04-20 06:04:43 +00:00
|
|
|
|
|
|
|
# THEN: Appropriate log entries should have been made and methods called/not called
|
|
|
|
mock_log.warning.assert_has_calls(log_warning_text)
|
|
|
|
mock_log.debug.assert_has_calls(log_debug_text)
|
2019-05-04 05:25:07 +00:00
|
|
|
assert (mock_process_cmd.call_count == 0), 'process_command should not have been called'
|
2017-12-25 08:44:30 +00:00
|
|
|
|
2019-05-04 05:25:07 +00:00
|
|
|
@patch.object(openlp.core.projectors.pjlinkcommands, 'process_command')
|
|
|
|
@patch.object(openlp.core.projectors.pjlink, 'log')
|
|
|
|
def test_projector_get_data_unknown_command(self, mock_log, mock_process_cmd):
|
2017-08-11 11:04:33 +00:00
|
|
|
"""
|
2019-05-04 05:25:07 +00:00
|
|
|
Test projector receiving invalid command
|
2017-08-11 11:04:33 +00:00
|
|
|
"""
|
2019-05-04 05:25:07 +00:00
|
|
|
# GIVEN: Test object
|
|
|
|
log_warning_text = [call('({ip}) get_data(): Invalid packet - '
|
|
|
|
'unknown command "UNKN"'.format(ip=self.pjlink.name)),
|
|
|
|
call('({ip}) _send_command(): Nothing to send - '
|
|
|
|
'returning'.format(ip=self.pjlink.name))]
|
|
|
|
log_debug_text = [call('({ip}) get_data(buffer="{pre}1UNKN=Huh?"'.format(ip=self.pjlink.name,
|
|
|
|
pre=PJLINK_PREFIX)),
|
|
|
|
call('({ip}) get_data(): Checking new data "{pre}1UNKN=Huh?"'.format(ip=self.pjlink.name,
|
|
|
|
pre=PJLINK_PREFIX)),
|
|
|
|
call('({ip}) get_data() header="{pre}1UNKN" data="Huh?"'.format(ip=self.pjlink.name,
|
|
|
|
pre=PJLINK_PREFIX)),
|
|
|
|
call('({ip}) get_data() version="1" cmd="UNKN" data="Huh?"'.format(ip=self.pjlink.name)),
|
|
|
|
call('({ip}) Cleaning buffer - msg = "get_data(): Invalid packet - '
|
|
|
|
'unknown command "UNKN""'.format(ip=self.pjlink.name)),
|
|
|
|
call('({ip}) Finished cleaning buffer - 0 bytes dropped'.format(ip=self.pjlink.name))]
|
2017-08-11 11:04:33 +00:00
|
|
|
|
2019-05-04 05:25:07 +00:00
|
|
|
# WHEN: get_data called with an unknown command
|
|
|
|
self.pjlink.get_data(buff='{prefix}1UNKN=Huh?'.format(prefix=PJLINK_PREFIX))
|
2017-08-11 11:04:33 +00:00
|
|
|
|
2019-05-04 05:25:07 +00:00
|
|
|
# THEN: Appropriate log entries should have been made and methods called/not called
|
|
|
|
mock_log.warning.assert_has_calls(log_warning_text)
|
|
|
|
mock_log.debug.assert_has_calls(log_debug_text)
|
|
|
|
assert (mock_process_cmd.call_count == 0), 'process_command should not have been called'
|
2017-12-25 08:44:30 +00:00
|
|
|
|
2019-05-04 05:25:07 +00:00
|
|
|
@patch.object(openlp.core.projectors.pjlinkcommands, 'process_command')
|
|
|
|
@patch.object(openlp.core.projectors.pjlink, 'log')
|
|
|
|
def test_projector_get_data_version_mismatch(self, mock_log, mock_process_cmd):
|
2017-08-11 11:04:33 +00:00
|
|
|
"""
|
2019-05-04 05:25:07 +00:00
|
|
|
Test projector received valid command with command version higher than projector
|
2017-08-11 11:04:33 +00:00
|
|
|
"""
|
|
|
|
# GIVEN: Test object
|
2019-05-04 05:25:07 +00:00
|
|
|
log_warning_text = [call('({ip}) get_data(): Projector returned class reply higher than projector '
|
|
|
|
'stated class'.format(ip=self.pjlink.name)),
|
|
|
|
call('({ip}) _send_command(): Nothing to send - returning'.format(ip=self.pjlink.name))]
|
|
|
|
|
|
|
|
log_debug_text = [call('({ip}) get_data(buffer="{pre}2ACKN=X"'.format(ip=self.pjlink.name,
|
|
|
|
pre=PJLINK_PREFIX)),
|
|
|
|
call('({ip}) get_data(): Checking new data "{pre}2ACKN=X"'.format(ip=self.pjlink.name,
|
|
|
|
pre=PJLINK_PREFIX)),
|
|
|
|
call('({ip}) get_data() header="{pre}2ACKN" data="X"'.format(ip=self.pjlink.name,
|
|
|
|
pre=PJLINK_PREFIX)),
|
|
|
|
call('({ip}) get_data() version="2" cmd="ACKN" data="X"'.format(ip=self.pjlink.name))]
|
|
|
|
self.pjlink.pjlink_class = '1'
|
2017-12-25 08:44:30 +00:00
|
|
|
|
2019-05-04 05:25:07 +00:00
|
|
|
# WHEN: get_data called with an unknown command
|
|
|
|
self.pjlink.get_data(buff='{prefix}2ACKN=X'.format(prefix=PJLINK_PREFIX))
|
2017-08-11 11:04:33 +00:00
|
|
|
|
2019-05-04 05:25:07 +00:00
|
|
|
# THEN: Appropriate log entries should have been made and methods called/not called
|
|
|
|
mock_log.warning.assert_has_calls(log_warning_text)
|
|
|
|
mock_log.debug.assert_has_calls(log_debug_text)
|
|
|
|
assert (mock_process_cmd.call_count == 0), 'process_command should not have been called'
|
2017-08-11 11:04:33 +00:00
|
|
|
|
2019-04-21 01:04:59 +00:00
|
|
|
@skip('Needs update to new setup')
|
2019-05-04 05:25:07 +00:00
|
|
|
def test_routing_err1(self):
|
2017-08-11 11:04:33 +00:00
|
|
|
"""
|
2017-12-25 08:44:30 +00:00
|
|
|
Test ERR1 - Undefined projector function
|
2017-08-11 11:04:33 +00:00
|
|
|
"""
|
|
|
|
# GIVEN: Test object
|
2017-12-25 08:44:30 +00:00
|
|
|
with patch.object(openlp.core.projectors.pjlink, 'log') as mock_log, \
|
|
|
|
patch.object(openlp.core.projectors.pjlink.PJLink, 'process_clss') as mock_process_clss:
|
|
|
|
|
|
|
|
pjlink = PJLink(Projector(**TEST1_DATA), no_poll=True)
|
2019-04-21 01:04:59 +00:00
|
|
|
log_error_text = [call('({ip}) CLSS: {msg}'.format(ip=self.pjlink.name, msg=STATUS_MSG[E_UNDEFINED]))]
|
|
|
|
log_debug_text = [call('({ip}) Processing command "CLSS" with data "ERR1"'.format(ip=self.pjlink.name)),
|
|
|
|
call('({ip}) Calling function for CLSS'.format(ip=self.pjlink.name))]
|
2017-08-11 11:04:33 +00:00
|
|
|
|
2017-12-25 08:44:30 +00:00
|
|
|
# WHEN: process_command called with ERR1
|
|
|
|
pjlink.process_command(cmd='CLSS', data=PJLINK_ERRORS[E_UNDEFINED])
|
2017-08-11 11:04:33 +00:00
|
|
|
|
2017-12-25 08:44:30 +00:00
|
|
|
# THEN: Appropriate log entries should have been made and methods called
|
|
|
|
mock_log.error.assert_has_calls(log_error_text)
|
|
|
|
mock_log.debug.assert_has_calls(log_debug_text)
|
|
|
|
mock_process_clss.assert_called_once_with(data=PJLINK_ERRORS[E_UNDEFINED])
|
2017-08-11 11:04:33 +00:00
|
|
|
|
2019-04-21 01:04:59 +00:00
|
|
|
@skip('Needs update to new setup')
|
2019-05-04 05:25:07 +00:00
|
|
|
def test_routing_err2(self):
|
2017-08-11 11:04:33 +00:00
|
|
|
"""
|
2017-12-25 08:44:30 +00:00
|
|
|
Test ERR2 - Parameter Error
|
2017-08-11 11:04:33 +00:00
|
|
|
"""
|
|
|
|
# GIVEN: Test object
|
2017-12-25 08:44:30 +00:00
|
|
|
with patch.object(openlp.core.projectors.pjlink, 'log') as mock_log, \
|
|
|
|
patch.object(openlp.core.projectors.pjlink.PJLink, 'process_clss') as mock_process_clss:
|
2017-08-11 11:04:33 +00:00
|
|
|
|
2017-12-25 08:44:30 +00:00
|
|
|
pjlink = PJLink(Projector(**TEST1_DATA), no_poll=True)
|
2019-04-21 01:04:59 +00:00
|
|
|
log_error_text = [call('({ip}) CLSS: {msg}'.format(ip=self.pjlink.name, msg=STATUS_MSG[E_PARAMETER]))]
|
|
|
|
log_debug_text = [call('({ip}) Processing command "CLSS" with data "ERR2"'.format(ip=self.pjlink.name)),
|
|
|
|
call('({ip}) Calling function for CLSS'.format(ip=self.pjlink.name))]
|
2017-08-11 11:04:33 +00:00
|
|
|
|
2017-12-25 08:44:30 +00:00
|
|
|
# WHEN: process_command called with ERR2
|
|
|
|
pjlink.process_command(cmd='CLSS', data=PJLINK_ERRORS[E_PARAMETER])
|
2017-08-11 11:04:33 +00:00
|
|
|
|
2017-12-25 08:44:30 +00:00
|
|
|
# THEN: Appropriate log entries should have been made and methods called/not called
|
|
|
|
mock_log.error.assert_has_calls(log_error_text)
|
|
|
|
mock_log.debug.assert_has_calls(log_debug_text)
|
|
|
|
mock_process_clss.assert_called_once_with(data=PJLINK_ERRORS[E_PARAMETER])
|
|
|
|
|
2019-04-21 01:04:59 +00:00
|
|
|
@skip('Needs update to new setup')
|
2019-05-04 05:25:07 +00:00
|
|
|
def test_routing_err3(self):
|
2017-08-11 11:04:33 +00:00
|
|
|
"""
|
|
|
|
Test ERR3 - Unavailable error
|
|
|
|
"""
|
|
|
|
# GIVEN: Test object
|
2017-12-25 08:44:30 +00:00
|
|
|
with patch.object(openlp.core.projectors.pjlink, 'log') as mock_log, \
|
|
|
|
patch.object(openlp.core.projectors.pjlink.PJLink, 'process_clss') as mock_process_clss:
|
2017-08-11 11:04:33 +00:00
|
|
|
|
2017-12-25 08:44:30 +00:00
|
|
|
pjlink = PJLink(Projector(**TEST1_DATA), no_poll=True)
|
2019-04-21 01:04:59 +00:00
|
|
|
log_error_text = [call('({ip}) CLSS: {msg}'.format(ip=self.pjlink.name, msg=STATUS_MSG[E_UNAVAILABLE]))]
|
|
|
|
log_debug_text = [call('({ip}) Processing command "CLSS" with data "ERR3"'.format(ip=self.pjlink.name)),
|
|
|
|
call('({ip}) Calling function for CLSS'.format(ip=self.pjlink.name))]
|
2017-08-11 11:04:33 +00:00
|
|
|
|
2017-12-25 08:44:30 +00:00
|
|
|
# WHEN: process_command called with ERR3
|
|
|
|
pjlink.process_command(cmd='CLSS', data=PJLINK_ERRORS[E_UNAVAILABLE])
|
2017-08-11 11:04:33 +00:00
|
|
|
|
2017-12-25 08:44:30 +00:00
|
|
|
# THEN: Appropriate log entries should have been made and methods called
|
|
|
|
mock_log.error.assert_has_calls(log_error_text)
|
|
|
|
mock_log.debug.assert_has_calls(log_debug_text)
|
|
|
|
mock_process_clss.assert_called_once_with(data=PJLINK_ERRORS[E_UNAVAILABLE])
|
|
|
|
|
2019-04-21 01:04:59 +00:00
|
|
|
@skip('Needs update to new setup')
|
2019-05-04 05:25:07 +00:00
|
|
|
def test_routing_err4(self):
|
2017-08-11 11:04:33 +00:00
|
|
|
"""
|
2017-12-25 08:44:30 +00:00
|
|
|
Test ERR3 - Unavailable error
|
2017-08-11 11:04:33 +00:00
|
|
|
"""
|
|
|
|
# GIVEN: Test object
|
2017-12-25 08:44:30 +00:00
|
|
|
with patch.object(openlp.core.projectors.pjlink, 'log') as mock_log, \
|
|
|
|
patch.object(openlp.core.projectors.pjlink.PJLink, 'process_clss') as mock_process_clss:
|
|
|
|
|
|
|
|
pjlink = PJLink(Projector(**TEST1_DATA), no_poll=True)
|
2019-04-21 01:04:59 +00:00
|
|
|
log_error_text = [call('({ip}) CLSS: {msg}'.format(ip=self.pjlink.name, msg=STATUS_MSG[E_PROJECTOR]))]
|
|
|
|
log_debug_text = [call('({ip}) Processing command "CLSS" with data "ERR4"'.format(ip=self.pjlink.name)),
|
|
|
|
call('({ip}) Calling function for CLSS'.format(ip=self.pjlink.name))]
|
2017-08-11 11:04:33 +00:00
|
|
|
|
2017-12-25 08:44:30 +00:00
|
|
|
# WHEN: process_command called with ERR4
|
|
|
|
pjlink.process_command(cmd='CLSS', data=PJLINK_ERRORS[E_PROJECTOR])
|
2017-08-11 11:04:33 +00:00
|
|
|
|
2017-12-25 08:44:30 +00:00
|
|
|
# THEN: Appropriate log entries should have been made and methods called
|
|
|
|
mock_log.error.assert_has_calls(log_error_text)
|
|
|
|
mock_log.debug.assert_has_calls(log_debug_text)
|
|
|
|
mock_process_clss.assert_called_once_with(data=PJLINK_ERRORS[E_PROJECTOR])
|
2017-08-11 11:04:33 +00:00
|
|
|
|
2019-04-21 01:04:59 +00:00
|
|
|
@skip('Needs update to new setup')
|
2019-05-04 05:25:07 +00:00
|
|
|
def test_routing_erra(self):
|
2017-08-11 11:04:33 +00:00
|
|
|
"""
|
2019-05-04 05:25:07 +00:00
|
|
|
Test ERRA - Authentication Error
|
2017-08-11 11:04:33 +00:00
|
|
|
"""
|
2017-12-25 08:44:30 +00:00
|
|
|
# GIVEN: Test object
|
|
|
|
with patch.object(openlp.core.projectors.pjlink, 'log') as mock_log, \
|
2019-05-04 05:25:07 +00:00
|
|
|
patch.object(openlp.core.projectors.pjlink.PJLink, 'process_pjlink') as mock_process_pjlink, \
|
|
|
|
patch.object(openlp.core.projectors.pjlink.PJLink, 'change_status') as mock_change_status, \
|
|
|
|
patch.object(openlp.core.projectors.pjlink.PJLink, 'disconnect_from_host') as mock_disconnect, \
|
|
|
|
patch.object(openlp.core.projectors.pjlink.PJLink, 'projectorAuthentication') as mock_authentication:
|
2017-08-11 11:04:33 +00:00
|
|
|
|
2017-12-25 08:44:30 +00:00
|
|
|
pjlink = PJLink(Projector(**TEST1_DATA), no_poll=True)
|
2019-05-04 05:25:07 +00:00
|
|
|
log_error_calls = [call('({ip}) PJLINK: {msg}'.format(ip=self.pjlink.name,
|
|
|
|
msg=STATUS_MSG[E_AUTHENTICATION]))]
|
|
|
|
log_debug_calls = [call('({ip}) Processing command "PJLINK" with data "ERRA"'.format(ip=self.pjlink.name))]
|
2017-08-11 11:04:33 +00:00
|
|
|
|
2019-05-04 05:25:07 +00:00
|
|
|
# WHEN: process_command called with ERRA
|
|
|
|
pjlink.process_command(cmd='PJLINK', data=PJLINK_ERRORS[E_AUTHENTICATION])
|
2017-08-11 11:04:33 +00:00
|
|
|
|
2017-12-25 08:44:30 +00:00
|
|
|
# THEN: Appropriate log entries should have been made and methods called/not called
|
2019-05-04 05:25:07 +00:00
|
|
|
assert mock_disconnect.called is True, 'disconnect_from_host should have been called'
|
|
|
|
mock_log.error.assert_has_calls(log_error_calls)
|
2017-12-25 08:44:30 +00:00
|
|
|
mock_log.debug.assert_has_calls(log_debug_calls)
|
2019-05-04 05:25:07 +00:00
|
|
|
mock_change_status.assert_called_once_with(status=E_AUTHENTICATION)
|
|
|
|
mock_authentication.emit.assert_called_once_with(pjlink.name)
|
|
|
|
mock_process_pjlink.assert_not_called()
|