diff --git a/custom_components/mail_and_packages/helpers.py b/custom_components/mail_and_packages/helpers.py index c0c3ffe4..93a50027 100644 --- a/custom_components/mail_and_packages/helpers.py +++ b/custom_components/mail_and_packages/helpers.py @@ -1193,6 +1193,15 @@ def get_items( ) continue email_msg = email_msg.decode("utf-8", "ignore") + + # Check message body for order number + if ( + (found := pattern.findall(email_msg)) + and len(found) > 0 + and found[0] not in order_number + ): + order_number.append(found[0]) + searches = const.AMAZON_TIME_PATTERN.split(",") for search in searches: _LOGGER.debug("Looking for: %s", search) diff --git a/tests/conftest.py b/tests/conftest.py index a817281a..81dc2805 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -480,6 +480,31 @@ def mock_imap_amazon_shipped_uk(): yield mock_conn +@pytest.fixture() +def mock_imap_amazon_shipped_uk_2(): + """Mock imap class values.""" + with patch( + "custom_components.mail_and_packages.helpers.imaplib" + ) as mock_imap_amazon_shipped: + mock_conn = mock.Mock(spec=imaplib.IMAP4_SSL) + mock_imap_amazon_shipped.IMAP4_SSL.return_value = mock_conn + + mock_conn.login.return_value = ( + "OK", + [b"user@fake.email authenticated (Success)"], + ) + mock_conn.list.return_value = ( + "OK", + [b'(\\HasNoChildren) "/" "INBOX"'], + ) + mock_conn.search.return_value = ("OK", [b"1"]) + f = open("tests/test_emails/amazon_uk_shipped_2.eml", "r") + email_file = f.read() + mock_conn.fetch.return_value = ("OK", [(b"", email_file.encode("utf-8"))]) + mock_conn.select.return_value = ("OK", []) + yield mock_conn + + @pytest.fixture() def mock_imap_amazon_shipped_alt(): """Mock imap class values.""" diff --git a/tests/test_emails/amazon_uk_shipped_2.eml b/tests/test_emails/amazon_uk_shipped_2.eml new file mode 100644 index 00000000..206ccc45 --- /dev/null +++ b/tests/test_emails/amazon_uk_shipped_2.eml @@ -0,0 +1,1481 @@ +From: "Amazon.co.uk" +Reply-To: no-reply@amazon.co.uk +To: testuser@hotmail.com +Subject: Your Amazon.co.uk order of "30 Ft Navy Blue Party..." has been + dispatched +Content-Type: multipart/alternative; + boundary="----=_Part_51705775_653353746.1636995720703" +Date: Mon, 15 Nov 2021 17:02:00 +0000 +MIME-Version: 1.0 + +------=_Part_51705775_653353746.1636995720703 +Content-Type: text/plain; charset=utf-8 +Content-Transfer-Encoding: quoted-printable + +Amazon.co.uk Dispatch Confirmation +Order: #123-4567890-1234567 +https://www.amazon.co.uk/ref=3DTE_tex_g + +___________________________________________________________________________ + +Hello, +We thought you'd like to know that we've dispatched this portion of your or= +der separately to give you a quicker service, at no additional cost to you.= + The remainder of your order will follow as soon as those items become avai= +lable. + + +Arriving: +Tuesday, November 16 + + +Track your item(s): https://www.amazon.co.uk/gp/css/shiptrack/view.html/ref= +=3DTE_typ?ie=3DUTF8&addressID=3Djolpmtkpkplq&latestArrivalDate=3D1637092800= +&orderID=REMOVE_FOR_SECURITY&shipmentDate=3D1636995337&orderingShipmentId= +=REMOVED_FOR_SECURITY&packageId=3D1 + + +Your Amazon Locker order has been sent to the chosen pick-up location: + +Amazon Hub Locker - REMOVED_FOR_SECURITY + + +Order Total: =C2=A311.99 +Paid by Visa: =C2=A311.99 + +Your item(s) will be delivered to the locker location that you have selecte= +d.Your tracking number is: CHANGED_FOR_SECURITY. Depending on the delivery method y= +ou chose, it's possible that the tracking information might not be visible = +immediately. Once your order arrives at the Locker, you'll receive an email= + with instructions on how to collect it. + +Learn more: https://www.amazon.co.uk/locker?ref=3DTE_tex_loc + + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= +=3D + +Order summary +1 X 30 Ft Navy Blue Party Decorations Glitter Metallic Paper Royal Blue Tri= +angle Banner Flag Garland Pennant Bunting for Birthday Baby Shower Graduati= +on A +Sold by MZY Co., Ltd +=C2=A311.99 +___________________________________________________________________________ +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= +=3D + +It's easy to return an item. Visit our Online Returns Centre:=20 +https://www.amazon.co.uk/returns-support?ref=3DTE_tex_r + +Learn how to recycle your packaging at Amazon Second Chance(https://www.ama= +zon.co.uk/amsc?ref_=3Dascyorn). +If you need further assistance with your order, please visit our Customer S= +ervice Help page at: https://www.amazon.co.uk/customerservice?ref=3DTE_tex_= +cs=20 + +We hope to see you again soon. +Amazon.co.uk +___________________________________________________________________________ + +You can cancel this order within 14 days, beginning from the day you receiv= +e the product (subject to certain exceptions: https://www.amazon.co.uk/gp/h= +elp/customer/display/detail.html?nodeId=3D1040616). We will reimburse all p= +ayments received from you for the goods purchased and will also reimburse o= +utbound delivery charges (for the least expensive type of delivery offered = +by us). You will be responsible for the cost of returning the product to us= + unless we delivered it to you in error, it is faulty, or you purchased sho= +es, clothing and accessories (check our Returns Policy: https://www.amazon.= +co.uk/gp/help/customer/display/detail.html?nodeId=3D1161002).You may be sub= +ject to increased return costs if the product can=E2=80=99t be returned nor= +mally by post.=20 + +You can request a cancellation by visiting our Returns Support Centre: http= +s://www.amazon.co.uk/returns; by contacting us: www.amazon.co.uk/contactus;= + or completing this form: https://www.amazon.co.uk/cancellationform and sen= +ding it by post.=20 + +Please also see our Returns Policy: https://www.amazon.co.uk/gp/help/custom= +er/display/detail.html?nodeId=3D1161002 to learn more about our 30 day retu= +rns guarantee which outlines that you can return items for a full refund of= + the item price within 30 days. + +Amazon EU, Soci=C3=A9t=C3=A9 =C3=A0 responsabilit=C3=A9 limit=C3=A9e, 38 av= +enue John F. Kennedy, L-1855 Luxembourg. Share capital: EUR 125.000; Regist= +ered in Luxembourg; RCS Luxembourg No: B 101818; Business Licence Number: 1= +34248; Luxembourg VAT Registration Number: LU 20260743. + +Learn more about your statutory rights here: https://www.amazon.co.uk/Statu= +toryRights?ref=3DTE_tex_sr. + +Please note: This email was sent from a notification-only address that can'= +t accept incoming email. Please do not reply to this message.=20 +------=_Part_51705775_653353746.1636995720703 +Content-Type: text/html; charset=utf-8 +Content-Transfer-Encoding: quoted-printable + + +=20 + =20 + =20 + =20 + =20 + =20 + =20 + + =20 + =20 + =20 + +
=20 + =20 + + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + +
=20 + =20 + + =20 + =20 + =20 + =20 + =20 + =20 + =20 + +
3D"Amazon.co.uk"=20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 +
Your Orders   |   Your Account   |   Amazon.co.uk

Dispatch Confirmation

Order: #026-44808= +09-9307517
=20 + =20 + + =20 + =20 + =20 + +
Hello,

We thought you'd= + like to know that we've dispatched this portion of your order separately t= +o give you a quicker service, at no additional cost to you. The remainder o= +f your order will follow as soon as those items become available. If you ne= +ed to return an item or manage other orders, please visit Your Orders = +on Amazon.co.uk

=20 + =20 + + =20 + =20 + =20 + =20 + +

Arriving:
Tuesday, November 16

=20 + =20 + + =20 + = +=20 + =20 + +
Track your package
Your order was sent to:

Ama= +zon Hub Locker - GOLD
=20 + + VILLAGE=20 +

=20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 +
Order Total: =C2=A311.99
Paid by Visa: =C2=A311.99
=20 + =20 + =20 + =20 + + =20 + =20 + =20 + =20 + +
=20 + =20 + 3D""=20 + =20 + =20 + =20 + + =20 + =20 + =20 + +
=20 + =20 + Just ask: "= +Alexa, where's my stuff?"=20 + =20 +
=20 +
=20 +
=20 + =20 + + =20 + =20 + =20 + +

Your tracking number is: REMOVED_FOR_SECURITY. Once your orde= +r arrives at the Locker, you'll receive an email with instructions on how t= +o collect it. Learn more.

If you have a smartphone, you can use the Amazon Shopping App to receive delivery notifications and track your = +order on the go.

=20 + =20 + + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + +
Order summary=20 + + + + + + +
=20 + =20 + + =20 + =20 + =20 + =20 + =20 + =20 + =20 + =20 + +

+

=C2=A311.99
=20 + =20 +
=20 + + + + + + +
=20 + =20 + + =20 + =20 + +
It's easy to return an item. Visit our Online Returns Centre.
Le= +arn how to recycle your packaging at Amazon Second Chance.
If you need further assistance wi= +th your order, please visit Customer Service.


We hope to see you again soon.
= + Amazon.co.uk

=20 +
=20 + =20 + + =20 + =20 + =20 + +
+ =20 +
+ = +=20 + =20 + =20 + + + + + + + + + + +
+ + + + + + +
Recommendations for all= + products:
+ + + + + + + +
+ + + + + + + + + +
+=3D"Show
+ + + + + + + + + +

Show Mode Charging Dock for Fire HD...

=C2=A349.99
+ + + + + + + + + +
+=3D"kenable
+ + + + + + + + + +

kenable PW01906 5.5 x 2.1mm DC Power...<= +/p>

=C2=A34.49
+ = +=20 + =20 + =20 + =20 +
=20 + =20 + + =20 + =20 + =20 + +

You can cancel this order within 14 day= +s, beginning from the day you receive the product (subject to certain exceptions). We will reimburse all payments= + received from you for the goods purchased and will also reimburse outbound= + delivery charges (for the least expensive type of delivery offered by us).= + You will be responsible for the cost of returning the product to us unless= + we delivered it to you in error, it is faulty, or you purchased shoes, clo= +thing and accessories (check our Returns= + Policy). You may be subject to increased return costs if the product c= +an=E2=80=99t be returned normally by post.

You can request a cancell= +ation by visiting our Returns Support Centre; by contacti= +ng us; or completing this = +form and sending it by post.

Please also see our Returns Policy to learn more about our 30 day returns= + guarantee which outlines that you can return items for a full refund of th= +e item price within 30 days.

Amazon EU, Soci=C3=A9t=C3=A9 =C3=A0 res= +ponsabilit=C3=A9 limit=C3=A9e, 38 avenue John F. Kennedy, L-1855 Luxembourg= +. Share capital: EUR 125.000; Registered in Luxembourg; RCS Luxembourg No: = +B 101818; Business Licence Number: 134248; Luxembourg VAT Registration Numb= +er: LU 20260743.

Learn more about your statutory rights here.

Please note: This email was sent from a n= +otification-only address that can't accept incoming email. Please do not re= +ply to this message.

=20 + += + +------=_Part_51705775_653353746.1636995720703-- \ No newline at end of file diff --git a/tests/test_helpers.py b/tests/test_helpers.py index ad543086..b32fb001 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -770,6 +770,11 @@ async def test_amazon_shipped_order_uk(hass, mock_imap_amazon_shipped_uk): assert result == ["123-4567890-1234567"] +async def test_amazon_shipped_order_uk(hass, mock_imap_amazon_shipped_uk_2): + result = get_items(mock_imap_amazon_shipped_uk_2, "order") + assert result == ["123-4567890-1234567"] + + async def test_amazon_shipped_order_it(hass, mock_imap_amazon_shipped_it): result = get_items(mock_imap_amazon_shipped_it, "order") assert result == ["405-5236882-9395563"]