add pytest plugin

This commit is contained in:
adbenitez
2022-12-01 03:09:23 -05:00
parent 09db062958
commit ffbfeab977
5 changed files with 60 additions and 50 deletions

View File

@@ -12,3 +12,5 @@ dependencies = [
dynamic = [ dynamic = [
"version" "version"
] ]
[project.entry-points.pytest11]
"deltachat_rpc_client.pytestplugin" = "deltachat_rpc_client.pytestplugin"

View File

@@ -2,4 +2,4 @@ from .account import Account
from .contact import Contact from .contact import Contact
from .deltachat import Deltachat from .deltachat import Deltachat
from .message import Message from .message import Message
from .rpc import Rpc, new_online_account, start_rpc_server from .rpc import Rpc, start_rpc_server

View File

@@ -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))

View File

@@ -1,10 +1,7 @@
import asyncio import asyncio
import json import json
import os
from typing import Any, Dict, Optional from typing import Any, Dict, Optional
import aiohttp
class JsonRpcError(Exception): class JsonRpcError(Exception):
pass pass
@@ -80,10 +77,3 @@ async def start_rpc_server(*args, **kwargs) -> Rpc:
) )
rpc = Rpc(proc) rpc = Rpc(proc)
return rpc 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())

View File

@@ -1,18 +1,4 @@
import asyncio
import os
import pytest 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 @pytest.mark.asyncio
@@ -37,16 +23,10 @@ async def test_email_address_validity(rpc):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_online_account(rpc): async def test_acfactory(acfactory):
account_json = await deltachat_rpc_client.new_online_account() account = await acfactory.new_configured_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)
while True: while True:
event = await rpc.wait_for_event(account_id) event = await account.wait_for_event()
if event["type"] == "ConfigureProgress": if event["type"] == "ConfigureProgress":
# Progress 0 indicates error. # Progress 0 indicates error.
assert event["progress"] != 0 assert event["progress"] != 0
@@ -60,22 +40,8 @@ async def test_online_account(rpc):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_object_account(rpc): async def test_object_account(acfactory):
deltachat = Deltachat(rpc) alice, bob = await acfactory.get_online_accounts(2)
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()
)
alice_contact_bob = await alice.create_contact(await bob.get_config("addr"), "Bob") alice_contact_bob = await alice.create_contact(await bob.get_config("addr"), "Bob")
alice_chat_bob = await alice_contact_bob.create_chat() alice_chat_bob = await alice_contact_bob.create_chat()
@@ -88,5 +54,7 @@ async def test_object_account(rpc):
msg_id = event["msgId"] msg_id = event["msgId"]
break break
rpc = acfactory.deltachat.rpc
message = await rpc.get_message(bob.account_id, msg_id) message = await rpc.get_message(bob.account_id, msg_id)
assert message["chatId"] == chat_id
assert message["text"] == "Hello!" assert message["text"] == "Hello!"