Skip to content

Commit

Permalink
Merge pull request #4974 from ministryofjustice/certificates_renewal_…
Browse files Browse the repository at this point in the history
…migration

certificate undeliverable report migrated
  • Loading branch information
abachleda-baca authored Nov 5, 2024
2 parents e00276b + 2c2f7b6 commit 9fcce2b
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 6 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/job-report-expiry-undeliverable-test-run.yml
Original file line number Diff line number Diff line change
@@ -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 }}

1 change: 0 additions & 1 deletion bin/check_certificate_expiry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}


Expand Down
65 changes: 65 additions & 0 deletions bin/report_certificate_expiry_undeliverable.py
Original file line number Diff line number Diff line change
@@ -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()
12 changes: 10 additions & 2 deletions services/notify_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions test/test_services/test_notify_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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

0 comments on commit 9fcce2b

Please sign in to comment.