From 2aeaa24871416c0df9772ba8f245c6add38ff977 Mon Sep 17 00:00:00 2001 From: Oliver Wieland Date: Sun, 8 Sep 2013 18:16:18 +0200 Subject: [PATCH] remotetab.py: separate get ip address for metter testing test_remotetab.py:tests for get_ip_address --- openlp/plugins/remotes/lib/remotetab.py | 30 ++++++++--------- .../openlp_plugins/remotes/test_remotetab.py | 33 ++++++++++++++++++- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/openlp/plugins/remotes/lib/remotetab.py b/openlp/plugins/remotes/lib/remotetab.py index b9c71f338..b84faf7bd 100644 --- a/openlp/plugins/remotes/lib/remotetab.py +++ b/openlp/plugins/remotes/lib/remotetab.py @@ -198,21 +198,7 @@ class RemoteTab(SettingsTab): """ Update the display based on the data input on the screen """ - ip_address = 'localhost' - if self.address_edit.text() == ZERO_URL: - interfaces = QtNetwork.QNetworkInterface.allInterfaces() - for interface in interfaces: - if not interface.isValid(): - continue - if not (interface.flags() & (QtNetwork.QNetworkInterface.IsUp | QtNetwork.QNetworkInterface.IsRunning)): - continue - for address in interface.addressEntries(): - ip = address.ip() - if ip.protocol() == 0 and ip != QtNetwork.QHostAddress.LocalHost: - ip_address = ip.toString() - break - else: - ip_address = self.address_edit.text() + ip_address = self.get_ip_address(self.address_edit.text()) http_url = 'http://%s:%s/' % (ip_address, self.port_spin_box.value()) https_url = 'https://%s:%s/' % (ip_address, self.https_port_spin_box.value()) self.remote_url.setText('%s' % (http_url, http_url)) @@ -226,6 +212,20 @@ class RemoteTab(SettingsTab): self.live_url.setText('%s' % (http_url_temp, http_url_temp)) self.live_https_url.setText('%s' % (https_url_temp, https_url_temp)) + def get_ip_address(self, ip): + if ip == ZERO_URL: + interfaces = QtNetwork.QNetworkInterface.allInterfaces() + for interface in interfaces: + if not interface.isValid(): + continue + if not (interface.flags() & (QtNetwork.QNetworkInterface.IsUp | QtNetwork.QNetworkInterface.IsRunning)): + continue + for address in interface.addressEntries(): + ip = address.ip() + if ip.protocol() == 0 and ip != QtNetwork.QHostAddress.LocalHost: + return ip.toString() + return ip + def load(self): """ Load the configuration and update the server configuration if necessary diff --git a/tests/functional/openlp_plugins/remotes/test_remotetab.py b/tests/functional/openlp_plugins/remotes/test_remotetab.py index 9ff795e73..81b67dbea 100644 --- a/tests/functional/openlp_plugins/remotes/test_remotetab.py +++ b/tests/functional/openlp_plugins/remotes/test_remotetab.py @@ -2,7 +2,7 @@ This module contains tests for the lib submodule of the Remotes plugin. """ import os - +import re from unittest import TestCase from tempfile import mkstemp from mock import patch @@ -52,6 +52,37 @@ class TestRemoteTab(TestCase): del self.form os.unlink(self.ini_file) + def get_ip_address_default_test(self): + """ + Test the get_ip_address function with ZERO_URL + """ + # GIVEN: A mocked location + with patch('openlp.core.utils.applocation.Settings') as mocked_class, \ + patch('openlp.core.utils.AppLocation.get_directory') as mocked_get_directory, \ + patch('openlp.core.utils.applocation.check_directory_exists') as mocked_check_directory_exists, \ + patch('openlp.core.utils.applocation.os') as mocked_os: + # GIVEN: A mocked out Settings class and a mocked out AppLocation.get_directory() + # WHEN: the default ip address is given + ip_address = self.form.get_ip_address(ZERO_URL) + # THEN: the default ip address will be returned + self.assertTrue(re.match('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', ip_address), 'The return value should be a valid ip address') + + def get_ip_address_with_ip_test(self): + """ + Test the get_ip_address function with given ip address + """ + # GIVEN: A mocked location + with patch('openlp.core.utils.applocation.Settings') as mocked_class, \ + patch('openlp.core.utils.AppLocation.get_directory') as mocked_get_directory, \ + patch('openlp.core.utils.applocation.check_directory_exists') as mocked_check_directory_exists, \ + patch('openlp.core.utils.applocation.os') as mocked_os: + # GIVEN: An ip address + given_ip = '192.168.1.1' + # WHEN: the default ip address is given + ip_address = self.form.get_ip_address(given_ip) + # THEN: the default ip address will be returned + self.assertEqual(ip_address, given_ip, 'The return value should be %s' % given_ip) + def set_basic_urls_test(self): """ Test the set_urls function with standard defaults