From 6b37b3dbab32fea631fbb25edd5ffd17d567f898 Mon Sep 17 00:00:00 2001 From: Kyle Robbertze Date: Fri, 22 Jan 2021 22:13:31 +0200 Subject: [PATCH] fully test set protocol --- .../api_clients/api_clients/api_client.py | 11 +-- python_apps/api_clients/tests/test_apcurl.py | 2 +- .../api_clients/tests/test_get_protocol.py | 75 +++++++++++++++++++ .../api_clients/tests/test_requestprovider.py | 3 + 4 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 python_apps/api_clients/tests/test_get_protocol.py diff --git a/python_apps/api_clients/api_clients/api_client.py b/python_apps/api_clients/api_clients/api_client.py index b2296e607..f9e72e4cd 100644 --- a/python_apps/api_clients/api_clients/api_client.py +++ b/python_apps/api_clients/api_clients/api_client.py @@ -68,13 +68,14 @@ api_config['bin_dir'] = '/usr/lib/airtime/api_clients/' api_config['update_metadata_on_tunein'] = 'update-metadata-on-tunein/api_key/%%api_key%%' def get_protocol(config): - port = config.get('general', 'base_port', fallback=80) - if config.getboolean('general', 'force_ssl', fallback=False): + positive_values = ['Yes', 'yes', 'True', 'true', True] + port = config['general'].get('base_port', 80) + force_ssl = config['general'].get('force_ssl', False) + if force_ssl in positive_values: protocol = 'https' else: - try: - protocol = config.get(CONFIG_SECTION, 'protocol') - except NoOptionError as e: + protocol = config['general'].get('protocol') + if not protocol: protocol = str(("http", "https")[int(port) == 443]) return protocol diff --git a/python_apps/api_clients/tests/test_apcurl.py b/python_apps/api_clients/tests/test_apcurl.py index e3c8b29d5..78e9fef56 100644 --- a/python_apps/api_clients/tests/test_apcurl.py +++ b/python_apps/api_clients/tests/test_apcurl.py @@ -5,7 +5,7 @@ class TestApcUrl(unittest.TestCase): def test_init(self): url = "/testing" u = ApcUrl(url) - self.assertEqual( u.base_url, url) + self.assertEqual(u.base_url, url) def test_params_1(self): u = ApcUrl("/testing/%%key%%") diff --git a/python_apps/api_clients/tests/test_get_protocol.py b/python_apps/api_clients/tests/test_get_protocol.py new file mode 100644 index 000000000..c19e7a9b5 --- /dev/null +++ b/python_apps/api_clients/tests/test_get_protocol.py @@ -0,0 +1,75 @@ +import unittest +import configparser +from api_clients.api_client import get_protocol + +def get_force_ssl(value, useConfigParser): + config = {} + if useConfigParser: + config = configparser.ConfigParser() + config['general'] = { + 'base_port': 80, + 'force_ssl': value, + } + return get_protocol(config) + +class TestGetProtocol(unittest.TestCase): + def test_dict_config_empty_http(self): + config = {'general': {}} + protocol = get_protocol(config) + self.assertEqual(protocol, 'http') + + def test_dict_config_http(self): + config = { + 'general': { + 'base_port': 80, + }, + } + protocol = get_protocol(config) + self.assertEqual(protocol, 'http') + + def test_dict_config_https(self): + config = { + 'general': { + 'base_port': 443, + }, + } + protocol = get_protocol(config) + self.assertEqual(protocol, 'https') + + def test_dict_config_force_https(self): + postive_values = ['yes', 'Yes', 'True', 'true', True] + negative_values = ['no', 'No', 'False', 'false', False] + for value in postive_values: + self.assertEqual(get_force_ssl(value, False), 'https') + for value in negative_values: + self.assertEqual(get_force_ssl(value, False), 'http') + + def test_configparser_config_empty_http(self): + config = configparser.ConfigParser() + config['general'] = {} + protocol = get_protocol(config) + self.assertEqual(protocol, 'http') + + def test_configparser_config_http(self): + config = configparser.ConfigParser() + config['general'] = { + 'base_port': 80, + } + protocol = get_protocol(config) + self.assertEqual(protocol, 'http') + + def test_configparser_config_https(self): + config = configparser.ConfigParser() + config['general'] = { + 'base_port': 443, + } + protocol = get_protocol(config) + self.assertEqual(protocol, 'https') + + def test_configparser_config_force_https(self): + postive_values = ['yes', 'Yes', 'True', 'true', True] + negative_values = ['no', 'No', 'False', 'false', False] + for value in postive_values: + self.assertEqual(get_force_ssl(value, True), 'https') + for value in negative_values: + self.assertEqual(get_force_ssl(value, True), 'http') diff --git a/python_apps/api_clients/tests/test_requestprovider.py b/python_apps/api_clients/tests/test_requestprovider.py index c210aad85..6c91a1947 100644 --- a/python_apps/api_clients/tests/test_requestprovider.py +++ b/python_apps/api_clients/tests/test_requestprovider.py @@ -13,11 +13,14 @@ class TestRequestProvider(unittest.TestCase): self.cfg['general']['base_url'] = 'localhost' self.cfg['general']['api_key'] = 'TEST_KEY' self.cfg['api_base'] = 'api' + def test_test(self): self.assertTrue('general' in self.cfg) + def test_init(self): rp = RequestProvider(self.cfg) self.assertTrue( len( rp.available_requests() ) > 0 ) + def test_contains(self): rp = RequestProvider(self.cfg) methods = ['upload_recorded', 'update_media_url', 'list_all_db_files']