2017-04-15 08:53:08 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-04-18 10:16:24 -04:00
|
|
|
import pytest
|
2017-04-15 08:46:22 -04:00
|
|
|
import requests
|
|
|
|
|
2017-04-18 10:16:24 -04:00
|
|
|
from toot import App, CLIENT_NAME, CLIENT_WEBSITE
|
|
|
|
from toot.api import create_app, login, SCOPES, AuthenticationError
|
|
|
|
from tests.utils import MockResponse
|
2017-04-15 08:46:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_create_app(monkeypatch):
|
2017-04-18 10:16:24 -04:00
|
|
|
response = {
|
|
|
|
'client_id': 'foo',
|
|
|
|
'client_secret': 'bar',
|
|
|
|
}
|
|
|
|
|
2017-04-15 08:46:22 -04:00
|
|
|
def mock_post(url, data):
|
|
|
|
assert url == 'https://bigfish.software/api/v1/apps'
|
|
|
|
assert data == {
|
2017-04-16 08:14:33 -04:00
|
|
|
'website': CLIENT_WEBSITE,
|
2017-04-15 08:46:22 -04:00
|
|
|
'client_name': CLIENT_NAME,
|
|
|
|
'scopes': SCOPES,
|
|
|
|
'redirect_uris': 'urn:ietf:wg:oauth:2.0:oob'
|
|
|
|
}
|
2017-04-18 10:16:24 -04:00
|
|
|
return MockResponse(response)
|
2017-04-15 08:46:22 -04:00
|
|
|
|
|
|
|
monkeypatch.setattr(requests, 'post', mock_post)
|
|
|
|
|
2017-04-18 10:16:24 -04:00
|
|
|
assert create_app('bigfish.software') == response
|
2017-04-15 08:46:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_login(monkeypatch):
|
2017-04-18 10:16:24 -04:00
|
|
|
app = App('bigfish.software', 'https://bigfish.software', 'foo', 'bar')
|
2017-04-15 08:46:22 -04:00
|
|
|
|
2017-04-18 10:16:24 -04:00
|
|
|
response = {
|
|
|
|
'token_type': 'bearer',
|
|
|
|
'scope': 'read write follow',
|
|
|
|
'access_token': 'xxx',
|
|
|
|
'created_at': 1492523699
|
|
|
|
}
|
|
|
|
|
|
|
|
def mock_post(url, data, allow_redirects):
|
|
|
|
assert not allow_redirects
|
2017-04-15 08:46:22 -04:00
|
|
|
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,
|
|
|
|
}
|
2017-04-18 10:16:24 -04:00
|
|
|
|
|
|
|
return MockResponse(response)
|
2017-04-15 08:46:22 -04:00
|
|
|
|
|
|
|
monkeypatch.setattr(requests, 'post', mock_post)
|
|
|
|
|
2017-04-18 10:16:24 -04:00
|
|
|
assert login(app, 'user', 'pass') == response
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
return MockResponse(is_redirect=True)
|
|
|
|
|
|
|
|
monkeypatch.setattr(requests, 'post', mock_post)
|
2017-04-15 08:46:22 -04:00
|
|
|
|
2017-04-18 10:16:24 -04:00
|
|
|
with pytest.raises(AuthenticationError):
|
|
|
|
login(app, 'user', 'pass')
|