2017-04-15 14:53:08 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-04-18 16:16:24 +02:00
|
|
|
import pytest
|
2017-04-15 14:46:22 +02:00
|
|
|
|
2017-12-30 16:30:35 +01:00
|
|
|
from requests import Request
|
|
|
|
|
2017-04-18 16:16:24 +02:00
|
|
|
from toot import App, CLIENT_NAME, CLIENT_WEBSITE
|
|
|
|
from toot.api import create_app, login, SCOPES, AuthenticationError
|
2017-12-30 16:30:35 +01:00
|
|
|
from tests.utils import MockResponse, Expectations
|
2017-04-15 14:46:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_create_app(monkeypatch):
|
2017-12-30 18:04:38 +01:00
|
|
|
request = Request('POST', 'https://bigfish.software/api/v1/apps',
|
2017-12-30 16:30:35 +01:00
|
|
|
data={'website': CLIENT_WEBSITE,
|
|
|
|
'client_name': CLIENT_NAME,
|
|
|
|
'scopes': SCOPES,
|
|
|
|
'redirect_uris': 'urn:ietf:wg:oauth:2.0:oob'})
|
2017-04-18 16:16:24 +02:00
|
|
|
|
2017-12-30 16:30:35 +01:00
|
|
|
response = MockResponse({'client_id': 'foo',
|
|
|
|
'client_secret': 'bar'})
|
2017-04-15 14:46:22 +02:00
|
|
|
|
2017-12-30 16:30:35 +01:00
|
|
|
e = Expectations()
|
|
|
|
e.add(request, response)
|
|
|
|
e.patch(monkeypatch)
|
2017-04-15 14:46:22 +02:00
|
|
|
|
2017-12-30 16:30:35 +01:00
|
|
|
create_app('bigfish.software')
|
2017-04-15 14:46:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_login(monkeypatch):
|
2017-04-18 16:16:24 +02:00
|
|
|
app = App('bigfish.software', 'https://bigfish.software', 'foo', 'bar')
|
2017-04-15 14:46:22 +02:00
|
|
|
|
2017-12-30 16:30:35 +01:00
|
|
|
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({
|
2017-04-18 16:16:24 +02:00
|
|
|
'token_type': 'bearer',
|
|
|
|
'scope': 'read write follow',
|
|
|
|
'access_token': 'xxx',
|
|
|
|
'created_at': 1492523699
|
2017-12-30 16:30:35 +01:00
|
|
|
})
|
2017-04-18 16:16:24 +02:00
|
|
|
|
2017-12-30 16:30:35 +01:00
|
|
|
e = Expectations()
|
|
|
|
e.add(request, response)
|
|
|
|
e.patch(monkeypatch)
|
2017-04-18 16:16:24 +02:00
|
|
|
|
2017-12-30 16:30:35 +01:00
|
|
|
login(app, 'user', 'pass')
|
2017-04-18 16:16:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_login_failed(monkeypatch):
|
|
|
|
app = App('bigfish.software', 'https://bigfish.software', 'foo', 'bar')
|
|
|
|
|
2017-12-30 16:30:35 +01:00
|
|
|
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)
|
2017-04-15 14:46:22 +02:00
|
|
|
|
2017-04-18 16:16:24 +02:00
|
|
|
with pytest.raises(AuthenticationError):
|
|
|
|
login(app, 'user', 'pass')
|