From 8298136306267fc06dd396d9719642b0dea410e6 Mon Sep 17 00:00:00 2001 From: link2xt Date: Sat, 22 Mar 2025 14:06:51 +0000 Subject: [PATCH 1/2] api(deltachat-rpc-client): accept Account as Account.create_contact() argument --- .../src/deltachat_rpc_client/account.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/deltachat-rpc-client/src/deltachat_rpc_client/account.py b/deltachat-rpc-client/src/deltachat_rpc_client/account.py index b2486c19ad..2bd4127f82 100644 --- a/deltachat-rpc-client/src/deltachat_rpc_client/account.py +++ b/deltachat-rpc-client/src/deltachat_rpc_client/account.py @@ -115,7 +115,7 @@ def bring_online(self): self.start_io() self.wait_for_event(EventType.IMAP_INBOX_IDLE) - def create_contact(self, obj: Union[int, str, Contact], name: Optional[str] = None) -> Contact: + def create_contact(self, obj: Union[int, str, Contact, "Account"], name: Optional[str] = None) -> Contact: """Create a new Contact or return an existing one. Calling this method will always result in the same @@ -123,9 +123,15 @@ def create_contact(self, obj: Union[int, str, Contact], name: Optional[str] = No with that e-mail address, it is unblocked and its display name is updated if specified. - :param obj: email-address or contact id. + :param obj: email-address, contact id or account. :param name: (optional) display name for this contact. """ + if isinstance(obj, Account): + vcard = obj.self_contact.make_vcard() + [contact] = self.import_vcard(vcard) + if name: + contact.set_name(name) + return contact if isinstance(obj, int): obj = Contact(self, obj) if isinstance(obj, Contact): @@ -146,9 +152,8 @@ def import_vcard(self, vcard: str) -> list[Contact]: return [Contact(self, contact_id) for contact_id in contact_ids] def create_chat(self, account: "Account") -> Chat: - vcard = account.self_contact.make_vcard() - [contact] = self.import_vcard(vcard) - return contact.create_chat() + """Create a 1:1 chat with another account.""" + return self.create_contact(account).create_chat() def get_device_chat(self) -> Chat: """Return device chat.""" From 8da21e32c102fe5154e4a514a480471bbd971506 Mon Sep 17 00:00:00 2001 From: link2xt Date: Sat, 22 Mar 2025 14:06:51 +0000 Subject: [PATCH 2/2] test: use vCards more in Python tests --- .../tests/test_chatlist_events.py | 9 ++--- deltachat-rpc-client/tests/test_securejoin.py | 22 +++++------- deltachat-rpc-client/tests/test_something.py | 34 ++++++------------- 3 files changed, 23 insertions(+), 42 deletions(-) diff --git a/deltachat-rpc-client/tests/test_chatlist_events.py b/deltachat-rpc-client/tests/test_chatlist_events.py index 94ca2b2045..7c9b63466a 100644 --- a/deltachat-rpc-client/tests/test_chatlist_events.py +++ b/deltachat-rpc-client/tests/test_chatlist_events.py @@ -48,8 +48,7 @@ def test_delivery_status(acfactory: ACFactory) -> None: """ alice, bob = acfactory.get_online_accounts(2) - bob_addr = bob.get_config("addr") - alice_contact_bob = alice.create_contact(bob_addr, "Bob") + alice_contact_bob = alice.create_contact(bob, "Bob") alice_chat_bob = alice_contact_bob.create_chat() alice.clear_all_events() @@ -119,8 +118,7 @@ def test_download_on_demand(acfactory: ACFactory) -> None: """ alice, bob = acfactory.get_online_accounts(2) - bob_addr = bob.get_config("addr") - alice_contact_bob = alice.create_contact(bob_addr, "Bob") + alice_contact_bob = alice.create_contact(bob, "Bob") alice_chat_bob = alice_contact_bob.create_chat() alice_chat_bob.send_text("hi") @@ -150,8 +148,7 @@ def test_download_on_demand(acfactory: ACFactory) -> None: def get_multi_account_test_setup(acfactory: ACFactory) -> [Account, Account, Account]: alice, bob = acfactory.get_online_accounts(2) - bob_addr = bob.get_config("addr") - alice_contact_bob = alice.create_contact(bob_addr, "Bob") + alice_contact_bob = alice.create_contact(bob, "Bob") alice_chat_bob = alice_contact_bob.create_chat() alice_chat_bob.send_text("hi") diff --git a/deltachat-rpc-client/tests/test_securejoin.py b/deltachat-rpc-client/tests/test_securejoin.py index a6a05fcc3f..63254dd485 100644 --- a/deltachat-rpc-client/tests/test_securejoin.py +++ b/deltachat-rpc-client/tests/test_securejoin.py @@ -117,8 +117,7 @@ def test_qr_securejoin_contact_request(acfactory) -> None: """Alice invites Bob to a group when Bob's chat with Alice is in a contact request mode.""" alice, bob = acfactory.get_online_accounts(2) - bob_addr = bob.get_config("addr") - alice_contact_bob = alice.create_contact(bob_addr, "Bob") + alice_contact_bob = alice.create_contact(bob, "Bob") alice_chat_bob = alice_contact_bob.create_chat() alice_chat_bob.send_text("Hello!") @@ -155,11 +154,8 @@ def test_qr_readreceipt(acfactory) -> None: logging.info("Alice creates a verified group") group = alice.create_group("Group", protect=True) - bob_addr = bob.get_config("addr") - charlie_addr = charlie.get_config("addr") - - alice_contact_bob = alice.create_contact(bob_addr, "Bob") - alice_contact_charlie = alice.create_contact(charlie_addr, "Charlie") + alice_contact_bob = alice.create_contact(bob, "Bob") + alice_contact_charlie = alice.create_contact(charlie, "Charlie") group.add_contact(alice_contact_bob) group.add_contact(alice_contact_charlie) @@ -186,7 +182,7 @@ def test_qr_readreceipt(acfactory) -> None: charlie_snapshot = charlie_message.get_snapshot() assert charlie_snapshot.text == "Hi from Bob!" - bob_contact_charlie = bob.create_contact(charlie_addr, "Charlie") + bob_contact_charlie = bob.create_contact(charlie, "Charlie") assert not bob.get_chat_by_contact(bob_contact_charlie) logging.info("Charlie reads Bob's message") @@ -517,9 +513,9 @@ def test_gossip_verification(acfactory) -> None: bob.secure_join(qr_code) bob.wait_for_securejoin_joiner_success() - bob_contact_alice = bob.create_contact(alice.get_config("addr"), "Alice") - bob_contact_carol = bob.create_contact(carol.get_config("addr"), "Carol") - carol_contact_alice = carol.create_contact(alice.get_config("addr"), "Alice") + bob_contact_alice = bob.create_contact(alice, "Alice") + bob_contact_carol = bob.create_contact(carol, "Carol") + carol_contact_alice = carol.create_contact(alice, "Alice") logging.info("Bob creates an Autocrypt group") bob_group_chat = bob.create_group("Autocrypt Group") @@ -579,7 +575,7 @@ def test_securejoin_after_contact_resetup(acfactory) -> None: ac2.wait_for_securejoin_joiner_success() # ac1 is verified for ac2. - ac2_contact_ac1 = ac2.create_contact(ac1.get_config("addr"), "") + ac2_contact_ac1 = ac2.create_contact(ac1, "") assert ac2_contact_ac1.get_snapshot().is_verified # ac1 resetups the account. @@ -594,7 +590,7 @@ def test_securejoin_after_contact_resetup(acfactory) -> None: # header sent by old ac1. while True: # ac1 sends a message to ac2. - ac1_contact_ac2 = ac1.create_contact(ac2.get_config("addr"), "") + ac1_contact_ac2 = ac1.create_contact(ac2, "") ac1_chat_ac2 = ac1_contact_ac2.create_chat() ac1_chat_ac2.send_text("Hello!") diff --git a/deltachat-rpc-client/tests/test_something.py b/deltachat-rpc-client/tests/test_something.py index d7015e1b26..9396a57149 100644 --- a/deltachat-rpc-client/tests/test_something.py +++ b/deltachat-rpc-client/tests/test_something.py @@ -106,7 +106,7 @@ def test_account(acfactory) -> None: alice, bob = acfactory.get_online_accounts(2) bob_addr = bob.get_config("addr") - alice_contact_bob = alice.create_contact(bob_addr, "Bob") + alice_contact_bob = alice.create_contact(bob, "Bob") alice_chat_bob = alice_contact_bob.create_chat() alice_chat_bob.send_text("Hello!") @@ -171,8 +171,7 @@ def test_account(acfactory) -> None: def test_chat(acfactory) -> None: alice, bob = acfactory.get_online_accounts(2) - bob_addr = bob.get_config("addr") - alice_contact_bob = alice.create_contact(bob_addr, "Bob") + alice_contact_bob = alice.create_contact(bob, "Bob") alice_chat_bob = alice_contact_bob.create_chat() alice_chat_bob.send_text("Hello!") @@ -238,7 +237,7 @@ def test_contact(acfactory) -> None: alice, bob = acfactory.get_online_accounts(2) bob_addr = bob.get_config("addr") - alice_contact_bob = alice.create_contact(bob_addr, "Bob") + alice_contact_bob = alice.create_contact(bob, "Bob") assert alice_contact_bob == alice.get_contact_by_id(alice_contact_bob.id) assert repr(alice_contact_bob) @@ -255,8 +254,7 @@ def test_contact(acfactory) -> None: def test_message(acfactory) -> None: alice, bob = acfactory.get_online_accounts(2) - bob_addr = bob.get_config("addr") - alice_contact_bob = alice.create_contact(bob_addr, "Bob") + alice_contact_bob = alice.create_contact(bob, "Bob") alice_chat_bob = alice_contact_bob.create_chat() alice_chat_bob.send_text("Hello!") @@ -314,8 +312,7 @@ def test_reaction_seen_on_another_dev(acfactory) -> None: alice2 = alice.clone() alice2.start_io() - bob_addr = bob.get_config("addr") - alice_contact_bob = alice.create_contact(bob_addr, "Bob") + alice_contact_bob = alice.create_contact(bob, "Bob") alice_chat_bob = alice_contact_bob.create_chat() alice_chat_bob.send_text("Hello!") @@ -332,8 +329,7 @@ def test_reaction_seen_on_another_dev(acfactory) -> None: alice2.clear_all_events() alice_chat_bob.mark_noticed() chat_id = alice2.wait_for_event(EventType.MSGS_NOTICED).chat_id - alice2_contact_bob = alice2.get_contact_by_addr(bob_addr) - alice2_chat_bob = alice2_contact_bob.create_chat() + alice2_chat_bob = alice2.create_chat(bob) assert chat_id == alice2_chat_bob.id @@ -341,8 +337,7 @@ def test_is_bot(acfactory) -> None: """Test that we can recognize messages submitted by bots.""" alice, bob = acfactory.get_online_accounts(2) - bob_addr = bob.get_config("addr") - alice_contact_bob = alice.create_contact(bob_addr, "Bob") + alice_contact_bob = alice.create_contact(bob, "Bob") alice_chat_bob = alice_contact_bob.create_chat() # Alice becomes a bot. @@ -412,8 +407,7 @@ def test_wait_next_messages(acfactory) -> None: # Bot starts waiting for messages. next_messages_task = executor.submit(bot.wait_next_messages) - bot_addr = bot.get_config("addr") - alice_contact_bot = alice.create_contact(bot_addr, "Bot") + alice_contact_bot = alice.create_contact(bot, "Bot") alice_chat_bot = alice_contact_bot.create_chat() alice_chat_bot.send_text("Hello!") @@ -437,9 +431,7 @@ def test_import_export_backup(acfactory, tmp_path) -> None: def test_import_export_keys(acfactory, tmp_path) -> None: alice, bob = acfactory.get_online_accounts(2) - bob_addr = bob.get_config("addr") - alice_contact_bob = alice.create_contact(bob_addr, "Bob") - alice_chat_bob = alice_contact_bob.create_chat() + alice_chat_bob = alice.create_chat(bob) alice_chat_bob.send_text("Hello Bob!") snapshot = bob.get_message_by_id(bob.wait_for_incoming_msg_event().msg_id).get_snapshot() @@ -489,9 +481,7 @@ def test_provider_info(rpc) -> None: def test_mdn_doesnt_break_autocrypt(acfactory) -> None: alice, bob = acfactory.get_online_accounts(2) - bob_addr = bob.get_config("addr") - - alice_contact_bob = alice.create_contact(bob_addr, "Bob") + alice_contact_bob = alice.create_contact(bob, "Bob") # Bob creates chat manually so chat with Alice is accepted. alice_chat_bob = alice_contact_bob.create_chat() @@ -633,9 +623,7 @@ def test_download_limit_chat_assignment(acfactory, tmp_path, n_accounts): chat.send_text("Hello Alice!") assert alice.get_message_by_id(alice.wait_for_incoming_msg_event().msg_id).get_snapshot().text == "Hello Alice!" - contact_addr = account.get_config("addr") - contact = alice.create_contact(contact_addr, "") - + contact = alice.create_contact(account) alice_group.add_contact(contact) if n_accounts == 2: