Skip to content

Commit

Permalink
refactor: less amazon forwarding email validation (#960)
Browse files Browse the repository at this point in the history
* refactor: less amazon forwarding email validation

* formatting

* remove unused import

* more linting
  • Loading branch information
firstof9 authored Jul 26, 2024
1 parent ddd3f20 commit 4a39728
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
18 changes: 7 additions & 11 deletions custom_components/mail_and_packages/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Adds config flow for Mail and Packages."""

import logging
import re
from os import path
from typing import Any

Expand Down Expand Up @@ -54,6 +53,9 @@
IMAP_SECURITY = ["none", "startTLS", "SSL"]
AMAZON_SENSORS = ["amazon_packages", "amazon_delivered", "amazon_exception"]
_LOGGER = logging.getLogger(__name__)
AMAZON_EMAIL_ERROR = (
"Amazon domain found in email: %s, this may cause errors when searching emails."
)


async def _check_amazon_forwards(forwards: str, domain: str) -> tuple:
Expand All @@ -69,14 +71,12 @@ async def _check_amazon_forwards(forwards: str, domain: str) -> tuple:
email = email.strip()

if "@" in email:
if not re.match(r"[a-zA-Z\.0-9\s]+@", email):
_LOGGER.error("Invalid email address: %s", email)
errors.append("invalid_email_format")

# Check for amazon domains
if f"@{domain}" in email:
_LOGGER.error("Invalid domain for email: %s", email)
errors.append("amazon_domain")
_LOGGER.error(
AMAZON_EMAIL_ERROR,
email,
)

# No forwards
elif forwards in ["", "(none)", '""']:
Expand All @@ -85,10 +85,6 @@ async def _check_amazon_forwards(forwards: str, domain: str) -> tuple:
else:
_LOGGER.error("Missing '@' in email address: %s", email)
errors.append("invalid_email_format")
# Add error message for Amazon emails
if re.match(rf"\b{domain}\b", email):
_LOGGER.error("Invalid domain for email: %s", email)
errors.append("amazon_domain")

if len(errors) == 0:
errors.append("ok")
Expand Down
10 changes: 5 additions & 5 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1528,10 +1528,11 @@ async def test_form_amazon_error(
result = await hass.config_entries.flow.async_configure(
result["flow_id"], input_3
)
assert result["type"] == "form"
assert result["step_id"] == step_id_3
assert "Invalid domain for email: [email protected]" in caplog.text
assert result["errors"] == {CONF_AMAZON_FWDS: "amazon_domain"}
assert result["type"] == "create_entry"
assert (
"Amazon domain found in email: [email protected], this may cause errors when searching emails."
in caplog.text
)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -1641,7 +1642,6 @@ async def test_form_amazon_error_2(
assert result["type"] == "form"
assert result["step_id"] == step_id_3
assert "Missing '@' in email address: amazon.com" in caplog.text
assert "Invalid domain for email: amazon.com" in caplog.text
assert result["errors"] == {CONF_AMAZON_FWDS: "invalid_email_format"}


Expand Down

0 comments on commit 4a39728

Please sign in to comment.