Skip to content

Commit

Permalink
Refactor and add test
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 28, 2023
1 parent 79271cd commit 1835753
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
15 changes: 11 additions & 4 deletions activefires_pp/fire_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,7 @@ def run(self):
LOG.debug("Message type not supported: %s", str(msg.type))
continue

product_name = msg.data.get('product')
product_list = self.options.get('products')
if product_list and product_name and product_name not in product_list:
LOG.info('Product %s will not generate a notification!', product_name)
if not self._product_name_supported(msg):
continue

output_msg = self.notify_end_users(msg)
Expand All @@ -197,6 +194,16 @@ def run(self):
else:
LOG.debug("No message to send")

def _product_name_supported(self, incoming_msg):
"""Check that the product name is supported via the configuration."""
product_name = incoming_msg.data.get('product')
product_list = self.options.get('products')
if product_list and product_name and product_name not in product_list:
LOG.info('Product %s will not generate a notification!', product_name)
return False

return True

def notify_end_users(self, msg):
"""Send notifications to configured end users (mail and text messages)."""
LOG.debug("Start sending notifications to configured end users.")
Expand Down
50 changes: 49 additions & 1 deletion activefires_pp/tests/test_fire_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from unittest.mock import patch
import yaml
import io
# from posttroll.message import Message
from posttroll.message import Message

from activefires_pp.fire_notifications import EndUserNotifier
from activefires_pp.fire_notifications import EndUserNotifierRegional
Expand Down Expand Up @@ -106,6 +106,8 @@

NATIONAL_TEST_MESSAGE = """pytroll://VIIRS/L2/Fires/PP/National file safusr.u@lxserv1043.smhi.se 2021-04-19T11:16:49.519087 v1.01 application/json {"start_time": "2021-04-16T12:29:53", "end_time": "2021-04-16T12:31:18", "orbit_number": 1, "platform_name": "NOAA-20", "sensor": "viirs", "data_processing_level": "2", "variant": "DR", "orig_orbit_number": 17666, "uri": "ssh://lxserv1043.smhi.se//san1/polar_out/direct_readout/viirs_active_fires/filtered/AFIMG_j01_d20210416_t122953.geojson", "uid": "AFIMG_j01_d20210416_t122953.geojson", "type": "GEOJSON-filtered", "format": "geojson", "product": "afimg"}""" # noqa

NATIONAL_TEST_MESSAGE2 = """pytroll://VIIRS/L2/Fires/PP/National file safusr.u@lxserv1043.smhi.se 2021-04-19T11:16:49.519087 v1.01 application/json {"start_time": "2021-04-16T12:29:53", "end_time": "2021-04-16T12:31:18", "orbit_number": 1, "platform_name": "NOAA-20", "sensor": "viirs", "data_processing_level": "2", "variant": "DR", "orig_orbit_number": 17666, "uri": "ssh://lxserv1043.smhi.se//san1/polar_out/direct_readout/viirs_active_fires/filtered/AFIMG_j01_d20210416_t122953.geojson", "uid": "AFIMG_j01_d20210416_t122953_somegeoid.geojson", "type": "GEOJSON-filtered", "format": "geojson", "product": "afimg_somegeoid"}""" # noqa


class MyNetrcMock(object):
"""Mocking the handling of secrets via the .netrc file."""
Expand All @@ -122,6 +124,52 @@ def authenticators(self, host):
class TestNotifyEndUsers(unittest.TestCase):
"""Test notifications on National fires."""

@patch('activefires_pp.fire_notifications.netrc')
@patch('activefires_pp.fire_notifications.socket.gethostname')
@patch('activefires_pp.fire_notifications.read_config')
@patch('activefires_pp.fire_notifications.EndUserNotifier._setup_and_start_communication')
def test_check_incoming_message_product_name_ok(self, setup_comm, read_config, gethostname, netrc):
"""Test the incoming message for the 'right' product (name)."""
secrets = MyNetrcMock()
netrc.return_value = secrets
gethostname.return_value = 'default'

myconfigfile = "/my/config/file/path"
natstream = io.StringIO(NAT_CONFIG)

read_config.return_value = yaml.load(natstream, Loader=yaml.UnsafeLoader)

this = EndUserNotifier(myconfigfile)

input_msg = Message.decode(rawstr=NATIONAL_TEST_MESSAGE)

result = this._product_name_supported(input_msg)

assert result is True

@patch('activefires_pp.fire_notifications.netrc')
@patch('activefires_pp.fire_notifications.socket.gethostname')
@patch('activefires_pp.fire_notifications.read_config')
@patch('activefires_pp.fire_notifications.EndUserNotifier._setup_and_start_communication')
def test_check_incoming_message_product_name_not_ok(self, setup_comm, read_config, gethostname, netrc):
"""Test the incoming message for the 'right' product (name)."""
secrets = MyNetrcMock()
netrc.return_value = secrets
gethostname.return_value = 'default'

myconfigfile = "/my/config/file/path"
natstream = io.StringIO(NAT_CONFIG)

read_config.return_value = yaml.load(natstream, Loader=yaml.UnsafeLoader)

this = EndUserNotifier(myconfigfile)

input_msg = Message.decode(rawstr=NATIONAL_TEST_MESSAGE2)

result = this._product_name_supported(input_msg)

assert result is False

@patch('activefires_pp.fire_notifications.netrc')
@patch('activefires_pp.fire_notifications.socket.gethostname')
@patch('activefires_pp.fire_notifications.read_config')
Expand Down
4 changes: 2 additions & 2 deletions activefires_pp/tests/test_messaging.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2021, 2022, 2023 Adam.Dybbroe
# Copyright (c) 2021 - 2023 Adam.Dybbroe

# Author(s):

# Adam.Dybbroe <a000680@c21856.ad.smhi.se>
# Adam Dybbroe <Firstname.Lastname at smhi.se>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down

0 comments on commit 1835753

Please sign in to comment.