-
Notifications
You must be signed in to change notification settings - Fork 85
/
test_helpers.py
1080 lines (888 loc) · 31.4 KB
/
test_helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Tests for helpers module."""
import datetime
import errno
from datetime import date, timezone
from unittest import mock
from unittest.mock import call, mock_open, patch
import pytest
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from pytest_homeassistant_custom_component.common import MockConfigEntry
from custom_components.mail_and_packages.const import DOMAIN
from custom_components.mail_and_packages.helpers import (
_generate_mp4,
amazon_exception,
amazon_hub,
amazon_search,
cleanup_images,
download_img,
email_fetch,
email_search,
get_count,
get_formatted_date,
get_items,
get_mails,
hash_file,
image_file_name,
login,
process_emails,
resize_images,
selectfolder,
update_time,
)
from tests.const import (
FAKE_CONFIG_DATA,
FAKE_CONFIG_DATA_BAD,
FAKE_CONFIG_DATA_CORRECTED,
FAKE_CONFIG_DATA_CORRECTED_EXTERNAL,
FAKE_CONFIG_DATA_CUSTOM_IMG,
FAKE_CONFIG_DATA_EXTERNAL,
FAKE_CONFIG_DATA_NO_RND,
)
MAIL_IMAGE_URL_ENTITY = "sensor.mail_image_url"
MAIL_IMAGE_SYSTEM_PATH = "sensor.mail_image_system_path"
async def test_get_formatted_date():
assert get_formatted_date() == datetime.datetime.today().strftime("%d-%b-%Y")
async def test_update_time():
assert isinstance(update_time(), datetime.datetime)
async def test_cleanup_images(mock_listdir, mock_osremove):
cleanup_images("/tests/fakedir/")
calls = [
call("/tests/fakedir/testfile.gif"),
call("/tests/fakedir/anotherfakefile.mp4"),
]
mock_osremove.assert_has_calls(calls)
async def test_cleanup_found_images_remove_err(
mock_listdir, mock_osremove_exception, caplog
):
cleanup_images("/tests/fakedir/")
assert mock_osremove_exception.called_with("/tests/fakedir/")
assert "Error attempting to remove found image:" in caplog.text
async def test_cleanup_images_remove_err(mock_listdir, mock_osremove_exception, caplog):
cleanup_images("/tests/fakedir/", "testimage.jpg")
assert mock_osremove_exception.called_with("/tests/fakedir/")
assert "Error attempting to remove image:" in caplog.text
async def test_process_emails(
hass,
mock_imap_no_email,
mock_osremove,
mock_osmakedir,
mock_listdir,
mock_copyfile,
mock_copytree,
mock_hash_file,
mock_getctime_today,
):
entry = MockConfigEntry(
domain=DOMAIN,
title="imap.test.email",
data=FAKE_CONFIG_DATA,
)
hass.config.internal_url = "http://127.0.0.1:8123/"
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
config = entry.data.copy()
assert config == FAKE_CONFIG_DATA_CORRECTED
state = hass.states.get(MAIL_IMAGE_SYSTEM_PATH)
assert state is not None
assert (
"/testing_config/custom_components/mail_and_packages/images/testfile.gif"
in state.state
)
state = hass.states.get(MAIL_IMAGE_URL_ENTITY)
assert state.state == "http://127.0.0.1:8123/local/mail_and_packages/testfile.gif"
result = process_emails(hass, config)
assert isinstance(result["mail_updated"],datetime.datetime)
assert result["zpackages_delivered"] == 0
assert result["zpackages_transit"] == 0
assert result["amazon_delivered"] == 0
assert result["amazon_hub"] == 0
assert result["amazon_packages"] == 0
assert result["amazon_order"] == []
assert result["amazon_hub_code"] == []
async def test_process_emails_external(
hass,
mock_imap_no_email,
mock_osremove,
mock_osmakedir,
mock_listdir,
mock_copyfile,
mock_copytree,
mock_hash_file,
mock_getctime_today,
):
entry = MockConfigEntry(
domain=DOMAIN,
title="imap.test.email",
data=FAKE_CONFIG_DATA_EXTERNAL,
)
hass.config.internal_url = "http://127.0.0.1:8123/"
hass.config.external_url = "http://really.fake.host.net:8123/"
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
config = entry.data.copy()
assert config == FAKE_CONFIG_DATA_CORRECTED_EXTERNAL
state = hass.states.get(MAIL_IMAGE_SYSTEM_PATH)
assert state is not None
assert (
"/testing_config/custom_components/mail_and_packages/images/testfile.gif"
in state.state
)
state = hass.states.get(MAIL_IMAGE_URL_ENTITY)
assert (
state.state
== "http://really.fake.host.net:8123/local/mail_and_packages/testfile.gif"
)
result = process_emails(hass, config)
assert isinstance(result["mail_updated"],datetime.datetime)
assert result["zpackages_delivered"] == 0
assert result["zpackages_transit"] == 0
assert result["amazon_delivered"] == 0
assert result["amazon_hub"] == 0
assert result["amazon_packages"] == 0
assert result["amazon_order"] == []
assert result["amazon_hub_code"] == []
assert (
"custom_components/mail_and_packages/images/" in mock_copytree.call_args.args[0]
)
assert "www/mail_and_packages" in mock_copytree.call_args.args[1]
assert mock_copytree.call_args.kwargs == {"dirs_exist_ok": True}
assert (
"www/mail_and_packages/amazon/anotherfakefile.mp4"
in mock_osremove.call_args.args[0]
)
async def test_process_emails_external_error(
hass,
mock_imap_no_email,
mock_osremove,
mock_osmakedir,
mock_listdir,
mock_copyfile,
mock_copytree,
mock_hash_file,
mock_getctime_today,
caplog,
):
entry = MockConfigEntry(
domain=DOMAIN,
title="imap.test.email",
data=FAKE_CONFIG_DATA_EXTERNAL,
)
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
config = entry.data.copy()
with patch("os.makedirs") as mock_osmakedirs:
mock_osmakedirs.side_effect = OSError
process_emails(hass, config)
assert "Problem creating:" in caplog.text
async def test_process_emails_copytree_error(
hass,
mock_imap_no_email,
mock_osremove,
mock_osmakedir,
mock_listdir,
mock_copyfile,
mock_hash_file,
mock_getctime_today,
caplog,
):
entry = MockConfigEntry(
domain=DOMAIN,
title="imap.test.email",
data=FAKE_CONFIG_DATA_EXTERNAL,
)
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
config = entry.data.copy()
with patch("custom_components.mail_and_packages.helpers.copytree") as mock_copytree:
mock_copytree.side_effect = Exception
process_emails(hass, config)
assert "Problem copying files from" in caplog.text
async def test_process_emails_bad(hass, mock_imap_no_email, mock_update):
entry = MockConfigEntry(
domain=DOMAIN,
title="imap.test.email",
data=FAKE_CONFIG_DATA_BAD,
)
entry.version = 2
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
async def test_process_emails_non_random(
hass,
mock_imap_no_email,
mock_osremove,
mock_osmakedir,
mock_listdir,
mock_copyfile,
mock_copytree,
mock_hash_file,
mock_getctime_today,
):
entry = MockConfigEntry(
domain=DOMAIN,
title="imap.test.email",
data=FAKE_CONFIG_DATA,
)
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
config = entry.data
result = process_emails(hass, config)
assert result["image_name"] == "testfile.gif"
async def test_process_emails_random(
hass,
mock_imap_no_email,
mock_osremove,
mock_osmakedir,
mock_listdir,
mock_copyfile,
mock_copytree,
mock_hash_file,
mock_getctime_yesterday,
):
entry = MockConfigEntry(
domain=DOMAIN,
title="imap.test.email",
data=FAKE_CONFIG_DATA,
)
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
config = entry.data
result = process_emails(hass, config)
assert ".gif" in result["image_name"]
async def test_process_nogif(
hass,
mock_imap_no_email,
mock_osremove,
mock_osmakedir,
mock_listdir_noimgs,
mock_copyfile,
mock_copytree,
mock_hash_file,
mock_getctime_today,
):
entry = MockConfigEntry(
domain=DOMAIN,
title="imap.test.email",
data=FAKE_CONFIG_DATA,
)
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
config = entry.data
result = process_emails(hass, config)
assert ".gif" in result["image_name"]
async def test_process_old_image(
hass,
mock_imap_no_email,
mock_osremove,
mock_osmakedir,
mock_listdir,
mock_copyfile,
mock_copytree,
mock_hash_file,
mock_getctime_yesterday,
):
entry = MockConfigEntry(
domain=DOMAIN,
title="imap.test.email",
data=FAKE_CONFIG_DATA,
)
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
config = entry.data
result = process_emails(hass, config)
assert ".gif" in result["image_name"]
async def test_process_folder_error(
hass,
mock_imap_no_email,
mock_osremove,
mock_osmakedir,
mock_listdir,
mock_copyfile,
mock_copytree,
mock_hash_file,
mock_getctime_yesterday,
):
entry = MockConfigEntry(
domain=DOMAIN,
title="imap.test.email",
data=FAKE_CONFIG_DATA,
)
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
config = entry.data
with patch(
"custom_components.mail_and_packages.helpers.selectfolder", return_value=False
):
result = process_emails(hass, config)
assert result == {}
async def test_image_filename_oserr(
hass,
mock_imap_no_email,
mock_osremove,
mock_osmakedir,
mock_listdir,
mock_copyfile,
mock_copytree,
mock_hash_file_oserr,
mock_getctime_today,
caplog,
):
"""Test settting up entities."""
entry = MockConfigEntry(
domain=DOMAIN,
title="imap.test.email",
data=FAKE_CONFIG_DATA,
)
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 43
assert "Problem accessing file:" in caplog.text
async def test_image_getctime_oserr(
hass,
mock_imap_no_email,
mock_osremove,
mock_osmakedir,
mock_listdir,
mock_copyfile,
mock_copytree,
mock_hash_file,
mock_getctime_err,
caplog,
):
"""Test settting up entities."""
entry = MockConfigEntry(
domain=DOMAIN,
title="imap.test.email",
data=FAKE_CONFIG_DATA,
)
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 43
assert "Problem accessing file:" in caplog.text
async def test_email_search(mock_imap_search_error, caplog):
result = email_search(mock_imap_search_error, "fake@eamil.address", "01-Jan-20")
assert result == ("BAD", "Invalid SEARCH format")
assert "Error searching emails:" in caplog.text
result = email_search(
mock_imap_search_error, "fake@eamil.address", "01-Jan-20", "Fake Subject"
)
assert result == ("BAD", "Invalid SEARCH format")
assert "Error searching emails:" in caplog.text
async def test_email_fetch(mock_imap_fetch_error, caplog):
result = email_fetch(mock_imap_fetch_error, 1, "(RFC822)")
assert result == ("BAD", "Invalid Email")
assert "Error fetching emails:" in caplog.text
async def test_get_mails(mock_imap_no_email, mock_copyfile):
result = get_mails(mock_imap_no_email, "./", "5", "mail_today.gif", False)
assert result == 0
async def test_get_mails_makedirs_error(mock_imap_no_email, mock_copyfile, caplog):
with patch("os.path.isdir", return_value=False), patch(
"os.makedirs", side_effect=OSError
):
get_mails(mock_imap_no_email, "./", "5", "mail_today.gif", False)
assert "Error creating directory:" in caplog.text
async def test_get_mails_copyfile_error(
mock_imap_usps_informed_digest_no_mail,
mock_copyoverlays,
mock_copyfile_exception,
caplog,
):
result = get_mails(
mock_imap_usps_informed_digest_no_mail, "./", "5", "mail_today.gif", False
)
assert "File not found" in caplog.text
async def test_get_mails_email_search_error(
mock_imap_usps_informed_digest_no_mail,
mock_copyoverlays,
mock_copyfile_exception,
caplog,
):
with patch(
"custom_components.mail_and_packages.helpers.email_search",
return_value=("BAD", []),
):
result = get_mails(
mock_imap_usps_informed_digest_no_mail, "./", "5", "mail_today.gif", False
)
assert result == 0
async def test_informed_delivery_emails(
mock_imap_usps_informed_digest,
mock_osremove,
mock_osmakedir,
mock_listdir,
mock_os_path_splitext,
mock_image,
mock_io,
mock_resizeimage,
mock_copyfile,
caplog,
):
m_open = mock_open()
with patch("builtins.open", m_open, create=True):
result = get_mails(
mock_imap_usps_informed_digest, "./", "5", "mail_today.gif", False
)
assert result == 3
assert "USPSInformedDelivery@usps.gov" in caplog.text
assert "USPSInformeddelivery@informeddelivery.usps.com" in caplog.text
assert "USPSInformeddelivery@email.informeddelivery.usps.com" in caplog.text
assert "USPS Informed Delivery" in caplog.text
async def test_get_mails_imageio_error(
mock_imap_usps_informed_digest,
mock_osremove,
mock_osmakedir,
mock_listdir,
mock_os_path_splitext,
mock_image,
mock_resizeimage,
mock_copyfile,
caplog,
):
with patch("custom_components.mail_and_packages.helpers.io") as mock_imageio:
m_open = mock_open()
with patch("builtins.open", m_open, create=True):
mock_imageio.return_value = mock.Mock(autospec=True)
mock_imageio.mimwrite.side_effect = Exception("Processing Error")
result = get_mails(
mock_imap_usps_informed_digest, "./", "5", "mail_today.gif", False
)
assert result == 3
assert "Error attempting to generate image:" in caplog.text
async def test_informed_delivery_emails_mp4(
mock_imap_usps_informed_digest,
mock_osremove,
mock_osmakedir,
mock_listdir,
mock_os_path_splitext,
mock_image,
mock_io,
mock_resizeimage,
mock_copyfile,
):
with patch(
"custom_components.mail_and_packages.helpers._generate_mp4"
) as mock_generate_mp4:
m_open = mock_open()
with patch("builtins.open", m_open, create=True):
result = get_mails(
mock_imap_usps_informed_digest, "./", "5", "mail_today.gif", True
)
assert result == 3
assert mock_generate_mp4.called_with("./", "mail_today.gif")
async def test_informed_delivery_emails_open_err(
mock_imap_usps_informed_digest,
mock_listdir,
mock_osremove,
mock_osmakedir,
mock_os_path_splitext,
mock_image,
mock_io,
mock_resizeimage,
mock_copyfile,
caplog,
):
get_mails(
mock_imap_usps_informed_digest,
"/totally/fake/path/",
"5",
"mail_today.gif",
False,
)
assert (
"Error opening filepath: [Errno 2] No such file or directory: '/totally/fake/path/1040327780-101.jpg'"
in caplog.text
)
async def test_informed_delivery_emails_io_err(
mock_imap_usps_informed_digest,
mock_listdir,
mock_osremove,
mock_osmakedir,
mock_os_path_splitext,
mock_image,
mock_resizeimage,
mock_copyfile,
):
with pytest.raises(FileNotFoundError) as exc_info:
m_open = mock_open()
with patch("builtins.open", m_open, create=True):
get_mails(
mock_imap_usps_informed_digest,
"/totally/fake/path/",
"5",
"mail_today.gif",
False,
)
assert type(exc_info.value) is FileNotFoundError
async def test_informed_delivery_missing_mailpiece(
mock_imap_usps_informed_digest_missing,
mock_listdir,
mock_osremove,
mock_osmakedir,
mock_os_path_splitext,
mock_image,
mock_io,
mock_resizeimage,
mock_copyfile,
):
m_open = mock_open()
with patch("builtins.open", m_open, create=True):
result = get_mails(
mock_imap_usps_informed_digest_missing, "./", "5", "mail_today.gif", False
)
assert result == 5
async def test_informed_delivery_no_mail(
mock_imap_usps_informed_digest_no_mail,
mock_listdir,
mock_osremove,
mock_osmakedir,
mock_os_path_splitext,
mock_image,
mock_io,
mock_resizeimage,
mock_os_path_isfile,
mock_copyfile,
):
m_open = mock_open()
with patch("builtins.open", m_open, create=True):
result = get_mails(
mock_imap_usps_informed_digest_no_mail, "./", "5", "mail_today.gif", False
)
assert result == 0
async def test_informed_delivery_no_mail_copy_error(
mock_imap_usps_informed_digest_no_mail,
mock_listdir,
mock_osremove,
mock_osmakedir,
mock_os_path_splitext,
mock_image,
mock_io,
mock_resizeimage,
mock_os_path_isfile,
mock_copy_overlays,
mock_copyfile_exception,
caplog,
):
m_open = mock_open()
with patch("builtins.open", m_open, create=True):
get_mails(
mock_imap_usps_informed_digest_no_mail, "./", "5", "mail_today.gif", False
)
assert mock_copyfile_exception.called_with("./mail_today.gif")
assert "File not found" in caplog.text
async def test_ups_out_for_delivery(hass, mock_imap_ups_out_for_delivery):
result = get_count(
mock_imap_ups_out_for_delivery, "ups_delivering", True, "./", hass
)
assert result["count"] == 1
assert result["tracking"] == ["1Z2345YY0678901234"]
async def test_ups_out_for_delivery_html_only(
hass, mock_imap_ups_out_for_delivery_html
):
result = get_count(
mock_imap_ups_out_for_delivery_html, "ups_delivering", True, "./", hass
)
assert result["count"] == 1
assert result["tracking"] == ["1Z0Y12345678031234"]
async def test_usps_out_for_delivery(hass, mock_imap_usps_out_for_delivery):
result = get_count(
mock_imap_usps_out_for_delivery, "usps_delivering", True, "./", hass
)
assert result["count"] == 1
assert result["tracking"] == ["92123456508577307776690000"]
async def test_dhl_out_for_delivery(hass, mock_imap_dhl_out_for_delivery):
result = get_count(
mock_imap_dhl_out_for_delivery, "dhl_delivering", True, "./", hass
)
assert result["count"] == 1
assert result["tracking"] == ["4212345678"]
async def test_hermes_out_for_delivery(hass, mock_imap_hermes_out_for_delivery):
result = get_count(
mock_imap_hermes_out_for_delivery, "hermes_delivering", True, "./", hass
)
assert result["count"] == 1
assert result["tracking"] == ["8888888888888888"]
async def test_royal_out_for_delivery(hass, mock_imap_royal_out_for_delivery):
result = get_count(
mock_imap_royal_out_for_delivery, "royal_delivering", True, "./", hass
)
assert result["count"] == 1
assert result["tracking"] == ["MA038501234GB"]
async def test_amazon_shipped_count(hass, mock_imap_amazon_shipped):
with patch("datetime.date") as mock_date:
mock_date.today.return_value = date(2020, 9, 11)
result = get_items(mock_imap_amazon_shipped, "count")
assert result == 1
async def test_amazon_shipped_order(hass, mock_imap_amazon_shipped):
result = get_items(mock_imap_amazon_shipped, "order")
assert result == ["123-1234567-1234567"]
async def test_amazon_shipped_order_alt(hass, mock_imap_amazon_shipped_alt):
result = get_items(mock_imap_amazon_shipped_alt, "order")
assert result == ["123-1234567-1234567"]
async def test_amazon_shipped_order_alt_2(hass, mock_imap_amazon_shipped_alt_2):
result = get_items(mock_imap_amazon_shipped_alt_2, "order")
assert result == ["113-9999999-8459426"]
with patch("datetime.date") as mock_date:
mock_date.today.return_value = date(2021, 12, 3)
result = get_items(mock_imap_amazon_shipped_alt_2, "count")
assert result == 1
async def test_amazon_shipped_order_alt_timeformat(
hass, mock_imap_amazon_shipped_alt_timeformat
):
result = get_items(mock_imap_amazon_shipped_alt_timeformat, "order")
assert result == ["321-1234567-1234567"]
async def test_amazon_shipped_order_uk(hass, mock_imap_amazon_shipped_uk):
result = get_items(mock_imap_amazon_shipped_uk, "order")
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"]
async def test_amazon_shipped_order_it_count(hass, mock_imap_amazon_shipped_it):
with patch("datetime.date") as mock_date:
mock_date.today.return_value = date(2021, 12, 1)
result = get_items(mock_imap_amazon_shipped_it, "count")
assert result == 1
async def test_amazon_search(hass, mock_imap_no_email):
result = amazon_search(mock_imap_no_email, "test/path", hass, "testfilename.jpg")
assert result == 0
async def test_amazon_search_results(hass, mock_imap_amazon_shipped):
result = amazon_search(
mock_imap_amazon_shipped, "test/path", hass, "testfilename.jpg"
)
assert result == 40
async def test_amazon_search_delivered(
hass, mock_imap_amazon_delivered, mock_download_img
):
result = amazon_search(
mock_imap_amazon_delivered, "test/path", hass, "testfilename.jpg"
)
assert result == 40
assert mock_download_img.called
async def test_amazon_search_delivered_it(
hass, mock_imap_amazon_delivered_it, mock_download_img
):
result = amazon_search(
mock_imap_amazon_delivered_it, "test/path", hass, "testfilename.jpg"
)
assert result == 40
async def test_amazon_hub(hass, mock_imap_amazon_the_hub):
result = amazon_hub(mock_imap_amazon_the_hub)
assert result["count"] == 1
assert result["code"] == ["123456"]
with patch(
"custom_components.mail_and_packages.helpers.email_search",
return_value=("BAD", []),
):
result = amazon_hub(mock_imap_amazon_the_hub)
assert result == {}
with patch(
"custom_components.mail_and_packages.helpers.email_search",
return_value=("OK", [None]),
):
result = amazon_hub(mock_imap_amazon_the_hub)
assert result == {}
async def test_amazon_hub_2(hass, mock_imap_amazon_the_hub_2):
result = amazon_hub(mock_imap_amazon_the_hub_2)
assert result["count"] == 1
assert result["code"] == ["123456"]
with patch(
"custom_components.mail_and_packages.helpers.email_search",
return_value=("BAD", []),
):
result = amazon_hub(mock_imap_amazon_the_hub_2)
assert result == {}
with patch(
"custom_components.mail_and_packages.helpers.email_search",
return_value=("OK", [None]),
):
result = amazon_hub(mock_imap_amazon_the_hub_2)
assert result == {}
async def test_amazon_shipped_order_exception(hass, mock_imap_amazon_shipped, caplog):
with patch("quopri.decodestring", side_effect=ValueError):
get_items(mock_imap_amazon_shipped, "order")
assert "Problem decoding email message:" in caplog.text
async def test_amazon_shipped_order_exception(hass, mock_imap_amazon_shipped, caplog):
with patch("quopri.decodestring", side_effect=ValueError):
get_items(mock_imap_amazon_shipped, "order")
assert "Problem decoding email message:" in caplog.text
async def test_generate_mp4(
mock_osremove, mock_os_path_join, mock_subprocess_call, mock_os_path_split
):
with patch("custom_components.mail_and_packages.helpers.cleanup_images"):
_generate_mp4("./", "testfile.gif")
mock_os_path_join.called_with("./", "testfile.gif")
mock_osremove.called_with("./", "testfile.mp4")
mock_subprocess_call.called_with(
"ffmpeg",
"-f",
"gif",
"-i",
"testfile.gif",
"-pix_fmt",
"yuv420p",
"-filter:v",
"crop='floor(in_w/2)*2:floor(in_h/2)*2'",
"testfile.mp4",
)
async def test_connection_error(caplog):
result = login("localhost", 993, "fakeuser", "suchfakemuchpassword")
assert not result
assert "Network error while connecting to server:" in caplog.text
async def test_login_error(mock_imap_login_error, caplog):
login("localhost", 993, "fakeuser", "suchfakemuchpassword")
assert "Error logging into IMAP Server:" in caplog.text
async def test_selectfolder_list_error(mock_imap_list_error, caplog):
assert not selectfolder(mock_imap_list_error, "somefolder")
assert "Error listing folders:" in caplog.text
async def test_selectfolder_select_error(mock_imap_select_error, caplog):
assert not selectfolder(mock_imap_select_error, "somefolder")
assert "Error selecting folder:" in caplog.text
async def test_resize_images_open_err(mock_open_excpetion, caplog):
resize_images(["testimage.jpg", "anothertest.jpg"], 724, 320)
assert "Error attempting to open image" in caplog.text
async def test_resize_images_read_err(mock_image_excpetion, caplog):
m_open = mock_open()
with patch("builtins.open", m_open, create=True):
resize_images(["testimage.jpg", "anothertest.jpg"], 724, 320)
assert "Error attempting to read image" in caplog.text
async def test_process_emails_random_image(hass, mock_imap_login_error, caplog):
entry = MockConfigEntry(
domain=DOMAIN,
title="imap.test.email",
data=FAKE_CONFIG_DATA_NO_RND,
)
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
config = entry.data
process_emails(hass, config)
assert "Error logging into IMAP Server:" in caplog.text
async def test_usps_exception(hass, mock_imap_usps_exception):
result = get_count(mock_imap_usps_exception, "usps_exception", False, "./", hass)
assert result["count"] == 1
async def test_download_img(
aioclient_mock,
mock_osremove,
mock_osmakedir,
mock_listdir_nogif,
mock_copyfile,
mock_hash_file,
mock_getctime_today,
caplog,
):
m_open = mock_open()
with patch("builtins.open", m_open, create=True):
await download_img(
"http://fake.website.com/not/a/real/website/image.jpg",
"/fake/directory/",
"testfilename.jpg",
)
assert m_open.call_count == 1
assert m_open.call_args == call("/fake/directory/amazon/testfilename.jpg", "wb")
assert "URL content-type: image/gif" in caplog.text
assert "Amazon image downloaded" in caplog.text
async def test_download_img_error(aioclient_mock_error, caplog):
m_open = mock_open()
with patch("builtins.open", m_open, create=True):
await download_img(
"http://fake.website.com/not/a/real/website/image.jpg",
"/fake/directory/",
"testfilename.jpg",
)
assert "Problem downloading file http error: 404" in caplog.text
async def test_image_file_name_path_error(hass, caplog):
config = FAKE_CONFIG_DATA_CORRECTED
with patch("os.path.exists", return_value=False), patch(
"os.makedirs", side_effect=OSError
):
result = image_file_name(hass, config)
assert result == "mail_none.gif"
assert "Problem creating:" in caplog.text
async def test_image_file_name_amazon(
hass, mock_listdir_nogif, mock_getctime_today, mock_hash_file, mock_copyfile, caplog
):
config = FAKE_CONFIG_DATA_CORRECTED
with patch("os.path.exists", return_value=True), patch(
"os.makedirs", return_value=True
):
result = image_file_name(hass, config, True)
assert result == "testfile.jpg"
async def test_image_file_name(
hass, mock_listdir_nogif, mock_getctime_today, mock_hash_file, mock_copyfile, caplog
):