Fix some bugs and enhance some methods to support unit tests.

This commit is contained in:
Christopher Broderick 2023-07-23 06:13:24 +01:00
parent 2e5697c11a
commit f7381d5b62

View File

@ -153,21 +153,21 @@ def generate_file_mime_part(locale: str="en_US",filename: str = None, content: s
encoders.encode_base64(part) encoders.encode_base64(part)
if not filename: if not filename:
filename = get_fake("word", locale=locale, min_length=8) + ".txt" filename = get_fake("word", locale=locale, min_length=8) + ".txt"
part.add_header('Content-Disposition', "attachment; filename= %s" % filename) part.add_header('Content-Disposition', "attachment; filename=%s" % filename)
return part return part
def generate_image_mime_part(locale: str="en_US",imagename: str = None) -> Message: def generate_image_mime_part(locale: str="en_US",imagename: str = None, disposition_primary_type: str = "attachment") -> Message:
""" """
:param locale: change this to generate locale specific file name and attachment content :param locale: change this to generate locale specific file name and attachment content
:param filename: pass a file name if you want to specify a specific name otherwise a random name will be generated :param filename: pass a file name if you want to specify a specific name otherwise a random name will be generated
""" """
part = MIMEImage(generate_random_image(image_format="JPEG", array_dims=(200, 200))) part = MIMEImage(generate_random_image(image_format="JPEG", array_dims=(200, 200)))
part.set_payload(get_fake("text", locale=locale, min_length=1024)) #part.set_payload(get_fake("text", locale=locale, min_length=1024))
encoders.encode_base64(part) encoders.encode_base64(part)
if not imagename: if not imagename:
imagename = get_fake("word", locale=locale, min_length=8) + ".jpg" imagename = get_fake("word", locale=locale, min_length=8) + ".jpg"
part.add_header('Content-Disposition', "attachment; filename= %s" % imagename) part.add_header('Content-Disposition', disposition_primary_type + "; filename= %s" % imagename)
return part return part
def generate_email_list(address_cnt: int = 3, def generate_email_list(address_cnt: int = 3,
@ -215,10 +215,10 @@ def generate_mime_part(locale: str="en_US",
""" """
if "plain" == part_type: if "plain" == part_type:
body = get_fake("text", locale=locale, min_length=1024) body = get_fake("text", locale=locale, min_length=1024)
msg = MIMEText(body) msg = MIMEText(body, part_type)
elif "html" == part_type: elif "html" == part_type:
body = get_fake_html(locale=locale, wrap_in_body_tag=True) body = get_fake_html(locale=locale, wrap_in_body_tag=True)
msg = MIMEText(body) msg = MIMEText(body, part_type)
elif "file" == part_type: elif "file" == part_type:
msg = generate_file_mime_part(locale=locale) msg = generate_file_mime_part(locale=locale)
elif "image" == part_type: elif "image" == part_type:
@ -229,6 +229,7 @@ def generate_mime_part(locale: str="en_US",
def generate_multipart_email(locale: str="en_US", def generate_multipart_email(locale: str="en_US",
type_list: typing.List[str]=["plain", "html", "image"], type_list: typing.List[str]=["plain", "html", "image"],
sub_type: str = None,
use_short_email: bool=False use_short_email: bool=False
) -> typing.Tuple[Message, typing.Tuple[str, str], typing.Tuple[str, str]]: ) -> typing.Tuple[Message, typing.Tuple[str, str], typing.Tuple[str, str]]:
""" """
@ -236,9 +237,10 @@ def generate_multipart_email(locale: str="en_US",
:param locale: :param locale:
:param type_list: options are plain, html, image (attachment), file (attachment) :param type_list: options are plain, html, image (attachment), file (attachment)
:param sub_type: multipart sub type that defaults to "mixed" if not specified
:param use_short_email: produces a "To" or "From" that is only the email address if True :param use_short_email: produces a "To" or "From" that is only the email address if True
""" """
msg = MIMEMultipart() msg = MIMEMultipart(_subtype=sub_type) if sub_type else MIMEMultipart()
for part_type in type_list: for part_type in type_list:
msg.attach(generate_mime_part(locale=locale, part_type=part_type)) msg.attach(generate_mime_part(locale=locale, part_type=part_type))
from_meta, to_meta = add_simple_email_headers(msg, locale=locale, use_short_email=use_short_email) from_meta, to_meta = add_simple_email_headers(msg, locale=locale, use_short_email=use_short_email)