intermittent commit
This commit is contained in:
78
tests/test_vcard.py
Normal file
78
tests/test_vcard.py
Normal file
@@ -0,0 +1,78 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
|
||||
from govoplan_addresses.backend.db.models import Contact, ContactEmail, ContactPhone, ContactPostalAddress
|
||||
from govoplan_addresses.backend.vcard import contact_to_vcard, parse_vcards_with_issues
|
||||
|
||||
|
||||
class VCardTests(unittest.TestCase):
|
||||
def test_parse_vcard_preserves_folded_and_escaped_text(self) -> None:
|
||||
result = parse_vcards_with_issues(
|
||||
"BEGIN:VCARD\r\n"
|
||||
"VERSION:4.0\r\n"
|
||||
"UID:contact-1\r\n"
|
||||
"FN:Ada \r\n"
|
||||
" Lovelace\r\n"
|
||||
"N:Lovelace;Ada;;;\r\n"
|
||||
"NOTE:Line one\\nLine two\r\n"
|
||||
"CATEGORIES:science\\,history,engineering\r\n"
|
||||
"EMAIL;TYPE=work;PREF=1:ada@example.local\r\n"
|
||||
"ADR;TYPE=work:;;Main Street 1;Berlin;BE;10115;Germany\r\n"
|
||||
"URL:https://example.local/ada\r\n"
|
||||
"END:VCARD\r\n"
|
||||
)
|
||||
|
||||
self.assertEqual([], result.issues)
|
||||
self.assertEqual(1, len(result.cards))
|
||||
payload = result.cards[0].payload
|
||||
self.assertEqual("Ada Lovelace", payload.display_name)
|
||||
self.assertEqual("Line one\nLine two", payload.note)
|
||||
self.assertEqual(["science,history", "engineering"], payload.tags)
|
||||
self.assertEqual("work", payload.emails[0].label)
|
||||
self.assertTrue(payload.emails[0].is_primary)
|
||||
self.assertEqual("Berlin", payload.postal_addresses[0].locality)
|
||||
self.assertEqual(["https://example.local/ada"], payload.provenance["vcard"]["urls"])
|
||||
|
||||
def test_contact_to_vcard_escapes_text_and_filters_urls(self) -> None:
|
||||
contact = Contact(
|
||||
address_book_id="book-1",
|
||||
display_name="Ada Lovelace",
|
||||
given_name="Ada",
|
||||
family_name="Lovelace",
|
||||
organization="Analytical Engine Office",
|
||||
role_title="Mathematician",
|
||||
note="Line one\nLine two",
|
||||
tags=["science,history", "engineering"],
|
||||
source_ref="contact-1",
|
||||
source_revision="rev-1",
|
||||
provenance={"vcard": {"urls": [" https://example.local/ada ", "", 42]}},
|
||||
)
|
||||
contact.emails = [ContactEmail(label="work", email="ada@example.local", is_primary=True, order_index=0)]
|
||||
contact.phones = [ContactPhone(label="work", phone="+49 30 123", is_primary=True, order_index=0)]
|
||||
contact.postal_addresses = [
|
||||
ContactPostalAddress(
|
||||
label="work",
|
||||
street="Main Street 1",
|
||||
locality="Berlin",
|
||||
region="BE",
|
||||
postal_code="10115",
|
||||
country="Germany",
|
||||
is_primary=True,
|
||||
order_index=0,
|
||||
)
|
||||
]
|
||||
|
||||
content = contact_to_vcard(contact)
|
||||
|
||||
self.assertIn("UID:contact-1\r\n", content)
|
||||
self.assertIn("REV:rev-1\r\n", content)
|
||||
self.assertIn("NOTE:Line one\\nLine two\r\n", content)
|
||||
self.assertIn("CATEGORIES:science\\,history,engineering\r\n", content)
|
||||
self.assertIn("EMAIL;TYPE=work:ada@example.local\r\n", content)
|
||||
self.assertIn("URL:https://example.local/ada\r\n", content)
|
||||
self.assertNotIn("URL:42", content)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user