mirror of
https://github.com/simple-login/app.git
synced 2024-11-16 17:08:30 +01:00
chore: extract alias recipient name into function (#2252)
This commit is contained in:
parent
eac73c4e8b
commit
9bc0c7d24d
3 changed files with 112 additions and 19 deletions
|
@ -1,6 +1,7 @@
|
|||
import csv
|
||||
from io import StringIO
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional, Tuple
|
||||
|
||||
from email_validator import validate_email, EmailNotValidError
|
||||
|
@ -23,6 +24,7 @@ from app.email_utils import (
|
|||
send_cannot_create_domain_alias,
|
||||
send_email,
|
||||
render,
|
||||
sl_formataddr,
|
||||
)
|
||||
from app.errors import AliasInTrashError
|
||||
from app.events.event_dispatcher import EventDispatcher
|
||||
|
@ -541,3 +543,31 @@ def change_alias_status(alias: Alias, enabled: bool, commit: bool = False):
|
|||
|
||||
if commit:
|
||||
Session.commit()
|
||||
|
||||
|
||||
@dataclass
|
||||
class AliasRecipientName:
|
||||
name: str
|
||||
message: Optional[str] = None
|
||||
|
||||
|
||||
def get_alias_recipient_name(alias: Alias) -> AliasRecipientName:
|
||||
"""
|
||||
Logic:
|
||||
1. If alias has name, use it
|
||||
2. If alias has custom domain, and custom domain has name, use it
|
||||
3. Otherwise, use the alias email as the recipient
|
||||
"""
|
||||
if alias.name:
|
||||
return AliasRecipientName(
|
||||
name=sl_formataddr((alias.name, alias.email)),
|
||||
message=f"Put alias name {alias.name} in from header",
|
||||
)
|
||||
elif alias.custom_domain:
|
||||
if alias.custom_domain.name:
|
||||
return AliasRecipientName(
|
||||
name=sl_formataddr((alias.custom_domain.name, alias.email)),
|
||||
message=f"Put domain default alias name {alias.custom_domain.name} in from header",
|
||||
)
|
||||
else:
|
||||
return AliasRecipientName(name=alias.email)
|
||||
|
|
|
@ -53,7 +53,11 @@ from flanker.addresslib.address import EmailAddress
|
|||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from app import pgp_utils, s3, config, contact_utils
|
||||
from app.alias_utils import try_auto_create, change_alias_status
|
||||
from app.alias_utils import (
|
||||
try_auto_create,
|
||||
change_alias_status,
|
||||
get_alias_recipient_name,
|
||||
)
|
||||
from app.config import (
|
||||
EMAIL_DOMAIN,
|
||||
URL,
|
||||
|
@ -1161,23 +1165,11 @@ def handle_reply(envelope, msg: Message, rcpt_to: str) -> (bool, str):
|
|||
|
||||
Session.commit()
|
||||
|
||||
# make the email comes from alias
|
||||
from_header = alias.email
|
||||
# add alias name from alias
|
||||
if alias.name:
|
||||
LOG.d("Put alias name %s in from header", alias.name)
|
||||
from_header = sl_formataddr((alias.name, alias.email))
|
||||
elif alias.custom_domain:
|
||||
# add alias name from domain
|
||||
if alias.custom_domain.name:
|
||||
LOG.d(
|
||||
"Put domain default alias name %s in from header",
|
||||
alias.custom_domain.name,
|
||||
)
|
||||
from_header = sl_formataddr((alias.custom_domain.name, alias.email))
|
||||
|
||||
LOG.d("From header is %s", from_header)
|
||||
add_or_replace_header(msg, headers.FROM, from_header)
|
||||
recipient_name = get_alias_recipient_name(alias)
|
||||
if recipient_name.message:
|
||||
LOG.d(recipient_name.message)
|
||||
LOG.d("From header is %s", recipient_name.name)
|
||||
add_or_replace_header(msg, headers.FROM, recipient_name.name)
|
||||
|
||||
try:
|
||||
if str(msg[headers.TO]).lower() == "undisclosed-recipients:;":
|
||||
|
|
|
@ -4,6 +4,7 @@ from app.alias_utils import (
|
|||
delete_alias,
|
||||
check_alias_prefix,
|
||||
get_user_if_alias_would_auto_create,
|
||||
get_alias_recipient_name,
|
||||
try_auto_create,
|
||||
)
|
||||
from app.config import ALIAS_DOMAINS
|
||||
|
@ -18,7 +19,8 @@ from app.models import (
|
|||
User,
|
||||
DomainDeletedAlias,
|
||||
)
|
||||
from tests.utils import create_new_user, random_domain, random_token
|
||||
from app.utils import random_string
|
||||
from tests.utils import create_new_user, random_domain, random_token, random_email
|
||||
|
||||
|
||||
def test_delete_alias(flask_client):
|
||||
|
@ -131,3 +133,72 @@ def test_auto_create_alias(flask_client):
|
|||
assert result, f"Case {test_id} - Failed address {address}"
|
||||
else:
|
||||
assert result is None, f"Case {test_id} - Failed address {address}"
|
||||
|
||||
|
||||
# get_alias_recipient_name
|
||||
def test_get_alias_recipient_name_no_overrides():
|
||||
user = create_new_user()
|
||||
alias = Alias.create(
|
||||
user_id=user.id,
|
||||
email=random_email(),
|
||||
mailbox_id=user.default_mailbox_id,
|
||||
commit=True,
|
||||
)
|
||||
res = get_alias_recipient_name(alias)
|
||||
assert res.message is None
|
||||
assert res.name == alias.email
|
||||
|
||||
|
||||
def test_get_alias_recipient_name_alias_name():
|
||||
user = create_new_user()
|
||||
alias = Alias.create(
|
||||
user_id=user.id,
|
||||
email=random_email(),
|
||||
mailbox_id=user.default_mailbox_id,
|
||||
name=random_string(),
|
||||
commit=True,
|
||||
)
|
||||
res = get_alias_recipient_name(alias)
|
||||
assert res.message is not None
|
||||
assert res.name == f"{alias.name} <{alias.email}>"
|
||||
|
||||
|
||||
def test_get_alias_recipient_alias_with_name_and_custom_domain_name():
|
||||
user = create_new_user()
|
||||
custom_domain = CustomDomain.create(
|
||||
user_id=user.id,
|
||||
domain=random_domain(),
|
||||
name=random_string(),
|
||||
verified=True,
|
||||
)
|
||||
alias = Alias.create(
|
||||
user_id=user.id,
|
||||
email=random_email(),
|
||||
mailbox_id=user.default_mailbox_id,
|
||||
name=random_string(),
|
||||
custom_domain_id=custom_domain.id,
|
||||
commit=True,
|
||||
)
|
||||
res = get_alias_recipient_name(alias)
|
||||
assert res.message is not None
|
||||
assert res.name == f"{alias.name} <{alias.email}>"
|
||||
|
||||
|
||||
def test_get_alias_recipient_alias_without_name_and_custom_domain_name():
|
||||
user = create_new_user()
|
||||
custom_domain = CustomDomain.create(
|
||||
user_id=user.id,
|
||||
domain=random_domain(),
|
||||
name=random_string(),
|
||||
verified=True,
|
||||
)
|
||||
alias = Alias.create(
|
||||
user_id=user.id,
|
||||
email=random_email(),
|
||||
mailbox_id=user.default_mailbox_id,
|
||||
custom_domain_id=custom_domain.id,
|
||||
commit=True,
|
||||
)
|
||||
res = get_alias_recipient_name(alias)
|
||||
assert res.message is not None
|
||||
assert res.name == f"{custom_domain.name} <{alias.email}>"
|
||||
|
|
Loading…
Reference in a new issue