diff --git a/.github/workflows/job-report-expiry-undeliverable-test-run.yml b/.github/workflows/job-report-expiry-undeliverable-test-run.yml new file mode 100644 index 000000000..2c36c1435 --- /dev/null +++ b/.github/workflows/job-report-expiry-undeliverable-test-run.yml @@ -0,0 +1,30 @@ +name: Certificate Expiry Undeliverable Report Test Run + +on: + workflow_dispatch: + inputs: + email: + description: What is the email address of the recipient? +jobs: + certificate-expiry-check: + name: Run certificate expiry undeliverable report script + runs-on: ubuntu-latest + permissions: + id-token: write + contents: read + steps: + - name: checkout repo content + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + - name: Python Setup + uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b # v5.3.0 + with: + python-version: '3.11' + - name: Install Python Packages from Requirements File + run: | + pip install pipenv + pipenv install + - name: Execute certificate expiry undeliverable report run + run: pipenv run python3 -m bin.report_certificate_expiry_undeliverable --test ${{ github.event.inputs.email }} + env: + NOTIFY_PROD_API_KEY: ${{ secrets.NOTIFY_PROD_API_KEY }} + \ No newline at end of file diff --git a/bin/check_certificate_expiry.py b/bin/check_certificate_expiry.py index 8151c13a6..762917ba6 100644 --- a/bin/check_certificate_expiry.py +++ b/bin/check_certificate_expiry.py @@ -20,7 +20,6 @@ "CERT_EXPIRY_THRESHOLDS": [30], "CERT_URL_EXTENSION": "v5/certificate/issued-certs", "CERT_REPORT_TEMPLATE_ID": "04b6ca6c-2945-4a0d-a267-53fb61b370ef", - "CERT_EXPIRY_TEMPALATE_ID": "06abd028-0a8f-43d9-a122-90a92f9b62ee" } diff --git a/bin/report_certificate_expiry_undeliverable.py b/bin/report_certificate_expiry_undeliverable.py new file mode 100644 index 000000000..55fe18fe3 --- /dev/null +++ b/bin/report_certificate_expiry_undeliverable.py @@ -0,0 +1,65 @@ +import os +import sys +import logging + +from services.notify_service import NotifyService + +from config.constants import MINISTRY_OF_JUSTICE + +logging.basicConfig( + level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" +) +logger = logging.getLogger(__name__) + + +cert_config = { + "CERT_REPLY_EMAIL": "certificates@digital.justice.gov.uk", + "CERT_EXPIRY_TEMPALATE_ID": "06abd028-0a8f-43d9-a122-90a92f9b62ee", + "CERT_UNDELIVERED_REPORT_TEMPALATE_ID": "6d0e7249-6b2d-4f0e-bf32-657e9300d09e" +} + + +def get_environment_variables(): + + notify_api_key = os.environ.get("NOTIFY_PROD_API_KEY") + if not notify_api_key: + raise ValueError("No NOTIFY_PROD_API_KEY environment variable set") + + return notify_api_key + + +def main(testrun: bool = False, test_email: str = ""): + + notify_api_key = get_environment_variables() + + logger.info("Instantiating services...") + notify_service = NotifyService(cert_config, notify_api_key, MINISTRY_OF_JUSTICE) + + print("Building undelivered email report...") + if undelivered_email_report := notify_service.check_for_undelivered_emails_for_template( + cert_config["CERT_EXPIRY_TEMPALATE_ID"] + ): + if testrun: + logger.info("Building undelivered email report...") + report = notify_service.build_undeliverable_email_report_string_crs( + undelivered_email_report) + logger.info("Sending test undelivered email test report to %s...", test_email) + notify_service.send_report_email_crs( + report, cert_config['CERT_UNDELIVERED_REPORT_TEMPALATE_ID'], test_email) + else: + logger.info("Building undelivered email live report...") + report = notify_service.build_undeliverable_email_report_string_crs( + undelivered_email_report) + logger.info("Sending live undelivered emailreport to Operations Engineering...") + notify_service.send_report_email_crs( + report, cert_config["CERT_UNDELIVERED_REPORT_TEMPALATE_ID"], cert_config["CERT_REPLY_EMAIL"]) + + +if __name__ == "__main__": + if len(sys.argv) > 1 and sys.argv[1] == '--test': + if len(sys.argv) > 2: + main(True, sys.argv[2]) + else: + raise SystemExit('Email address of recipient expected.') + else: + main() diff --git a/services/notify_service.py b/services/notify_service.py index f35bf3198..11540ac2a 100644 --- a/services/notify_service.py +++ b/services/notify_service.py @@ -65,13 +65,13 @@ def send_removed_email(self, email_address: str): ) def check_for_undelivered_first_emails(self): - return self._check_for_undelivered_emails_for_template( + return self.check_for_undelivered_emails_for_template( self._get_first_email_template_id()) def _get_notifications_by_type_and_status(self, template_type, status): return self.client.get_all_notifications(status=status, template_type=template_type) - def _check_for_undelivered_emails_for_template(self, template_id): + def check_for_undelivered_emails_for_template(self, template_id): notifications = self._get_notifications_by_type_and_status('email', 'failed')[ 'notifications'] today = datetime.now(timezone.utc).date() @@ -158,6 +158,14 @@ def build_main_report_string_crs(self, email_parameter_list): for email_parameter in email_parameter_list ) + def build_undeliverable_email_report_string_crs(self, undeliverable_email_list): + return "".join( + f"Email Address: {undeliverable_email['email_address']}\n" + f"Sent at: {undeliverable_email['created_at']}\n" + f"Status: {undeliverable_email['status']} \n\n" + for undeliverable_email in undeliverable_email_list + ) + def send_report_email_crs(self, report, template_id, email): try: self.client.send_email_notification( diff --git a/test/test_services/test_notify_service.py b/test/test_services/test_notify_service.py index e6ef12267..b93265ee2 100644 --- a/test/test_services/test_notify_service.py +++ b/test/test_services/test_notify_service.py @@ -27,7 +27,7 @@ def test_undelivered_emails_not_found_returns_none(self, mock_get_notifications_ "notifications": [] } - result = self.notify_service._check_for_undelivered_emails_for_template( + result = self.notify_service.check_for_undelivered_emails_for_template( self.template_id) self.assertEqual(len(result), 0) @@ -45,7 +45,7 @@ def test_undelivered_emails_found_returns_expected_data(self, mock_get_notificat ] } - result = self.notify_service._check_for_undelivered_emails_for_template( + result = self.notify_service.check_for_undelivered_emails_for_template( self.template_id) self.assertEqual(len(result), 1) @@ -201,7 +201,7 @@ def test_get_first_email_template_id_moj_org(self): self.assertEqual(self.notify_service._get_first_email_template_id( ), "30351e8f-320b-4ebe-b0bf-d6aa0a7c607d") - @patch.object(NotifyService, "_check_for_undelivered_emails_for_template") + @patch.object(NotifyService, "check_for_undelivered_emails_for_template") @patch.object(NotifyService, "_get_first_email_template_id") def test_check_for_undelivered_first_emails(self, mock_get_first_email_template_id, mock_check_for_undelivered_emails_for_template): mock_check_for_undelivered_emails_for_template.return_value = "some-value"