Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: less amazon forwarding email validation #960

Merged
merged 4 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading