def test_timeline_with_re(mock_get, monkeypatch, capsys): mock_get.return_value = MockResponse([{ 'id': '111111111111111111', 'account': { 'display_name': 'Frank Zappa', 'username': '******' }, 'created_at': '2017-04-12T15:53:18.174Z', 'content': "<p>The computer can't tell you the emotional story. It can give you the exact mathematical design, but what's missing is the eyebrows.</p>", 'reblog': None, 'in_reply_to_id': '111111111111111110' }]) console.run_command(app, user, 'timeline', []) mock_get.assert_called_once_with(app, user, '/api/v1/timelines/home') out, err = capsys.readouterr() assert "The computer can't tell you the emotional story." in out assert "but what's missing is the eyebrows." in out assert "Frank Zappa" in out assert "@fz" in out assert "id: 111111111111111111" in out assert "[RE]" in out
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_reblogged_by(mock_get, monkeypatch, capsys): mock_get.return_value = MockResponse([{ 'display_name': 'Terry Bozzio', 'acct': '*****@*****.**', }, { 'display_name': 'Dweezil', 'acct': '*****@*****.**', }]) console.run_command(app, user, 'reblogged_by', ['111111111111111111']) calls = [ mock.call(app, user, '/api/v1/statuses/111111111111111111/reblogged_by'), ] mock_get.assert_has_calls(calls, any_order=False) out, err = capsys.readouterr() # Display order expected = "\n".join([ "Terry Bozzio", " @[email protected]", "Dweezil", " @[email protected]", "", ]) assert out == expected
def test_search(mock_get, capsys): mock_get.return_value = MockResponse({ 'hashtags': ['foo', 'bar', 'baz'], 'accounts': [{ 'acct': 'thequeen', 'display_name': 'Freddy Mercury' }, { 'acct': '*****@*****.**', 'display_name': 'Mercury Freddy' }], 'statuses': [], }) console.run_command(app, user, 'search', ['freddy']) mock_get.assert_called_once_with(app, user, '/api/v1/search', { 'q': 'freddy', 'resolve': False, }) out, err = capsys.readouterr() assert "Hashtags:\n\033[32m#foo\033[0m, \033[32m#bar\033[0m, \033[32m#baz\033[0m" in out assert "Accounts:" in out assert "\033[32m@thequeen\033[0m Freddy Mercury" in out assert "\033[32m@[email protected]\033[0m Mercury Freddy" in out
def test_timeline(mock_get, monkeypatch, capsys): mock_get.return_value = MockResponse([{ 'id': '111111111111111111', 'account': { 'display_name': 'Frank Zappa 🎸', 'acct': 'fz' }, 'created_at': '2017-04-12T15:53:18.174Z', 'content': "<p>The computer can't tell you the emotional story. It can give you the exact mathematical design, but what's missing is the eyebrows.</p>", 'reblog': None, 'in_reply_to_id': None, 'media_attachments': [], }]) console.run_command(app, user, 'timeline', ['--once']) mock_get.assert_called_once_with(app, user, '/api/v1/timelines/home?limit=10', None) out, err = capsys.readouterr() lines = out.split("\n") assert "Frank Zappa 🎸" in lines[1] assert "@fz" in lines[1] assert "2017-04-12 15:53" in lines[1] assert ( "The computer can't tell you the emotional story. It can give you the " "exact mathematical design, but\nwhat's missing is the eyebrows." in out) assert "111111111111111111" in lines[-3] assert err == ""
def test_post_with_options(mock_post, mock_uuid, capsys): mock_uuid.return_value = MockUuid("up-the-irons") args = [ 'Hello world', '--visibility', 'unlisted', '--sensitive', '--spoiler-text', 'Spoiler!', '--reply-to', '123' ] mock_post.return_value = MockResponse( {'url': 'https://habunek.com/@ihabunek/1234567890'}) console.run_command(app, user, 'post', args) mock_post.assert_called_once_with( app, user, '/api/v1/statuses', { 'status': 'Hello world', 'media_ids[]': None, 'visibility': 'unlisted', 'sensitive': "true", 'spoiler_text': "Spoiler!", 'in_reply_to_id': 123, }, headers={"Idempotency-Key": "up-the-irons"}) out, err = capsys.readouterr() assert 'Toot posted' in out assert 'https://habunek.com/@ihabunek/1234567890' in out assert not err
def mock_send(*args): return MockResponse({ 'id': 123, 'url': 'https://bigfish.software/123/456', 'preview_url': 'https://bigfish.software/789/012', 'text_url': 'https://bigfish.software/345/678', 'type': 'image', })
def mock_get(url, params, headers): assert url == 'https://habunek.com/api/v1/search' assert params == {'q': 'blixa', 'resolve': False} assert headers == {'Authorization': 'Bearer xxx'} return MockResponse({ 'accounts': [] })
def mock_post(url, data): assert url == 'https://bigfish.software/api/v1/apps' assert data == { 'website': CLIENT_WEBSITE, 'client_name': CLIENT_NAME, 'scopes': SCOPES, 'redirect_uris': 'urn:ietf:wg:oauth:2.0:oob' } return MockResponse(response)
def test_follow_not_found(mock_get, capsys): mock_get.return_value = MockResponse() with pytest.raises(ConsoleError) as ex: console.run_command(app, user, 'follow', ['blixa']) mock_get.assert_called_once_with(app, user, '/api/v1/accounts/search', {'q': 'blixa'}) assert "Account not found" == str(ex.value)
def test_http_exception_messeges(self): resp = MockResponse(404, 'Not Found', '{"detail": "Not found"}') msglist = [str(ApiException(None, resp)), str(ClientException(None, resp)), '404, Not Found'] self.assertTrue(msglist[1:] == msglist[:-1], u'HTTP exception messages fail to print as expected.')
def mock_send(*args, **kwargs): return MockResponse([{ 'account': { 'display_name': 'Frank Zappa', 'username': '******' }, 'created_at': '2017-04-12T15:53:18.174Z', 'content': "<p>The computer can't tell you the emotional story. It can give you the exact mathematical design, but what's missing is the eyebrows.</p>", 'reblog': None, }])
def test_get(self): with mock.patch.object(self.sm.conn, 'get', return_value=MockResponse(200, 'Success', '{}')) as mock_get: self.sm.get('test_pk') mock_get.assert_called_with( '{}/test_pk?'.format(self.stats_path)) self.sm.get('test_pk', 'methodX') mock_get.assert_called_with( '{}/test_pk?method=methodX'.format(self.stats_path))
def test_unfollow(monkeypatch, capsys): req1 = Request('GET', 'https://habunek.com/api/v1/accounts/search', params={'q': 'blixa'}, headers={'Authorization': 'Bearer xxx'}) res1 = MockResponse([ {'id': 123, 'acct': '*****@*****.**'}, {'id': 321, 'acct': 'blixa'}, ]) req2 = Request('POST', 'https://habunek.com/api/v1/accounts/321/unfollow', headers={'Authorization': 'Bearer xxx'}) res2 = MockResponse() expectations = Expectations([req1, req2], [res1, res2]) expectations.patch(monkeypatch) console.run_command(app, user, 'unfollow', ['blixa']) out, err = capsys.readouterr() assert "You are no longer following blixa" in out
def test_unfollow_not_found(monkeypatch, capsys): req = Request('GET', 'https://habunek.com/api/v1/accounts/search', params={'q': 'blixa'}, headers={'Authorization': 'Bearer xxx'}) res = MockResponse([]) expectations = Expectations([req], [res]) expectations.patch(monkeypatch) with pytest.raises(ConsoleError) as ex: console.run_command(app, user, 'unfollow', ['blixa']) assert "Account not found" == str(ex.value)
def mock_send(*args, **kwargs): return MockResponse({ 'hashtags': ['foo', 'bar', 'baz'], 'accounts': [{ 'acct': 'thequeen', 'display_name': 'Freddy Mercury' }, { 'acct': '*****@*****.**', 'display_name': 'Mercury Freddy' }], 'statuses': [], })
def test_notifications_empty(mock_get, capsys): mock_get.return_value = MockResponse([]) console.run_command(app, user, 'notifications', []) mock_get.assert_called_once_with(app, user, '/api/v1/notifications') out, err = capsys.readouterr() out = uncolorize(out) assert not err assert out == "No notification\n"
def test_follow(mock_get, mock_post, capsys): mock_get.return_value = MockResponse([ { 'id': 123, 'acct': '*****@*****.**' }, { 'id': 321, 'acct': 'blixa' }, ]) mock_post.return_value = MockResponse() console.run_command(app, user, 'follow', ['blixa']) mock_get.assert_called_once_with(app, user, '/api/v1/accounts/search', {'q': 'blixa'}) mock_post.assert_called_once_with(app, user, '/api/v1/accounts/321/follow') out, err = capsys.readouterr() assert "You are now following blixa" in out
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)
def mock_get(url, params, headers=None): assert url == 'https://habunek.com/api/v1/timelines/home' assert headers == {'Authorization': 'Bearer xxx'} assert params is None return MockResponse([{ 'account': { 'display_name': 'Frank Zappa', 'username': '******' }, 'created_at': '2017-04-12T15:53:18.174Z', 'content': "<p>The computer can't tell you the emotional story. It can give you the exact mathematical design, but what's missing is the eyebrows.</p>", 'reblog': None, }])
def test_create_app(mock_post): mock_post.return_value = MockResponse({ 'client_id': 'foo', 'client_secret': 'bar', }) create_app('bigfish.software') mock_post.assert_called_once_with('https://bigfish.software/api/v1/apps', { 'website': CLIENT_WEBSITE, 'client_name': CLIENT_NAME, 'scopes': SCOPES, 'redirect_uris': 'urn:ietf:wg:oauth:2.0:oob', })
def test_create_app(monkeypatch): request = Request('POST', 'https://bigfish.software/api/v1/apps', data={ 'website': CLIENT_WEBSITE, 'client_name': CLIENT_NAME, 'scopes': SCOPES, 'redirect_uris': 'urn:ietf:wg:oauth:2.0:oob' }) response = MockResponse({'client_id': 'foo', 'client_secret': 'bar'}) e = Expectations() e.add(request, response) e.patch(monkeypatch) create_app('bigfish.software')
def mock_get(url, params, headers=None): assert url == 'https://habunek.com/api/v1/search' assert headers == {'Authorization': 'Bearer xxx'} assert params == { 'q': 'freddy', 'resolve': False, } return MockResponse({ 'hashtags': ['foo', 'bar', 'baz'], 'accounts': [{ 'acct': 'thequeen', 'display_name': 'Freddy Mercury' }, { 'acct': '*****@*****.**', 'display_name': 'Mercury Freddy' }], 'statuses': [], })
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_upload(mock_post, capsys): mock_post.return_value = MockResponse({ 'id': 123, 'url': 'https://bigfish.software/123/456', 'preview_url': 'https://bigfish.software/789/012', 'text_url': 'https://bigfish.software/345/678', 'type': 'image', }) console.run_command(app, user, 'upload', [__file__]) mock_post.call_count == 1 args, kwargs = http.post.call_args assert args == (app, user, '/api/v1/media') assert isinstance(kwargs['files']['file'], io.BufferedReader) out, err = capsys.readouterr() assert "Uploading media" in out assert __file__ in out
def test_search(mock_get, capsys): mock_get.return_value = MockResponse({ 'hashtags': [ { 'history': [], 'name': 'foo', 'url': 'https://mastodon.social/tags/foo' }, { 'history': [], 'name': 'bar', 'url': 'https://mastodon.social/tags/bar' }, { 'history': [], 'name': 'baz', 'url': 'https://mastodon.social/tags/baz' }, ], 'accounts': [{ 'acct': 'thequeen', 'display_name': 'Freddy Mercury' }, { 'acct': '*****@*****.**', 'display_name': 'Mercury Freddy' }], 'statuses': [], }) console.run_command(app, user, 'search', ['freddy']) mock_get.assert_called_once_with(app, user, '/api/v2/search', { 'q': 'freddy', 'resolve': False, }) out, err = capsys.readouterr() assert "Hashtags:\n#foo, #bar, #baz" in out assert "Accounts:" in out assert "@thequeen Freddy Mercury" in out assert "@[email protected] Mercury Freddy" in out
def mock_get(url, params, headers=None): assert url == 'https://habunek.com/api/v1/accounts/verify_credentials' assert headers == {'Authorization': 'Bearer xxx'} assert params is None return MockResponse({ 'acct': 'ihabunek', 'avatar': 'https://files.mastodon.social/accounts/avatars/000/046/103/original/6a1304e135cac514.jpg?1491312434', 'avatar_static': 'https://files.mastodon.social/accounts/avatars/000/046/103/original/6a1304e135cac514.jpg?1491312434', 'created_at': '2017-04-04T13:23:09.777Z', 'display_name': 'Ivan Habunek', 'followers_count': 5, 'following_count': 9, 'header': '/headers/original/missing.png', 'header_static': '/headers/original/missing.png', 'id': 46103, 'locked': False, 'note': 'A developer.', 'statuses_count': 19, 'url': 'https://mastodon.social/@ihabunek', 'username': '******' })
def test_whoami(monkeypatch, capsys): req = Request('GET', 'https://habunek.com/api/v1/accounts/verify_credentials', headers={'Authorization': 'Bearer xxx'}) res = MockResponse({ 'acct': 'ihabunek', 'avatar': 'https://files.mastodon.social/accounts/avatars/000/046/103/original/6a1304e135cac514.jpg?1491312434', 'avatar_static': 'https://files.mastodon.social/accounts/avatars/000/046/103/original/6a1304e135cac514.jpg?1491312434', 'created_at': '2017-04-04T13:23:09.777Z', 'display_name': 'Ivan Habunek', 'followers_count': 5, 'following_count': 9, 'header': '/headers/original/missing.png', 'header_static': '/headers/original/missing.png', 'id': 46103, 'locked': False, 'note': 'A developer.', 'statuses_count': 19, 'url': 'https://mastodon.social/@ihabunek', 'username': '******' }) expectations = Expectations([req], [res]) expectations.patch(monkeypatch) console.run_command(app, user, 'whoami', []) out, err = capsys.readouterr() out = uncolorize(out) assert "@ihabunek Ivan Habunek" in out assert "A developer." in out assert "https://mastodon.social/@ihabunek" in out assert "ID: 46103" in out assert "Since: 2017-04-04 @ 13:23:09" in out assert "Followers: 5" in out assert "Following: 9" in out assert "Statuses: 19" in out
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_post_defaults(mock_post, mock_uuid, capsys): mock_uuid.return_value = MockUuid("rock-on") mock_post.return_value = MockResponse( {'url': 'https://habunek.com/@ihabunek/1234567890'}) console.run_command(app, user, 'post', ['Hello world']) mock_post.assert_called_once_with(app, user, '/api/v1/statuses', { 'status': 'Hello world', 'visibility': 'public', 'media_ids[]': None, 'sensitive': "false", 'spoiler_text': None, 'in_reply_to_id': None, }, headers={"Idempotency-Key": "rock-on"}) out, err = capsys.readouterr() assert 'Toot posted' in out assert 'https://habunek.com/@ihabunek/1234567890' in out assert not err