2020-02-07 14:56:55 +00:00
|
|
|
from email.message import EmailMessage
|
|
|
|
|
2019-12-30 17:18:10 +00:00
|
|
|
from app.email_utils import (
|
|
|
|
get_email_part,
|
|
|
|
get_email_domain_part,
|
2020-01-22 09:13:58 +00:00
|
|
|
email_belongs_to_alias_domains,
|
2020-01-25 15:40:30 +00:00
|
|
|
can_be_used_as_personal_email,
|
2020-02-07 14:56:55 +00:00
|
|
|
delete_header,
|
2020-02-07 15:02:33 +00:00
|
|
|
add_or_replace_header,
|
2019-12-30 17:18:10 +00:00
|
|
|
)
|
2020-01-25 15:40:30 +00:00
|
|
|
from app.extensions import db
|
|
|
|
from app.models import User, CustomDomain
|
2019-11-19 20:47:58 +00:00
|
|
|
|
|
|
|
|
2019-12-09 21:26:20 +00:00
|
|
|
def test_get_email_part():
|
|
|
|
assert get_email_part("First Last <ab@cd.com>") == "ab@cd.com"
|
|
|
|
assert get_email_part("First Last<ab@cd.com>") == "ab@cd.com"
|
|
|
|
assert get_email_part(" First Last <ab@cd.com>") == "ab@cd.com"
|
|
|
|
assert get_email_part("ab@cd.com") == "ab@cd.com"
|
2019-12-30 17:18:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_email_domain_part():
|
|
|
|
assert get_email_domain_part("ab@cd.com") == "cd.com"
|
2020-01-22 09:13:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
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")
|
2020-01-25 15:40:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_can_be_used_as_personal_email(flask_client):
|
|
|
|
# default alias domain
|
|
|
|
assert not can_be_used_as_personal_email("ab@sl.local")
|
|
|
|
assert not can_be_used_as_personal_email("hey@d1.test")
|
|
|
|
|
|
|
|
assert can_be_used_as_personal_email("hey@ab.cd")
|
|
|
|
# custom domain
|
|
|
|
user = User.create(
|
|
|
|
email="a@b.c", password="password", name="Test User", activated=True
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
CustomDomain.create(user_id=user.id, domain="ab.cd", verified=True)
|
|
|
|
db.session.commit()
|
|
|
|
assert not can_be_used_as_personal_email("hey@ab.cd")
|
2020-02-07 14:56:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_delete_header():
|
|
|
|
msg = EmailMessage()
|
|
|
|
assert msg._headers == []
|
|
|
|
|
|
|
|
msg["H"] = "abcd"
|
|
|
|
msg["H"] = "xyzt"
|
|
|
|
|
|
|
|
assert msg._headers == [("H", "abcd"), ("H", "xyzt")]
|
|
|
|
|
|
|
|
delete_header(msg, "H")
|
|
|
|
assert msg._headers == []
|
2020-02-07 15:02:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_add_or_replace_header():
|
|
|
|
msg = EmailMessage()
|
|
|
|
msg["H"] = "abcd"
|
|
|
|
msg["H"] = "xyzt"
|
|
|
|
assert msg._headers == [("H", "abcd"), ("H", "xyzt")]
|
|
|
|
|
|
|
|
add_or_replace_header(msg, "H", "new")
|
|
|
|
assert msg._headers == [("H", "new")]
|