Pārlūkot izejas kodu

add email_belongs_to_alias_domains() to verify if an email belongs to one of the alias domains

Son NK 5 gadi atpakaļ
vecāks
revīzija
d7ed0d77bd
2 mainītis faili ar 20 papildinājumiem un 0 dzēšanām
  1. 10 0
      app/email_utils.py
  2. 10 0
      tests/test_email_utils.py

+ 10 - 0
app/email_utils.py

@@ -14,6 +14,7 @@ from app.config import (
     DKIM_SELECTOR,
     DKIM_SELECTOR,
     DKIM_PRIVATE_KEY,
     DKIM_PRIVATE_KEY,
     DKIM_HEADERS,
     DKIM_HEADERS,
+    ALIAS_DOMAINS,
 )
 )
 from app.log import LOG
 from app.log import LOG
 
 
@@ -253,3 +254,12 @@ def delete_header(msg: Message, header: str):
     for h in msg._headers:
     for h in msg._headers:
         if h[0].lower() == header.lower():
         if h[0].lower() == header.lower():
             msg._headers.remove(h)
             msg._headers.remove(h)
+
+
+def email_belongs_to_alias_domains(email: str) -> bool:
+    """return True if an emails ends with one of the alias domains provided by SimpleLogin"""
+    for domain in ALIAS_DOMAINS:
+        if email.endswith("@" + domain):
+            return True
+
+    return False

+ 10 - 0
tests/test_email_utils.py

@@ -3,6 +3,7 @@ from app.email_utils import (
     get_email_part,
     get_email_part,
     get_email_local_part,
     get_email_local_part,
     get_email_domain_part,
     get_email_domain_part,
+    email_belongs_to_alias_domains,
 )
 )
 
 
 
 
@@ -26,3 +27,12 @@ def test_get_email_local_part():
 
 
 def test_get_email_domain_part():
 def test_get_email_domain_part():
     assert get_email_domain_part("ab@cd.com") == "cd.com"
     assert get_email_domain_part("ab@cd.com") == "cd.com"
+
+
+def test_email_belongs_to_alias_domains():
+    # default alias domain
+    assert email_belongs_to_alias_domains("ab@sl.local")
+    assert not email_belongs_to_alias_domains("ab@not-exist.local")
+
+    assert email_belongs_to_alias_domains("hey@d1.test")
+    assert not email_belongs_to_alias_domains("hey@d3.test")