From 2112414ec87e65c2f9b3cfe84646ee046d129d70 Mon Sep 17 00:00:00 2001 From: "Adam.Dybbroe" Date: Wed, 31 Aug 2022 13:19:09 +0200 Subject: [PATCH] Refactor and change to use yaml file for storing authentication tokens Signed-off-by: Adam.Dybbroe --- activefires_pp/config.py | 11 ++++++-- .../spatiotemporal_alarm_filtering.py | 12 +++------ activefires_pp/tests/test_config.py | 25 +++++++++++++++++-- .../test_spatiotemporal_alarm_filtering.py | 25 +++---------------- 4 files changed, 39 insertions(+), 34 deletions(-) diff --git a/activefires_pp/config.py b/activefires_pp/config.py index 527fe25..0c37c98 100644 --- a/activefires_pp/config.py +++ b/activefires_pp/config.py @@ -20,8 +20,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -"""Handling the yaml configurations. -""" +"""Handling the yaml configurations.""" import yaml from yaml import UnsafeLoader @@ -33,3 +32,11 @@ def read_config(config_filepath): config = yaml.load(fp_, Loader=UnsafeLoader) return config + + +def get_xauthentication_token(xauth_filepath): + """Get the X-Authentication-token needed for posting to the API.""" + with open(xauth_filepath, 'r') as fp_: + tokens = yaml.load(fp_, Loader=UnsafeLoader) + + return tokens['xauth_tokens']['x-auth-satellite-alarm'] diff --git a/activefires_pp/spatiotemporal_alarm_filtering.py b/activefires_pp/spatiotemporal_alarm_filtering.py index 1dfe0b9..e0cd8bf 100644 --- a/activefires_pp/spatiotemporal_alarm_filtering.py +++ b/activefires_pp/spatiotemporal_alarm_filtering.py @@ -56,6 +56,8 @@ from activefires_pp.utils import get_filename_from_posttroll_message from activefires_pp.config import read_config +from activefires_pp.config import get_xauthentication_token + from activefires_pp.geojson_utils import read_geojson_data from activefires_pp.geojson_utils import get_recent_geojson_files from activefires_pp.geojson_utils import store_geojson_alarm @@ -87,7 +89,7 @@ def __init__(self, configfile): self.sos_alarms_file_pattern = self.options['geojson_file_pattern_alarms'] self.restapi_url = self.options['restapi_url'] _xauth_filepath = get_xauthentication_filepath_from_environment() - self._xauth_token = _get_xauthentication_token(_xauth_filepath) + self._xauth_token = get_xauthentication_token(_xauth_filepath) self.fire_alarms_dir = Path(self.options['fire_alarms_dir']) @@ -219,14 +221,6 @@ def get_xauthentication_filepath_from_environment(): return xauth_filepath -def _get_xauthentication_token(xauth_filepath): - """Get the X-Authentication-token needed for posting to the API.""" - with open(xauth_filepath, 'r') as fpt: - xauth_token = fpt.readline() - - return xauth_token - - def dump_collection(idx, features): """Dump the list of features as a Geojson Feature Collection.""" tmpdir = Path(DIR_SPATIAL_FILTER) diff --git a/activefires_pp/tests/test_config.py b/activefires_pp/tests/test_config.py index 5b57127..b13418a 100644 --- a/activefires_pp/tests/test_config.py +++ b/activefires_pp/tests/test_config.py @@ -20,11 +20,12 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -"""Test getting the yaml configurations from file. -""" +"""Test getting the yaml configurations from file.""" import pytest from activefires_pp.config import read_config +from activefires_pp.config import get_xauthentication_token + TEST_YAML_CONFIG_CONTENT = """# Publish/subscribe subscribe_topics: /VIIRS/L2/Fires/PP/National @@ -37,6 +38,20 @@ restapi_url: "https://xxx.smhi.se:xxxx" """ +TEST_YAML_TOKENS = """xauth_tokens: + x-auth-satellite-alarm : 'my-token' +""" + + +@pytest.fixture +def fake_token_file(tmp_path): + """Write fake token file.""" + file_path = tmp_path / '.sometokenfile.yaml' + with open(file_path, 'w') as fpt: + fpt.write(TEST_YAML_TOKENS) + + yield file_path + @pytest.fixture def fake_yamlconfig_file(tmp_path): @@ -56,3 +71,9 @@ def test_get_yaml_configuration(fake_yamlconfig_file): assert config['geojson_file_pattern_alarms'] == 'sos_{start_time:%Y%m%d_%H%M%S}_{id:d}.geojson' assert config['fire_alarms_dir'] == '/path/where/the/filtered/alarms/will/be/stored' assert config['restapi_url'] == 'https://xxx.smhi.se:xxxx' + + +def test_get_xauthentication_token(fake_token_file): + """Test getting the xauthentication token from a file.""" + fake_token = get_xauthentication_token(fake_token_file) + assert fake_token == 'my-token' diff --git a/activefires_pp/tests/test_spatiotemporal_alarm_filtering.py b/activefires_pp/tests/test_spatiotemporal_alarm_filtering.py index 3140e8b..634ecaf 100644 --- a/activefires_pp/tests/test_spatiotemporal_alarm_filtering.py +++ b/activefires_pp/tests/test_spatiotemporal_alarm_filtering.py @@ -36,7 +36,6 @@ from activefires_pp.spatiotemporal_alarm_filtering import create_single_point_alarms_from_collections from activefires_pp.spatiotemporal_alarm_filtering import AlarmFilterRunner from activefires_pp.spatiotemporal_alarm_filtering import get_xauthentication_filepath_from_environment -from activefires_pp.spatiotemporal_alarm_filtering import _get_xauthentication_token from activefires_pp.api_posting import post_alarm @@ -230,16 +229,6 @@ 'restapi_url': 'https://xxx.smhi.se:xxxx'} -@pytest.fixture -def fake_token_file(tmp_path): - """Return file path to a file with a dummy token.""" - file_path = tmp_path / '.sometokenfile' - with open(file_path, 'w') as fpt: - fpt.writelines(['some_fake_token']) - - yield file_path - - @pytest.fixture def fake_geojson_file_many_detections(tmp_path): """Write fake geojson file with many close detections.""" @@ -476,13 +465,7 @@ def test_alarm_filter_runner_init_no_env(os_environ_get): assert str(exec_info.value) == expected -def test_get_xauthentication_token(fake_token_file): - """Test getting the xauthentication token from a file.""" - fake_token = _get_xauthentication_token(fake_token_file) - assert fake_token == 'some_fake_token' - - -@patch('activefires_pp.spatiotemporal_alarm_filtering._get_xauthentication_token') +@patch('activefires_pp.spatiotemporal_alarm_filtering.get_xauthentication_token') @patch('activefires_pp.spatiotemporal_alarm_filtering.get_xauthentication_filepath_from_environment') @patch('activefires_pp.spatiotemporal_alarm_filtering.read_config') @patch('activefires_pp.spatiotemporal_alarm_filtering.AlarmFilterRunner._setup_and_start_communication') @@ -515,7 +498,7 @@ def test_alarm_filter_runner_init(setup_comm, 'restapi_url': 'https://xxx.smhi.se:xxxx'} -@patch('activefires_pp.spatiotemporal_alarm_filtering._get_xauthentication_token') +@patch('activefires_pp.spatiotemporal_alarm_filtering.get_xauthentication_token') @patch('activefires_pp.spatiotemporal_alarm_filtering.get_xauthentication_filepath_from_environment') @patch('activefires_pp.spatiotemporal_alarm_filtering.read_config') @patch('activefires_pp.spatiotemporal_alarm_filtering.AlarmFilterRunner._setup_and_start_communication') @@ -553,7 +536,7 @@ def test_alarm_filter_runner_call_spatio_temporal_alarm_filtering_has_alarms(sen assert result[0] == alarm -@patch('activefires_pp.spatiotemporal_alarm_filtering._get_xauthentication_token') +@patch('activefires_pp.spatiotemporal_alarm_filtering.get_xauthentication_token') @patch('activefires_pp.spatiotemporal_alarm_filtering.get_xauthentication_filepath_from_environment') @patch('activefires_pp.spatiotemporal_alarm_filtering.read_config') @patch('activefires_pp.spatiotemporal_alarm_filtering.AlarmFilterRunner._setup_and_start_communication') @@ -578,7 +561,7 @@ def test_alarm_filter_runner_call_spatio_temporal_alarm_filtering_no_firedata(re assert result is None -@patch('activefires_pp.spatiotemporal_alarm_filtering._get_xauthentication_token') +@patch('activefires_pp.spatiotemporal_alarm_filtering.get_xauthentication_token') @patch('activefires_pp.spatiotemporal_alarm_filtering.get_xauthentication_filepath_from_environment') @patch('activefires_pp.spatiotemporal_alarm_filtering.read_config') @patch('activefires_pp.spatiotemporal_alarm_filtering.AlarmFilterRunner._setup_and_start_communication')