From d8bbe2fccea527d9f891333e193a09c393bd050c Mon Sep 17 00:00:00 2001 From: holger krekel Date: Mon, 8 Jun 2020 14:16:40 +0200 Subject: [PATCH] refine test / chat API --- python/examples/echo_and_quit.py | 2 +- python/examples/group_tracking.py | 2 +- python/src/deltachat/events.py | 6 +++ python/src/deltachat/message.py | 8 ++-- python/tests/stress_test_db.py | 2 +- python/tests/test_account.py | 61 +++++++++++-------------------- 6 files changed, 34 insertions(+), 47 deletions(-) diff --git a/python/examples/echo_and_quit.py b/python/examples/echo_and_quit.py index 0dd3707fe..e6ae69b7f 100644 --- a/python/examples/echo_and_quit.py +++ b/python/examples/echo_and_quit.py @@ -12,7 +12,7 @@ class EchoPlugin: message.account.shutdown() else: # unconditionally accept the chat - message.accept_sender_contact() + message.create_chat() addr = message.get_sender_contact().addr if message.is_system_message(): message.chat.send_text("echoing system message from {}:\n{}".format(addr, message)) diff --git a/python/examples/group_tracking.py b/python/examples/group_tracking.py index d7f180985..8e7cd92f4 100644 --- a/python/examples/group_tracking.py +++ b/python/examples/group_tracking.py @@ -12,7 +12,7 @@ class GroupTrackingPlugin: message.account.shutdown() else: # unconditionally accept the chat - message.accept_sender_contact() + message.create_chat() addr = message.get_sender_contact().addr text = message.text message.chat.send_text("echoing from {}:\n{}".format(addr, text)) diff --git a/python/src/deltachat/events.py b/python/src/deltachat/events.py index 967002f65..7042b9641 100644 --- a/python/src/deltachat/events.py +++ b/python/src/deltachat/events.py @@ -127,6 +127,12 @@ class FFIEventTracker: if ev.data2 > 0: return self.account.get_message_by_id(ev.data2) + def wait_msg_delivered(self, msg): + ev = self.get_matching("DC_EVENT_MSG_DELIVERED") + assert ev.data1 == msg.chat.id + assert ev.data2 == msg.id + assert msg.is_out_delivered() + class EventThread(threading.Thread): """ Event Thread for an account. diff --git a/python/src/deltachat/message.py b/python/src/deltachat/message.py index 41bd51276..a959f80db 100644 --- a/python/src/deltachat/message.py +++ b/python/src/deltachat/message.py @@ -53,15 +53,15 @@ class Message(object): lib.dc_msg_unref )) - def accept_sender_contact(self): - """ ensure that the sender is an accepted contact - and that the message has a non-deaddrop chat object. + def create_chat(self): + """ create a chat from a message (eg Contact-Request message). """ - self.account.create_chat_by_message(self) + chat = self.account.create_chat_by_message(self) self._dc_msg = ffi.gc( lib.dc_get_msg(self.account._dc_context, self.id), lib.dc_msg_unref ) + return chat @props.with_doc def text(self): diff --git a/python/tests/stress_test_db.py b/python/tests/stress_test_db.py index 178ddcba6..4a9229302 100644 --- a/python/tests/stress_test_db.py +++ b/python/tests/stress_test_db.py @@ -110,7 +110,7 @@ class AutoReplier: if self.current_sent >= self.num_send: self.report_func(self, ReportType.exit) return - message.accept_sender_contact() + message.create_chat() message.mark_seen() self.log("incoming message: {}".format(message)) diff --git a/python/tests/test_account.py b/python/tests/test_account.py index 144cb0004..268bc9f3d 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -818,29 +818,24 @@ class TestOnlineAccount: chat = acfactory.get_chat(ac1, ac2, both=False) lp.sec("sending text message from ac1 to ac2") - msg_out = chat.send_text("message1") - ev = ac1._evtracker.get_matching("DC_EVENT_MSG_DELIVERED") - assert ev.data1 == chat.id - assert ev.data2 == msg_out.id - assert msg_out.is_out_delivered() + msg1 = chat.send_text("message1") + ac1._evtracker.wait_msg_delivered(msg1) lp.sec("wait for ac2 to receive message") - ev = ac2._evtracker.get_matching("DC_EVENT_MSGS_CHANGED") - assert ev.data2 == msg_out.id - msg_in = ac2.get_message_by_id(msg_out.id) - assert msg_in.text == "message1" - assert not msg_in.is_forwarded() - assert msg_in.get_sender_contact().display_name == ac1.get_config("displayname") + msg2 = ac2._evtracker.wait_next_messages_changed() + assert msg2.text == "message1" + assert not msg2.is_forwarded() + assert msg2.get_sender_contact().display_name == ac1.get_config("displayname") lp.sec("check the message arrived in contact-requests/deaddrop") - chat2 = msg_in.chat - assert msg_in in chat2.get_messages() + chat2 = msg2.chat + assert msg2 in chat2.get_messages() assert chat2.is_deaddrop() assert chat2.count_fresh_messages() == 0 - assert msg_in.time_received >= msg_out.time_sent + assert msg2.time_received >= msg1.time_sent lp.sec("create new chat with contact and verify it's proper") - chat2b = ac2.create_chat_by_message(msg_in) + chat2b = msg2.create_chat() assert not chat2b.is_deaddrop() assert chat2b.count_fresh_messages() == 1 @@ -851,27 +846,25 @@ class TestOnlineAccount: ac2._evtracker.consume_events() lp.sec("sending a second message from ac1 to ac2") - msg_out2 = chat.send_text("message2") + msg3 = chat.send_text("message2") lp.sec("wait for ac2 to receive second message") - ev = ac2._evtracker.get_matching("DC_EVENT_INCOMING_MSG") - assert ev.data2 == msg_out2.id - msg_in2 = ac2.get_message_by_id(msg_out2.id) + msg4 = ac2._evtracker.wait_next_incoming_message() lp.sec("mark messages as seen on ac2, wait for changes on ac1") - ac2.mark_seen_messages([msg_in, msg_in2]) + ac2.mark_seen_messages([msg2, msg4]) lp.step("1") for i in range(2): ev = ac1._evtracker.get_matching("DC_EVENT_MSG_READ") assert ev.data1 > const.DC_CHAT_ID_LAST_SPECIAL assert ev.data2 > const.DC_MSG_ID_LAST_SPECIAL lp.step("2") - assert msg_out.is_out_mdn_received() - assert msg_out2.is_out_mdn_received() + assert msg1.is_out_mdn_received() + assert msg3.is_out_mdn_received() - lp.sec("check that a second call to mark_seen does not create change or smtp job") + lp.sec("try check that a second call to mark_seen doesn't happen") ac2._evtracker.consume_events() - msg_in.mark_seen() + msg2.mark_seen() try: ac2._evtracker.get_matching("DC_EVENT_MSG_READ", timeout=0.01) except queue.Empty: @@ -1049,7 +1042,7 @@ class TestOnlineAccount: lp.sec("sending text message from ac1 to ac2") msg_out = chat.send_text("message1") - ac1._evtracker.get_matching("DC_EVENT_MSG_DELIVERED") + ac1._evtracker.wait_msg_delivered(msg_out) assert msg_out.get_mime_headers() is None lp.sec("wait for ac2 to receive message") @@ -1117,10 +1110,7 @@ class TestOnlineAccount: lp.sec("sending image message from ac1 to ac2") path = data.get_path("d.png") msg_out = chat.send_image(path) - ev = ac1._evtracker.get_matching("DC_EVENT_MSG_DELIVERED") - assert ev.data1 == chat.id - assert ev.data2 == msg_out.id - assert msg_out.is_out_delivered() + ac1._evtracker.wait_msg_delivered(msg_out) m = out.get() assert m == msg_out m = delivered.get() @@ -1351,7 +1341,7 @@ class TestOnlineAccount: def ac_incoming_message(self, message): # we immediately accept the sender because # otherwise we won't see member_added contacts - message.accept_sender_contact() + message.create_chat() @account_hookimpl def ac_chat_modified(self, chat): @@ -1381,7 +1371,7 @@ class TestOnlineAccount: sorted(x.addr for x in ev.chat.get_contacts()) lp.sec("ac1: add address2") - # note that if the above accept_sender_contact() would not + # note that if the above create_chat() would not # happen we would not receive a proper member_added event contact2 = ac1.create_contact(email="notexistingaccountihope@testrun.org") chat.add_contact(contact2) @@ -1462,15 +1452,6 @@ class TestOnlineAccount: assert msg_back.chat == chat assert chat.get_profile_image() is None - def test_accept_sender_contact(self, acfactory, lp): - ac1, ac2 = acfactory.get_two_online_accounts() - ch = ac1.create_chat_by_contact(ac1.create_contact(ac2.get_config("addr"))) - ch.send_text("hello") - msg = ac2._evtracker.wait_next_messages_changed() - assert msg.chat.is_deaddrop() - msg.accept_sender_contact() - assert not msg.chat.is_deaddrop() - def test_send_receive_locations(self, acfactory, lp): now = datetime.utcnow() ac1, ac2 = acfactory.get_two_online_accounts()