added tests for setup and get_crew_id

This commit is contained in:
missytake 2023-10-07 14:22:08 +02:00
parent 41a8ea60a1
commit 97f661e6db
4 changed files with 105 additions and 4 deletions

View file

@ -45,10 +45,12 @@ commands =
flake8 src tests flake8 src tests
[testenv] [testenv]
passenv = *
deps = deps =
pytest pytest
pytest-xdist
commands = commands =
pytest tests pytest tests -n 4
[flake8] [flake8]
max_line_length = 121 max_line_length = 121

View file

@ -6,15 +6,15 @@ from deltachat import account_hookimpl
class SetupPlugin: class SetupPlugin:
def __init__(self, admingrpid): def __init__(self, crew_id):
self.member_added = Event() self.member_added = Event()
self.admingrpid = admingrpid self.crew_id = crew_id
self.message_sent = Event() self.message_sent = Event()
self.outgoing_messages = 0 self.outgoing_messages = 0
@account_hookimpl @account_hookimpl
def ac_member_added(self, chat: deltachat.Chat, contact, actor, message): def ac_member_added(self, chat: deltachat.Chat, contact, actor, message):
if chat.id == self.admingrpid and chat.num_contacts() == 2: if chat.id == self.crew_id and chat.num_contacts() == 2:
self.member_added.set() self.member_added.set()
@account_hookimpl @account_hookimpl

View file

@ -1,4 +1,5 @@
import os import os
import requests
import deltachat import deltachat
import pytest import pytest
@ -74,6 +75,60 @@ def tmp_file_path(request, tmpdir):
return path return path
@pytest.fixture
def crew(teams_bot, teams_user):
from teams_bot.bot import SetupPlugin
crew = teams_bot.create_group_chat(
f"Team: {teams_bot.get_config('addr')}", verified=True
)
setupplugin = SetupPlugin(crew.id)
teams_bot.add_account_plugin(setupplugin)
qr = crew.get_join_qr()
teams_user.qr_join_chat(qr)
setupplugin.member_added.wait(timeout=30)
crew.user = teams_user
crew.bot = teams_bot
crew.bot.setupplugin = setupplugin
yield crew
@pytest.fixture
def teams_bot(tmpdir):
ac = account(tmpdir + "/bot.sqlite", show_ffi=True)
yield ac
ac.shutdown()
ac.wait_shutdown()
@pytest.fixture
def teams_user(tmpdir):
ac = account(tmpdir + "/user.sqlite")
yield ac
ac.shutdown()
ac.wait_shutdown()
@pytest.fixture
def outsider(tmpdir):
ac = account(tmpdir + "/outsider.sqlite")
yield ac
ac.shutdown()
ac.wait_shutdown()
def account(db_path, show_ffi=False):
token = os.environ.get("DCC_NEW_TMP_EMAIL")
print(token)
ac = deltachat.Account(str(db_path))
credentials = requests.post(token).json()
email = credentials["email"]
password = credentials["password"]
print(db_path, email, password)
ac.run_account(email, password, show_ffi=show_ffi)
return ac
@pytest.fixture @pytest.fixture
def chat(tmpdir): def chat(tmpdir):
token = os.getenv("DCC_NEW_TMP") token = os.getenv("DCC_NEW_TMP")

44
tests/test_bot.py Normal file
View file

@ -0,0 +1,44 @@
from teams_bot.bot import get_crew_id
def test_get_crew_id(crew):
"""Test if crew is properly found in delta chat database."""
assert crew.id == get_crew_id(crew.bot)
def test_disable_old_crew(crew, outsider):
"""Test if crew is properly disabled if someone else creates a new crew on the command line."""
old_crew_id = get_crew_id(crew.bot)
# wait until old user is properly added to crew
last_message = crew.user.wait_next_incoming_message().text
while (
f"Member Me ({crew.user.get_config('addr')}) added by bot" not in last_message
):
print("User received message:", last_message)
last_message = crew.user.wait_next_incoming_message().text
# outsider fires up the command line and creates a new crew
new_crew = crew.bot.create_group_chat(
f"Team: {crew.bot.get_config('addr')}", verified=True
)
assert new_crew.id != old_crew_id
qr = new_crew.get_join_qr()
# prepare setupplugin for waiting on second group join
crew.bot.setupplugin.member_added.clear()
crew.bot.setupplugin.crew_id = new_crew.id
# outsider joins new crew
outsider.qr_join_chat(qr)
crew.bot.setupplugin.member_added.wait(timeout=30)
assert len(new_crew.get_contacts()) == 2
assert new_crew.get_name() == f"Team: {crew.bot.get_config('addr')}"
assert new_crew.is_protected()
assert new_crew.id == get_crew_id(crew.bot, crew.bot.setupplugin)
# old user receives disable warning
crew.user.wait_next_incoming_message()
quit_message = crew.user.wait_next_incoming_message()
assert "There is a new Group for the Team now" in quit_message.text
assert outsider.get_config("addr") in quit_message.text