-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
102 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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('[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'): | ||
|
Empty file.
57 changes: 57 additions & 0 deletions
57
tests/unittest/cloud_governance/common/postfix/test_postfix.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |