Prechádzať zdrojové kódy

add email_utils.copy()

Son NK 5 rokov pred
rodič
commit
5705842415
2 zmenil súbory, kde vykonal 22 pridanie a 0 odobranie
  1. 6 0
      app/email_utils.py
  2. 16 0
      tests/test_email_utils.py

+ 6 - 0
app/email_utils.py

@@ -1,3 +1,4 @@
+import email
 import os
 from email.header import decode_header
 from email.message import Message
@@ -498,3 +499,8 @@ def parseaddr_unicode(addr) -> (str, str):
             name = decoded_string
 
     return name, email
+
+
+def copy(msg: Message) -> Message:
+    """return a copy of message"""
+    return email.message_from_bytes(msg.as_bytes())

+ 16 - 0
tests/test_email_utils.py

@@ -1,3 +1,4 @@
+import email
 from email.message import EmailMessage
 
 from app.config import MAX_ALERT_24H
@@ -9,6 +10,7 @@ from app.email_utils import (
     add_or_replace_header,
     parseaddr_unicode,
     send_email_with_rate_control,
+    copy,
 )
 from app.extensions import db
 from app.models import User, CustomDomain
@@ -118,3 +120,17 @@ def test_send_email_with_rate_control(flask_client):
     assert not send_email_with_rate_control(
         user, "test alert type", "abcd@gmail.com", "subject", "plaintext"
     )
+
+
+def test_copy():
+    email_str = """
+    From: abcd@gmail.com
+    To: hey@example.org
+    Subject: subject
+    
+    Body    
+    """
+    msg = email.message_from_string(email_str)
+    msg2 = copy(msg)
+
+    assert msg.as_bytes() == msg2.as_bytes()