From 97f661e6db2fee227e5840a04915e531316b4670 Mon Sep 17 00:00:00 2001 From: missytake Date: Sat, 7 Oct 2023 14:22:08 +0200 Subject: [PATCH] added tests for setup and get_crew_id --- setup.cfg | 4 +++- src/teams_bot/bot.py | 6 ++--- tests/conftest.py | 55 ++++++++++++++++++++++++++++++++++++++++++++ tests/test_bot.py | 44 +++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 tests/test_bot.py diff --git a/setup.cfg b/setup.cfg index 056f833..8f7d696 100644 --- a/setup.cfg +++ b/setup.cfg @@ -45,10 +45,12 @@ commands = flake8 src tests [testenv] +passenv = * deps = pytest + pytest-xdist commands = - pytest tests + pytest tests -n 4 [flake8] max_line_length = 121 diff --git a/src/teams_bot/bot.py b/src/teams_bot/bot.py index a5c50fd..13e6133 100644 --- a/src/teams_bot/bot.py +++ b/src/teams_bot/bot.py @@ -6,15 +6,15 @@ from deltachat import account_hookimpl class SetupPlugin: - def __init__(self, admingrpid): + def __init__(self, crew_id): self.member_added = Event() - self.admingrpid = admingrpid + self.crew_id = crew_id self.message_sent = Event() self.outgoing_messages = 0 @account_hookimpl 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() @account_hookimpl diff --git a/tests/conftest.py b/tests/conftest.py index af47f5b..e13bab9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,5 @@ import os +import requests import deltachat import pytest @@ -74,6 +75,60 @@ def tmp_file_path(request, tmpdir): 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 def chat(tmpdir): token = os.getenv("DCC_NEW_TMP") diff --git a/tests/test_bot.py b/tests/test_bot.py new file mode 100644 index 0000000..2e90867 --- /dev/null +++ b/tests/test_bot.py @@ -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