diff --git a/deltachat-rpc-client/pyproject.toml b/deltachat-rpc-client/pyproject.toml index cabcc848a..acdbeb404 100644 --- a/deltachat-rpc-client/pyproject.toml +++ b/deltachat-rpc-client/pyproject.toml @@ -12,3 +12,5 @@ dependencies = [ dynamic = [ "version" ] +[project.entry-points.pytest11] +"deltachat_rpc_client.pytestplugin" = "deltachat_rpc_client.pytestplugin" diff --git a/deltachat-rpc-client/src/deltachat_rpc_client/__init__.py b/deltachat-rpc-client/src/deltachat_rpc_client/__init__.py index 720e1ebdb..11a168a07 100644 --- a/deltachat-rpc-client/src/deltachat_rpc_client/__init__.py +++ b/deltachat-rpc-client/src/deltachat_rpc_client/__init__.py @@ -2,4 +2,4 @@ from .account import Account from .contact import Contact from .deltachat import Deltachat from .message import Message -from .rpc import Rpc, new_online_account, start_rpc_server +from .rpc import Rpc, start_rpc_server diff --git a/deltachat-rpc-client/src/deltachat_rpc_client/pytestplugin.py b/deltachat-rpc-client/src/deltachat_rpc_client/pytestplugin.py new file mode 100644 index 000000000..bddccb0de --- /dev/null +++ b/deltachat-rpc-client/src/deltachat_rpc_client/pytestplugin.py @@ -0,0 +1,50 @@ +import json +import os +from typing import List + +import aiohttp +import pytest_asyncio + +from .account import Account +from .deltachat import Deltachat +from .rpc import Rpc, start_rpc_server + + +async def get_temp_credentials() -> dict: + url = os.getenv("DCC_NEW_TMP_EMAIL") + assert url, "Failed to get online account, DCC_NEW_TMP_EMAIL is not set" + async with aiohttp.ClientSession() as session: + async with session.post(url) as response: + return json.loads(await response.text()) + + +class ACFactory: + def __init__(self, deltachat: Deltachat) -> None: + self.deltachat = deltachat + + async def new_configured_account(self) -> Account: + credentials = await get_temp_credentials() + account = await self.deltachat.add_account() + assert not await account.is_configured() + await account.set_config("addr", credentials["email"]) + await account.set_config("mail_pw", credentials["password"]) + await account.configure() + assert await account.is_configured() + return account + + async def get_online_accounts(self, num: int) -> List[Account]: + accounts = [await self.new_configured_account() for _ in range(num)] + await self.deltachat.start_io() + return accounts + + +@pytest_asyncio.fixture +async def rpc(tmp_path) -> Rpc: + return await start_rpc_server( + env={**os.environ, "DC_ACCOUNTS_PATH": str(tmp_path / "accounts")} + ) + + +@pytest_asyncio.fixture +async def acfactory(rpc) -> ACFactory: + return ACFactory(Deltachat(rpc)) diff --git a/deltachat-rpc-client/src/deltachat_rpc_client/rpc.py b/deltachat-rpc-client/src/deltachat_rpc_client/rpc.py index 22c87ca4a..abdc33d45 100644 --- a/deltachat-rpc-client/src/deltachat_rpc_client/rpc.py +++ b/deltachat-rpc-client/src/deltachat_rpc_client/rpc.py @@ -1,10 +1,7 @@ import asyncio import json -import os from typing import Any, Dict, Optional -import aiohttp - class JsonRpcError(Exception): pass @@ -80,10 +77,3 @@ async def start_rpc_server(*args, **kwargs) -> Rpc: ) rpc = Rpc(proc) return rpc - - -async def new_online_account() -> dict: - url = os.getenv("DCC_NEW_TMP_EMAIL") - async with aiohttp.ClientSession() as session: - async with session.post(url) as response: - return json.loads(await response.text()) diff --git a/deltachat-rpc-client/tests/test_something.py b/deltachat-rpc-client/tests/test_something.py index 1744f3de9..b6f10af16 100644 --- a/deltachat-rpc-client/tests/test_something.py +++ b/deltachat-rpc-client/tests/test_something.py @@ -1,18 +1,4 @@ -import asyncio -import os - import pytest -import pytest_asyncio - -import deltachat_rpc_client -from deltachat_rpc_client import Deltachat - - -@pytest_asyncio.fixture -async def rpc(tmp_path): - return await deltachat_rpc_client.start_rpc_server( - env={**os.environ, "DC_ACCOUNTS_PATH": str(tmp_path / "accounts")} - ) @pytest.mark.asyncio @@ -37,16 +23,10 @@ async def test_email_address_validity(rpc): @pytest.mark.asyncio -async def test_online_account(rpc): - account_json = await deltachat_rpc_client.new_online_account() - - account_id = await rpc.add_account() - await rpc.set_config(account_id, "addr", account_json["email"]) - await rpc.set_config(account_id, "mail_pw", account_json["password"]) - - await rpc.configure(account_id) +async def test_acfactory(acfactory): + account = await acfactory.new_configured_account() while True: - event = await rpc.wait_for_event(account_id) + event = await account.wait_for_event() if event["type"] == "ConfigureProgress": # Progress 0 indicates error. assert event["progress"] != 0 @@ -60,22 +40,8 @@ async def test_online_account(rpc): @pytest.mark.asyncio -async def test_object_account(rpc): - deltachat = Deltachat(rpc) - - async def create_configured_account(): - account = await deltachat.add_account() - assert not await account.is_configured() - account_json = await deltachat_rpc_client.new_online_account() - await account.set_config("addr", account_json["email"]) - await account.set_config("mail_pw", account_json["password"]) - await account.configure() - assert await account.is_configured() - return account - - alice, bob = await asyncio.gather( - create_configured_account(), create_configured_account() - ) +async def test_object_account(acfactory): + alice, bob = await acfactory.get_online_accounts(2) alice_contact_bob = await alice.create_contact(await bob.get_config("addr"), "Bob") alice_chat_bob = await alice_contact_bob.create_chat() @@ -88,5 +54,7 @@ async def test_object_account(rpc): msg_id = event["msgId"] break + rpc = acfactory.deltachat.rpc message = await rpc.get_message(bob.account_id, msg_id) + assert message["chatId"] == chat_id assert message["text"] == "Hello!"