2017-11-27 20:43:27 +01:00
|
|
|
import pytest
|
|
|
|
from mastodon.Mastodon import MastodonIllegalArgumentError
|
|
|
|
from mastodon import Mastodon
|
2017-11-27 18:44:15 +01:00
|
|
|
try:
|
|
|
|
from urllib.parse import urlparse, parse_qs
|
|
|
|
except ImportError:
|
2017-11-27 20:43:27 +01:00
|
|
|
from urlparse import urlparse, parse_qs
|
|
|
|
|
2017-11-27 18:44:15 +01:00
|
|
|
|
|
|
|
def test_auth_request_url(api):
|
|
|
|
url = api.auth_request_url()
|
|
|
|
parse = urlparse(url)
|
|
|
|
assert parse.path == '/oauth/authorize'
|
|
|
|
query = parse_qs(parse.query)
|
|
|
|
assert query['client_id'][0] == api.client_id
|
|
|
|
assert query['response_type'][0] == 'code'
|
|
|
|
assert query['redirect_uri'][0] == 'urn:ietf:wg:oauth:2.0:oob'
|
2018-06-05 17:47:26 +02:00
|
|
|
assert set(query['scope'][0].split()) == set(('read', 'write', 'follow', 'push'))
|
2017-11-27 18:44:15 +01:00
|
|
|
|
|
|
|
|
2017-11-27 20:59:14 +01:00
|
|
|
def test_log_in_none(api_anonymous):
|
|
|
|
with pytest.raises(MastodonIllegalArgumentError):
|
|
|
|
api_anonymous.log_in()
|
|
|
|
|
|
|
|
|
2017-11-27 20:43:27 +01:00
|
|
|
@pytest.mark.vcr()
|
|
|
|
def test_log_in_password(api_anonymous):
|
|
|
|
token = api_anonymous.log_in(
|
2019-06-22 15:28:27 +02:00
|
|
|
username='admin@localhost',
|
2017-11-27 20:43:27 +01:00
|
|
|
password='mastodonadmin')
|
|
|
|
assert token
|
|
|
|
|
|
|
|
@pytest.mark.vcr()
|
|
|
|
def test_log_in_password_incorrect(api_anonymous):
|
|
|
|
with pytest.raises(MastodonIllegalArgumentError):
|
|
|
|
api_anonymous.log_in(
|
2019-06-22 15:28:27 +02:00
|
|
|
username='admin@localhost',
|
2017-11-27 20:43:27 +01:00
|
|
|
password='hunter2')
|
|
|
|
|
|
|
|
@pytest.mark.vcr()
|
|
|
|
def test_log_in_password_to_file(api_anonymous, tmpdir):
|
|
|
|
filepath = tmpdir.join('token')
|
|
|
|
api_anonymous.log_in(
|
2019-06-22 15:28:27 +02:00
|
|
|
username='admin@localhost',
|
2017-11-27 20:43:27 +01:00
|
|
|
password='mastodonadmin',
|
|
|
|
to_file=str(filepath))
|
2019-10-12 18:58:46 +02:00
|
|
|
token = filepath.read_text('UTF-8').rstrip().split("\n")[0]
|
2017-11-27 20:43:27 +01:00
|
|
|
assert token
|
|
|
|
api = api_anonymous
|
|
|
|
api.access_token = token
|
|
|
|
assert api.account_verify_credentials()
|
|
|
|
|
2019-10-12 18:58:46 +02:00
|
|
|
@pytest.mark.vcr()
|
|
|
|
def test_url_errors(tmpdir):
|
|
|
|
clientid_good = tmpdir.join("clientid")
|
|
|
|
token_good = tmpdir.join("token")
|
|
|
|
clientid_bad = tmpdir.join("clientid_bad")
|
|
|
|
token_bad = tmpdir.join("token_bad")
|
|
|
|
|
|
|
|
clientid_good.write_text("foo\nbar\nhttps://zombo.com\n", "UTF-8")
|
|
|
|
token_good.write_text("foo\nhttps://zombo.com\n", "UTF-8")
|
|
|
|
clientid_bad.write_text("foo\nbar\nhttps://evil.org\n", "UTF-8")
|
|
|
|
token_bad.write_text("foo\nhttps://evil.org\n", "UTF-8")
|
|
|
|
|
|
|
|
api = Mastodon(client_id = clientid_good, access_token = token_good)
|
|
|
|
assert api
|
|
|
|
assert api.api_base_url == "https://zombo.com"
|
|
|
|
assert Mastodon(client_id = clientid_good, access_token = token_good, api_base_url = "zombo.com")
|
|
|
|
|
|
|
|
with pytest.raises(MastodonIllegalArgumentError):
|
|
|
|
Mastodon(client_id = clientid_good, access_token = token_bad, api_base_url = "zombo.com")
|
|
|
|
|
|
|
|
with pytest.raises(MastodonIllegalArgumentError):
|
|
|
|
Mastodon(client_id = clientid_bad, access_token = token_good, api_base_url = "zombo.com")
|
|
|
|
|
|
|
|
with pytest.raises(MastodonIllegalArgumentError):
|
|
|
|
Mastodon(client_id = clientid_bad, access_token = token_bad, api_base_url = "zombo.com")
|
2017-11-27 20:43:27 +01:00
|
|
|
|
|
|
|
@pytest.mark.skip(reason="Not sure how to test this without setting up selenium or a similar browser automation suite to click on the allow button")
|
|
|
|
def test_log_in_code(api_anonymous):
|
|
|
|
pass
|
|
|
|
|
2017-11-27 20:47:14 +01:00
|
|
|
@pytest.mark.skip(reason="Not supported by Mastodon >:@ (yet?)")
|
2017-11-27 20:43:27 +01:00
|
|
|
def test_log_in_refresh(api_anonymous):
|
|
|
|
pass
|