diff --git a/tests/test_console.py b/tests/test_console.py index c9c60eb..0a3b3fa 100644 --- a/tests/test_console.py +++ b/tests/test_console.py @@ -179,16 +179,14 @@ def test_search(monkeypatch, capsys): def test_follow(monkeypatch, capsys): def mock_get(url, params, headers): - assert url == 'https://habunek.com/api/v1/search' - assert params == {'q': 'blixa', 'resolve': False} + assert url == 'https://habunek.com/api/v1/accounts/search' + assert params == {'q': 'blixa'} assert headers == {'Authorization': 'Bearer xxx'} - return MockResponse({ - 'accounts': [ - {'id': 123, 'acct': 'blixa@other.acc'}, - {'id': 321, 'acct': 'blixa'}, - ] - }) + return MockResponse([ + {'id': 123, 'acct': 'blixa@other.acc'}, + {'id': 321, 'acct': 'blixa'}, + ]) def mock_prepare(request): assert request.url == 'https://habunek.com/api/v1/accounts/321/follow' @@ -208,13 +206,11 @@ def test_follow(monkeypatch, capsys): def test_follow_not_found(monkeypatch, capsys): def mock_get(url, params, headers): - assert url == 'https://habunek.com/api/v1/search' - assert params == {'q': 'blixa', 'resolve': False} + assert url == 'https://habunek.com/api/v1/accounts/search' + assert params == {'q': 'blixa'} assert headers == {'Authorization': 'Bearer xxx'} - return MockResponse({ - 'accounts': [] - }) + return MockResponse([]) monkeypatch.setattr(requests, 'get', mock_get) @@ -225,16 +221,14 @@ def test_follow_not_found(monkeypatch, capsys): def test_unfollow(monkeypatch, capsys): def mock_get(url, params, headers): - assert url == 'https://habunek.com/api/v1/search' - assert params == {'q': 'blixa', 'resolve': False} + assert url == 'https://habunek.com/api/v1/accounts/search' + assert params == {'q': 'blixa'} assert headers == {'Authorization': 'Bearer xxx'} - return MockResponse({ - 'accounts': [ - {'id': 123, 'acct': 'blixa@other.acc'}, - {'id': 321, 'acct': 'blixa'}, - ] - }) + return MockResponse([ + {'id': 123, 'acct': 'blixa@other.acc'}, + {'id': 321, 'acct': 'blixa'}, + ]) def mock_prepare(request): assert request.url == 'https://habunek.com/api/v1/accounts/321/unfollow' @@ -254,13 +248,11 @@ def test_unfollow(monkeypatch, capsys): def test_unfollow_not_found(monkeypatch, capsys): def mock_get(url, params, headers): - assert url == 'https://habunek.com/api/v1/search' - assert params == {'q': 'blixa', 'resolve': False} + assert url == 'https://habunek.com/api/v1/accounts/search' + assert params == {'q': 'blixa'} assert headers == {'Authorization': 'Bearer xxx'} - return MockResponse({ - 'accounts': [] - }) + return MockResponse([]) monkeypatch.setattr(requests, 'get', mock_get) diff --git a/toot/commands.py b/toot/commands.py index 8c86fdb..8db7d14 100644 --- a/toot/commands.py +++ b/toot/commands.py @@ -266,11 +266,20 @@ def _do_upload(app, user, file): def _find_account(app, user, account_name): - """For a given account name, returns the Account object or raises an exception if not found.""" - response = api.search(app, user, account_name, False) + """For a given account name, returns the Account object. - for account in response['accounts']: - if account['acct'] == account_name or "@" + account['acct'] == account_name: + Raises an exception if not found. + """ + if not account_name: + raise ConsoleError("Empty account name given") + + accounts = api.search_accounts(app, user, account_name) + + if account_name[0] == "@": + account_name = account_name[1:] + + for account in accounts: + if account['acct'] == account_name: return account raise ConsoleError("Account not found")