2023-11-21 10:09:04 +01:00
|
|
|
import json
|
|
|
|
import re
|
2023-03-30 10:56:03 +02:00
|
|
|
|
2023-11-26 18:00:57 +01:00
|
|
|
from toot import api, cli
|
|
|
|
from toot.entities import Account, Status, from_dict, from_dict_list
|
2023-11-21 10:09:04 +01:00
|
|
|
from uuid import uuid4
|
2023-03-30 10:56:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_instance(app, run):
|
2023-11-26 18:00:57 +01: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 10:56:03 +02:00
|
|
|
|
|
|
|
|
2023-11-21 10:09:04 +01:00
|
|
|
def test_instance_json(app, run):
|
2023-11-26 18:00:57 +01:00
|
|
|
result = run(cli.instance, "--json")
|
|
|
|
assert result.exit_code == 0
|
|
|
|
|
|
|
|
data = json.loads(result.stdout)
|
2023-11-21 10:09:04 +01:00
|
|
|
assert data["title"] is not None
|
|
|
|
assert data["description"] is not None
|
|
|
|
assert data["version"] is not None
|
|
|
|
|
|
|
|
|
2023-04-07 11:07:38 +02:00
|
|
|
def test_instance_anon(app, run_anon, base_url):
|
2023-11-26 18:00:57 +01: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 10:56:03 +02:00
|
|
|
|
|
|
|
# Need to specify the instance name when running anon
|
2023-11-26 18:00:57 +01:00
|
|
|
result = run_anon(cli.instance)
|
|
|
|
assert result.exit_code == 1
|
|
|
|
assert result.stderr == "Error: Please specify an instance.\n"
|
2023-03-30 10:56:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_whoami(user, run):
|
2023-11-26 18:00:57 +01: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 10:56:03 +02: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 18:00:57 +01: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 10:56:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_search_account(friend, run):
|
2023-11-26 18:00:57 +01:00
|
|
|
result = run(cli.search, friend.username)
|
|
|
|
assert result.exit_code == 0
|
|
|
|
assert result.stdout.strip() == f"Accounts:\n* @{friend.username}"
|
|
|
|
|
2023-03-30 10:56:03 +02:00
|
|
|
|
2023-11-26 18:00:57 +01:00
|
|
|
def test_search_account_json(friend, run):
|
|
|
|
result = run(cli.search, friend.username, "--json")
|
|
|
|
assert result.exit_code == 0
|
2023-03-30 10:56:03 +02:00
|
|
|
|
2023-11-26 18:00:57 +01:00
|
|
|
data = json.loads(result.stdout)
|
|
|
|
[account] = from_dict_list(Account, data["accounts"])
|
2023-11-21 18:16:23 +01:00
|
|
|
assert account.acct == friend.username
|
|
|
|
|
|
|
|
|
2023-03-30 10:56:03 +02: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 18:00:57 +01: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 10:56:03 +02:00
|
|
|
|
|
|
|
|
2023-11-26 18:00:57 +01:00
|
|
|
def test_search_hashtag_json(app, user, run):
|
2023-11-21 18:16:23 +01: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 18:00:57 +01: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 18:16:23 +01:00
|
|
|
|
|
|
|
assert h1["name"] == "hashtag_x"
|
|
|
|
assert h2["name"] == "hashtag_y"
|
|
|
|
assert h3["name"] == "hashtag_z"
|
|
|
|
|
|
|
|
|
2023-04-07 11:07:38 +02:00
|
|
|
def test_tags(run, base_url):
|
2023-11-26 18:00:57 +01:00
|
|
|
result = run(cli.tags_followed)
|
|
|
|
assert result.exit_code == 0
|
|
|
|
assert result.stdout.strip() == "You're not following any hashtags."
|
2023-03-30 10:56:03 +02:00
|
|
|
|
2023-11-26 18:00:57 +01:00
|
|
|
result = run(cli.tags_follow, "foo")
|
|
|
|
assert result.exit_code == 0
|
|
|
|
assert result.stdout.strip() == "✓ You are now following #foo"
|
2023-03-30 10:56:03 +02:00
|
|
|
|
2023-11-26 18:00:57 +01: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 10:56:03 +02:00
|
|
|
|
2023-11-26 18:00:57 +01:00
|
|
|
result = run(cli.tags_follow, "bar")
|
|
|
|
assert result.exit_code == 0
|
|
|
|
assert result.stdout.strip() == "✓ You are now following #bar"
|
2023-03-30 10:56:03 +02:00
|
|
|
|
2023-11-26 18:00:57 +01:00
|
|
|
result = run(cli.tags_followed)
|
|
|
|
assert result.exit_code == 0
|
|
|
|
assert result.stdout.strip() == "\n".join([
|
2023-04-07 11:07:38 +02:00
|
|
|
f"* #bar\t{base_url}/tags/bar",
|
|
|
|
f"* #foo\t{base_url}/tags/foo",
|
2023-03-30 10:56:03 +02:00
|
|
|
])
|
|
|
|
|
2023-11-26 18:00:57 +01: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 10:56:03 +02:00
|
|
|
|
2023-11-26 18:00:57 +01:00
|
|
|
result = run(cli.tags_followed)
|
|
|
|
assert result.exit_code == 0
|
|
|
|
assert result.stdout.strip() == f"* #bar\t{base_url}/tags/bar"
|
2023-06-28 12:38:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_status(app, user, run):
|
|
|
|
uuid = str(uuid4())
|
2023-11-26 18:00:57 +01:00
|
|
|
status_id = api.post_status(app, user, uuid).json()["id"]
|
|
|
|
|
|
|
|
result = run(cli.status, status_id)
|
|
|
|
assert result.exit_code == 0
|
2023-06-28 12:38:29 +02:00
|
|
|
|
2023-11-26 18:00:57 +01:00
|
|
|
out = result.stdout.strip()
|
2023-06-28 12:38:29 +02:00
|
|
|
assert uuid in out
|
|
|
|
assert user.username in out
|
2023-11-26 18:00:57 +01:00
|
|
|
assert status_id in out
|
2023-06-28 12:38:29 +02:00
|
|
|
|
|
|
|
|
2023-11-26 18:00:57 +01:00
|
|
|
def test_status_json(app, user, run):
|
2023-06-28 12:38:29 +02:00
|
|
|
uuid = str(uuid4())
|
2023-11-26 18:00:57 +01: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()
|
2023-06-28 12:38:29 +02:00
|
|
|
|
|
|
|
for status in [s1, s2, s3]:
|
2023-11-26 18:00:57 +01:00
|
|
|
result = run(cli.thread, status["id"])
|
|
|
|
assert result.exit_code == 0
|
|
|
|
|
|
|
|
bits = re.split(r"─+", result.stdout.strip())
|
2023-06-28 12:38:29 +02:00
|
|
|
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 18:00:57 +01:00
|
|
|
assert uuid1 in bits[0]
|
|
|
|
assert uuid2 in bits[1]
|
|
|
|
assert uuid3 in bits[2]
|