From 9bc0c7d24dcff1126659c3d7815bc8b3ec066af7 Mon Sep 17 00:00:00 2001 From: Carlos Quintana <74399022+cquintana92@users.noreply.github.com> Date: Tue, 8 Oct 2024 12:48:53 +0200 Subject: [PATCH] chore: extract alias recipient name into function (#2252) --- app/alias_utils.py | 30 ++++++++++++++++ email_handler.py | 28 ++++++--------- tests/test_alias_utils.py | 73 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 112 insertions(+), 19 deletions(-) diff --git a/app/alias_utils.py b/app/alias_utils.py index e9a27974..f8a9e197 100644 --- a/app/alias_utils.py +++ b/app/alias_utils.py @@ -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) diff --git a/email_handler.py b/email_handler.py index 2baee1ad..d44555b3 100644 --- a/email_handler.py +++ b/email_handler.py @@ -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:;": diff --git a/tests/test_alias_utils.py b/tests/test_alias_utils.py index 2499e5fa..23839c3a 100644 --- a/tests/test_alias_utils.py +++ b/tests/test_alias_utils.py @@ -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}>"