🤖 ldap2nextcloud : synchronisation LDAP vers un carnet Nextcloud (CardDAV)
Historique regroupé en un seul commit pour ne pas publier de données de production. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
f6a01b85e1
42 files changed
+5538
No files matched your search
@@ -0,0 +1,159 @@
|
||||
import base64
|
||||
|
||||
import pytest
|
||||
import responses
|
||||
|
||||
from ldap2nextcloud.carddav import CardDavClient, CardDavError
|
||||
|
||||
BOOK = "https://cloud.test/remote.php/dav/addressbooks/users/annuaire/annuaire/"
|
||||
BOOK_PATH = "/remote.php/dav/addressbooks/users/annuaire/annuaire/"
|
||||
|
||||
|
||||
def multistatus(*items):
|
||||
body = "".join(
|
||||
f"""<d:response><d:href>{href}</d:href><d:propstat><d:prop>
|
||||
{f'<d:getetag>{etag}</d:getetag>' if etag else ''}
|
||||
{f'<card:address-data>{data}</card:address-data>' if data else ''}
|
||||
</d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response>"""
|
||||
for href, etag, data in items
|
||||
)
|
||||
return (
|
||||
'<?xml version="1.0"?><d:multistatus xmlns:d="DAV:" '
|
||||
f'xmlns:card="urn:ietf:params:xml:ns:carddav">{body}</d:multistatus>'
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
return CardDavClient(BOOK, "annuaire", "secret")
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_list_etags_normalizes_hrefs(client):
|
||||
responses.add(
|
||||
"PROPFIND", BOOK, status=207,
|
||||
body=multistatus(
|
||||
(BOOK_PATH, None, None),
|
||||
(BOOK_PATH + "a%40b.vcf", '"123"', None),
|
||||
(BOOK + "c.vcf", '"456"', None),
|
||||
),
|
||||
)
|
||||
assert client.list_etags() == {"a@b.vcf": "123", "c.vcf": "456"}
|
||||
assert responses.calls[0].request.headers["Depth"] == "1"
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_list_etags_empty_addressbook(client):
|
||||
responses.add("PROPFIND", BOOK, status=207, body=multistatus((BOOK_PATH, None, None)))
|
||||
assert client.list_etags() == {}
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_list_etags_error_raises(client):
|
||||
responses.add("PROPFIND", BOOK, status=401)
|
||||
with pytest.raises(CardDavError) as err:
|
||||
client.list_etags()
|
||||
assert err.value.status == 401
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_put_create_sends_if_none_match(client):
|
||||
responses.add("PUT", BOOK + "x.vcf", status=201, headers={"ETag": '"e1"'})
|
||||
assert client.put("x.vcf", "BEGIN:VCARD", None) == "e1"
|
||||
request = responses.calls[0].request
|
||||
assert request.headers["If-None-Match"] == "*"
|
||||
assert "If-Match" not in request.headers
|
||||
assert request.headers["Content-Type"] == "text/vcard; charset=utf-8"
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_put_update_sends_if_match(client):
|
||||
responses.add("PUT", BOOK + "x.vcf", status=204, headers={"ETag": '"e1"'})
|
||||
client.put("x.vcf", "BEGIN:VCARD", "e0")
|
||||
assert responses.calls[0].request.headers["If-Match"] == '"e0"'
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_put_without_etag_in_response_returns_none(client):
|
||||
responses.add("PUT", BOOK + "x.vcf", status=201)
|
||||
assert client.put("x.vcf", "BEGIN:VCARD", None) is None
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_put_412_raises_with_status(client):
|
||||
responses.add("PUT", BOOK + "x.vcf", status=412)
|
||||
with pytest.raises(CardDavError) as err:
|
||||
client.put("x.vcf", "...", "old")
|
||||
assert err.value.status == 412
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_put_encodes_href(client):
|
||||
responses.add("PUT", BOOK + "a%40b.vcf", status=201)
|
||||
client.put("a@b.vcf", "BEGIN:VCARD", None)
|
||||
assert responses.calls[0].request.url == BOOK + "a%40b.vcf"
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_delete_sends_if_match(client):
|
||||
responses.add("DELETE", BOOK + "x.vcf", status=204)
|
||||
client.delete("x.vcf", "e1")
|
||||
assert responses.calls[0].request.headers["If-Match"] == '"e1"'
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_delete_404_is_success(client):
|
||||
responses.add("DELETE", BOOK + "x.vcf", status=404)
|
||||
client.delete("x.vcf", None)
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_delete_error_raises(client):
|
||||
responses.add("DELETE", BOOK + "x.vcf", status=500)
|
||||
with pytest.raises(CardDavError):
|
||||
client.delete("x.vcf", None)
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_multiget_returns_card_text(client):
|
||||
responses.add(
|
||||
"REPORT", BOOK, status=207,
|
||||
body=multistatus((BOOK_PATH + "a.vcf", '"1"', "BEGIN:VCARD \nFN:A \nEND:VCARD")),
|
||||
)
|
||||
assert client.multiget(["a.vcf"]) == {"a.vcf": "BEGIN:VCARD\r\nFN:A\r\nEND:VCARD"}
|
||||
assert BOOK_PATH + "a.vcf" in responses.calls[0].request.body.decode()
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_multiget_batches_by_50(client):
|
||||
hrefs = [f"{i}.vcf" for i in range(120)]
|
||||
|
||||
def reply(request):
|
||||
body = request.body.decode()
|
||||
items = [(BOOK_PATH + h, '"1"', "BEGIN:VCARD") for h in hrefs if f"/{h}<" in body]
|
||||
return 207, {}, multistatus(*items)
|
||||
|
||||
responses.add_callback("REPORT", BOOK, callback=reply)
|
||||
result = client.multiget(hrefs)
|
||||
assert len(responses.calls) == 3
|
||||
assert set(result) == set(hrefs)
|
||||
|
||||
|
||||
def test_multiget_empty_makes_no_request(client):
|
||||
assert client.multiget([]) == {}
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_basic_auth_sent(client):
|
||||
responses.add("PROPFIND", BOOK, status=207, body=multistatus())
|
||||
client.list_etags()
|
||||
expected = "Basic " + base64.b64encode(b"annuaire:secret").decode()
|
||||
assert responses.calls[0].request.headers["Authorization"] == expected
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_weak_etags_are_normalized_everywhere(client):
|
||||
responses.add("PROPFIND", BOOK, status=207, body=multistatus((BOOK_PATH + "a.vcf", 'W/"123"', None)))
|
||||
responses.add("PUT", BOOK + "a.vcf", status=204, headers={"ETag": 'W/"456"'})
|
||||
assert client.list_etags() == {"a.vcf": "123"}
|
||||
assert client.put("a.vcf", "BEGIN:VCARD", "123") == "456"
|
||||
Reference in new issue
Block a user