From a6766aeb63a8168a02a08ef1d5c5848df0677722 Mon Sep 17 00:00:00 2001 From: Thirumalesh Aaraveti <97395760+athiruma@users.noreply.github.com> Date: Wed, 10 Apr 2024 11:19:29 +0530 Subject: [PATCH] Fixed the postfix to, cc values (#750) --- cloud_governance/common/mails/postfix.py | 52 ++++++++++++++--- .../common/postfix/__init__.py | 0 .../common/postfix/test_postfix.py | 57 +++++++++++++++++++ 3 files changed, 102 insertions(+), 7 deletions(-) create mode 100644 tests/unittest/cloud_governance/common/postfix/__init__.py create mode 100644 tests/unittest/cloud_governance/common/postfix/test_postfix.py diff --git a/cloud_governance/common/mails/postfix.py b/cloud_governance/common/mails/postfix.py index eeb2fe0e..7daecdb4 100644 --- a/cloud_governance/common/mails/postfix.py +++ b/cloud_governance/common/mails/postfix.py @@ -3,6 +3,7 @@ from email.message import EmailMessage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText +from typing import Union, Optional from cloud_governance.common.clouds.aws.s3.s3_operations import S3Operations from cloud_governance.common.elasticsearch.elasticsearch_operations import ElasticSearchOperations @@ -58,6 +59,48 @@ def get_bucket_name(self): bucket_name = self.__policy_output return bucket_name, key + def split_emails(self, email_str: str): + """ + This method split the mail list + :param email_str: + :type email_str: + :return: + :rtype: + """ + splitter = "," if "," in email_str else " " + return [item.strip() for item in email_str.split(splitter)] + + def prettify_to(self, to: Union[str, list]): + """ + This method prettify to + :param to: + :type to: + :return: + :rtype: + """ + + if isinstance(to, str): + to = self.split_emails(email_str=to) + if isinstance(to, list): + return ','.join([item if '@redhat.com' in item else f'{item}@redhat.com' for item in to]) + return to + + def prettify_cc(self, cc: Union[str, list], to: str = ''): + """ + This method prettify cc + :param to: + :type to: + :param cc: + :type cc: + :return: + :rtype: + """ + if isinstance(cc, str): + cc = self.split_emails(email_str=cc) + cc_unique_values = [cc_user if '@redhat.com' in cc_user else f'{cc_user}@redhat.com' for cc_user in cc] + to = self.prettify_to(to=to).split(',') + return ','.join(list(set(cc_unique_values) - set(to))) + @logger_time_stamp def send_email_postfix(self, subject: str, to: any, cc: list, content: str, **kwargs): if self.__email_alert: @@ -67,19 +110,14 @@ def send_email_postfix(self, subject: str, to: any, cc: list, content: str, **kw cc = self.__mail_cc if not self.__ldap_search.get_user_details(user_name=to): cc.append('athiruma@redhat.com') - cc = [cc_user for cc_user in cc if to and to not in cc_user] - cc = [cc_user if '@redhat.com' in cc_user else f'{cc_user}@redhat.com' for cc_user in cc] msg = MIMEMultipart('alternative') msg["Subject"] = subject msg["From"] = "%s <%s>" % ( 'cloud-governance', "@".join(["noreply-cloud-governance", 'redhat.com']), ) - if isinstance(to, str): - msg["To"] = "@".join([to, 'redhat.com']) - elif isinstance(to, list): - msg["To"] = ", ".join(to) - msg["Cc"] = ",".join(cc) + msg["To"] = self.prettify_to(to) + msg["Cc"] = self.prettify_cc(cc=cc, to=to) # msg.add_header("Reply-To", self.reply_to) # msg.add_header("User-Agent", self.reply_to) if kwargs.get('filename'): diff --git a/tests/unittest/cloud_governance/common/postfix/__init__.py b/tests/unittest/cloud_governance/common/postfix/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unittest/cloud_governance/common/postfix/test_postfix.py b/tests/unittest/cloud_governance/common/postfix/test_postfix.py new file mode 100644 index 00000000..d0c1660f --- /dev/null +++ b/tests/unittest/cloud_governance/common/postfix/test_postfix.py @@ -0,0 +1,57 @@ +from cloud_governance.common.mails.postfix import Postfix + + +def test_prettify_to(): + """ + This method tests the postfix to and cc + :return: + :rtype: + """ + postfix = Postfix() + response = postfix.prettify_to(to="test@redhat.com") + assert response == "test@redhat.com" + + +def test_prettify_to_multiple_values(): + """ + This method tests the postfix to and cc + :return: + :rtype: + """ + postfix = Postfix() + response = postfix.prettify_to(to="test@redhat.com, test1") + assert response == "test@redhat.com,test1@redhat.com" + + +def test_prettify_to_with_list(): + """ + This method tests the postfix to and cc + :return: + :rtype: + """ + postfix = Postfix() + response = postfix.prettify_to(to=["test@redhat.com", "test1"]) + assert response == "test@redhat.com,test1@redhat.com" + + +def test_prettify_cc(): + """ + This method tests the cc + :return: + :rtype: + """ + postfix = Postfix() + response = postfix.prettify_cc(cc=["test@redhat.com", "test1"]) + assert "test@redhat.com" in response + assert "test1@redhat.com" in response + + +def test_prettify_cc_with_to(): + """ + This method tests the cc + :return: + :rtype: + """ + postfix = Postfix() + response = postfix.prettify_cc(cc=["test@redhat.com", "test1"], to="test1, test") + assert not response