Skip to content

Commit 83dc816

Browse files
committed
Online test for message send-receive
1 parent bb950ad commit 83dc816

File tree

6 files changed

+105
-22
lines changed

6 files changed

+105
-22
lines changed

deltachat-rpc-client/src/deltachat_rpc_client/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from .account import Account
2+
from .contact import Contact
23
from .deltachat import Deltachat
4+
from .message import Message
35
from .rpc import Rpc, new_online_account, start_rpc_server
4-
from .contact import Contact
56

67

78
async def main():
Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,43 @@
11
from typing import Optional
22

3+
from .contact import Contact
4+
35

46
class Account:
5-
def __init__(self, rpc, id):
6-
self.id = id
7+
def __init__(self, rpc, account_id):
78
self.rpc = rpc
9+
self.account_id = account_id
810

911
async def remove(self) -> None:
10-
await self.rpc.remove_account(self.id)
12+
await self.rpc.remove_account(self.account_id)
1113

1214
async def start_io(self) -> None:
13-
await self.rpc.start_io(self.id)
15+
await self.rpc.start_io(self.account_id)
1416

1517
async def stop_io(self) -> None:
16-
await self.rpc.stop_io(self.id)
18+
await self.rpc.stop_io(self.account_id)
1719

1820
async def get_info(self):
19-
return await self.rpc.get_info(self.id)
21+
return await self.rpc.get_info(self.account_id)
2022

2123
async def get_file_size(self):
22-
return await self.rpc.get_account_file_size(self.id)
24+
return await self.rpc.get_account_file_size(self.account_id)
2325

2426
async def is_configured(self) -> bool:
25-
return await self.rpc.is_configured(self.id)
27+
return await self.rpc.is_configured(self.account_id)
28+
29+
async def set_config(self, key: str, value: Optional[str]):
30+
await self.rpc.set_config(self.account_id, key, value)
31+
32+
async def get_config(self, key: str) -> Optional[str]:
33+
return await self.rpc.get_config(self.account_id, key)
2634

27-
async def set_config(key: str, value: Optional[str]):
28-
await self.rpc.set_config(self.id, key, value)
35+
async def configure(self):
36+
await self.rpc.configure(self.account_id)
2937

30-
async def get_config(key: str) -> Optional[str]:
31-
return await self.rpc.get_config(self.id, key)
38+
async def create_contact(self, email: str, name: Optional[str]):
39+
return Contact(
40+
self.rpc,
41+
self.account_id,
42+
await self.rpc.create_contact(self.account_id, email, name),
43+
)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from .message import Message
2+
3+
4+
class Chat:
5+
def __init__(self, rpc, account_id, chat_id):
6+
self.rpc = rpc
7+
self.account_id = account_id
8+
self.chat_id = chat_id
9+
10+
async def block(self):
11+
await self.rpc.block_chat(self.account_id, self.chat_id)
12+
13+
async def accept(self):
14+
await self.rpc.accept_chat(self.account_id, self.chat_id)
15+
16+
async def delete(self):
17+
await self.rpc.delete_chat(self.account_id, self.chat_id)
18+
19+
async def get_encryption_info(self):
20+
await self.rpc.get_chat_encryption_info(self.account_id, self.chat_id)
21+
22+
async def send_text(self, text: str):
23+
msg_id = await self.rpc.misc_send_text_message(
24+
self.account_id, self.chat_id, text
25+
)
26+
return Message(self.rpc, self.account_id, msg_id)

deltachat-rpc-client/src/deltachat_rpc_client/contact.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
from .chat import Chat
2+
3+
14
class Contact:
25
"""
36
Contact API.
47
58
Essentially a wrapper for RPC, account ID and a contact ID.
69
"""
10+
711
def __init__(self, rpc, account_id, contact_id):
812
self.rpc = rpc
913
self.account_id = account_id
@@ -25,8 +29,17 @@ async def change_name(self, name: str):
2529
await self.rpc.change_contact_name(self.account_id, self.contact_id, name)
2630

2731
async def get_encryption_info(self) -> str:
28-
return await self.rpc.get_contact_encryption_info(self.account_id, self.contact_id)
32+
return await self.rpc.get_contact_encryption_info(
33+
self.account_id, self.contact_id
34+
)
2935

3036
async def get_dictionary(self):
3137
"""Returns a dictionary with a snapshot of all contact properties."""
3238
return await self.rpc.get_contact(self.account_id, self.contact_id)
39+
40+
async def create_chat(self):
41+
return Chat(
42+
self.rpc,
43+
self.account_id,
44+
await self.rpc.create_chat_by_contact_id(self.account_id, self.contact_id),
45+
)

deltachat-rpc-client/src/deltachat_rpc_client/rpc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ async def reader_loop(self):
2727
if "id" in response:
2828
fut = self.request_events.pop(response["id"])
2929
fut.set_result(response)
30+
elif response["method"] == "event":
31+
# An event notification.
32+
await self.event_queue.put(response["params"])
3033
else:
31-
if response["method"] == "event":
32-
# An event notification.
33-
await self.event_queue.put(response["params"]["event"])
34+
print(response)
3435

3536
async def get_next_event(self):
3637
"""Returns next event."""

deltachat-rpc-client/tests/test_something.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import os
23

34
import pytest
@@ -45,7 +46,7 @@ async def test_online_account(rpc):
4546

4647
await rpc.configure(account_id)
4748
while True:
48-
event = await rpc.get_next_event()
49+
event = (await rpc.get_next_event())["event"]
4950
if event["type"] == "ConfigureProgress":
5051
# Progress 0 indicates error.
5152
assert event["progress"] != 0
@@ -61,7 +62,36 @@ async def test_online_account(rpc):
6162
@pytest.mark.asyncio
6263
async def test_object_account(rpc):
6364
deltachat = Deltachat(rpc)
64-
account = await deltachat.add_account()
65-
assert not await account.is_configured()
66-
info = await account.get_info()
67-
print(info)
65+
66+
async def create_configured_account():
67+
account = await deltachat.add_account()
68+
assert not await account.is_configured()
69+
account_json = await deltachat_rpc_client.new_online_account()
70+
await account.set_config("addr", account_json["email"])
71+
await account.set_config("mail_pw", account_json["password"])
72+
await account.configure()
73+
assert await account.is_configured()
74+
return account
75+
76+
alice_task = asyncio.create_task(create_configured_account())
77+
bob_task = asyncio.create_task(create_configured_account())
78+
79+
alice = await alice_task
80+
bob = await bob_task
81+
82+
alice_contact_bob = await alice.create_contact(await bob.get_config("addr"), "Bob")
83+
alice_chat_bob = await alice_contact_bob.create_chat()
84+
await alice_chat_bob.send_text("Hello!")
85+
86+
while True:
87+
event = await rpc.get_next_event()
88+
if event["contextId"] != bob.account_id:
89+
continue
90+
event = event["event"]
91+
if event["type"] == "IncomingMsg":
92+
chat_id = event["chatId"]
93+
msg_id = event["msgId"]
94+
break
95+
96+
message = await rpc.get_message(bob.account_id, msg_id)
97+
assert message["text"] == "Hello!"

0 commit comments

Comments
 (0)