From 18357538353e958eeb6fe117044846f2f5cf80d9 Mon Sep 17 00:00:00 2001 From: "Adam.Dybbroe" Date: Mon, 28 Aug 2023 21:28:34 +0200 Subject: [PATCH] Refactor and add test Signed-off-by: Adam.Dybbroe --- activefires_pp/fire_notifications.py | 15 ++++-- .../tests/test_fire_notifications.py | 50 ++++++++++++++++++- activefires_pp/tests/test_messaging.py | 4 +- 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/activefires_pp/fire_notifications.py b/activefires_pp/fire_notifications.py index 1db2a20..416ee84 100644 --- a/activefires_pp/fire_notifications.py +++ b/activefires_pp/fire_notifications.py @@ -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) @@ -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.") diff --git a/activefires_pp/tests/test_fire_notifications.py b/activefires_pp/tests/test_fire_notifications.py index 91306ac..4cce314 100644 --- a/activefires_pp/tests/test_fire_notifications.py +++ b/activefires_pp/tests/test_fire_notifications.py @@ -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 @@ -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.""" @@ -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') diff --git a/activefires_pp/tests/test_messaging.py b/activefires_pp/tests/test_messaging.py index 5bda7d1..ce65d61 100644 --- a/activefires_pp/tests/test_messaging.py +++ b/activefires_pp/tests/test_messaging.py @@ -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 +# Adam Dybbroe # 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