Extract fetching list ID

Also don't check if account is found, that function alredy raises a
ConsoleError.
pull/351/head
Ivan Habunek 2023-03-30 12:31:04 +02:00
rodzic c659ed7a5d
commit e3394c1693
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: CDBD63C43A30BB95
2 zmienionych plików z 29 dodań i 25 usunięć

Wyświetl plik

@ -1,6 +1,7 @@
from toot import api import pytest
from tests.integration.conftest import register_account from tests.integration.conftest import register_account
from toot.exceptions import ConsoleError
def test_lists_empty(run): def test_lists_empty(run):
@ -35,6 +36,10 @@ def test_list_create_delete(run):
out = run("lists") out = run("lists")
assert out == "You have no lists defined." assert out == "You have no lists defined."
with pytest.raises(ConsoleError) as exc:
run("list_delete", "mango")
assert str(exc.value) == "List not found"
def test_list_add_remove(run, app): def test_list_add_remove(run, app):
acc = register_account(app) acc = register_account(app)
@ -51,6 +56,16 @@ def test_list_add_remove(run, app):
out = run("list_accounts", "foo") out = run("list_accounts", "foo")
assert acc.username in out assert acc.username in out
# Account doesn't exist
with pytest.raises(ConsoleError) as exc:
run("list_add", "foo", "does_not_exist")
assert str(exc.value) == "Account not found"
# List doesn't exist
with pytest.raises(ConsoleError) as exc:
run("list_add", "does_not_exist", acc.username)
assert str(exc.value) == "List not found"
out = run("list_remove", "foo", acc.username) out = run("list_remove", "foo", acc.username)
assert out == f'✓ Removed account "{acc.username}"' assert out == f'✓ Removed account "{acc.username}"'

Wyświetl plik

@ -434,11 +434,7 @@ def lists(app, user, args):
def list_accounts(app, user, args): def list_accounts(app, user, args):
list_id = args.id if args.id else api.find_list_id(app, user, args.title) list_id = _get_list_id(app, user, args)
if not list_id:
print_out("<red>List not found</red>")
return
response = api.get_list_accounts(app, user, list_id) response = api.get_list_accounts(app, user, list_id)
print_list_accounts(response) print_list_accounts(response)
@ -449,24 +445,15 @@ def list_create(app, user, args):
def list_delete(app, user, args): def list_delete(app, user, args):
list_id = args.id if args.id else api.find_list_id(app, user, args.title) list_id = _get_list_id(app, user, args)
if not list_id:
print_out("<red>List not found</red>")
return
api.delete_list(app, user, list_id) api.delete_list(app, user, list_id)
print_out(f"<green>✓ List \"{args.title if args.title else args.id}\"</green> <red>deleted.</red>") print_out(f"<green>✓ List \"{args.title if args.title else args.id}\"</green> <red>deleted.</red>")
def list_add(app, user, args): def list_add(app, user, args):
list_id = args.id if args.id else api.find_list_id(app, user, args.title) list_id = _get_list_id(app, user, args)
if not list_id:
print_out("<red>List not found</red>")
return
account = find_account(app, user, args.account) account = find_account(app, user, args.account)
if not account:
print_out("<red>Account not found</red>")
return
try: try:
api.add_accounts_to_list(app, user, list_id, [account['id']]) api.add_accounts_to_list(app, user, list_id, [account['id']])
except Exception as ex: except Exception as ex:
@ -484,22 +471,24 @@ def list_add(app, user, args):
else: else:
print_out(f"<red>{ex}</red>") print_out(f"<red>{ex}</red>")
return return
print_out(f"<green>✓ Added account \"{args.account}\"</green>") print_out(f"<green>✓ Added account \"{args.account}\"</green>")
def list_remove(app, user, args): def list_remove(app, user, args):
list_id = args.id if args.id else api.find_list_id(app, user, args.title) list_id = _get_list_id(app, user, args)
if not list_id:
print_out("<red>List not found</red>")
return
account = find_account(app, user, args.account) account = find_account(app, user, args.account)
if not account:
print_out("<red>Account not found</red>")
return
api.remove_accounts_from_list(app, user, list_id, [account['id']]) api.remove_accounts_from_list(app, user, list_id, [account['id']])
print_out(f"<green>✓ Removed account \"{args.account}\"</green>") print_out(f"<green>✓ Removed account \"{args.account}\"</green>")
def _get_list_id(app, user, args):
list_id = args.id or api.find_list_id(app, user, args.title)
if not list_id:
raise ConsoleError("List not found")
return list_id
def mute(app, user, args): def mute(app, user, args):
account = find_account(app, user, args.account) account = find_account(app, user, args.account)
api.mute(app, user, account['id']) api.mute(app, user, account['id'])