streamline fixtures for online accounts, reducing test functions

This commit is contained in:
holger krekel
2019-09-18 10:33:23 +02:00
parent ee327dc87d
commit fc1f1ce37c
3 changed files with 44 additions and 74 deletions

View File

@@ -396,7 +396,7 @@ class Account(object):
def shutdown(self, wait=True): def shutdown(self, wait=True):
""" stop threads and close and remove underlying dc_context and callbacks. """ """ stop threads and close and remove underlying dc_context and callbacks. """
if hasattr(self, "_dc_context") and hasattr(self, "_threads"): if hasattr(self, "_dc_context") and hasattr(self, "_threads"):
print("SHUTDOWN", self) # print("SHUTDOWN", self)
self.stop_threads(wait=False) self.stop_threads(wait=False)
lib.dc_close(self._dc_context) lib.dc_close(self._dc_context)
self.stop_threads(wait=wait) # to wait for threads self.stop_threads(wait=wait) # to wait for threads

View File

@@ -125,13 +125,17 @@ def acfactory(pytestconfig, tmpdir, request, session_liveconfig):
fin = self._finalizers.pop() fin = self._finalizers.pop()
fin() fin()
def make_account(self, path, logid):
ac = Account(path, logid=logid)
self._finalizers.append(ac.shutdown)
return ac
def get_unconfigured_account(self): def get_unconfigured_account(self):
self.offline_count += 1 self.offline_count += 1
tmpdb = tmpdir.join("offlinedb%d" % self.offline_count) tmpdb = tmpdir.join("offlinedb%d" % self.offline_count)
ac = Account(tmpdb.strpath, logid="ac{}".format(self.offline_count)) ac = self.make_account(tmpdb.strpath, logid="ac{}".format(self.offline_count))
ac._evlogger.init_time = self.init_time ac._evlogger.init_time = self.init_time
ac._evlogger.set_timeout(2) ac._evlogger.set_timeout(2)
self._finalizers.append(ac.shutdown)
return ac return ac
def get_configured_offline_account(self): def get_configured_offline_account(self):
@@ -154,23 +158,30 @@ def acfactory(pytestconfig, tmpdir, request, session_liveconfig):
if "e2ee_enabled" not in configdict: if "e2ee_enabled" not in configdict:
configdict["e2ee_enabled"] = "1" configdict["e2ee_enabled"] = "1"
tmpdb = tmpdir.join("livedb%d" % self.live_count) tmpdb = tmpdir.join("livedb%d" % self.live_count)
ac = Account(tmpdb.strpath, logid="ac{}".format(self.live_count)) ac = self.make_account(tmpdb.strpath, logid="ac{}".format(self.live_count))
ac._evlogger.init_time = self.init_time ac._evlogger.init_time = self.init_time
ac._evlogger.set_timeout(30) ac._evlogger.set_timeout(30)
ac.configure(**configdict) ac.configure(**configdict)
ac.start_threads() ac.start_threads()
self._finalizers.append(ac.shutdown)
return ac return ac
def get_two_online_accounts(self):
ac1 = self.get_online_configuring_account()
ac2 = self.get_online_configuring_account()
wait_successful_IMAP_SMTP_connection(ac1)
wait_configuration_progress(ac1, 1000)
wait_successful_IMAP_SMTP_connection(ac2)
wait_configuration_progress(ac2, 1000)
return ac1, ac2
def clone_online_account(self, account): def clone_online_account(self, account):
self.live_count += 1 self.live_count += 1
tmpdb = tmpdir.join("livedb%d" % self.live_count) tmpdb = tmpdir.join("livedb%d" % self.live_count)
ac = Account(tmpdb.strpath, logid="ac{}".format(self.live_count)) ac = self.make_account(tmpdb.strpath, logid="ac{}".format(self.live_count))
ac._evlogger.init_time = self.init_time ac._evlogger.init_time = self.init_time
ac._evlogger.set_timeout(30) ac._evlogger.set_timeout(30)
ac.configure(addr=account.get_config("addr"), mail_pw=account.get_config("mail_pw")) ac.configure(addr=account.get_config("addr"), mail_pw=account.get_config("mail_pw"))
ac.start_threads() ac.start_threads()
self._finalizers.append(ac.shutdown)
return ac return ac
am = AccountMaker() am = AccountMaker()

View File

@@ -334,10 +334,11 @@ class TestOfflineChat:
class TestOnlineAccount: class TestOnlineAccount:
def test_one_account_init(self, acfactory): def get_chat(self, ac1, ac2):
ac1 = acfactory.get_online_configuring_account() c2 = ac1.create_contact(email=ac2.get_config("addr"))
wait_successful_IMAP_SMTP_connection(ac1) chat = ac1.create_chat_by_contact(c2)
wait_configuration_progress(ac1, 1000) assert chat.id >= const.DC_CHAT_ID_LAST_SPECIAL
return chat
def test_one_account_send(self, acfactory): def test_one_account_send(self, acfactory):
ac1 = acfactory.get_online_configuring_account() ac1 = acfactory.get_online_configuring_account()
@@ -353,15 +354,8 @@ class TestOnlineAccount:
assert ev[1] == msg_out.id assert ev[1] == msg_out.id
def test_two_accounts_send_receive(self, acfactory): def test_two_accounts_send_receive(self, acfactory):
ac1 = acfactory.get_online_configuring_account() ac1, ac2 = acfactory.get_two_online_accounts()
ac2 = acfactory.get_online_configuring_account() chat = self.get_chat(ac1, ac2)
c2 = ac1.create_contact(email=ac2.get_config("addr"))
chat = ac1.create_chat_by_contact(c2)
assert chat.id >= const.DC_CHAT_ID_LAST_SPECIAL
wait_successful_IMAP_SMTP_connection(ac1)
wait_configuration_progress(ac1, 1000)
wait_successful_IMAP_SMTP_connection(ac2)
wait_configuration_progress(ac2, 1000)
msg_out = chat.send_text("message1") msg_out = chat.send_text("message1")
@@ -372,15 +366,8 @@ class TestOnlineAccount:
assert msg_in.text == "message1" assert msg_in.text == "message1"
def test_forward_messages(self, acfactory): def test_forward_messages(self, acfactory):
ac1 = acfactory.get_online_configuring_account() ac1, ac2 = acfactory.get_two_online_accounts()
ac2 = acfactory.get_online_configuring_account() chat = self.get_chat(ac1, ac2)
c2 = ac1.create_contact(email=ac2.get_config("addr"))
chat = ac1.create_chat_by_contact(c2)
assert chat.id >= const.DC_CHAT_ID_LAST_SPECIAL
wait_successful_IMAP_SMTP_connection(ac1)
wait_configuration_progress(ac1, 1000)
wait_successful_IMAP_SMTP_connection(ac2)
wait_configuration_progress(ac2, 1000)
msg_out = chat.send_text("message2") msg_out = chat.send_text("message2")
@@ -404,15 +391,10 @@ class TestOnlineAccount:
assert not chat3.get_messages() assert not chat3.get_messages()
def test_send_and_receive_message(self, acfactory, lp): def test_send_and_receive_message(self, acfactory, lp):
lp.sec("starting accounts, waiting for configuration") ac1, ac2 = acfactory.get_two_online_accounts()
ac1 = acfactory.get_online_configuring_account()
ac2 = acfactory.get_online_configuring_account()
c2 = ac1.create_contact(email=ac2.get_config("addr"))
chat = ac1.create_chat_by_contact(c2)
assert chat.id >= const.DC_CHAT_ID_LAST_SPECIAL
wait_configuration_progress(ac1, 1000) lp.sec("ac1: create chat with ac2")
wait_configuration_progress(ac2, 1000) chat = self.get_chat(ac1, ac2)
lp.sec("sending text message from ac1 to ac2") lp.sec("sending text message from ac1 to ac2")
msg_out = chat.send_text("message1") msg_out = chat.send_text("message1")
@@ -454,15 +436,10 @@ class TestOnlineAccount:
assert msg_out.is_out_mdn_received() assert msg_out.is_out_mdn_received()
def test_send_and_receive_will_encrypt_decrypt(self, acfactory, lp): def test_send_and_receive_will_encrypt_decrypt(self, acfactory, lp):
lp.sec("starting accounts, waiting for configuration") ac1, ac2 = acfactory.get_two_online_accounts()
ac1 = acfactory.get_online_configuring_account()
ac2 = acfactory.get_online_configuring_account()
c2 = ac1.create_contact(email=ac2.get_config("addr"))
chat = ac1.create_chat_by_contact(c2)
assert chat.id >= const.DC_CHAT_ID_LAST_SPECIAL
wait_configuration_progress(ac1, 1000) lp.sec("ac1: create chat with ac2")
wait_configuration_progress(ac2, 1000) chat = self.get_chat(ac1, ac2)
lp.sec("sending text message from ac1 to ac2") lp.sec("sending text message from ac1 to ac2")
msg_out = chat.send_text("message1") msg_out = chat.send_text("message1")
@@ -485,14 +462,12 @@ class TestOnlineAccount:
assert msg_back.text == "message-back" assert msg_back.text == "message-back"
def test_saved_mime_on_received_message(self, acfactory, lp): def test_saved_mime_on_received_message(self, acfactory, lp):
lp.sec("starting accounts, waiting for configuration") ac1, ac2 = acfactory.get_two_online_accounts()
ac1 = acfactory.get_online_configuring_account()
ac2 = acfactory.get_online_configuring_account() lp.sec("configure ac2 to save mime headers, create ac1/ac2 chat")
ac2.set_config("save_mime_headers", "1") ac2.set_config("save_mime_headers", "1")
c2 = ac1.create_contact(email=ac2.get_config("addr")) chat = self.get_chat(ac1, ac2)
chat = ac1.create_chat_by_contact(c2)
wait_configuration_progress(ac1, 1000)
wait_configuration_progress(ac2, 1000)
lp.sec("sending text message from ac1 to ac2") lp.sec("sending text message from ac1 to ac2")
msg_out = chat.send_text("message1") msg_out = chat.send_text("message1")
ac1._evlogger.get_matching("DC_EVENT_MSG_DELIVERED") ac1._evlogger.get_matching("DC_EVENT_MSG_DELIVERED")
@@ -506,14 +481,8 @@ class TestOnlineAccount:
assert mime.get_all("Received") assert mime.get_all("Received")
def test_send_and_receive_image(self, acfactory, lp, data): def test_send_and_receive_image(self, acfactory, lp, data):
lp.sec("starting accounts, waiting for configuration") ac1, ac2 = acfactory.get_two_online_accounts()
ac1 = acfactory.get_online_configuring_account() chat = self.get_chat(ac1, ac2)
ac2 = acfactory.get_online_configuring_account()
c2 = ac1.create_contact(email=ac2.get_config("addr"))
chat = ac1.create_chat_by_contact(c2)
wait_configuration_progress(ac1, 1000)
wait_configuration_progress(ac2, 1000)
lp.sec("sending image message from ac1 to ac2") lp.sec("sending image message from ac1 to ac2")
path = data.get_path("d.png") path = data.get_path("d.png")
@@ -533,13 +502,13 @@ class TestOnlineAccount:
assert os.stat(msg_in.filename).st_size == os.stat(path).st_size assert os.stat(msg_in.filename).st_size == os.stat(path).st_size
def test_import_export_online(self, acfactory, tmpdir): def test_import_export_online(self, acfactory, tmpdir):
backupdir = tmpdir.mkdir("backup")
ac1 = acfactory.get_online_configuring_account() ac1 = acfactory.get_online_configuring_account()
wait_configuration_progress(ac1, 1000) wait_configuration_progress(ac1, 1000)
contact1 = ac1.create_contact("some1@hello.com", name="some1") contact1 = ac1.create_contact("some1@hello.com", name="some1")
chat = ac1.create_chat_by_contact(contact1) chat = ac1.create_chat_by_contact(contact1)
chat.send_text("msg1") chat.send_text("msg1")
backupdir = tmpdir.mkdir("backup")
path = ac1.export_to_dir(backupdir.strpath) path = ac1.export_to_dir(backupdir.strpath)
assert os.path.exists(path) assert os.path.exists(path)
@@ -574,10 +543,7 @@ class TestOnlineAccount:
assert ac1.get_info()["fingerprint"] == ac2.get_info()["fingerprint"] assert ac1.get_info()["fingerprint"] == ac2.get_info()["fingerprint"]
def test_qr_setup_contact(self, acfactory, lp): def test_qr_setup_contact(self, acfactory, lp):
ac1 = acfactory.get_online_configuring_account() ac1, ac2 = acfactory.get_two_online_accounts()
ac2 = acfactory.get_online_configuring_account()
wait_configuration_progress(ac2, 1000)
wait_configuration_progress(ac1, 1000)
lp.sec("ac1: create QR code and let ac2 scan it, starting the securejoin") lp.sec("ac1: create QR code and let ac2 scan it, starting the securejoin")
qr = ac1.get_setup_contact_qr() qr = ac1.get_setup_contact_qr()
lp.sec("ac2: start QR-code based setup contact protocol") lp.sec("ac2: start QR-code based setup contact protocol")
@@ -586,12 +552,8 @@ class TestOnlineAccount:
wait_securejoin_inviter_progress(ac1, 1000) wait_securejoin_inviter_progress(ac1, 1000)
def test_qr_join_chat(self, acfactory, lp): def test_qr_join_chat(self, acfactory, lp):
ac1 = acfactory.get_online_configuring_account() ac1, ac2 = acfactory.get_two_online_accounts()
ac2 = acfactory.get_online_configuring_account()
wait_configuration_progress(ac2, 1000)
wait_configuration_progress(ac1, 1000)
lp.sec("ac1: create QR code and let ac2 scan it, starting the securejoin") lp.sec("ac1: create QR code and let ac2 scan it, starting the securejoin")
chat = ac1.create_group_chat("hello") chat = ac1.create_group_chat("hello")
qr = chat.get_join_qr() qr = chat.get_join_qr()
lp.sec("ac2: start QR-code based join-group protocol") lp.sec("ac2: start QR-code based join-group protocol")
@@ -600,10 +562,7 @@ class TestOnlineAccount:
wait_securejoin_inviter_progress(ac1, 1000) wait_securejoin_inviter_progress(ac1, 1000)
def test_set_get_profile_image(self, acfactory, data, lp): def test_set_get_profile_image(self, acfactory, data, lp):
ac1 = acfactory.get_online_configuring_account() ac1, ac2 = acfactory.get_two_online_accounts()
ac2 = acfactory.get_online_configuring_account()
wait_configuration_progress(ac2, 1000)
wait_configuration_progress(ac1, 1000)
lp.sec("create unpromoted group chat") lp.sec("create unpromoted group chat")
chat = ac1.create_group_chat("hello") chat = ac1.create_group_chat("hello")