2017-04-18 10:16:24 -04:00
|
|
|
import pytest
|
2017-04-15 08:46:22 -04:00
|
|
|
|
2018-06-07 04:00:50 -04:00
|
|
|
from unittest import mock
|
2017-12-30 10:30:35 -05:00
|
|
|
|
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
|
2018-06-07 04:00:50 -04:00
|
|
|
from tests.utils import MockResponse
|
2017-04-15 08:46:22 -04:00
|
|
|
|
|
|
|
|
2018-06-07 04:00:50 -04:00
|
|
|
@mock.patch('toot.http.anon_post')
|
|
|
|
def test_create_app(mock_post):
|
|
|
|
mock_post.return_value = MockResponse({
|
|
|
|
'client_id': 'foo',
|
|
|
|
'client_secret': 'bar',
|
|
|
|
})
|
2017-04-15 08:46:22 -04:00
|
|
|
|
2017-12-30 10:30:35 -05:00
|
|
|
create_app('bigfish.software')
|
2017-04-15 08:46:22 -04:00
|
|
|
|
2022-11-22 03:51:09 -05:00
|
|
|
mock_post.assert_called_once_with('https://bigfish.software/api/v1/apps', json={
|
2018-06-07 04:00:50 -04:00
|
|
|
'website': CLIENT_WEBSITE,
|
|
|
|
'client_name': CLIENT_NAME,
|
|
|
|
'scopes': SCOPES,
|
|
|
|
'redirect_uris': 'urn:ietf:wg:oauth:2.0:oob',
|
|
|
|
})
|
|
|
|
|
2017-04-15 08:46:22 -04:00
|
|
|
|
2018-06-07 04:00:50 -04:00
|
|
|
@mock.patch('toot.http.anon_post')
|
|
|
|
def test_login(mock_post):
|
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-12-30 10:30:35 -05:00
|
|
|
data = {
|
|
|
|
'grant_type': 'password',
|
|
|
|
'client_id': app.client_id,
|
|
|
|
'client_secret': app.client_secret,
|
|
|
|
'username': 'user',
|
|
|
|
'password': 'pass',
|
|
|
|
'scope': SCOPES,
|
|
|
|
}
|
|
|
|
|
2018-06-07 04:00:50 -04:00
|
|
|
mock_post.return_value = MockResponse({
|
2017-04-18 10:16:24 -04:00
|
|
|
'token_type': 'bearer',
|
|
|
|
'scope': 'read write follow',
|
|
|
|
'access_token': 'xxx',
|
|
|
|
'created_at': 1492523699
|
2017-12-30 10:30:35 -05:00
|
|
|
})
|
2017-04-18 10:16:24 -04:00
|
|
|
|
2017-12-30 10:30:35 -05:00
|
|
|
login(app, 'user', 'pass')
|
2017-04-18 10:16:24 -04:00
|
|
|
|
2018-06-07 04:00:50 -04:00
|
|
|
mock_post.assert_called_once_with(
|
2022-11-22 03:51:09 -05:00
|
|
|
'https://bigfish.software/oauth/token', data=data, allow_redirects=False)
|
2018-06-07 04:00:50 -04:00
|
|
|
|
2017-04-18 10:16:24 -04:00
|
|
|
|
2018-06-07 04:00:50 -04:00
|
|
|
@mock.patch('toot.http.anon_post')
|
|
|
|
def test_login_failed(mock_post):
|
2017-04-18 10:16:24 -04:00
|
|
|
app = App('bigfish.software', 'https://bigfish.software', 'foo', 'bar')
|
|
|
|
|
2017-12-30 10:30:35 -05:00
|
|
|
data = {
|
|
|
|
'grant_type': 'password',
|
|
|
|
'client_id': app.client_id,
|
|
|
|
'client_secret': app.client_secret,
|
|
|
|
'username': 'user',
|
|
|
|
'password': 'pass',
|
|
|
|
'scope': SCOPES,
|
|
|
|
}
|
|
|
|
|
2018-06-07 04:00:50 -04:00
|
|
|
mock_post.return_value = MockResponse(is_redirect=True)
|
2017-04-15 08:46:22 -04:00
|
|
|
|
2017-04-18 10:16:24 -04:00
|
|
|
with pytest.raises(AuthenticationError):
|
|
|
|
login(app, 'user', 'pass')
|
2018-06-07 04:00:50 -04:00
|
|
|
|
|
|
|
mock_post.assert_called_once_with(
|
2022-11-22 03:51:09 -05:00
|
|
|
'https://bigfish.software/oauth/token', data=data, allow_redirects=False)
|