take into account include_sender_in_reverse_alias when creating reverse-alias
This commit is contained in:
parent
eab09d8c32
commit
ce2d68a64d
|
@ -410,7 +410,7 @@ def create_contact_route(alias_id):
|
|||
alias_id=alias.id,
|
||||
website_email=contact_email,
|
||||
name=contact_name,
|
||||
reply_email=generate_reply_email(contact_email),
|
||||
reply_email=generate_reply_email(contact_email, user),
|
||||
)
|
||||
|
||||
LOG.d("create reverse-alias for %s %s", contact_addr, alias)
|
||||
|
|
|
@ -203,7 +203,7 @@ def alias_contact_manager(alias_id):
|
|||
alias_id=alias.id,
|
||||
website_email=contact_email,
|
||||
name=contact_name,
|
||||
reply_email=generate_reply_email(contact_email),
|
||||
reply_email=generate_reply_email(contact_email, current_user),
|
||||
)
|
||||
|
||||
LOG.d("create reverse-alias for %s", contact_addr)
|
||||
|
|
|
@ -802,7 +802,7 @@ def replace(msg: Message, old, new) -> Message:
|
|||
return msg
|
||||
|
||||
|
||||
def generate_reply_email(contact_email: str) -> str:
|
||||
def generate_reply_email(contact_email: str, user: User) -> str:
|
||||
"""
|
||||
generate a reply_email (aka reverse-alias), make sure it isn't used by any contact
|
||||
"""
|
||||
|
@ -811,7 +811,14 @@ def generate_reply_email(contact_email: str) -> str:
|
|||
# "The maximum total length of a user name or other local-part is 64
|
||||
# octets."
|
||||
|
||||
if contact_email:
|
||||
# todo: turns this to False after Dec 20 2020
|
||||
include_sender_in_reverse_alias = True
|
||||
|
||||
# user has chosen an option explicitly
|
||||
if user.include_sender_in_reverse_alias is not None:
|
||||
include_sender_in_reverse_alias = user.include_sender_in_reverse_alias
|
||||
|
||||
if include_sender_in_reverse_alias and contact_email:
|
||||
# control char: 4 chars (ra+, +)
|
||||
# random suffix: max 10 chars
|
||||
# maximum: 64
|
||||
|
@ -825,13 +832,13 @@ def generate_reply_email(contact_email: str) -> str:
|
|||
|
||||
# not use while to avoid infinite loop
|
||||
for _ in range(1000):
|
||||
if contact_email:
|
||||
if include_sender_in_reverse_alias and contact_email:
|
||||
random_length = random.randint(5, 10)
|
||||
reply_email = (
|
||||
f"ra+{contact_email}+{random_string(random_length)}@{EMAIL_DOMAIN}"
|
||||
)
|
||||
else:
|
||||
random_length = random.randint(10, 50)
|
||||
random_length = random.randint(20, 50)
|
||||
reply_email = f"ra+{random_string(random_length)}@{EMAIL_DOMAIN}"
|
||||
|
||||
if not Contact.get_by(reply_email=reply_email):
|
||||
|
|
|
@ -229,7 +229,7 @@ def get_or_create_contact(from_header: str, mail_from: str, alias: Alias) -> Con
|
|||
name=contact_name,
|
||||
mail_from=mail_from,
|
||||
from_header=from_header,
|
||||
reply_email=generate_reply_email(contact_email)
|
||||
reply_email=generate_reply_email(contact_email, alias.user)
|
||||
if is_valid_email(contact_email)
|
||||
else NOREPLY,
|
||||
)
|
||||
|
@ -292,7 +292,7 @@ def replace_header_when_forward(msg: Message, alias: Alias, header: str):
|
|||
alias_id=alias.id,
|
||||
website_email=contact_email,
|
||||
name=contact_name,
|
||||
reply_email=generate_reply_email(contact_email),
|
||||
reply_email=generate_reply_email(contact_email, alias.user),
|
||||
is_cc=header.lower() == "cc",
|
||||
from_header=addr,
|
||||
)
|
||||
|
|
|
@ -25,6 +25,7 @@ from app.email_utils import (
|
|||
)
|
||||
from app.extensions import db
|
||||
from app.models import User, CustomDomain
|
||||
from tests.utils import login
|
||||
|
||||
|
||||
def test_get_email_domain_part():
|
||||
|
@ -464,23 +465,50 @@ def test_to_bytes():
|
|||
|
||||
|
||||
def test_generate_reply_email(flask_client):
|
||||
reply_email = generate_reply_email("test@example.org")
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
)
|
||||
reply_email = generate_reply_email("test@example.org", user)
|
||||
# return something like
|
||||
# ra+test.at.example.org+gjbnnddll@sl.local
|
||||
assert reply_email.startswith("ra+test.at.example.org+")
|
||||
# ra+<random>@sl.local
|
||||
assert reply_email.endswith(EMAIL_DOMAIN)
|
||||
|
||||
reply_email = generate_reply_email("")
|
||||
reply_email = generate_reply_email("", user)
|
||||
# return something like
|
||||
# ra+qdrcxzppngmvtajklnhqvvuyyzgkyityrzjwikk@sl.local
|
||||
assert reply_email.startswith("ra+")
|
||||
assert reply_email.endswith(EMAIL_DOMAIN)
|
||||
|
||||
reply_email = generate_reply_email("👌汉字@example.org")
|
||||
|
||||
def test_generate_reply_email_include_sender_in_reverse_alias(flask_client):
|
||||
# user enables include_sender_in_reverse_alias
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
password="password",
|
||||
name="Test User",
|
||||
activated=True,
|
||||
include_sender_in_reverse_alias=True,
|
||||
)
|
||||
reply_email = generate_reply_email("test@example.org", user)
|
||||
# return something like
|
||||
# ra+test.at.example.org+gjbnnddll@sl.local
|
||||
assert reply_email.startswith("ra+test.at.example.org+")
|
||||
assert reply_email.endswith(EMAIL_DOMAIN)
|
||||
|
||||
reply_email = generate_reply_email("", user)
|
||||
# return something like
|
||||
# ra+qdrcxzppngmvtajklnhqvvuyyzgkyityrzjwikk@sl.local
|
||||
assert reply_email.startswith("ra+")
|
||||
assert reply_email.endswith(EMAIL_DOMAIN)
|
||||
|
||||
reply_email = generate_reply_email("👌汉字@example.org", user)
|
||||
assert reply_email.startswith("ra+yizi.at.example.org+")
|
||||
|
||||
# make sure reply_email only contain lowercase
|
||||
reply_email = generate_reply_email("TEST@example.org")
|
||||
reply_email = generate_reply_email("TEST@example.org", user)
|
||||
assert reply_email.startswith("ra+test.at.example.org")
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue