Toot-Mastodon-CLI-TUI-clien.../tests/integration/conftest.py

161 lines
4.1 KiB
Python
Raw Normal View History

2023-03-30 10:56:03 +02:00
"""
This module contains integration tests meant to run against a test Mastodon instance.
You can set up a test instance locally by following this guide:
https://docs.joinmastodon.org/dev/setup/
To enable integration tests, export the following environment variables to match
your test server and database:
```
2023-06-28 14:42:44 +02:00
export TOOT_TEST_BASE_URL="localhost:3000"
2023-03-30 10:56:03 +02:00
export TOOT_TEST_DATABASE_DSN="dbname=mastodon_development"
```
"""
2023-11-21 18:16:23 +01:00
import json
2023-03-30 10:56:03 +02:00
import re
import os
import psycopg2
import pytest
import uuid
2023-11-26 18:00:57 +01:00
from click.testing import CliRunner, Result
2023-03-30 10:56:03 +02:00
from pathlib import Path
from toot import api, App, User
2023-11-26 18:00:57 +01:00
from toot.cli import Context
2023-03-30 10:56:03 +02:00
2023-06-28 14:55:28 +02:00
def pytest_configure(config):
import toot.settings
toot.settings.DISABLE_SETTINGS = True
2023-03-30 10:56:03 +02:00
# Mastodon database name, used to confirm user registration without having to click the link
DATABASE_DSN = os.getenv("TOOT_TEST_DATABASE_DSN")
2023-11-30 20:08:59 +01:00
TOOT_TEST_BASE_URL = os.getenv("TOOT_TEST_BASE_URL")
2023-03-30 10:56:03 +02:00
# Toot logo used for testing image upload
TRUMPET = str(Path(__file__).parent.parent.parent / "trumpet.png")
ASSETS_DIR = str(Path(__file__).parent.parent / "assets")
2023-04-07 11:07:38 +02:00
def create_app(base_url):
instance = api.get_instance(base_url).json()
2023-04-07 11:07:38 +02:00
response = api.create_app(base_url)
return App(instance["uri"], base_url, response["client_id"], response["client_secret"])
2023-03-30 10:56:03 +02:00
def register_account(app: App):
username = str(uuid.uuid4())[-10:]
email = f"{username}@example.com"
response = api.register_account(app, username, email, "password", "en")
confirm_user(email)
return User(app.instance, username, response["access_token"])
def confirm_user(email):
conn = psycopg2.connect(DATABASE_DSN)
cursor = conn.cursor()
cursor.execute("UPDATE users SET confirmed_at = now() WHERE email = %s;", (email,))
conn.commit()
2023-04-07 11:07:38 +02:00
# ------------------------------------------------------------------------------
# Fixtures
# ------------------------------------------------------------------------------
# Host name of a test instance to run integration tests against
# DO NOT USE PUBLIC INSTANCES!!!
@pytest.fixture(scope="session")
def base_url():
2023-11-30 20:08:59 +01:00
if not TOOT_TEST_BASE_URL:
2023-04-07 11:07:38 +02:00
pytest.skip("Skipping integration tests, TOOT_TEST_BASE_URL not set")
2023-11-30 20:08:59 +01:00
return TOOT_TEST_BASE_URL
2023-04-07 11:07:38 +02:00
2023-03-30 10:56:03 +02:00
@pytest.fixture(scope="session")
2023-04-07 11:07:38 +02:00
def app(base_url):
return create_app(base_url)
2023-03-30 10:56:03 +02:00
@pytest.fixture(scope="session")
def user(app):
return register_account(app)
@pytest.fixture(scope="session")
def friend(app):
return register_account(app)
2023-11-21 18:16:23 +01:00
@pytest.fixture(scope="session")
def user_id(app, user):
return api.find_account(app, user, user.username)["id"]
@pytest.fixture(scope="session")
def friend_id(app, user, friend):
return api.find_account(app, user, friend.username)["id"]
2023-11-26 18:00:57 +01:00
@pytest.fixture(scope="session", autouse=True)
def testing_env():
os.environ["TOOT_TESTING"] = "true"
@pytest.fixture(scope="session")
def runner():
return CliRunner(mix_stderr=False)
2023-03-30 10:56:03 +02:00
@pytest.fixture
2023-11-26 18:00:57 +01:00
def run(app, user, runner):
2023-11-30 20:08:59 +01:00
def _run(command, *params, as_user=None, input=None) -> Result:
2023-11-26 18:00:57 +01:00
ctx = Context(app, as_user or user)
2023-11-30 20:08:59 +01:00
return runner.invoke(command, params, obj=ctx, input=input)
2023-03-30 10:56:03 +02:00
return _run
2023-11-21 18:16:23 +01:00
@pytest.fixture
2023-11-28 14:05:44 +01:00
def run_json(app, user, runner):
2023-11-21 18:16:23 +01:00
def _run_json(command, *params):
2023-11-28 14:05:44 +01:00
ctx = Context(app, user)
result = runner.invoke(command, params, obj=ctx)
assert result.exit_code == 0
return json.loads(result.stdout)
2023-11-21 18:16:23 +01:00
return _run_json
2023-03-30 10:56:03 +02:00
@pytest.fixture
2023-11-26 18:00:57 +01:00
def run_anon(runner):
def _run(command, *params) -> Result:
ctx = Context(None, None)
return runner.invoke(command, params, obj=ctx)
2023-03-30 10:56:03 +02:00
return _run
# ------------------------------------------------------------------------------
# Utils
# ------------------------------------------------------------------------------
strip_ansi_pattern = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
def strip_ansi(string):
return strip_ansi_pattern.sub("", string).strip()
def posted_status_id(out):
pattern = re.compile(r"Toot posted: http://([^/]+)/([^/]+)/(.+)")
match = re.search(pattern, out)
assert match
_, _, status_id = match.groups()
return status_id