From ee687510b0c00c28a6615044a61f0258e199bbac Mon Sep 17 00:00:00 2001 From: codl Date: Mon, 27 Nov 2017 16:30:35 +0100 Subject: add tests for create_app --- tests/test_create_app.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tests/test_create_app.py (limited to 'tests') diff --git a/tests/test_create_app.py b/tests/test_create_app.py new file mode 100644 index 0000000..32f5994 --- /dev/null +++ b/tests/test_create_app.py @@ -0,0 +1,47 @@ +from mastodon import Mastodon +import pytest +import requests +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): + # 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 + ) + + assert app == ('foo', 'bar') + assert requests.post.called + +def test_create_app_to_file(mocker): + import tempfile, os + (fd, filename) = tempfile.mkstemp(text=True) + + test_create_app(mocker, to_file=filename) + with open(fd) as f: + assert f.read() == "foo\nbar\n" + + os.remove(filename) + +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_website(mocker): + test_create_app(mocker, website='http://example.net') + kwargs = requests.post.call_args[1] + assert kwargs['data']['website'] == 'http://example.net' -- cgit v1.2.3