diff --git a/python/src/deltachat/account.py b/python/src/deltachat/account.py index bec760da2..75e5f414b 100644 --- a/python/src/deltachat/account.py +++ b/python/src/deltachat/account.py @@ -306,10 +306,17 @@ class Account(object): self._imex_completed.wait() def initiate_key_transfer(self): - """initiate Autocrypt key transfer by sending a self-sent message. - + """return setup code after a Autocrypt setup message + has been successfully sent to our own e-mail address ("self-sent message"). + If sending out was unsuccessful, a RuntimeError is raised. """ - return from_dc_charpointer(lib.dc_initiate_key_transfer(self._dc_context)) + self.check_is_configured() + if not self._threads.is_started(): + raise RuntimeError("threads not running, can not send out") + res = lib.dc_initiate_key_transfer(self._dc_context) + if res == ffi.NULL: + raise RuntimeError("could not send out autocrypt setup message") + return from_dc_charpointer(res) def start_threads(self): """ start IMAP/SMTP threads (and configure account if it hasn't happened). diff --git a/python/src/deltachat/message.py b/python/src/deltachat/message.py index 0b4029af6..aa4d49596 100644 --- a/python/src/deltachat/message.py +++ b/python/src/deltachat/message.py @@ -95,7 +95,11 @@ class Message(object): def is_setup_message(self): """ return True if this message is a setup message. """ - return lib.dc_is_setupmessage(self._dc_msg) + return lib.dc_msg_is_setupmessage(self._dc_msg) + + def continue_key_transfer(self, setup_code): + """ extract key and use it as primary key for this account. """ + lib.dc_continue_key_transfer(self._dc_context, self.id, as_dc_charpointer(setup_code)) @props.with_doc def time_sent(self): diff --git a/python/tests/test_account.py b/python/tests/test_account.py index e78b8fe0b..be8735db2 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -252,6 +252,11 @@ class TestOfflineAccount: assert messages[0].text == "msg1" assert os.path.exists(messages[1].filename) + def test_ac_setup_message_fails(self, acfactory): + ac1 = acfactory.get_configured_offline_account() + with pytest.raises(RuntimeError): + ac1.initiate_key_transfer() + class TestOnlineAccount: def test_one_account_init(self, acfactory): @@ -442,14 +447,20 @@ class TestOnlineAccount: assert len(messages) == 1 assert messages[0].text == "msg1" - def test_ac_setup(self, acfactory): + def test_ac_setup_message(self, acfactory): + # note that the receiving account needs to be configured and running + # before ther setup message is send. DC does not read old messages + # as of Jul2019 ac1 = acfactory.get_online_configuring_account() - wait_configuration_progress(ac1, 1000) - setup_code = ac1.initiate_key_transfer() ac2 = acfactory.clone_online_account(ac1) wait_configuration_progress(ac2, 1000) - ac2._timeout = 10 + wait_configuration_progress(ac1, 1000) + assert ac1.get_info()["fingerprint"] != ac2.get_info()["fingerprint"] + setup_code = ac1.initiate_key_transfer() + ac2._evlogger.set_timeout(10) ev = ac2._evlogger.get_matching("DC_EVENT_INCOMING_MSG|DC_EVENT_MSGS_CHANGED") msg = ac2.get_message_by_id(ev[2]) assert msg.is_setup_message() - assert 0, setup_code + print("*************** Incoming ASM File at: ", msg.filename) + msg.continue_key_transfer(setup_code) + assert ac1.get_info()["fingerprint"] == ac2.get_info()["fingerprint"]