Skip to content

Commit

Permalink
Change how to obtain the xauth token - now get it from a file
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 30, 2022
1 parent eefbd83 commit aad4f1e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 33 deletions.
25 changes: 17 additions & 8 deletions activefires_pp/spatiotemporal_alarm_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ def __init__(self, configfile):

self.sos_alarms_file_pattern = self.options['geojson_file_pattern_alarms']
self.restapi_url = self.options['restapi_url']
self.restapi_xauth = get_xauth_environment_variable()
_xauth_filepath = get_xauthentication_filepath_from_environment()
self._xauth_token = _get_xauthentication_token(_xauth_filepath)

self.fire_alarms_dir = Path(self.options['fire_alarms_dir'])

Expand Down Expand Up @@ -184,7 +185,7 @@ def send_alarms(self, geojson_alarms, msg):
# 2) Wite to a file
output_filename = store_geojson_alarm(self.fire_alarms_dir, p__, idx, alarm)
try:
post_alarm(alarm['features'], self.restapi_url, self.restapi_xauth)
post_alarm(alarm['features'], self.restapi_url, self._xauth_token)
LOG.info('Alarm sent - status OK')
except HTTPError:
LOG.exception('Failed sending alarm!')
Expand All @@ -209,13 +210,21 @@ def close(self):
LOG.exception("Couldn't stop publisher.")


def get_xauth_environment_variable():
"""Get the environment variable for the X-Authentication-token needed for posting to the API."""
restapi_xauth = os.environ.get('XAUTH_FIREALARMS_REST_API')
if restapi_xauth is None:
raise OSError("Environment variable XAUTH_FIREALARMS_REST_API not set!")
def get_xauthentication_filepath_from_environment():
"""Get the filename with the X-Authentication-token from environment."""
xauth_filepath = os.environ.get('FIREALARMS_XAUTH_FILEPATH')
if xauth_filepath is None:
raise OSError("Environment variable FIREALARMS_XAUTH_FILEPATH not set!")

return restapi_xauth
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):
Expand Down
80 changes: 55 additions & 25 deletions activefires_pp/tests/test_spatiotemporal_alarm_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
from activefires_pp.spatiotemporal_alarm_filtering import create_one_detection_from_collection
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_xauth_environment_variable
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 @@ -229,6 +230,16 @@
'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 @@ -453,13 +464,35 @@ def test_create_alarms_from_fire_detections(fake_past_detections_dir):
assert alarms[0]['features']['properties']['observation_time'] == '2021-06-19T02:58:45.700000+02:00'


@patch('activefires_pp.spatiotemporal_alarm_filtering.get_xauth_environment_variable')
@patch('activefires_pp.spatiotemporal_alarm_filtering.os.environ.get')
def test_alarm_filter_runner_init_no_env(os_environ_get):
"""Test initialize the AlarmFilterRunner class."""
os_environ_get.return_value = None

with pytest.raises(OSError) as exec_info:
_ = get_xauthentication_filepath_from_environment()

expected = "Environment variable FIREALARMS_XAUTH_FILEPATH not set!"
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_filepath_from_environment')
@patch('activefires_pp.spatiotemporal_alarm_filtering.read_config')
@patch('activefires_pp.spatiotemporal_alarm_filtering.AlarmFilterRunner._setup_and_start_communication')
def test_alarm_filter_runner_init(setup_comm, get_config, get_xauth):
def test_alarm_filter_runner_init(setup_comm,
get_config, get_xauth_filepath,
get_xauth_token):
"""Test initialize the AlarmFilterRunner class."""
get_xauth_filepath.return_value = 'some_filename'
get_xauth_token.return_value = 'some-token'
get_config.return_value = CONFIG_EXAMPLE
get_xauth.return_value = 'some-token'

myconfigfile = "/my/config/file/path"

Expand All @@ -482,19 +515,8 @@ def test_alarm_filter_runner_init(setup_comm, get_config, get_xauth):
'restapi_url': 'https://xxx.smhi.se:xxxx'}


@patch('activefires_pp.spatiotemporal_alarm_filtering.os.environ.get')
def test_alarm_filter_runner_init_no_env(os_environ_get):
"""Test initialize the AlarmFilterRunner class."""
os_environ_get.return_value = None

with pytest.raises(OSError) as exec_info:
_ = get_xauth_environment_variable()

expected = "Environment variable XAUTH_FIREALARMS_REST_API not set!"
assert str(exec_info.value) == expected


@patch('activefires_pp.spatiotemporal_alarm_filtering.get_xauth_environment_variable')
@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')
@patch('activefires_pp.spatiotemporal_alarm_filtering.get_filename_from_posttroll_message')
Expand All @@ -503,9 +525,11 @@ def test_alarm_filter_runner_init_no_env(os_environ_get):
@patch('activefires_pp.spatiotemporal_alarm_filtering.AlarmFilterRunner.send_alarms')
def test_alarm_filter_runner_call_spatio_temporal_alarm_filtering_has_alarms(send_alarms, create_alarms, read_geojson,
get_filename_from_pmsg, setup_comm,
get_config, get_xauth):
get_config, get_xauth_filepath,
get_xauth_token):
"""Test run the spatio_temporal_alarm_filtering method of the AlarmFilterRunner class."""
get_xauth.return_value = 'some-token'
get_xauth_filepath.return_value = 'some_filename'
get_xauth_token.return_value = 'some-token'
get_config.return_value = CONFIG_EXAMPLE
json_test_data = json.loads(TEST_MONSTERAS_FIRST_COLLECTION)
read_geojson.return_value = json_test_data
Expand All @@ -529,16 +553,19 @@ def test_alarm_filter_runner_call_spatio_temporal_alarm_filtering_has_alarms(sen
assert result[0] == alarm


@patch('activefires_pp.spatiotemporal_alarm_filtering.get_xauth_environment_variable')
@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')
@patch('activefires_pp.spatiotemporal_alarm_filtering.get_filename_from_posttroll_message')
@patch('activefires_pp.spatiotemporal_alarm_filtering.read_geojson_data')
def test_alarm_filter_runner_call_spatio_temporal_alarm_filtering_no_firedata(read_geojson,
get_filename_from_pmsg, setup_comm,
get_config, get_xauth):
get_config, get_xauth_filepath,
get_xauth_token):
"""Test run the spatio_temporal_alarm_filtering method of the AlarmFilterRunner class - no fires."""
get_xauth.return_value = 'some-token'
get_xauth_filepath.return_value = 'some_filename'
get_xauth_token.return_value = 'some-token'
get_config.return_value = CONFIG_EXAMPLE
read_geojson.return_value = None

Expand All @@ -551,17 +578,20 @@ def test_alarm_filter_runner_call_spatio_temporal_alarm_filtering_no_firedata(re
assert result is None


@patch('activefires_pp.spatiotemporal_alarm_filtering.get_xauth_environment_variable')
@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')
@patch('activefires_pp.spatiotemporal_alarm_filtering.get_filename_from_posttroll_message')
@patch('activefires_pp.spatiotemporal_alarm_filtering.read_geojson_data')
@patch('activefires_pp.spatiotemporal_alarm_filtering.create_alarms_from_fire_detections')
def test_alarm_filter_runner_call_spatio_temporal_alarm_filtering_no_alarms(create_alarms, read_geojson,
get_filename_from_pmsg, setup_comm,
get_config, get_xauth):
get_config, get_xauth_filepath,
get_xauth_token):
"""Test run the spatio_temporal_alarm_filtering method of the AlarmFilterRunner class - no alarms."""
get_xauth.return_value = 'some-token'
get_xauth_filepath.return_value = 'some_filename'
get_xauth_token.return_value = 'some-token'
get_config.return_value = CONFIG_EXAMPLE
json_test_data = json.loads(TEST_MONSTERAS_FIRST_COLLECTION)
read_geojson.return_value = json_test_data
Expand Down

0 comments on commit aad4f1e

Please sign in to comment.