File: test_create_app.py

package info (click to toggle)
python-mastodon 2.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,836 kB
  • sloc: python: 9,438; makefile: 206; sql: 98; sh: 27
file content (124 lines) | stat: -rw-r--r-- 4,250 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from mastodon import Mastodon, MastodonNetworkError
import pytest
import requests
from requests import HTTPError
import time

try:
    from mock import Mock
except ImportError:
    from unittest.mock import Mock

def test_create_app(mocker, to_file=None, redirect_uris=None, website=None, user_agent="mastodonpy"):
    # there is no easy way to delete an anonymously created app so
    # instead we mock Requests
    resp = Mock()
    resp.json = Mock(return_value=dict(
            client_id='foo',
            client_secret='bar',
        ))
    mocker.patch('requests.post', return_value=resp)

    app = Mastodon.create_app("Mastodon.py test suite",
            api_base_url="example.com",
            to_file=to_file,
            redirect_uris=redirect_uris,
            website=website,
            user_agent=user_agent
            )

    assert app == ('foo', 'bar')
    assert requests.post.called

def test_create_app_to_file(mocker, tmpdir):
    filepath = tmpdir.join('credentials')
    test_create_app(mocker, to_file=str(filepath))
    assert filepath.read_text('UTF-8') == "foo\nbar\nhttps://example.com\nMastodon.py test suite\n"

def test_create_app_redirect_uris(mocker):
    test_create_app(mocker, redirect_uris='http://example.net')
    kwargs = requests.post.call_args[1]
    assert kwargs['data']['redirect_uris'] == 'http://example.net'

def test_create_app_multiple_redirect_uris(mocker):
    test_create_app(mocker, redirect_uris=['http://example.net', 'https://example.net'])
    kwargs = requests.post.call_args[1]
    assert kwargs['data']['redirect_uris'] == 'http://example.net\nhttps://example.net'

def test_create_app_website(mocker):
    test_create_app(mocker, website='http://example.net')
    kwargs = requests.post.call_args[1]
    assert kwargs['data']['website'] == 'http://example.net'

def test_create_app_session():
    resp = Mock(**{'json.return_value': {'client_id': 'foo', 'client_secret': 'bar'}})
    sess = Mock(**{'post.return_value': resp})

    app = Mastodon.create_app("Mastodon.py test suite", api_base_url="example.com", session=sess)

    assert app == ('foo', 'bar')
    sess.post.assert_called()

def test_create_app_error(mocker):
    def post(_url, **_kwargs):
        raise HTTPError("Unauthorized")

    mocker.patch('requests.post', side_effect=post)

    with pytest.raises(MastodonNetworkError):
        Mastodon.create_app("Mastodon.py test suite", api_base_url="example.com")

def test_create_app_user_agent(mocker):
    test_create_app(mocker, user_agent="pytest")
    kwargs = requests.post.call_args[1]
    assert kwargs['headers']['User-Agent'] == 'pytest'

@pytest.mark.vcr()
def test_app_verify_credentials(api):
    app = api.app_verify_credentials()
    assert app
    assert app.name == 'Mastodon.py test suite'
    
@pytest.mark.vcr(match_on=['path'])
def test_app_account_create():    
    # This leaves behind stuff on the test server, which is unfortunate, but eh.
    suffix = str(time.time()).replace(".", "")[-5:]
    
    test_app = Mastodon.create_app(
        "mastodon.py generated test app", 
        api_base_url="http://localhost:3000/"
    )
    
    test_app_api = Mastodon(
        test_app[0], 
        test_app[1], 
        api_base_url="http://localhost:3000/"
    )
    test_token = test_app_api.create_account(f"coolguy{suffix}", "swordfish", f"email@localhost{suffix}", agreement=True)
    assert test_token

    # We can also test resending (marginally)
    test_app_api.email_resend_confirmation()

@pytest.mark.vcr(match_on=['path'])
def test_app_account_create_invalid():    
    suffix = str(time.time()).replace(".", "")[-5:]
    
    test_app = Mastodon.create_app(
        "mastodon.py generated test app", 
        api_base_url="http://localhost:3000/"
    )
    
    test_app_api = Mastodon(
        test_app[0], 
        test_app[1], 
        api_base_url="http://localhost:3000/"
    )
    test_token, error = test_app_api.create_account("coolguy" + suffix, "", "email@localhost" + suffix, agreement=False, return_detailed_error=True)
    print(error)
    assert test_token is None
    assert "details" in error
    assert "password" in error.details
    assert "password" in error.details
    assert error.details.username is None