2023-10-07 08:09:55 +00:00
|
|
|
import os
|
2023-10-08 08:32:44 +00:00
|
|
|
|
|
|
|
import pickledb
|
2023-10-07 12:22:08 +00:00
|
|
|
import requests
|
2023-10-07 08:09:55 +00:00
|
|
|
|
|
|
|
import deltachat
|
|
|
|
import pytest
|
|
|
|
from _pytest.pytester import LineMatcher
|
|
|
|
|
2024-02-03 16:21:58 +00:00
|
|
|
from team_bot.bot import RelayPlugin
|
2023-10-08 06:19:57 +00:00
|
|
|
|
2023-10-07 08:09:55 +00:00
|
|
|
|
|
|
|
class ClickRunner:
|
|
|
|
def __init__(self, main):
|
|
|
|
from click.testing import CliRunner
|
|
|
|
|
|
|
|
self.runner = CliRunner()
|
|
|
|
self._main = main
|
|
|
|
self._rootargs = []
|
|
|
|
|
|
|
|
def set_basedir(self, account_dir):
|
|
|
|
self._rootargs.insert(0, "--basedir")
|
|
|
|
self._rootargs.insert(1, account_dir)
|
|
|
|
|
|
|
|
def run_ok(self, args, fnl=None, input=None):
|
|
|
|
__tracebackhide__ = True
|
|
|
|
argv = self._rootargs + args
|
|
|
|
# we use our nextbackup helper to cache account creation
|
|
|
|
# unless --no-test-cache is specified
|
|
|
|
res = self.runner.invoke(self._main, argv, catch_exceptions=False, input=input)
|
|
|
|
if res.exit_code != 0:
|
|
|
|
print(res.output)
|
|
|
|
raise Exception("cmd exited with %d: %s" % (res.exit_code, argv))
|
|
|
|
return _perform_match(res.output, fnl)
|
|
|
|
|
|
|
|
def run_fail(self, args, fnl=None, input=None, code=None):
|
|
|
|
__tracebackhide__ = True
|
|
|
|
argv = self._rootargs + args
|
|
|
|
res = self.runner.invoke(self._main, argv, catch_exceptions=False, input=input)
|
|
|
|
if res.exit_code == 0 or (code is not None and res.exit_code != code):
|
|
|
|
print(res.output)
|
|
|
|
raise Exception(
|
|
|
|
"got exit code {!r}, expected {!r}, output: {}".format(
|
|
|
|
res.exit_code,
|
|
|
|
code,
|
|
|
|
res.output,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return _perform_match(res.output, fnl)
|
|
|
|
|
|
|
|
|
|
|
|
def _perform_match(output, fnl):
|
|
|
|
__tracebackhide__ = True
|
|
|
|
if fnl:
|
|
|
|
lm = LineMatcher(output.splitlines())
|
|
|
|
lines = [x.strip() for x in fnl.strip().splitlines()]
|
|
|
|
try:
|
|
|
|
lm.fnmatch_lines(lines)
|
|
|
|
except Exception:
|
|
|
|
print(output)
|
|
|
|
raise
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def cmd():
|
|
|
|
"""invoke a command line subcommand."""
|
2024-02-03 16:21:58 +00:00
|
|
|
from team_bot.cli import team_bot
|
2023-10-07 08:09:55 +00:00
|
|
|
|
2024-02-03 16:21:58 +00:00
|
|
|
return ClickRunner(team_bot)
|
2023-10-07 08:09:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def tmp_file_path(request, tmpdir):
|
|
|
|
if request.param:
|
|
|
|
path = str(tmpdir) + "/" + str(request.param)
|
|
|
|
with open(path, "w+", encoding="utf-8") as f:
|
|
|
|
f.write("test")
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
2023-10-08 06:19:57 +00:00
|
|
|
@pytest.fixture
|
2024-04-29 21:00:54 +00:00
|
|
|
def relaycrew(crew) -> deltachat.Chat:
|
2023-10-08 08:32:44 +00:00
|
|
|
crew.bot.relayplugin = RelayPlugin(crew.bot, crew.kvstore)
|
2023-10-08 06:19:57 +00:00
|
|
|
crew.bot.add_account_plugin(crew.bot.relayplugin)
|
|
|
|
assert not crew.bot.relayplugin.is_relay_group(crew)
|
|
|
|
yield crew
|
|
|
|
|
|
|
|
|
2023-10-07 12:22:08 +00:00
|
|
|
@pytest.fixture
|
2024-04-29 21:00:54 +00:00
|
|
|
def crew(team_bot, team_user, tmpdir) -> deltachat.Chat:
|
2024-02-03 16:21:58 +00:00
|
|
|
from team_bot.bot import SetupPlugin
|
2023-10-07 12:22:08 +00:00
|
|
|
|
2024-02-03 16:21:58 +00:00
|
|
|
crew = team_bot.create_group_chat(
|
|
|
|
f"Team: {team_bot.get_config('addr')}", verified=True
|
2023-10-07 12:22:08 +00:00
|
|
|
)
|
|
|
|
setupplugin = SetupPlugin(crew.id)
|
2024-02-03 16:21:58 +00:00
|
|
|
team_bot.add_account_plugin(setupplugin)
|
2023-10-07 12:22:08 +00:00
|
|
|
qr = crew.get_join_qr()
|
2024-02-03 16:21:58 +00:00
|
|
|
team_user.qr_join_chat(qr)
|
2023-10-07 12:22:08 +00:00
|
|
|
setupplugin.member_added.wait(timeout=30)
|
2024-02-03 16:21:58 +00:00
|
|
|
crew.user = team_user
|
|
|
|
crew.bot = team_bot
|
2023-10-07 12:22:08 +00:00
|
|
|
crew.bot.setupplugin = setupplugin
|
2023-10-07 15:20:58 +00:00
|
|
|
|
|
|
|
# wait until old user is properly added to crew
|
2024-04-29 21:00:54 +00:00
|
|
|
team_user._evtracker.wait_securejoin_joiner_progress(1000)
|
|
|
|
team_user._evtracker.wait_next_incoming_message() # member added message
|
2023-10-07 15:20:58 +00:00
|
|
|
|
2023-10-08 08:32:44 +00:00
|
|
|
crew.kvstore = pickledb.load(tmpdir + "pickle.db", True)
|
|
|
|
crew.kvstore.set("crew_id", crew.id)
|
2023-10-07 12:22:08 +00:00
|
|
|
yield crew
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-04-29 21:00:54 +00:00
|
|
|
def team_bot(tmpdir) -> deltachat.Account:
|
2023-10-07 12:22:08 +00:00
|
|
|
ac = account(tmpdir + "/bot.sqlite", show_ffi=True)
|
|
|
|
yield ac
|
|
|
|
ac.shutdown()
|
|
|
|
ac.wait_shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-04-29 21:00:54 +00:00
|
|
|
def team_user(tmpdir) -> deltachat.Account:
|
2023-10-07 12:22:08 +00:00
|
|
|
ac = account(tmpdir + "/user.sqlite")
|
|
|
|
yield ac
|
|
|
|
ac.shutdown()
|
|
|
|
ac.wait_shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-04-29 21:00:54 +00:00
|
|
|
def outsider(tmpdir) -> deltachat.Account:
|
2023-10-07 12:22:08 +00:00
|
|
|
ac = account(tmpdir + "/outsider.sqlite")
|
|
|
|
yield ac
|
|
|
|
ac.shutdown()
|
|
|
|
ac.wait_shutdown()
|
|
|
|
|
|
|
|
|
2024-04-29 21:00:54 +00:00
|
|
|
def account(db_path, show_ffi=False) -> deltachat.Account:
|
2023-12-08 12:14:50 +00:00
|
|
|
token = os.environ.get(
|
|
|
|
"DCC_NEW_TMP_EMAIL", "https://nine.testrun.org/cgi-bin/newemail.py"
|
|
|
|
)
|
2023-10-07 12:22:08 +00:00
|
|
|
print(token)
|
|
|
|
ac = deltachat.Account(str(db_path))
|
2024-04-29 21:00:54 +00:00
|
|
|
ac._evtracker = ac.add_account_plugin(deltachat.events.FFIEventTracker(ac))
|
2023-10-07 12:22:08 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2023-10-07 08:09:55 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def chat(tmpdir):
|
|
|
|
token = os.getenv("DCC_NEW_TMP")
|
|
|
|
ac = deltachat.Account(str(tmpdir) + "/db.sqlite")
|
|
|
|
# create bot account from token
|
|
|
|
# create chat partner from token
|
|
|
|
# initiate a chat between them
|
2023-10-07 08:12:30 +00:00
|
|
|
# return the chat object
|
|
|
|
print(token, str(ac.get_config("addr")))
|