Search only accounts when looking for users

Instead of using general search.
This commit is contained in:
Ivan Habunek 2017-05-07 10:42:04 +02:00
parent 02e364b521
commit 6766cf83b4
No known key found for this signature in database
GPG key ID: CDBD63C43A30BB95
2 changed files with 31 additions and 30 deletions

View file

@ -179,16 +179,14 @@ def test_search(monkeypatch, capsys):
def test_follow(monkeypatch, capsys): def test_follow(monkeypatch, capsys):
def mock_get(url, params, headers): def mock_get(url, params, headers):
assert url == 'https://habunek.com/api/v1/search' assert url == 'https://habunek.com/api/v1/accounts/search'
assert params == {'q': 'blixa', 'resolve': False} assert params == {'q': 'blixa'}
assert headers == {'Authorization': 'Bearer xxx'} assert headers == {'Authorization': 'Bearer xxx'}
return MockResponse({ return MockResponse([
'accounts': [
{'id': 123, 'acct': 'blixa@other.acc'}, {'id': 123, 'acct': 'blixa@other.acc'},
{'id': 321, 'acct': 'blixa'}, {'id': 321, 'acct': 'blixa'},
] ])
})
def mock_prepare(request): def mock_prepare(request):
assert request.url == 'https://habunek.com/api/v1/accounts/321/follow' 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 test_follow_not_found(monkeypatch, capsys):
def mock_get(url, params, headers): def mock_get(url, params, headers):
assert url == 'https://habunek.com/api/v1/search' assert url == 'https://habunek.com/api/v1/accounts/search'
assert params == {'q': 'blixa', 'resolve': False} assert params == {'q': 'blixa'}
assert headers == {'Authorization': 'Bearer xxx'} assert headers == {'Authorization': 'Bearer xxx'}
return MockResponse({ return MockResponse([])
'accounts': []
})
monkeypatch.setattr(requests, 'get', mock_get) monkeypatch.setattr(requests, 'get', mock_get)
@ -225,16 +221,14 @@ def test_follow_not_found(monkeypatch, capsys):
def test_unfollow(monkeypatch, capsys): def test_unfollow(monkeypatch, capsys):
def mock_get(url, params, headers): def mock_get(url, params, headers):
assert url == 'https://habunek.com/api/v1/search' assert url == 'https://habunek.com/api/v1/accounts/search'
assert params == {'q': 'blixa', 'resolve': False} assert params == {'q': 'blixa'}
assert headers == {'Authorization': 'Bearer xxx'} assert headers == {'Authorization': 'Bearer xxx'}
return MockResponse({ return MockResponse([
'accounts': [
{'id': 123, 'acct': 'blixa@other.acc'}, {'id': 123, 'acct': 'blixa@other.acc'},
{'id': 321, 'acct': 'blixa'}, {'id': 321, 'acct': 'blixa'},
] ])
})
def mock_prepare(request): def mock_prepare(request):
assert request.url == 'https://habunek.com/api/v1/accounts/321/unfollow' 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 test_unfollow_not_found(monkeypatch, capsys):
def mock_get(url, params, headers): def mock_get(url, params, headers):
assert url == 'https://habunek.com/api/v1/search' assert url == 'https://habunek.com/api/v1/accounts/search'
assert params == {'q': 'blixa', 'resolve': False} assert params == {'q': 'blixa'}
assert headers == {'Authorization': 'Bearer xxx'} assert headers == {'Authorization': 'Bearer xxx'}
return MockResponse({ return MockResponse([])
'accounts': []
})
monkeypatch.setattr(requests, 'get', mock_get) monkeypatch.setattr(requests, 'get', mock_get)

View file

@ -266,11 +266,20 @@ def _do_upload(app, user, file):
def _find_account(app, user, account_name): def _find_account(app, user, account_name):
"""For a given account name, returns the Account object or raises an exception if not found.""" """For a given account name, returns the Account object.
response = api.search(app, user, account_name, False)
for account in response['accounts']: Raises an exception if not found.
if account['acct'] == account_name or "@" + account['acct'] == account_name: """
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 return account
raise ConsoleError("Account not found") raise ConsoleError("Account not found")