Skip to content

Commit

Permalink
fix: fix string validation on amazon forwards config (#946)
Browse files Browse the repository at this point in the history
* fix: fix string validation on amazon forwards config

* clean up commented code
  • Loading branch information
firstof9 authored Jul 12, 2024
1 parent 7b636c0 commit c7a9a55
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
10 changes: 1 addition & 9 deletions custom_components/mail_and_packages/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,17 @@ async def _check_amazon_forwards(forwards: str) -> tuple:
Returns tuple: dict of errors, list of email addresses
"""
amazon_forwards_list = []
amazon_forwards_list = forwards
errors = []

# Check for amazon domains
if "@amazon" in forwards:
errors.append("amazon_domain")

# Check for commas
if "," in forwards:
amazon_forwards_list = forwards.split(",")

# No forwards
elif forwards in ["", "(none)", '""']:
amazon_forwards_list = []

# If only one address append it to the list
elif forwards:
amazon_forwards_list.append(forwards)

if len(errors) == 0:
errors.append("ok")

Expand Down
11 changes: 9 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
from aioresponses import aioresponses
from pytest_homeassistant_custom_component.common import MockConfigEntry

from custom_components.mail_and_packages.const import CONF_AMAZON_DOMAIN, DOMAIN
from custom_components.mail_and_packages.const import (
CONF_AMAZON_DOMAIN,
CONFIG_VERSION,
DOMAIN,
)
from tests.const import (
FAKE_CONFIG_DATA,
FAKE_CONFIG_DATA_AMAZON_FWD_STRING,
Expand Down Expand Up @@ -52,7 +56,10 @@ def mock_update():
async def integration_fixture(hass):
"""Set up the mail_and_packages integration."""
entry = MockConfigEntry(
domain=DOMAIN, title="imap.test.email", data=FAKE_CONFIG_DATA
domain=DOMAIN,
title="imap.test.email",
data=FAKE_CONFIG_DATA,
version=CONFIG_VERSION,
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
Expand Down
4 changes: 2 additions & 2 deletions tests/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"gif_duration": 5,
"host": "imap.test.email",
"image_name": "mail_today.gif",
"image_path": "/config/www/mail_and_packages/",
"image_path": "custom_components/mail_and_packages/images/",
"image_security": True,
"imap_security": "SSL",
"imap_timeout": 30,
Expand Down Expand Up @@ -256,7 +256,7 @@
"allow_external": False,
"amazon_days": 3,
"amazon_domain": "amazon.com",
"amazon_fwds": ["fakeuser@fake.email", "fakeuser2@fake.email"],
"amazon_fwds": "fakeuser@fake.email, fakeuser2@fake.email",
"custom_img": False,
"folder": '"INBOX"',
"generate_mp4": False,
Expand Down
11 changes: 5 additions & 6 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
""" Test Mail and Packages config flow """

from unittest.mock import patch

import logging
from unittest.mock import patch

import pytest
from homeassistant import config_entries, setup
Expand Down Expand Up @@ -89,7 +88,7 @@
"allow_external": False,
"amazon_days": 3,
"amazon_domain": "amazon.com",
"amazon_fwds": ["fakeuser@test.email", "fakeuser2@test.email"],
"amazon_fwds": "fakeuser@test.email,fakeuser2@test.email",
"custom_img": True,
"custom_img_file": "images/test.gif",
"host": "imap.test.email",
Expand Down Expand Up @@ -1598,7 +1597,7 @@ async def test_form_amazon_error(
"allow_external": False,
"amazon_days": 3,
"amazon_domain": "amazon.com",
"amazon_fwds": ["fakeuser@test.email", "fakeuser2@test.email"],
"amazon_fwds": "fakeuser@test.email,fakeuser2@test.email",
"custom_img": True,
"custom_img_file": "images/test.gif",
"host": "imap.test.email",
Expand Down Expand Up @@ -1778,7 +1777,7 @@ async def test_reconfigure(
"allow_external": False,
"amazon_days": 3,
"amazon_domain": "amazon.com",
"amazon_fwds": ["fakeuser@fake.email", "fakeuser2@fake.email"],
"amazon_fwds": "fakeuser@fake.email, fakeuser2@fake.email",
"custom_img": True,
"custom_img_file": "images/test.gif",
"host": "imap.test.email",
Expand Down Expand Up @@ -1943,7 +1942,7 @@ async def test_reconfigure_no_amazon(
"allow_external": False,
"amazon_days": 3,
"amazon_domain": "amazon.com",
"amazon_fwds": ["fakeuser@fake.email", "fakeuser2@fake.email"],
"amazon_fwds": "fakeuser@fake.email, fakeuser2@fake.email",
"custom_img": False,
"host": "imap.test.email",
"port": 993,
Expand Down
5 changes: 4 additions & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,10 @@ async def test_amazon_search_delivered(
result = amazon_search(
mock_imap_amazon_delivered, "test/path", hass, "testfilename.jpg", "amazon.com"
)
assert "Amazon email search address: ['order-update@amazon.com', 'update-bestelling@amazon.com']" in caplog.text
assert (
"Amazon email search address: ['order-update@amazon.com', 'update-bestelling@amazon.com']"
in caplog.text
)
assert result == 7
assert mock_download_img.called

Expand Down

0 comments on commit c7a9a55

Please sign in to comment.