Skip to content

Commit

Permalink
Fixed the postfix to, cc values
Browse files Browse the repository at this point in the history
  • Loading branch information
athiruma committed Apr 10, 2024
1 parent c1e86aa commit 6409fdf
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 7 deletions.
52 changes: 45 additions & 7 deletions cloud_governance/common/mails/postfix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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('[email protected]')
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'):
Expand Down
Empty file.
57 changes: 57 additions & 0 deletions tests/unittest/cloud_governance/common/postfix/test_postfix.py
Original file line number Diff line number Diff line change
@@ -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="[email protected]")
assert response == "[email protected]"


def test_prettify_to_multiple_values():
"""
This method tests the postfix to and cc
:return:
:rtype:
"""
postfix = Postfix()
response = postfix.prettify_to(to="[email protected], test1")
assert response == "[email protected],[email protected]"


def test_prettify_to_with_list():
"""
This method tests the postfix to and cc
:return:
:rtype:
"""
postfix = Postfix()
response = postfix.prettify_to(to=["[email protected]", "test1"])
assert response == "[email protected],[email protected]"


def test_prettify_cc():
"""
This method tests the cc
:return:
:rtype:
"""
postfix = Postfix()
response = postfix.prettify_cc(cc=["[email protected]", "test1"])
assert "[email protected]" in response
assert "[email protected]" in response


def test_prettify_cc_with_to():
"""
This method tests the cc
:return:
:rtype:
"""
postfix = Postfix()
response = postfix.prettify_cc(cc=["[email protected]", "test1"], to="test1, test")
assert not response

0 comments on commit 6409fdf

Please sign in to comment.