-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathtest_async_davclient.py
More file actions
1026 lines (831 loc) · 36.7 KB
/
test_async_davclient.py
File metadata and controls
1026 lines (831 loc) · 36.7 KB
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
#!/usr/bin/env python
"""
Unit tests for async_davclient module.
Rule: None of the tests in this file should initiate any internet
communication. We use Mock/MagicMock to emulate server communication.
"""
import os
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from caldav.async_davclient import AsyncDAVClient, AsyncDAVResponse, get_davclient
from caldav.lib import error
# Sample XML responses for testing
SAMPLE_MULTISTATUS_XML = b"""<?xml version="1.0" encoding="utf-8" ?>
<d:multistatus xmlns:d="DAV:" xmlns:cal="urn:ietf:params:xml:ns:caldav">
<d:response>
<d:href>/calendars/user/calendar/</d:href>
<d:propstat>
<d:prop>
<d:displayname>My Calendar</d:displayname>
</d:prop>
<d:status>HTTP/1.1 200 OK</d:status>
</d:propstat>
</d:response>
</d:multistatus>
"""
SAMPLE_PROPFIND_XML = b"""<?xml version="1.0" encoding="utf-8" ?>
<d:multistatus xmlns:d="DAV:">
<d:response>
<d:href>/dav/</d:href>
<d:propstat>
<d:prop>
<d:current-user-principal>
<d:href>/dav/principals/user/</d:href>
</d:current-user-principal>
</d:prop>
<d:status>HTTP/1.1 200 OK</d:status>
</d:propstat>
</d:response>
</d:multistatus>
"""
SAMPLE_OPTIONS_HEADERS = {
"DAV": "1, 2, calendar-access",
"Allow": "OPTIONS, GET, HEAD, POST, PUT, DELETE, PROPFIND, PROPPATCH, REPORT",
}
def create_mock_response(
content: bytes = b"",
status_code: int = 200,
reason: str = "OK",
headers: dict = None,
) -> MagicMock:
"""Create a mock HTTP response."""
resp = MagicMock()
resp.content = content
resp.status_code = status_code
resp.reason = reason
resp.reason_phrase = reason # httpx uses reason_phrase
resp.headers = headers or {}
resp.text = content.decode("utf-8") if content else ""
return resp
class TestAsyncDAVResponse:
"""Tests for AsyncDAVResponse class."""
def test_response_with_xml_content(self) -> None:
"""Test parsing XML response."""
resp = create_mock_response(
content=SAMPLE_MULTISTATUS_XML,
status_code=207,
reason="Multi-Status",
headers={"Content-Type": "text/xml; charset=utf-8"},
)
dav_response = AsyncDAVResponse(resp)
assert dav_response.status == 207
assert dav_response.reason == "Multi-Status"
assert dav_response.tree is not None
assert dav_response.tree.tag.endswith("multistatus")
def test_response_with_empty_content(self) -> None:
"""Test response with no content."""
resp = create_mock_response(
content=b"",
status_code=204,
reason="No Content",
headers={"Content-Length": "0"},
)
dav_response = AsyncDAVResponse(resp)
assert dav_response.status == 204
assert dav_response.tree is None
assert dav_response._raw == ""
def test_response_with_non_xml_content(self) -> None:
"""Test response with non-XML content."""
resp = create_mock_response(
content=b"Plain text response",
status_code=200,
headers={"Content-Type": "text/plain"},
)
dav_response = AsyncDAVResponse(resp)
assert dav_response.status == 200
assert dav_response.tree is None
assert b"Plain text response" in dav_response._raw
def test_response_raw_property(self) -> None:
"""Test raw property returns string."""
resp = create_mock_response(content=b"test content")
dav_response = AsyncDAVResponse(resp)
assert isinstance(dav_response.raw, str)
assert "test content" in dav_response.raw
def test_response_crlf_normalization(self) -> None:
"""Test that CRLF is normalized to LF."""
resp = create_mock_response(content=b"line1\r\nline2\r\nline3")
dav_response = AsyncDAVResponse(resp)
assert b"\r\n" not in dav_response._raw
assert b"\n" in dav_response._raw
class TestAsyncDAVClient:
"""Tests for AsyncDAVClient class."""
def test_client_initialization(self) -> None:
"""Test basic client initialization."""
client = AsyncDAVClient(url="https://caldav.example.com/dav/")
assert client.url.scheme == "https"
assert "caldav.example.com" in str(client.url)
assert "User-Agent" in client.headers
assert "caldav-async" in client.headers["User-Agent"]
def test_client_with_credentials(self) -> None:
"""Test client initialization with username/password."""
client = AsyncDAVClient(
url="https://caldav.example.com/dav/",
username="testuser",
password="testpass",
)
assert client.username == "testuser"
assert client.password == "testpass"
def test_client_with_auth_in_url(self) -> None:
"""Test extracting credentials from URL."""
client = AsyncDAVClient(url="https://user:pass@caldav.example.com/dav/")
assert client.username == "user"
assert client.password == "pass"
def test_client_with_proxy(self) -> None:
"""Test client with proxy configuration."""
client = AsyncDAVClient(
url="https://caldav.example.com/dav/",
proxy="proxy.example.com:8080",
)
assert client.proxy == "http://proxy.example.com:8080"
def test_client_with_ssl_verify(self) -> None:
"""Test SSL verification settings."""
client = AsyncDAVClient(
url="https://caldav.example.com/dav/",
ssl_verify_cert=False,
)
assert client.ssl_verify_cert is False
def test_client_with_custom_headers(self) -> None:
"""Test client with custom headers."""
custom_headers = {"X-Custom-Header": "test-value"}
client = AsyncDAVClient(
url="https://caldav.example.com/dav/",
headers=custom_headers,
)
assert "X-Custom-Header" in client.headers
assert client.headers["X-Custom-Header"] == "test-value"
assert "User-Agent" in client.headers # Default headers still present
def test_build_method_headers(self) -> None:
"""Test _build_method_headers helper."""
# Test with depth
headers = AsyncDAVClient._build_method_headers("PROPFIND", depth=1)
assert headers["Depth"] == "1"
# Test REPORT method adds Content-Type
headers = AsyncDAVClient._build_method_headers("REPORT", depth=0)
assert "Content-Type" in headers
assert "application/xml" in headers["Content-Type"]
# Test with extra headers
extra = {"X-Test": "value"}
headers = AsyncDAVClient._build_method_headers("PROPFIND", depth=0, extra_headers=extra)
assert headers["X-Test"] == "value"
assert headers["Depth"] == "0"
@pytest.mark.asyncio
async def test_context_manager(self) -> None:
"""Test async context manager protocol."""
async with AsyncDAVClient(url="https://caldav.example.com/dav/") as client:
assert client is not None
assert hasattr(client, "session")
# After exit, session should be closed (we can't easily verify this without mocking)
@pytest.mark.asyncio
async def test_close(self) -> None:
"""Test close method."""
from caldav.async_davclient import _USE_HTTPX
client = AsyncDAVClient(url="https://caldav.example.com/dav/")
client.session = AsyncMock()
# httpx uses aclose(), niquests uses close()
client.session.aclose = AsyncMock()
client.session.close = AsyncMock()
await client.close()
if _USE_HTTPX:
client.session.aclose.assert_called_once()
else:
client.session.close.assert_called_once()
@pytest.mark.asyncio
async def test_request_method(self) -> None:
"""Test request method."""
client = AsyncDAVClient(url="https://caldav.example.com/dav/")
# Mock the session.request method
mock_response = create_mock_response(
content=SAMPLE_MULTISTATUS_XML,
status_code=207,
headers={"Content-Type": "text/xml"},
)
client.session.request = AsyncMock(return_value=mock_response)
response = await client.request("/test/path", "GET")
assert isinstance(response, AsyncDAVResponse)
assert response.status == 207
client.session.request.assert_called_once()
@pytest.mark.asyncio
async def test_propfind_method(self) -> None:
"""Test propfind method."""
client = AsyncDAVClient(url="https://caldav.example.com/dav/")
mock_response = create_mock_response(
content=SAMPLE_PROPFIND_XML,
status_code=207,
headers={"Content-Type": "text/xml"},
)
client.session.request = AsyncMock(return_value=mock_response)
# Test with default URL
response = await client.propfind(body="<propfind/>", depth=1)
assert response.status == 207
call_args = client.session.request.call_args
# httpx uses kwargs for method and headers
assert call_args.kwargs["method"] == "PROPFIND"
assert "Depth" in call_args.kwargs["headers"]
assert call_args.kwargs["headers"]["Depth"] == "1"
@pytest.mark.asyncio
async def test_propfind_with_custom_url(self) -> None:
"""Test propfind with custom URL."""
client = AsyncDAVClient(url="https://caldav.example.com/dav/")
mock_response = create_mock_response(
content=SAMPLE_PROPFIND_XML,
status_code=207,
headers={"Content-Type": "text/xml"},
)
client.session.request = AsyncMock(return_value=mock_response)
response = await client.propfind(
url="https://caldav.example.com/dav/calendars/",
body="<propfind/>",
depth=0,
)
assert response.status == 207
call_args = client.session.request.call_args
# httpx uses kwargs for url
assert "calendars" in call_args.kwargs["url"]
@pytest.mark.asyncio
async def test_report_method(self) -> None:
"""Test report method."""
client = AsyncDAVClient(url="https://caldav.example.com/dav/")
mock_response = create_mock_response(
content=SAMPLE_MULTISTATUS_XML,
status_code=207,
headers={"Content-Type": "text/xml"},
)
client.session.request = AsyncMock(return_value=mock_response)
response = await client.report(body="<report/>", depth=0)
assert response.status == 207
call_args = client.session.request.call_args
assert call_args.kwargs["method"] == "REPORT"
assert "Content-Type" in call_args.kwargs["headers"]
assert "application/xml" in call_args.kwargs["headers"]["Content-Type"]
@pytest.mark.asyncio
async def test_options_method(self) -> None:
"""Test options method."""
client = AsyncDAVClient(url="https://caldav.example.com/dav/")
mock_response = create_mock_response(
content=b"",
status_code=200,
headers=SAMPLE_OPTIONS_HEADERS,
)
client.session.request = AsyncMock(return_value=mock_response)
response = await client.options()
assert response.status == 200
assert "DAV" in response.headers
call_args = client.session.request.call_args
assert call_args.kwargs["method"] == "OPTIONS"
@pytest.mark.asyncio
async def test_proppatch_method(self) -> None:
"""Test proppatch method (requires URL)."""
client = AsyncDAVClient(url="https://caldav.example.com/dav/")
mock_response = create_mock_response(status_code=207)
client.session.request = AsyncMock(return_value=mock_response)
response = await client.proppatch(
url="https://caldav.example.com/dav/calendar/",
body="<propertyupdate/>",
)
assert response.status == 207
call_args = client.session.request.call_args
assert call_args.kwargs["method"] == "PROPPATCH"
@pytest.mark.asyncio
async def test_put_method(self) -> None:
"""Test put method (requires URL)."""
client = AsyncDAVClient(url="https://caldav.example.com/dav/")
mock_response = create_mock_response(status_code=201, reason="Created")
client.session.request = AsyncMock(return_value=mock_response)
response = await client.put(
url="https://caldav.example.com/dav/calendar/event.ics",
body="BEGIN:VCALENDAR...",
)
assert response.status == 201
call_args = client.session.request.call_args
assert call_args.kwargs["method"] == "PUT"
@pytest.mark.asyncio
async def test_delete_method(self) -> None:
"""Test delete method (requires URL)."""
client = AsyncDAVClient(url="https://caldav.example.com/dav/")
mock_response = create_mock_response(status_code=204, reason="No Content")
client.session.request = AsyncMock(return_value=mock_response)
response = await client.delete(url="https://caldav.example.com/dav/calendar/event.ics")
assert response.status == 204
call_args = client.session.request.call_args
assert call_args.kwargs["method"] == "DELETE"
@pytest.mark.asyncio
async def test_post_method(self) -> None:
"""Test post method (requires URL)."""
client = AsyncDAVClient(url="https://caldav.example.com/dav/")
mock_response = create_mock_response(status_code=200)
client.session.request = AsyncMock(return_value=mock_response)
response = await client.post(
url="https://caldav.example.com/dav/outbox/",
body="<schedule-request/>",
)
assert response.status == 200
call_args = client.session.request.call_args
assert call_args.kwargs["method"] == "POST"
@pytest.mark.asyncio
async def test_mkcol_method(self) -> None:
"""Test mkcol method (requires URL)."""
client = AsyncDAVClient(url="https://caldav.example.com/dav/")
mock_response = create_mock_response(status_code=201)
client.session.request = AsyncMock(return_value=mock_response)
response = await client.mkcol(url="https://caldav.example.com/dav/newcollection/")
assert response.status == 201
call_args = client.session.request.call_args
assert call_args.kwargs["method"] == "MKCOL"
@pytest.mark.asyncio
async def test_mkcalendar_method(self) -> None:
"""Test mkcalendar method (requires URL)."""
client = AsyncDAVClient(url="https://caldav.example.com/dav/")
mock_response = create_mock_response(status_code=201)
client.session.request = AsyncMock(return_value=mock_response)
response = await client.mkcalendar(
url="https://caldav.example.com/dav/newcalendar/",
body="<mkcalendar/>",
)
assert response.status == 201
call_args = client.session.request.call_args
assert call_args.kwargs["method"] == "MKCALENDAR"
def test_extract_auth_types(self) -> None:
"""Test extracting auth types from WWW-Authenticate header."""
client = AsyncDAVClient(url="https://caldav.example.com/dav/")
# Single auth type
auth_types = client.extract_auth_types('Basic realm="Test"')
assert "basic" in auth_types
# Multiple auth types
auth_types = client.extract_auth_types('Basic realm="Test", Digest realm="Test"')
assert "basic" in auth_types
assert "digest" in auth_types
def test_build_auth_object_basic(self) -> None:
"""Test building Basic auth object."""
client = AsyncDAVClient(
url="https://caldav.example.com/dav/",
username="user",
password="pass",
)
client.build_auth_object(["basic"])
assert client.auth is not None
# Can't easily test the auth object type without importing HTTPBasicAuth
def test_build_auth_object_digest(self) -> None:
"""Test building Digest auth object."""
client = AsyncDAVClient(
url="https://caldav.example.com/dav/",
username="user",
password="pass",
)
client.build_auth_object(["digest"])
assert client.auth is not None
def test_build_auth_object_bearer(self) -> None:
"""Test building Bearer auth object."""
client = AsyncDAVClient(
url="https://caldav.example.com/dav/",
password="bearer-token",
)
client.build_auth_object(["bearer"])
assert client.auth is not None
def test_build_auth_object_preference(self) -> None:
"""Test auth type preference (digest > basic > bearer)."""
client = AsyncDAVClient(
url="https://caldav.example.com/dav/",
username="user",
password="pass",
)
# Should prefer digest
client.build_auth_object(["basic", "digest", "bearer"])
# Can't easily verify which was chosen without inspecting auth object type
def test_build_auth_object_with_explicit_type(self) -> None:
"""Test building auth with explicit auth_type."""
client = AsyncDAVClient(
url="https://caldav.example.com/dav/",
username="user",
password="pass",
auth_type="basic",
)
# build_auth_object should have been called in __init__
assert client.auth is not None
class TestGetDAVClient:
"""Tests for get_davclient factory function."""
@pytest.mark.asyncio
async def test_get_davclient_basic(self) -> None:
"""Test basic get_davclient usage."""
with patch.object(AsyncDAVClient, "options") as mock_options:
mock_response = create_mock_response(
status_code=200,
headers=SAMPLE_OPTIONS_HEADERS,
)
mock_response_obj = AsyncDAVResponse(mock_response)
mock_options.return_value = mock_response_obj
client = await get_davclient(
url="https://caldav.example.com/dav/",
username="user",
password="pass",
)
assert client is not None
assert isinstance(client, AsyncDAVClient)
mock_options.assert_called_once()
@pytest.mark.asyncio
async def test_get_davclient_without_probe(self) -> None:
"""Test get_davclient with probe disabled."""
client = await get_davclient(
url="https://caldav.example.com/dav/",
username="user",
password="pass",
probe=False,
)
assert client is not None
assert isinstance(client, AsyncDAVClient)
@pytest.mark.asyncio
async def test_get_davclient_env_vars(self) -> None:
"""Test get_davclient with environment variables."""
with patch.dict(
os.environ,
{
"CALDAV_URL": "https://env.example.com/dav/",
"CALDAV_USERNAME": "envuser",
"CALDAV_PASSWORD": "envpass",
},
):
client = await get_davclient(probe=False)
assert "env.example.com" in str(client.url)
assert client.username == "envuser"
assert client.password == "envpass"
@pytest.mark.asyncio
async def test_get_davclient_params_override_env(self) -> None:
"""Test that explicit params override environment variables."""
with patch.dict(
os.environ,
{
"CALDAV_URL": "https://env.example.com/dav/",
"CALDAV_USERNAME": "envuser",
"CALDAV_PASSWORD": "envpass",
},
):
client = await get_davclient(
url="https://param.example.com/dav/",
username="paramuser",
password="parampass",
probe=False,
)
assert "param.example.com" in str(client.url)
assert client.username == "paramuser"
assert client.password == "parampass"
@pytest.mark.asyncio
async def test_get_davclient_missing_url(self) -> None:
"""Test that get_davclient raises error without URL."""
# Clear any env vars that might be set
with patch.dict(os.environ, {}, clear=True):
with pytest.raises(ValueError, match="No configuration found"):
await get_davclient(username="user", password="pass", probe=False)
@pytest.mark.asyncio
async def test_get_davclient_probe_failure(self) -> None:
"""Test get_davclient when probe fails."""
with patch.object(AsyncDAVClient, "options") as mock_options:
mock_options.side_effect = Exception("Connection failed")
with pytest.raises(error.DAVError, match="Failed to connect"):
await get_davclient(
url="https://caldav.example.com/dav/",
username="user",
password="pass",
probe=True,
)
@pytest.mark.asyncio
async def test_get_davclient_additional_kwargs(self) -> None:
"""Test passing additional kwargs to AsyncDAVClient."""
client = await get_davclient(
url="https://caldav.example.com/dav/",
username="user",
password="pass",
probe=False,
timeout=30,
ssl_verify_cert=False,
)
assert client.timeout == 30
assert client.ssl_verify_cert is False
class TestAPIImprovements:
"""Tests verifying that API improvements were applied."""
@pytest.mark.asyncio
async def test_no_dummy_parameters(self) -> None:
"""Verify dummy parameters are not present in async API."""
import inspect
# Check proppatch signature
sig = inspect.signature(AsyncDAVClient.proppatch)
assert "dummy" not in sig.parameters
# Check mkcol signature
sig = inspect.signature(AsyncDAVClient.mkcol)
assert "dummy" not in sig.parameters
# Check mkcalendar signature
sig = inspect.signature(AsyncDAVClient.mkcalendar)
assert "dummy" not in sig.parameters
@pytest.mark.asyncio
async def test_standardized_body_parameter(self) -> None:
"""Verify methods have appropriate parameters.
propfind has both 'body' (legacy) and 'props' (new protocol-based).
report uses 'body' for raw XML.
"""
import inspect
# Check propfind has both body (legacy) and props (new)
sig = inspect.signature(AsyncDAVClient.propfind)
assert "body" in sig.parameters # Legacy parameter
assert "props" in sig.parameters # New protocol-based parameter
# Check report uses 'body', not 'query'
sig = inspect.signature(AsyncDAVClient.report)
assert "body" in sig.parameters
assert "query" not in sig.parameters
@pytest.mark.asyncio
async def test_all_methods_have_headers_parameter(self) -> None:
"""Verify all HTTP methods accept headers parameter."""
import inspect
methods = [
"propfind",
"report",
"options",
"proppatch",
"mkcol",
"mkcalendar",
"put",
"post",
"delete",
]
for method_name in methods:
method = getattr(AsyncDAVClient, method_name)
sig = inspect.signature(method)
assert "headers" in sig.parameters, f"{method_name} missing headers parameter"
@pytest.mark.asyncio
async def test_url_requirements_split(self) -> None:
"""Verify URL parameter requirements are split correctly."""
import inspect
# Query methods - URL should be Optional
query_methods = ["propfind", "report", "options"]
for method_name in query_methods:
method = getattr(AsyncDAVClient, method_name)
sig = inspect.signature(method)
url_param = sig.parameters["url"]
# Check default is None or has default
assert url_param.default is None or url_param.default != inspect.Parameter.empty
# Resource methods - URL should be required (no default)
resource_methods = ["proppatch", "mkcol", "mkcalendar", "put", "post", "delete"]
for method_name in resource_methods:
method = getattr(AsyncDAVClient, method_name)
sig = inspect.signature(method)
url_param = sig.parameters["url"]
# URL should not have None as annotation type (should be str, not Optional[str])
# This is a simplified check - in reality we'd need to inspect annotations more carefully
class TestTypeHints:
"""Tests verifying type hints are present."""
def test_client_has_return_type_annotations(self) -> None:
"""Verify methods have return type annotations."""
import inspect
methods = [
"propfind",
"report",
"options",
"proppatch",
"put",
"delete",
]
for method_name in methods:
method = getattr(AsyncDAVClient, method_name)
sig = inspect.signature(method)
assert sig.return_annotation != inspect.Signature.empty, (
f"{method_name} missing return type annotation"
)
def test_get_davclient_has_return_type(self) -> None:
"""Verify get_davclient has return type annotation."""
import inspect
sig = inspect.signature(get_davclient)
assert sig.return_annotation != inspect.Signature.empty
class TestAsyncCalendarObjectResource:
"""Tests for AsyncCalendarObjectResource class."""
def test_has_component_method_exists(self) -> None:
"""
Test that AsyncCalendarObjectResource has the has_component() method.
This test catches a bug where AsyncCalendarObjectResource was missing
the has_component() method that's used in AsyncCalendar.search() to
filter out empty search results (a Google quirk).
See async_collection.py:779 which calls:
objects = [o for o in objects if o.has_component()]
"""
from caldav.aio import (
AsyncCalendarObjectResource,
AsyncEvent,
AsyncJournal,
AsyncTodo,
)
# Verify has_component exists on all async calendar object classes
for cls in [AsyncCalendarObjectResource, AsyncEvent, AsyncTodo, AsyncJournal]:
assert hasattr(cls, "has_component"), f"{cls.__name__} missing has_component method"
def test_has_component_with_data(self) -> None:
"""Test has_component returns True when object has VEVENT/VTODO/VJOURNAL."""
from caldav.aio import AsyncEvent
event_data = """BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VEVENT
UID:test@example.com
DTSTART:20200101T100000Z
DTEND:20200101T110000Z
SUMMARY:Test Event
END:VEVENT
END:VCALENDAR"""
event = AsyncEvent(client=None, data=event_data)
assert event.has_component() is True
def test_has_component_without_data(self) -> None:
"""Test has_component returns False when object has no data."""
from caldav.aio import AsyncCalendarObjectResource
obj = AsyncCalendarObjectResource(client=None, data=None)
assert obj.has_component() is False
def test_has_component_with_empty_data(self) -> None:
"""Test has_component returns False when object has no data.
Note: The sync CalendarObjectResource validates data on assignment,
so we use data=None instead of data="" to test the "no data" case.
"""
from caldav.aio import AsyncCalendarObjectResource
obj = AsyncCalendarObjectResource(client=None, data=None)
assert obj.has_component() is False
def test_has_component_with_only_vcalendar(self) -> None:
"""Test has_component returns False when only VCALENDAR wrapper exists."""
from caldav.aio import AsyncCalendarObjectResource
# Only VCALENDAR wrapper, no actual component
data = """BEGIN:VCALENDAR
VERSION:2.0
END:VCALENDAR"""
obj = AsyncCalendarObjectResource(client=None, data=data)
# This should return False since there's no VEVENT/VTODO/VJOURNAL
assert obj.has_component() is False
class TestAsyncCalendarAddObject:
"""Tests for Calendar.add_object/add_event/add_todo with async clients (issue #631)."""
SIMPLE_EVENT = """BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Test//Test//EN
BEGIN:VEVENT
UID:test-async-add-event@example.com
DTSTART:20200101T100000Z
DTEND:20200101T110000Z
SUMMARY:Test Async Add Event
END:VEVENT
END:VCALENDAR"""
@pytest.mark.asyncio
async def test_add_event_returns_coroutine_with_async_client(self) -> None:
"""Calendar.add_event() must be awaitable when using AsyncDAVClient.
Regression test for issue #631: o.save() returns a coroutine for async
clients, so add_object() must await it instead of doing o.url on the
coroutine object.
"""
import inspect
from caldav.aio import AsyncEvent
from caldav.collection import Calendar
client = AsyncDAVClient(url="https://caldav.example.com/dav/")
calendar = Calendar(client=client, url="https://caldav.example.com/dav/calendars/test/")
with patch.object(AsyncEvent, "_async_create", new_callable=AsyncMock):
result = calendar.add_event(self.SIMPLE_EVENT)
# With an async client, add_event must return a coroutine
assert inspect.isawaitable(result), (
"add_event() should return a coroutine when using AsyncDAVClient, "
"got %r instead" % result
)
event = await result
assert isinstance(event, AsyncEvent)
@pytest.mark.asyncio
async def test_add_event_result_has_url(self) -> None:
"""Awaiting add_event() with async client returns an Event with a URL."""
from caldav.aio import AsyncEvent
from caldav.collection import Calendar
client = AsyncDAVClient(url="https://caldav.example.com/dav/")
calendar = Calendar(client=client, url="https://caldav.example.com/dav/calendars/test/")
with patch.object(AsyncEvent, "_async_create", new_callable=AsyncMock):
event = await calendar.add_event(self.SIMPLE_EVENT)
# Should have a URL set (or None, but not crash)
_ = event.url # must not raise AttributeError
class TestAsyncRateLimiting:
"""
Unit tests for 429/503 rate-limit handling in AsyncDAVClient.
Mirrors TestRateLimiting in test_caldav_unit.py.
No real server communication.
"""
def _make_response(self, status_code, headers=None):
resp = MagicMock()
resp.status_code = status_code
resp.headers = headers or {}
resp.reason = "Too Many Requests" if status_code == 429 else "Service Unavailable"
resp.reason_phrase = resp.reason
return resp
@pytest.mark.asyncio
async def test_429_no_retry_after_raises(self):
client = AsyncDAVClient(url="http://cal.example.com/")
client.session.request = AsyncMock(return_value=self._make_response(429))
with pytest.raises(error.RateLimitError) as exc_info:
await client.request("/")
assert exc_info.value.retry_after is None
assert exc_info.value.retry_after_seconds is None
@pytest.mark.asyncio
async def test_429_with_integer_retry_after(self):
client = AsyncDAVClient(url="http://cal.example.com/")
client.session.request = AsyncMock(
return_value=self._make_response(429, {"Retry-After": "30"})
)
with pytest.raises(error.RateLimitError) as exc_info:
await client.request("/")
assert exc_info.value.retry_after == "30"
assert exc_info.value.retry_after_seconds == 30.0
@pytest.mark.asyncio
async def test_503_without_retry_after_does_not_raise_rate_limit(self):
client = AsyncDAVClient(url="http://cal.example.com/")
client.session.request = AsyncMock(return_value=self._make_response(503))
# Should not raise RateLimitError; falls through as a normal 503 response
response = await client.request("/")
assert response.status == 503
@pytest.mark.asyncio
async def test_503_with_retry_after_raises(self):
client = AsyncDAVClient(url="http://cal.example.com/")
client.session.request = AsyncMock(
return_value=self._make_response(503, {"Retry-After": "10"})
)
with pytest.raises(error.RateLimitError) as exc_info:
await client.request("/")
assert exc_info.value.retry_after_seconds == 10.0
@pytest.mark.asyncio
async def test_rate_limit_handle_sleeps_and_retries(self):
ok_response = self._make_response(200)
client = AsyncDAVClient(url="http://cal.example.com/", rate_limit_handle=True)
client.session.request = AsyncMock(
side_effect=[
self._make_response(429, {"Retry-After": "5"}),
ok_response,
]
)
with patch("caldav.async_davclient.asyncio.sleep", new_callable=AsyncMock) as mock_sleep:
response = await client.request("/")
mock_sleep.assert_awaited_once_with(5.0)
assert response.status == 200
assert client.session.request.call_count == 2
@pytest.mark.asyncio
async def test_rate_limit_handle_default_sleep_used_when_no_retry_after(self):
ok_response = self._make_response(200)
client = AsyncDAVClient(
url="http://cal.example.com/", rate_limit_handle=True, rate_limit_default_sleep=3
)
client.session.request = AsyncMock(side_effect=[self._make_response(429), ok_response])
with patch("caldav.async_davclient.asyncio.sleep", new_callable=AsyncMock) as mock_sleep:
response = await client.request("/")
mock_sleep.assert_awaited_once_with(3.0)
assert response.status == 200
@pytest.mark.asyncio
async def test_rate_limit_handle_no_sleep_info_raises(self):
client = AsyncDAVClient(url="http://cal.example.com/", rate_limit_handle=True)
client.session.request = AsyncMock(return_value=self._make_response(429))
with pytest.raises(error.RateLimitError):
await client.request("/")
@pytest.mark.asyncio
async def test_rate_limit_max_sleep_caps_sleep_time(self):
ok_response = self._make_response(200)
client = AsyncDAVClient(
url="http://cal.example.com/", rate_limit_handle=True, rate_limit_max_sleep=60
)
client.session.request = AsyncMock(
side_effect=[
self._make_response(429, {"Retry-After": "3600"}),
ok_response,
]
)
with patch("caldav.async_davclient.asyncio.sleep", new_callable=AsyncMock) as mock_sleep:
await client.request("/")
mock_sleep.assert_awaited_once_with(60.0)
@pytest.mark.asyncio
async def test_rate_limit_max_sleep_zero_raises(self):
client = AsyncDAVClient(
url="http://cal.example.com/", rate_limit_handle=True, rate_limit_max_sleep=0
)
client.session.request = AsyncMock(
return_value=self._make_response(429, {"Retry-After": "30"})
)
with pytest.raises(error.RateLimitError):
await client.request("/")
@pytest.mark.asyncio
async def test_rate_limit_adaptive_sleep_increases_on_repeated_retries(self):
"""On repeated 429s the sleep grows: first sleep uses Retry-After, second adds half of already-slept."""
ok_response = self._make_response(200)
client = AsyncDAVClient(url="http://cal.example.com/", rate_limit_handle=True)
client.session.request = AsyncMock(
side_effect=[