Skip to content

Commit

Permalink
Refactor and change to use yaml file for storing authentication tokens
Browse files Browse the repository at this point in the history
Signed-off-by: Adam.Dybbroe <a000680@c21856.ad.smhi.se>
  • Loading branch information
Adam.Dybbroe committed Aug 31, 2022
1 parent aad4f1e commit 2112414
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 34 deletions.
11 changes: 9 additions & 2 deletions activefires_pp/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Handling the yaml configurations.
"""
"""Handling the yaml configurations."""

import yaml
from yaml import UnsafeLoader
Expand All @@ -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']
12 changes: 3 additions & 9 deletions activefires_pp/spatiotemporal_alarm_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'])

Expand Down Expand Up @@ -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)
Expand Down
25 changes: 23 additions & 2 deletions activefires_pp/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""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
Expand All @@ -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):
Expand All @@ -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'
25 changes: 4 additions & 21 deletions activefires_pp/tests/test_spatiotemporal_alarm_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand Down

0 comments on commit 2112414

Please sign in to comment.