mirror of
https://github.com/ihabunek/toot.git
synced 2025-10-21 19:44:16 -04:00
Use http methods instead of requests directly
This commit is contained in:
@@ -2,79 +2,76 @@
|
||||
import pytest
|
||||
import requests
|
||||
|
||||
from requests import Request
|
||||
|
||||
from toot import App, CLIENT_NAME, CLIENT_WEBSITE
|
||||
from toot.api import create_app, login, SCOPES, AuthenticationError
|
||||
from tests.utils import MockResponse
|
||||
from tests.utils import MockResponse, Expectations
|
||||
|
||||
|
||||
def test_create_app(monkeypatch):
|
||||
response = {
|
||||
'client_id': 'foo',
|
||||
'client_secret': 'bar',
|
||||
}
|
||||
request = Request('POST', 'http://bigfish.software/api/v1/apps',
|
||||
data={'website': CLIENT_WEBSITE,
|
||||
'client_name': CLIENT_NAME,
|
||||
'scopes': SCOPES,
|
||||
'redirect_uris': 'urn:ietf:wg:oauth:2.0:oob'})
|
||||
|
||||
def mock_post(url, data):
|
||||
assert url == 'https://bigfish.software/api/v1/apps'
|
||||
assert data == {
|
||||
'website': CLIENT_WEBSITE,
|
||||
'client_name': CLIENT_NAME,
|
||||
'scopes': SCOPES,
|
||||
'redirect_uris': 'urn:ietf:wg:oauth:2.0:oob'
|
||||
}
|
||||
return MockResponse(response)
|
||||
response = MockResponse({'client_id': 'foo',
|
||||
'client_secret': 'bar'})
|
||||
|
||||
monkeypatch.setattr(requests, 'post', mock_post)
|
||||
e = Expectations()
|
||||
e.add(request, response)
|
||||
e.patch(monkeypatch)
|
||||
|
||||
assert create_app('bigfish.software') == response
|
||||
create_app('bigfish.software')
|
||||
|
||||
|
||||
def test_login(monkeypatch):
|
||||
app = App('bigfish.software', 'https://bigfish.software', 'foo', 'bar')
|
||||
|
||||
response = {
|
||||
data = {
|
||||
'grant_type': 'password',
|
||||
'client_id': app.client_id,
|
||||
'client_secret': app.client_secret,
|
||||
'username': 'user',
|
||||
'password': 'pass',
|
||||
'scope': SCOPES,
|
||||
}
|
||||
|
||||
request = Request('POST', 'https://bigfish.software/oauth/token', data=data)
|
||||
|
||||
response = MockResponse({
|
||||
'token_type': 'bearer',
|
||||
'scope': 'read write follow',
|
||||
'access_token': 'xxx',
|
||||
'created_at': 1492523699
|
||||
}
|
||||
})
|
||||
|
||||
def mock_post(url, data, allow_redirects):
|
||||
assert not allow_redirects
|
||||
assert url == 'https://bigfish.software/oauth/token'
|
||||
assert data == {
|
||||
'grant_type': 'password',
|
||||
'client_id': app.client_id,
|
||||
'client_secret': app.client_secret,
|
||||
'username': 'user',
|
||||
'password': 'pass',
|
||||
'scope': SCOPES,
|
||||
}
|
||||
e = Expectations()
|
||||
e.add(request, response)
|
||||
e.patch(monkeypatch)
|
||||
|
||||
return MockResponse(response)
|
||||
|
||||
monkeypatch.setattr(requests, 'post', mock_post)
|
||||
|
||||
assert login(app, 'user', 'pass') == response
|
||||
login(app, 'user', 'pass')
|
||||
|
||||
|
||||
def test_login_failed(monkeypatch):
|
||||
app = App('bigfish.software', 'https://bigfish.software', 'foo', 'bar')
|
||||
|
||||
def mock_post(url, data, allow_redirects):
|
||||
assert not allow_redirects
|
||||
assert url == 'https://bigfish.software/oauth/token'
|
||||
assert data == {
|
||||
'grant_type': 'password',
|
||||
'client_id': app.client_id,
|
||||
'client_secret': app.client_secret,
|
||||
'username': 'user',
|
||||
'password': 'pass',
|
||||
'scope': SCOPES,
|
||||
}
|
||||
data = {
|
||||
'grant_type': 'password',
|
||||
'client_id': app.client_id,
|
||||
'client_secret': app.client_secret,
|
||||
'username': 'user',
|
||||
'password': 'pass',
|
||||
'scope': SCOPES,
|
||||
}
|
||||
|
||||
return MockResponse(is_redirect=True)
|
||||
request = Request('POST', 'https://bigfish.software/oauth/token', data=data)
|
||||
response = MockResponse(is_redirect=True)
|
||||
|
||||
monkeypatch.setattr(requests, 'post', mock_post)
|
||||
e = Expectations()
|
||||
e.add(request, response)
|
||||
e.patch(monkeypatch)
|
||||
|
||||
with pytest.raises(AuthenticationError):
|
||||
login(app, 'user', 'pass')
|
||||
|
||||
Reference in New Issue
Block a user