1
0
mirror of https://github.com/ihabunek/toot.git synced 2024-11-03 04:17:21 -05:00
toot/tests/integration/test_read.py

237 lines
6.8 KiB
Python
Raw Normal View History

2023-11-21 04:09:04 -05:00
import json
import re
2023-03-30 04:56:03 -04:00
2023-11-30 14:08:59 -05:00
from tests.integration.conftest import TOOT_TEST_BASE_URL
2023-11-26 12:00:57 -05:00
from toot import api, cli
from toot.entities import Account, Status, from_dict, from_dict_list
2023-11-21 04:09:04 -05:00
from uuid import uuid4
2023-03-30 04:56:03 -04:00
2023-11-30 14:08:59 -05:00
def test_instance_default(app, run):
2023-11-26 12:00:57 -05:00
result = run(cli.instance)
assert result.exit_code == 0
assert "Mastodon" in result.stdout
assert app.instance in result.stdout
assert "running Mastodon" in result.stdout
2023-03-30 04:56:03 -04:00
2023-11-30 14:08:59 -05:00
def test_instance_with_url(app, run):
result = run(cli.instance, TOOT_TEST_BASE_URL)
assert result.exit_code == 0
assert "Mastodon" in result.stdout
assert app.instance in result.stdout
assert "running Mastodon" in result.stdout
2023-11-21 04:09:04 -05:00
def test_instance_json(app, run):
2023-11-26 12:00:57 -05:00
result = run(cli.instance, "--json")
assert result.exit_code == 0
data = json.loads(result.stdout)
2023-11-21 04:09:04 -05:00
assert data["title"] is not None
assert data["description"] is not None
assert data["version"] is not None
2023-04-07 05:07:38 -04:00
def test_instance_anon(app, run_anon, base_url):
2023-11-26 12:00:57 -05:00
result = run_anon(cli.instance, base_url)
assert result.exit_code == 0
assert "Mastodon" in result.stdout
assert app.instance in result.stdout
assert "running Mastodon" in result.stdout
2023-03-30 04:56:03 -04:00
# Need to specify the instance name when running anon
2023-11-26 12:00:57 -05:00
result = run_anon(cli.instance)
assert result.exit_code == 1
assert result.stderr == "Error: Please specify an instance.\n"
2023-03-30 04:56:03 -04:00
def test_whoami(user, run):
2023-11-26 12:00:57 -05:00
result = run(cli.whoami)
assert result.exit_code == 0
assert f"@{user.username}" in result.stdout
def test_whoami_json(user, run):
result = run(cli.whoami, "--json")
assert result.exit_code == 0
data = json.loads(result.stdout)
account = from_dict(Account, data)
assert account.username == user.username
assert account.acct == user.username
2023-03-30 04:56:03 -04:00
def test_whois(app, friend, run):
variants = [
friend.username,
f"@{friend.username}",
f"{friend.username}@{app.instance}",
f"@{friend.username}@{app.instance}",
]
for username in variants:
2023-11-26 12:00:57 -05:00
result = run(cli.whois, username)
assert result.exit_code == 0
assert f"@{friend.username}" in result.stdout
def test_whois_json(app, friend, run):
result = run(cli.whois, friend.username, "--json")
assert result.exit_code == 0
data = json.loads(result.stdout)
account = from_dict(Account, data)
assert account.username == friend.username
assert account.acct == friend.username
2023-03-30 04:56:03 -04:00
def test_search_account(friend, run):
2023-11-26 12:00:57 -05:00
result = run(cli.search, friend.username)
assert result.exit_code == 0
assert result.stdout.strip() == f"Accounts:\n* @{friend.username}"
2023-03-30 04:56:03 -04:00
2023-11-26 12:00:57 -05:00
def test_search_account_json(friend, run):
result = run(cli.search, friend.username, "--json")
assert result.exit_code == 0
2023-03-30 04:56:03 -04:00
2023-11-26 12:00:57 -05:00
data = json.loads(result.stdout)
[account] = from_dict_list(Account, data["accounts"])
2023-11-21 12:16:23 -05:00
assert account.acct == friend.username
2023-03-30 04:56:03 -04:00
def test_search_hashtag(app, user, run):
api.post_status(app, user, "#hashtag_x")
api.post_status(app, user, "#hashtag_y")
api.post_status(app, user, "#hashtag_z")
2023-11-26 12:00:57 -05:00
result = run(cli.search, "#hashtag")
assert result.exit_code == 0
assert result.stdout.strip() == "Hashtags:\n#hashtag_x, #hashtag_y, #hashtag_z"
2023-03-30 04:56:03 -04:00
2023-11-26 12:00:57 -05:00
def test_search_hashtag_json(app, user, run):
2023-11-21 12:16:23 -05:00
api.post_status(app, user, "#hashtag_x")
api.post_status(app, user, "#hashtag_y")
api.post_status(app, user, "#hashtag_z")
2023-11-26 12:00:57 -05:00
result = run(cli.search, "#hashtag", "--json")
assert result.exit_code == 0
data = json.loads(result.stdout)
[h1, h2, h3] = sorted(data["hashtags"], key=lambda h: h["name"])
2023-11-21 12:16:23 -05:00
assert h1["name"] == "hashtag_x"
assert h2["name"] == "hashtag_y"
assert h3["name"] == "hashtag_z"
2023-04-07 05:07:38 -04:00
def test_tags(run, base_url):
2023-11-26 12:00:57 -05:00
result = run(cli.tags_followed)
assert result.exit_code == 0
assert result.stdout.strip() == "You're not following any hashtags."
2023-03-30 04:56:03 -04:00
2023-11-26 12:00:57 -05:00
result = run(cli.tags_follow, "foo")
assert result.exit_code == 0
assert result.stdout.strip() == "✓ You are now following #foo"
2023-03-30 04:56:03 -04:00
2023-11-26 12:00:57 -05:00
result = run(cli.tags_followed)
assert result.exit_code == 0
assert result.stdout.strip() == f"* #foo\t{base_url}/tags/foo"
2023-03-30 04:56:03 -04:00
2023-11-26 12:00:57 -05:00
result = run(cli.tags_follow, "bar")
assert result.exit_code == 0
assert result.stdout.strip() == "✓ You are now following #bar"
2023-03-30 04:56:03 -04:00
2023-11-26 12:00:57 -05:00
result = run(cli.tags_followed)
assert result.exit_code == 0
assert result.stdout.strip() == "\n".join([
2023-04-07 05:07:38 -04:00
f"* #bar\t{base_url}/tags/bar",
f"* #foo\t{base_url}/tags/foo",
2023-03-30 04:56:03 -04:00
])
2023-11-26 12:00:57 -05:00
result = run(cli.tags_unfollow, "foo")
assert result.exit_code == 0
assert result.stdout.strip() == "✓ You are no longer following #foo"
2023-03-30 04:56:03 -04:00
2023-11-26 12:00:57 -05:00
result = run(cli.tags_followed)
assert result.exit_code == 0
assert result.stdout.strip() == f"* #bar\t{base_url}/tags/bar"
def test_status(app, user, run):
uuid = str(uuid4())
2023-11-26 12:00:57 -05:00
status_id = api.post_status(app, user, uuid).json()["id"]
result = run(cli.status, status_id)
assert result.exit_code == 0
2023-11-26 12:00:57 -05:00
out = result.stdout.strip()
assert uuid in out
assert user.username in out
2023-11-26 12:00:57 -05:00
assert status_id in out
2023-11-26 12:00:57 -05:00
def test_status_json(app, user, run):
uuid = str(uuid4())
2023-11-26 12:00:57 -05:00
status_id = api.post_status(app, user, uuid).json()["id"]
result = run(cli.status, status_id, "--json")
assert result.exit_code == 0
status = from_dict(Status, json.loads(result.stdout))
assert status.id == status_id
assert status.account.acct == user.username
assert uuid in status.content
def test_thread(app, user, run):
uuid1 = str(uuid4())
uuid2 = str(uuid4())
uuid3 = str(uuid4())
s1 = api.post_status(app, user, uuid1).json()
s2 = api.post_status(app, user, uuid2, in_reply_to_id=s1["id"]).json()
s3 = api.post_status(app, user, uuid3, in_reply_to_id=s2["id"]).json()
for status in [s1, s2, s3]:
2023-11-26 12:00:57 -05:00
result = run(cli.thread, status["id"])
assert result.exit_code == 0
bits = re.split(r"─+", result.stdout.strip())
bits = [b for b in bits if b]
assert len(bits) == 3
assert s1["id"] in bits[0]
assert s2["id"] in bits[1]
assert s3["id"] in bits[2]
2023-11-26 12:00:57 -05:00
assert uuid1 in bits[0]
assert uuid2 in bits[1]
assert uuid3 in bits[2]
2023-11-28 06:26:08 -05:00
def test_thread_json(app, user, run):
uuid1 = str(uuid4())
uuid2 = str(uuid4())
uuid3 = str(uuid4())
s1 = api.post_status(app, user, uuid1).json()
s2 = api.post_status(app, user, uuid2, in_reply_to_id=s1["id"]).json()
s3 = api.post_status(app, user, uuid3, in_reply_to_id=s2["id"]).json()
result = run(cli.thread, s2["id"], "--json")
assert result.exit_code == 0
result = json.loads(result.stdout)
[ancestor] = [from_dict(Status, s) for s in result["ancestors"]]
[descendent] = [from_dict(Status, s) for s in result["descendants"]]
assert ancestor.id == s1["id"]
assert descendent.id == s3["id"]