1
0
mirror of https://github.com/ihabunek/toot.git synced 2024-06-30 06:35:24 +00:00
toot/tests/test_api.py
Ivan Habunek 062bd864eb
Fix tests
2017-12-30 18:04:38 +01:00

77 lines
2.0 KiB
Python

# -*- coding: utf-8 -*-
import pytest
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, Expectations
def test_create_app(monkeypatch):
request = Request('POST', 'https://bigfish.software/api/v1/apps',
data={'website': CLIENT_WEBSITE,
'client_name': CLIENT_NAME,
'scopes': SCOPES,
'redirect_uris': 'urn:ietf:wg:oauth:2.0:oob'})
response = MockResponse({'client_id': 'foo',
'client_secret': 'bar'})
e = Expectations()
e.add(request, response)
e.patch(monkeypatch)
create_app('bigfish.software')
def test_login(monkeypatch):
app = App('bigfish.software', 'https://bigfish.software', 'foo', 'bar')
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
})
e = Expectations()
e.add(request, response)
e.patch(monkeypatch)
login(app, 'user', 'pass')
def test_login_failed(monkeypatch):
app = App('bigfish.software', 'https://bigfish.software', 'foo', 'bar')
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(is_redirect=True)
e = Expectations()
e.add(request, response)
e.patch(monkeypatch)
with pytest.raises(AuthenticationError):
login(app, 'user', 'pass')