mirror of
https://github.com/chatmail/core.git
synced 2026-05-07 08:56:30 +03:00
test: get_protected_chat(): Use FFIEventTracker instead of dc_wait_next_msgs() (#5207)
The way it was implemented it threw out all remaining messages after finding the next incoming message. Better use FFIEventTracker functions, they are used in all the tests anyway.
This commit is contained in:
@@ -375,22 +375,6 @@ class Account:
|
|||||||
dc_array = ffi.gc(lib.dc_get_fresh_msgs(self._dc_context), lib.dc_array_unref)
|
dc_array = ffi.gc(lib.dc_get_fresh_msgs(self._dc_context), lib.dc_array_unref)
|
||||||
return (x for x in iter_array(dc_array, lambda x: Message.from_db(self, x)) if x is not None)
|
return (x for x in iter_array(dc_array, lambda x: Message.from_db(self, x)) if x is not None)
|
||||||
|
|
||||||
def _wait_next_message_ids(self) -> List[int]:
|
|
||||||
"""Return IDs of all next messages from all chats."""
|
|
||||||
dc_array = ffi.gc(lib.dc_wait_next_msgs(self._dc_context), lib.dc_array_unref)
|
|
||||||
return [lib.dc_array_get_id(dc_array, i) for i in range(lib.dc_array_get_cnt(dc_array))]
|
|
||||||
|
|
||||||
def wait_next_incoming_message(self) -> Message:
|
|
||||||
"""Waits until the next incoming message
|
|
||||||
with ID higher than given is received and returns it."""
|
|
||||||
while True:
|
|
||||||
message_ids = self._wait_next_message_ids()
|
|
||||||
for msg_id in message_ids:
|
|
||||||
message = Message.from_db(self, msg_id)
|
|
||||||
if message and not message.is_from_self() and not message.is_from_device():
|
|
||||||
self.set_config("last_msg_id", str(msg_id))
|
|
||||||
return message
|
|
||||||
|
|
||||||
def create_chat(self, obj) -> Chat:
|
def create_chat(self, obj) -> Chat:
|
||||||
"""Create a 1:1 chat with Account, Contact or e-mail address."""
|
"""Create a 1:1 chat with Account, Contact or e-mail address."""
|
||||||
return self.create_contact(obj).create_chat()
|
return self.create_contact(obj).create_chat()
|
||||||
|
|||||||
@@ -182,6 +182,12 @@ class FFIEventTracker:
|
|||||||
print(f"** SECUREJOINT-INVITER PROGRESS {target}", self.account)
|
print(f"** SECUREJOINT-INVITER PROGRESS {target}", self.account)
|
||||||
break
|
break
|
||||||
|
|
||||||
|
def wait_securejoin_joiner_progress(self, target):
|
||||||
|
while True:
|
||||||
|
event = self.get_matching("DC_EVENT_SECUREJOIN_JOINER_PROGRESS")
|
||||||
|
if event.data2 >= target:
|
||||||
|
break
|
||||||
|
|
||||||
def wait_idle_inbox_ready(self):
|
def wait_idle_inbox_ready(self):
|
||||||
"""Has to be called after start_io() to wait for fetch_existing_msgs to run
|
"""Has to be called after start_io() to wait for fetch_existing_msgs to run
|
||||||
so that new messages are not mistaken for old ones:
|
so that new messages are not mistaken for old ones:
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import time
|
|||||||
import weakref
|
import weakref
|
||||||
import random
|
import random
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
from threading import Event
|
|
||||||
from typing import Callable, Dict, List, Optional, Set
|
from typing import Callable, Dict, List, Optional, Set
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@@ -592,23 +591,16 @@ class ACFactory:
|
|||||||
return ac1.create_chat(ac2)
|
return ac1.create_chat(ac2)
|
||||||
|
|
||||||
def get_protected_chat(self, ac1: Account, ac2: Account):
|
def get_protected_chat(self, ac1: Account, ac2: Account):
|
||||||
class SetupPlugin:
|
|
||||||
def __init__(self) -> None:
|
|
||||||
self.member_added = Event()
|
|
||||||
|
|
||||||
@account_hookimpl
|
|
||||||
def ac_member_added(self, chat: deltachat.Chat, contact, actor, message):
|
|
||||||
self.member_added.set()
|
|
||||||
|
|
||||||
setupplugin = SetupPlugin()
|
|
||||||
ac1.add_account_plugin(setupplugin)
|
|
||||||
chat = ac1.create_group_chat("Protected Group", verified=True)
|
chat = ac1.create_group_chat("Protected Group", verified=True)
|
||||||
qr = chat.get_join_qr()
|
qr = chat.get_join_qr()
|
||||||
ac2.qr_join_chat(qr)
|
ac2.qr_join_chat(qr)
|
||||||
setupplugin.member_added.wait()
|
ac2._evtracker.wait_securejoin_joiner_progress(1000)
|
||||||
msg = ac2.wait_next_incoming_message()
|
ev = ac2._evtracker.get_matching("DC_EVENT_MSGS_CHANGED")
|
||||||
|
msg = ac2.get_message_by_id(ev.data2)
|
||||||
|
assert msg is not None
|
||||||
assert msg.text == "Messages are guaranteed to be end-to-end encrypted from now on."
|
assert msg.text == "Messages are guaranteed to be end-to-end encrypted from now on."
|
||||||
msg = ac2.wait_next_incoming_message()
|
msg = ac2._evtracker.wait_next_incoming_message()
|
||||||
|
assert msg is not None
|
||||||
assert "Member Me " in msg.text and " added by " in msg.text
|
assert "Member Me " in msg.text and " added by " in msg.text
|
||||||
return chat
|
return chat
|
||||||
|
|
||||||
|
|||||||
@@ -44,21 +44,21 @@ def test_configure_generate_key(acfactory, lp):
|
|||||||
lp.sec("ac1: send unencrypted message to ac2")
|
lp.sec("ac1: send unencrypted message to ac2")
|
||||||
chat.send_text("message1")
|
chat.send_text("message1")
|
||||||
lp.sec("ac2: waiting for message from ac1")
|
lp.sec("ac2: waiting for message from ac1")
|
||||||
msg_in = ac2.wait_next_incoming_message()
|
msg_in = ac2._evtracker.wait_next_incoming_message()
|
||||||
assert msg_in.text == "message1"
|
assert msg_in.text == "message1"
|
||||||
assert not msg_in.is_encrypted()
|
assert not msg_in.is_encrypted()
|
||||||
|
|
||||||
lp.sec("ac2: send encrypted message to ac1")
|
lp.sec("ac2: send encrypted message to ac1")
|
||||||
msg_in.chat.send_text("message2")
|
msg_in.chat.send_text("message2")
|
||||||
lp.sec("ac1: waiting for message from ac2")
|
lp.sec("ac1: waiting for message from ac2")
|
||||||
msg2_in = ac1.wait_next_incoming_message()
|
msg2_in = ac1._evtracker.wait_next_incoming_message()
|
||||||
assert msg2_in.text == "message2"
|
assert msg2_in.text == "message2"
|
||||||
assert msg2_in.is_encrypted()
|
assert msg2_in.is_encrypted()
|
||||||
|
|
||||||
lp.sec("ac1: send encrypted message to ac2")
|
lp.sec("ac1: send encrypted message to ac2")
|
||||||
msg2_in.chat.send_text("message3")
|
msg2_in.chat.send_text("message3")
|
||||||
lp.sec("ac2: waiting for message from ac1")
|
lp.sec("ac2: waiting for message from ac1")
|
||||||
msg3_in = ac2.wait_next_incoming_message()
|
msg3_in = ac2._evtracker.wait_next_incoming_message()
|
||||||
assert msg3_in.text == "message3"
|
assert msg3_in.text == "message3"
|
||||||
assert msg3_in.is_encrypted()
|
assert msg3_in.is_encrypted()
|
||||||
|
|
||||||
@@ -520,7 +520,7 @@ def test_forward_encrypted_to_unencrypted(acfactory, lp):
|
|||||||
lp.sec("ac1: send encrypted message to ac2")
|
lp.sec("ac1: send encrypted message to ac2")
|
||||||
txt = "This should be encrypted"
|
txt = "This should be encrypted"
|
||||||
chat.send_text(txt)
|
chat.send_text(txt)
|
||||||
msg = ac2.wait_next_incoming_message()
|
msg = ac2._evtracker.wait_next_incoming_message()
|
||||||
assert msg.text == txt
|
assert msg.text == txt
|
||||||
assert msg.is_encrypted()
|
assert msg.is_encrypted()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user