2018-06-07 10:00:50 +02:00
import io
2017-04-15 14:46:22 +02:00
import pytest
2017-04-16 17:52:54 +02:00
import re
2017-04-15 14:46:22 +02:00
2018-06-13 13:22:52 +02:00
from collections import namedtuple
2018-06-07 10:00:50 +02:00
from unittest import mock
2017-12-30 16:30:35 +01:00
2018-06-07 10:00:50 +02:00
from toot import console , User , App , http
2017-12-30 13:32:52 +01:00
from toot . exceptions import ConsoleError
2017-04-15 14:46:22 +02:00
2018-06-07 10:00:50 +02:00
from tests . utils import MockResponse
2017-04-15 14:46:22 +02:00
2017-04-18 16:16:24 +02:00
app = App ( ' habunek.com ' , ' https://habunek.com ' , ' foo ' , ' bar ' )
user = User ( ' habunek.com ' , ' ivan@habunek.com ' , ' xxx ' )
2017-04-15 14:46:22 +02:00
2018-06-13 13:22:52 +02:00
MockUuid = namedtuple ( " MockUuid " , [ " hex " ] )
2017-04-15 14:46:22 +02:00
2017-04-16 17:52:54 +02:00
def uncolorize ( text ) :
""" Remove ANSI color sequences from a string """
return re . sub ( r ' \ x1b[^m]*m ' , ' ' , text )
2017-04-18 16:16:24 +02:00
def test_print_usage ( capsys ) :
2017-04-16 17:15:05 +02:00
console . print_usage ( )
2017-04-16 14:06:16 +02:00
out , err = capsys . readouterr ( )
2017-04-19 14:47:30 +02:00
assert " toot - a Mastodon CLI client " in out
2017-04-16 14:06:16 +02:00
2018-06-13 13:22:52 +02:00
@mock.patch ( ' uuid.uuid4 ' )
2018-06-07 10:00:50 +02:00
@mock.patch ( ' toot.http.post ' )
2018-06-13 13:22:52 +02:00
def test_post_defaults ( mock_post , mock_uuid , capsys ) :
mock_uuid . return_value = MockUuid ( " rock-on " )
2018-06-07 10:00:50 +02:00
mock_post . return_value = MockResponse ( {
' url ' : ' https://habunek.com/@ihabunek/1234567890 '
} )
2017-04-15 14:46:22 +02:00
2017-04-19 14:47:30 +02:00
console . run_command ( app , user , ' post ' , [ ' Hello world ' ] )
2017-04-16 14:06:16 +02:00
2022-11-22 09:51:09 +01:00
mock_post . assert_called_once_with ( app , user , ' /api/v1/statuses ' , json = {
2018-06-07 10:00:50 +02:00
' status ' : ' Hello world ' ,
' visibility ' : ' public ' ,
2022-11-22 09:51:09 +01:00
' media_ids ' : [ ] ,
2022-12-07 14:59:06 +01:00
' sensitive ' : False ,
2018-06-13 13:22:52 +02:00
} , headers = { " Idempotency-Key " : " rock-on " } )
2017-04-15 14:46:22 +02:00
2018-06-07 10:00:50 +02:00
out , err = capsys . readouterr ( )
assert ' Toot posted ' in out
assert ' https://habunek.com/@ihabunek/1234567890 ' in out
assert not err
2017-04-15 14:46:22 +02:00
2018-06-13 13:22:52 +02:00
@mock.patch ( ' uuid.uuid4 ' )
2018-06-07 10:00:50 +02:00
@mock.patch ( ' toot.http.post ' )
2018-06-13 13:22:52 +02:00
def test_post_with_options ( mock_post , mock_uuid , capsys ) :
mock_uuid . return_value = MockUuid ( " up-the-irons " )
2018-06-13 12:43:31 +02:00
args = [
' Hello world ' ,
' --visibility ' , ' unlisted ' ,
' --sensitive ' ,
' --spoiler-text ' , ' Spoiler! ' ,
2020-09-01 16:09:29 +02:00
' --reply-to ' , ' 123a ' ,
2022-12-11 23:03:08 +01:00
' --language ' , ' hr ' ,
2018-06-13 12:43:31 +02:00
]
2017-04-15 14:46:22 +02:00
2018-06-07 10:00:50 +02:00
mock_post . return_value = MockResponse ( {
' url ' : ' https://habunek.com/@ihabunek/1234567890 '
} )
2017-04-16 14:06:16 +02:00
2017-04-19 14:47:30 +02:00
console . run_command ( app , user , ' post ' , args )
2017-04-16 14:06:16 +02:00
2022-11-22 09:51:09 +01:00
mock_post . assert_called_once_with ( app , user , ' /api/v1/statuses ' , json = {
2018-06-07 10:00:50 +02:00
' status ' : ' Hello world ' ,
2022-11-22 09:51:09 +01:00
' media_ids ' : [ ] ,
2018-06-07 10:00:50 +02:00
' visibility ' : ' unlisted ' ,
2022-12-07 14:59:06 +01:00
' sensitive ' : True ,
2018-06-07 10:04:50 +02:00
' spoiler_text ' : " Spoiler! " ,
2020-09-29 10:07:55 +02:00
' in_reply_to_id ' : ' 123a ' ,
2022-12-11 23:03:08 +01:00
' language ' : ' hr ' ,
2018-06-13 13:22:52 +02:00
} , headers = { " Idempotency-Key " : " up-the-irons " } )
2018-06-07 10:00:50 +02:00
2017-04-16 14:06:16 +02:00
out , err = capsys . readouterr ( )
2018-06-07 10:00:50 +02:00
assert ' Toot posted ' in out
assert ' https://habunek.com/@ihabunek/1234567890 ' in out
assert not err
2017-04-16 14:06:16 +02:00
2018-06-07 10:00:50 +02:00
def test_post_invalid_visibility ( capsys ) :
2017-04-16 14:06:16 +02:00
args = [ ' Hello world ' , ' --visibility ' , ' foo ' ]
with pytest . raises ( SystemExit ) :
2017-04-19 14:47:30 +02:00
console . run_command ( app , user , ' post ' , args )
2017-04-16 14:06:16 +02:00
out , err = capsys . readouterr ( )
assert " invalid visibility value: ' foo ' " in err
2018-06-07 10:00:50 +02:00
def test_post_invalid_media ( capsys ) :
2017-04-16 14:06:16 +02:00
args = [ ' Hello world ' , ' --media ' , ' does_not_exist.jpg ' ]
with pytest . raises ( SystemExit ) :
2017-04-19 14:47:30 +02:00
console . run_command ( app , user , ' post ' , args )
2017-04-15 14:46:22 +02:00
2017-04-16 14:06:16 +02:00
out , err = capsys . readouterr ( )
assert " can ' t open ' does_not_exist.jpg ' " in err
2017-04-15 14:46:22 +02:00
2018-06-14 10:40:16 +02:00
@mock.patch ( ' toot.http.delete ' )
def test_delete ( mock_delete , capsys ) :
console . run_command ( app , user , ' delete ' , [ ' 12321 ' ] )
mock_delete . assert_called_once_with ( app , user , ' /api/v1/statuses/12321 ' )
out , err = capsys . readouterr ( )
assert ' Status deleted ' in out
assert not err
2018-06-07 10:00:50 +02:00
@mock.patch ( ' toot.http.get ' )
def test_timeline ( mock_get , monkeypatch , capsys ) :
mock_get . return_value = MockResponse ( [ {
2019-01-19 18:38:17 +01:00
' id ' : ' 111111111111111111 ' ,
2018-06-07 10:00:50 +02:00
' account ' : {
2019-01-02 13:22:06 +01:00
' display_name ' : ' Frank Zappa 🎸 ' ,
2023-09-24 08:09:06 +02:00
' last_status_at ' : ' 2017-04-12T15:53:18.174Z ' ,
2019-02-15 13:26:22 +01:00
' acct ' : ' fz '
2018-06-07 10:00:50 +02:00
} ,
' created_at ' : ' 2017-04-12T15:53:18.174Z ' ,
2019-01-01 22:55:49 +01:00
' 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> " ,
2018-06-07 10:00:50 +02:00
' reblog ' : None ,
2019-02-14 16:53:58 +01:00
' in_reply_to_id ' : None ,
' media_attachments ' : [ ] ,
2018-06-07 10:00:50 +02:00
} ] )
2017-04-16 14:06:16 +02:00
2019-02-13 14:15:47 +01:00
console . run_command ( app , user , ' timeline ' , [ ' --once ' ] )
2017-04-16 14:06:16 +02:00
2023-02-04 09:01:48 +01:00
mock_get . assert_called_once_with ( app , user , ' /api/v1/timelines/home ' , { ' limit ' : 10 } )
2018-06-07 10:00:50 +02:00
2017-04-16 14:06:16 +02:00
out , err = capsys . readouterr ( )
2019-02-14 16:53:58 +01:00
lines = out . split ( " \n " )
assert " Frank Zappa 🎸 " in lines [ 1 ]
assert " @fz " in lines [ 1 ]
2022-11-21 08:31:09 +01:00
assert " 2017-04-12 15:53 UTC " in lines [ 1 ]
2019-02-14 16:53:58 +01:00
assert (
" The computer can ' t tell you the emotional story. It can give you the "
" exact mathematical design, but \n what ' s missing is the eyebrows. " in out )
assert " 111111111111111111 " in lines [ - 3 ]
2019-01-02 13:17:02 +01:00
assert err == " "
2017-04-16 14:06:16 +02:00
2019-02-13 14:15:47 +01:00
2019-01-19 18:38:17 +01:00
@mock.patch ( ' toot.http.get ' )
def test_timeline_with_re ( mock_get , monkeypatch , capsys ) :
mock_get . return_value = MockResponse ( [ {
' id ' : ' 111111111111111111 ' ,
2019-02-14 16:53:58 +01:00
' created_at ' : ' 2017-04-12T15:53:18.174Z ' ,
2019-01-19 18:38:17 +01:00
' account ' : {
' display_name ' : ' Frank Zappa ' ,
2019-02-15 13:26:22 +01:00
' acct ' : ' fz '
2019-01-19 18:38:17 +01:00
} ,
2019-02-14 16:53:58 +01:00
' reblog ' : {
2023-06-26 16:02:21 +02:00
' created_at ' : ' 2017-04-12T15:53:18.174Z ' ,
2019-02-14 16:53:58 +01:00
' account ' : {
' display_name ' : ' Johnny Cash ' ,
2023-09-24 08:09:06 +02:00
' last_status_at ' : ' 2011-04-12 ' ,
2019-02-15 13:26:22 +01:00
' acct ' : ' jc '
2019-02-14 16:53:58 +01:00
} ,
' 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> " ,
' media_attachments ' : [ ] ,
} ,
' in_reply_to_id ' : ' 111111111111111110 ' ,
' media_attachments ' : [ ] ,
2019-01-19 18:38:17 +01:00
} ] )
2019-02-13 14:15:47 +01:00
console . run_command ( app , user , ' timeline ' , [ ' --once ' ] )
2019-01-19 18:38:17 +01:00
2023-02-04 09:01:48 +01:00
mock_get . assert_called_once_with ( app , user , ' /api/v1/timelines/home ' , { ' limit ' : 10 } )
2019-01-19 18:38:17 +01:00
out , err = capsys . readouterr ( )
2023-03-30 12:09:38 +02:00
lines = uncolorize ( out ) . split ( " \n " )
2019-02-14 16:53:58 +01:00
2023-06-26 16:02:21 +02:00
assert " Johnny Cash " in lines [ 1 ]
assert " @jc " in lines [ 1 ]
2022-11-21 08:31:09 +01:00
assert " 2017-04-12 15:53 UTC " in lines [ 1 ]
2019-02-14 16:53:58 +01:00
assert (
" The computer can ' t tell you the emotional story. It can give you the "
" exact mathematical design, but \n what ' s missing is the eyebrows. " in out )
assert " 111111111111111111 " in lines [ - 3 ]
2023-06-26 16:02:21 +02:00
assert " ↻ @fz boosted " in lines [ - 3 ]
2019-02-14 16:53:58 +01:00
assert err == " "
2019-01-19 18:38:17 +01:00
2018-06-07 10:00:50 +02:00
@mock.patch ( ' toot.http.post ' )
def test_upload ( mock_post , capsys ) :
mock_post . return_value = MockResponse ( {
' id ' : 123 ,
' preview_url ' : ' https://bigfish.software/789/012 ' ,
2022-11-30 08:55:46 +01:00
' url ' : ' https://bigfish.software/345/678 ' ,
2018-06-07 10:00:50 +02:00
' type ' : ' image ' ,
} )
2017-04-16 14:06:16 +02:00
2018-06-07 10:00:50 +02:00
console . run_command ( app , user , ' upload ' , [ __file__ ] )
2017-04-16 14:06:16 +02:00
2023-03-03 08:57:34 +01:00
assert mock_post . call_count == 1
2017-04-15 14:46:22 +02:00
2018-06-07 10:00:50 +02:00
args , kwargs = http . post . call_args
2023-03-03 08:57:34 +01:00
assert args == ( app , user , ' /api/v2/media ' )
2018-06-07 10:00:50 +02:00
assert isinstance ( kwargs [ ' files ' ] [ ' file ' ] , io . BufferedReader )
2017-04-15 14:46:22 +02:00
2017-04-16 14:06:16 +02:00
out , err = capsys . readouterr ( )
assert " Uploading media " in out
assert __file__ in out
2017-04-16 15:07:27 +02:00
2018-06-07 10:00:50 +02:00
@mock.patch ( ' toot.http.get ' )
def test_whoami ( mock_get , capsys ) :
mock_get . return_value = MockResponse ( {
2017-12-30 16:30:35 +01:00
' 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 ' ,
2023-02-02 09:03:12 +01:00
' username ' : ' ihabunek ' ,
' fields ' : [ ]
2017-12-30 16:30:35 +01:00
} )
2017-04-19 14:47:30 +02:00
console . run_command ( app , user , ' whoami ' , [ ] )
2017-04-16 17:52:54 +02:00
2018-06-07 10:00:50 +02:00
mock_get . assert_called_once_with ( app , user , ' /api/v1/accounts/verify_credentials ' )
2017-04-16 17:52:54 +02:00
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
2022-12-12 12:36:36 +01:00
assert " Since: 2017-04-04 " in out
2017-04-16 17:52:54 +02:00
assert " Followers: 5 " in out
assert " Following: 9 " in out
assert " Statuses: 19 " in out
2018-01-02 10:44:32 +01:00
2019-02-17 14:03:11 +01:00
@mock.patch ( ' toot.http.get ' )
def test_notifications ( mock_get , capsys ) :
mock_get . return_value = MockResponse ( [ {
' id ' : ' 1 ' ,
' type ' : ' follow ' ,
' created_at ' : ' 2019-02-16T07:01:20.714Z ' ,
' account ' : {
' display_name ' : ' Frank Zappa ' ,
' acct ' : ' frank@zappa.social ' ,
} ,
} , {
' id ' : ' 2 ' ,
' type ' : ' mention ' ,
' created_at ' : ' 2017-01-12T12:12:12.0Z ' ,
' account ' : {
' display_name ' : ' Dweezil Zappa ' ,
' acct ' : ' dweezil@zappa.social ' ,
} ,
' status ' : {
' id ' : ' 111111111111111111 ' ,
' account ' : {
' display_name ' : ' Dweezil Zappa ' ,
' acct ' : ' dweezil@zappa.social ' ,
} ,
' created_at ' : ' 2017-04-12T15:53:18.174Z ' ,
' content ' : " <p>We still have fans in 2017 @fan123</p> " ,
' reblog ' : None ,
' in_reply_to_id ' : None ,
' media_attachments ' : [ ] ,
} ,
} , {
' id ' : ' 3 ' ,
' type ' : ' reblog ' ,
' created_at ' : ' 1983-11-03T03:03:03.333Z ' ,
' account ' : {
' display_name ' : ' Terry Bozzio ' ,
' acct ' : ' terry@bozzio.social ' ,
} ,
' status ' : {
' id ' : ' 1234 ' ,
' account ' : {
' display_name ' : ' Zappa Fan ' ,
' acct ' : ' fan123@zappa-fans.social '
} ,
' created_at ' : ' 1983-11-04T15:53:18.174Z ' ,
' content ' : " <p>The Black Page, a masterpiece</p> " ,
' reblog ' : None ,
' in_reply_to_id ' : None ,
' media_attachments ' : [ ] ,
} ,
} , {
' id ' : ' 4 ' ,
' type ' : ' favourite ' ,
' created_at ' : ' 1983-12-13T01:02:03.444Z ' ,
' account ' : {
' display_name ' : ' Zappa Old Fan ' ,
' acct ' : ' fan9@zappa-fans.social ' ,
} ,
' status ' : {
' id ' : ' 1234 ' ,
' account ' : {
' display_name ' : ' Zappa Fan ' ,
' acct ' : ' fan123@zappa-fans.social '
} ,
' created_at ' : ' 1983-11-04T15:53:18.174Z ' ,
' content ' : " <p>The Black Page, a masterpiece</p> " ,
' reblog ' : None ,
' in_reply_to_id ' : None ,
' media_attachments ' : [ ] ,
} ,
} ] )
console . run_command ( app , user , ' notifications ' , [ ] )
2021-08-28 21:15:21 +02:00
mock_get . assert_called_once_with ( app , user , ' /api/v1/notifications ' , { ' exclude_types[] ' : [ ] , ' limit ' : 20 } )
2019-02-17 14:03:11 +01:00
out , err = capsys . readouterr ( )
out = uncolorize ( out )
assert not err
assert out == " \n " . join ( [
2022-11-12 09:55:12 +01:00
" ──────────────────────────────────────────────────────────────────────────────────────────────────── " ,
2019-02-17 14:03:11 +01:00
" Frank Zappa @frank@zappa.social now follows you " ,
2022-11-12 09:55:12 +01:00
" ──────────────────────────────────────────────────────────────────────────────────────────────────── " ,
2019-02-17 14:03:11 +01:00
" Dweezil Zappa @dweezil@zappa.social mentioned you in " ,
2022-11-21 08:31:09 +01:00
" Dweezil Zappa @dweezil@zappa.social 2017-04-12 15:53 UTC " ,
2019-02-17 14:03:11 +01:00
" " ,
" We still have fans in 2017 @fan123 " ,
" " ,
2022-12-12 12:46:24 +01:00
" ID 111111111111111111 " ,
2022-11-12 09:55:12 +01:00
" ──────────────────────────────────────────────────────────────────────────────────────────────────── " ,
2019-04-23 13:16:24 +02:00
" Terry Bozzio @terry@bozzio.social reblogged your status " ,
2022-11-21 08:31:09 +01:00
" Zappa Fan @fan123@zappa-fans.social 1983-11-04 15:53 UTC " ,
2019-02-17 14:03:11 +01:00
" " ,
" The Black Page, a masterpiece " ,
" " ,
2022-12-12 12:46:24 +01:00
" ID 1234 " ,
2022-11-12 09:55:12 +01:00
" ──────────────────────────────────────────────────────────────────────────────────────────────────── " ,
2019-04-23 13:16:24 +02:00
" Zappa Old Fan @fan9@zappa-fans.social favourited your status " ,
2022-11-21 08:31:09 +01:00
" Zappa Fan @fan123@zappa-fans.social 1983-11-04 15:53 UTC " ,
2019-02-17 14:03:11 +01:00
" " ,
" The Black Page, a masterpiece " ,
" " ,
2022-12-12 12:46:24 +01:00
" ID 1234 " ,
2022-11-12 09:55:12 +01:00
" ──────────────────────────────────────────────────────────────────────────────────────────────────── " ,
2019-02-17 14:03:11 +01:00
" " ,
] )
2019-04-23 13:16:24 +02:00
2019-02-17 15:02:05 +01:00
@mock.patch ( ' toot.http.get ' )
def test_notifications_empty ( mock_get , capsys ) :
mock_get . return_value = MockResponse ( [ ] )
console . run_command ( app , user , ' notifications ' , [ ] )
2021-08-28 21:15:21 +02:00
mock_get . assert_called_once_with ( app , user , ' /api/v1/notifications ' , { ' exclude_types[] ' : [ ] , ' limit ' : 20 } )
2019-02-17 15:02:05 +01:00
out , err = capsys . readouterr ( )
out = uncolorize ( out )
assert not err
assert out == " No notification \n "
2019-02-17 14:03:11 +01:00
2019-02-17 14:18:51 +01:00
@mock.patch ( ' toot.http.post ' )
def test_notifications_clear ( mock_post , capsys ) :
console . run_command ( app , user , ' notifications ' , [ ' --clear ' ] )
out , err = capsys . readouterr ( )
out = uncolorize ( out )
mock_post . assert_called_once_with ( app , user , ' /api/v1/notifications/clear ' )
assert not err
assert out == ' Cleared notifications \n '
2018-01-02 10:44:32 +01:00
def u ( user_id , access_token = " abc " ) :
username , instance = user_id . split ( " @ " )
return {
" instance " : instance ,
" username " : username ,
" access_token " : access_token ,
}
2018-06-07 10:00:50 +02:00
@mock.patch ( ' toot.config.save_config ' )
@mock.patch ( ' toot.config.load_config ' )
def test_logout ( mock_load , mock_save , capsys ) :
mock_load . return_value = {
" users " : {
" king@gizzard.social " : u ( " king@gizzard.social " ) ,
" lizard@wizard.social " : u ( " lizard@wizard.social " ) ,
} ,
" active_user " : " king@gizzard.social " ,
}
2018-01-02 10:44:32 +01:00
2018-06-07 10:00:50 +02:00
console . run_command ( app , user , " logout " , [ " king@gizzard.social " ] )
2018-01-02 10:44:32 +01:00
2018-06-07 10:00:50 +02:00
mock_save . assert_called_once_with ( {
' users ' : {
' lizard@wizard.social ' : u ( " lizard@wizard.social " )
} ,
' active_user ' : None
} )
2018-01-02 10:44:32 +01:00
out , err = capsys . readouterr ( )
assert " ✓ User king@gizzard.social logged out " in out
2018-06-07 10:00:50 +02:00
@mock.patch ( ' toot.config.save_config ' )
@mock.patch ( ' toot.config.load_config ' )
def test_activate ( mock_load , mock_save , capsys ) :
mock_load . return_value = {
" users " : {
2018-01-02 10:44:32 +01:00
" king@gizzard.social " : u ( " king@gizzard.social " ) ,
" lizard@wizard.social " : u ( " lizard@wizard.social " ) ,
2018-06-07 10:00:50 +02:00
} ,
" active_user " : " king@gizzard.social " ,
}
2018-01-02 10:44:32 +01:00
2018-06-07 10:00:50 +02:00
console . run_command ( app , user , " activate " , [ " lizard@wizard.social " ] )
2018-01-02 10:44:32 +01:00
2018-06-07 10:00:50 +02:00
mock_save . assert_called_once_with ( {
' users ' : {
" king@gizzard.social " : u ( " king@gizzard.social " ) ,
' lizard@wizard.social ' : u ( " lizard@wizard.social " )
} ,
' active_user ' : " lizard@wizard.social "
} )
2018-01-02 10:44:32 +01:00
out , err = capsys . readouterr ( )
assert " ✓ User lizard@wizard.social active " in out