From e2d89c2d85ece29b0f2f85b889dd49aaed0ee07e Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Mon, 12 Dec 2022 12:37:24 +0100 Subject: [PATCH] Fix matching fully qualified account names fixes #254 --- tests/test_integration.py | 15 +++++++++++---- toot/commands.py | 13 +++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/tests/test_integration.py b/tests/test_integration.py index d2636df..9c51ba2 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -378,11 +378,18 @@ def test_whoami(user, run): assert f"http://{HOSTNAME}/@{user.username}" in out -def test_whois(friend, run): - out = run("whois", friend.username) +def test_whois(app, friend, run): + variants = [ + friend.username, + f"@{friend.username}", + f"{friend.username}@{app.instance}", + f"@{friend.username}@{app.instance}", + ] - assert f"@{friend.username}" in out - assert f"http://{HOSTNAME}/@{friend.username}" in out + for username in variants: + out = run("whois", username) + assert f"@{friend.username}" in out + assert f"http://{HOSTNAME}/@{friend.username}" in out def test_search_account(friend, run): diff --git a/toot/commands.py b/toot/commands.py index 0340eb9..7319e7e 100644 --- a/toot/commands.py +++ b/toot/commands.py @@ -270,15 +270,20 @@ def _do_upload(app, user, file, description): def _find_account(app, user, account_name): - """For a given account name, returns the Account object. - - Raises an exception if not found. - """ if not account_name: raise ConsoleError("Empty account name given") normalized_name = account_name.lstrip("@").lower() + # Strip @ from accounts on the local instance. The `acct` + # field in account object contains the qualified name for users of other + # instances, but only the username for users of the local instance. This is + # required in order to match the account name below. + if "@" in normalized_name: + [username, instance] = normalized_name.split("@", maxsplit=1) + if instance == app.instance: + normalized_name = username + response = api.search(app, user, account_name, type="accounts", resolve=True) for account in response["accounts"]: if account["acct"].lower() == normalized_name: