def test_login(monkeypatch): app = App('bigfish.software', 'https://bigfish.software', 'foo', 'bar') data = { 'grant_type': 'password', 'client_id': app.client_id, 'client_secret': app.client_secret, 'username': '******', 'password': '******', 'scope': SCOPES, } request = Request('POST', 'https://bigfish.software/oauth/token', data=data) response = MockResponse({ 'token_type': 'bearer', 'scope': 'read write follow', 'access_token': 'xxx', 'created_at': 1492523699 }) e = Expectations() e.add(request, response) e.patch(monkeypatch) login(app, 'user', 'pass')
def test_login(monkeypatch): app = App('bigfish.software', 'https://bigfish.software', 'foo', 'bar') 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 assert url == 'https://bigfish.software/oauth/token' assert data == { 'grant_type': 'password', 'client_id': app.client_id, 'client_secret': app.client_secret, 'username': '******', 'password': '******', 'scope': SCOPES, } return MockResponse(response) monkeypatch.setattr(requests, 'post', mock_post) assert login(app, 'user', 'pass') == response
def test_save_app(sample_config): app = App('xxx.yyy', 2, 3, 4) app2 = App('moo.foo', 5, 6, 7) app_count = len(sample_config['apps']) assert 'xxx.yyy' not in sample_config['apps'] assert 'moo.foo' not in sample_config['apps'] # Sets config.save_app.__wrapped__(sample_config, app) assert len(sample_config['apps']) == app_count + 1 assert 'xxx.yyy' in sample_config['apps'] assert sample_config['apps']['xxx.yyy']['instance'] == 'xxx.yyy' assert sample_config['apps']['xxx.yyy']['base_url'] == 2 assert sample_config['apps']['xxx.yyy']['client_id'] == 3 assert sample_config['apps']['xxx.yyy']['client_secret'] == 4 # Overwrites config.save_app.__wrapped__(sample_config, app2) assert len(sample_config['apps']) == app_count + 2 assert 'xxx.yyy' in sample_config['apps'] assert 'moo.foo' in sample_config['apps'] assert sample_config['apps']['xxx.yyy']['instance'] == 'xxx.yyy' assert sample_config['apps']['xxx.yyy']['base_url'] == 2 assert sample_config['apps']['xxx.yyy']['client_id'] == 3 assert sample_config['apps']['xxx.yyy']['client_secret'] == 4 assert sample_config['apps']['moo.foo']['instance'] == 'moo.foo' assert sample_config['apps']['moo.foo']['base_url'] == 5 assert sample_config['apps']['moo.foo']['client_id'] == 6 assert sample_config['apps']['moo.foo']['client_secret'] == 7 # Idempotent config.save_app.__wrapped__(sample_config, app2) assert len(sample_config['apps']) == app_count + 2 assert 'xxx.yyy' in sample_config['apps'] assert 'moo.foo' in sample_config['apps'] assert sample_config['apps']['xxx.yyy']['instance'] == 'xxx.yyy' assert sample_config['apps']['xxx.yyy']['base_url'] == 2 assert sample_config['apps']['xxx.yyy']['client_id'] == 3 assert sample_config['apps']['xxx.yyy']['client_secret'] == 4 assert sample_config['apps']['moo.foo']['instance'] == 'moo.foo' assert sample_config['apps']['moo.foo']['base_url'] == 5 assert sample_config['apps']['moo.foo']['client_id'] == 6 assert sample_config['apps']['moo.foo']['client_secret'] == 7
def extract_user_app(config, user_id): if user_id not in config['users']: return None, None user_data = config['users'][user_id] instance = user_data['instance'] if instance not in config['apps']: return None, None app_data = config['apps'][instance] return User(**user_data), App(**app_data)
def test_delete_app(sample_config): app = App('foo.social', 2, 3, 4) app_count = len(sample_config['apps']) assert 'foo.social' in sample_config['apps'] config.delete_app.__wrapped__(sample_config, app) assert 'foo.social' not in sample_config['apps'] assert len(sample_config['apps']) == app_count - 1 # Idempotent config.delete_app.__wrapped__(sample_config, app) assert 'foo.social' not in sample_config['apps'] assert len(sample_config['apps']) == app_count - 1
def create_app(base_url): url = base_url + '/api/v1/apps' response = requests.post(url, { 'client_name': CLIENT_NAME, 'redirect_uris': 'urn:ietf:wg:oauth:2.0:oob', 'scopes': SCOPES, 'website': CLIENT_WEBSITE, }) response.raise_for_status() data = response.json() client_id = data.get('client_id') client_secret = data.get('client_secret') return App(base_url, client_id, client_secret)
def test_create_user(monkeypatch): app = App(4, 5, 6, 7) def assert_user(user, activate=True): assert activate assert isinstance(user, User) assert user.instance == app.instance assert user.username == "foo" assert user.access_token == "abc" monkeypatch.setattr(config, 'save_user', assert_user) monkeypatch.setattr(api, 'verify_credentials', lambda x, y: {"username": "******"}) user = auth.create_user(app, 'abc') assert_user(user)
def register_app(instance): print("Registering application with %s" % green(instance)) try: response = api.create_app(instance) except: raise ConsoleError( "Registration failed. Did you enter a valid instance?") base_url = 'https://' + instance app = App(instance, base_url, response['client_id'], response['client_secret']) path = config.save_app(app) print("Application tokens saved to: {}\n".format(green(path))) return app
def test_login_failed(mock_post): app = App('bigfish.software', 'https://bigfish.software', 'foo', 'bar') data = { 'grant_type': 'password', 'client_id': app.client_id, 'client_secret': app.client_secret, 'username': '******', 'password': '******', 'scope': SCOPES, } mock_post.return_value = MockResponse(is_redirect=True) with pytest.raises(AuthenticationError): login(app, 'user', 'pass') mock_post.assert_called_once_with( 'https://bigfish.software/oauth/token', data, allow_redirects=False)
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': '******', 'password': '******', 'scope': SCOPES, } return MockResponse(is_redirect=True) monkeypatch.setattr(requests, 'post', mock_post) with pytest.raises(AuthenticationError): login(app, 'user', 'pass')
def register_app(domain, scheme='https'): print_out("Looking up instance info...") instance = api.get_instance(domain) print_out( "Found instance <blue>{}</blue> running Mastodon version <yellow>{}</yellow>" .format(instance['title'], instance['version'])) try: print_out("Registering application...") response = api.create_app(domain, scheme) except ApiError: raise ConsoleError("Registration failed.") base_url = scheme + '://' + domain app = App(domain, base_url, response['client_id'], response['client_secret']) config.save_app(app) print_out("Application tokens saved.") return app
def test_login(mock_post): app = App('bigfish.software', 'https://bigfish.software', 'foo', 'bar') data = { 'grant_type': 'password', 'client_id': app.client_id, 'client_secret': app.client_secret, 'username': '******', 'password': '******', 'scope': SCOPES, } mock_post.return_value = MockResponse({ 'token_type': 'bearer', 'scope': 'read write follow', 'access_token': 'xxx', 'created_at': 1492523699 }) login(app, 'user', 'pass') mock_post.assert_called_once_with( 'https://bigfish.software/oauth/token', data, allow_redirects=False)
def test_login_failed(monkeypatch): app = App('bigfish.software', 'https://bigfish.software', 'foo', 'bar') data = { 'grant_type': 'password', 'client_id': app.client_id, 'client_secret': app.client_secret, 'username': '******', 'password': '******', 'scope': SCOPES, } request = Request('POST', 'https://bigfish.software/oauth/token', data=data) response = MockResponse(is_redirect=True) e = Expectations() e.add(request, response) e.patch(monkeypatch) with pytest.raises(AuthenticationError): login(app, 'user', 'pass')
def test_login(monkeypatch): app = App('https://bigfish.software', 'foo', 'bar') def mock_post(url, data): assert url == 'https://bigfish.software/oauth/token' assert data == { 'grant_type': 'password', 'client_id': app.client_id, 'client_secret': app.client_secret, 'username': '******', 'password': '******', 'scope': SCOPES, } return MockResponse({ 'access_token': 'xxx', }) monkeypatch.setattr(requests, 'post', mock_post) user = login(app, 'user', 'pass') assert isinstance(user, User) assert user.username == 'user' assert user.access_token == 'xxx'
def load_app(instance): config = load_config() if instance in config['apps']: return App(**config['apps'][instance])
# -*- coding: utf-8 -*- import io import pytest import re from collections import namedtuple from unittest import mock from toot import console, User, App, http from toot.exceptions import ConsoleError from tests.utils import MockResponse app = App('habunek.com', 'https://habunek.com', 'foo', 'bar') user = User('habunek.com', '*****@*****.**', 'xxx') MockUuid = namedtuple("MockUuid", ["hex"]) def uncolorize(text): """Remove ANSI color sequences from a string""" return re.sub(r'\x1b[^m]*m', '', text) def test_print_usage(capsys): console.print_usage() out, err = capsys.readouterr() assert "toot - a Mastodon CLI client" in out @mock.patch('uuid.uuid4')