Skip to content

Commit

Permalink
Rework how commands are defined
Browse files Browse the repository at this point in the history
  • Loading branch information
ihabunek committed Apr 19, 2017
1 parent 2a3d66b commit 373f264
Show file tree
Hide file tree
Showing 7 changed files with 543 additions and 476 deletions.
38 changes: 24 additions & 14 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,30 @@ Running ``toot`` displays a list of available commands.

Running ``toot <command> -h`` shows the documentation for the given command.

=================== ===============================================================
Command Description
=================== ===============================================================
``toot login`` Log into a Mastodon instance.
``toot 2fa`` Log into a Mastodon instance using two factor authentication.
``toot logout`` Log out, deletes stored access keys.
``toot auth`` Display stored authenitication tokens.
``toot whoami`` Display logged in user details.
``toot post`` Post a status to your timeline.
``toot search`` Search for accounts or hashtags.
``toot timeline`` Display recent items in your public timeline.
``toot follow`` Follow an account.
``toot unfollow`` Unfollow an account.
=================== ===============================================================
.. code-block::
$ toot
toot - a Mastodon CLI client
Usage:
toot login Log into a Mastodon instance
toot login_2fa Log in using two factor authentication (experimental)
toot logout Log out, delete stored access keys
toot auth Show stored credentials
toot whoami Display logged in user details
toot post Post a status text to your timeline
toot upload Upload an image or video file
toot search Search for users or hashtags
toot follow Follow an account
toot unfollow Unfollow an account
toot timeline Show recent items in your public timeline
To get help for each command run:
toot <command> --help
https://github.com/ihabunek/toot
Authentication
--------------
Expand Down
34 changes: 17 additions & 17 deletions tests/test_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def uncolorize(text):
def test_print_usage(capsys):
console.print_usage()
out, err = capsys.readouterr()
assert "toot - interact with Mastodon from the command line" in out
assert "toot - a Mastodon CLI client" in out


def test_post_status_defaults(monkeypatch, capsys):
def test_post_defaults(monkeypatch, capsys):
def mock_prepare(request):
assert request.method == 'POST'
assert request.url == 'https://habunek.com/api/v1/statuses'
Expand All @@ -41,13 +41,13 @@ def mock_send(*args):
monkeypatch.setattr(requests.Request, 'prepare', mock_prepare)
monkeypatch.setattr(requests.Session, 'send', mock_send)

console.cmd_post_status(app, user, ['Hello world'])
console.run_command(app, user, 'post', ['Hello world'])

out, err = capsys.readouterr()
assert "Toot posted" in out


def test_post_status_with_options(monkeypatch, capsys):
def test_post_with_options(monkeypatch, capsys):
def mock_prepare(request):
assert request.method == 'POST'
assert request.url == 'https://habunek.com/api/v1/statuses'
Expand All @@ -68,27 +68,27 @@ def mock_send(*args):

args = ['"Hello world"', '--visibility', 'unlisted']

console.cmd_post_status(app, user, args)
console.run_command(app, user, 'post', args)

out, err = capsys.readouterr()
assert "Toot posted" in out


def test_post_status_invalid_visibility(monkeypatch, capsys):
def test_post_invalid_visibility(monkeypatch, capsys):
args = ['Hello world', '--visibility', 'foo']

with pytest.raises(SystemExit):
console.cmd_post_status(app, user, args)
console.run_command(app, user, 'post', args)

out, err = capsys.readouterr()
assert "invalid visibility value: 'foo'" in err


def test_post_status_invalid_media(monkeypatch, capsys):
def test_post_invalid_media(monkeypatch, capsys):
args = ['Hello world', '--media', 'does_not_exist.jpg']

with pytest.raises(SystemExit):
console.cmd_post_status(app, user, args)
console.run_command(app, user, 'post', args)

out, err = capsys.readouterr()
assert "can't open 'does_not_exist.jpg'" in err
Expand All @@ -112,7 +112,7 @@ def mock_get(url, params, headers=None):

monkeypatch.setattr(requests, 'get', mock_get)

console.cmd_timeline(app, user, [])
console.run_command(app, user, 'timeline', [])

out, err = capsys.readouterr()
assert "The computer can't tell you the emotional story." in out
Expand All @@ -138,7 +138,7 @@ def mock_send(*args):
monkeypatch.setattr(requests.Request, 'prepare', mock_prepare)
monkeypatch.setattr(requests.Session, 'send', mock_send)

console.cmd_upload(app, user, [__file__])
console.run_command(app, user, 'upload', [__file__])

out, err = capsys.readouterr()
assert "Uploading media" in out
Expand Down Expand Up @@ -168,7 +168,7 @@ def mock_get(url, params, headers=None):

monkeypatch.setattr(requests, 'get', mock_get)

console.cmd_search(app, user, ['freddy'])
console.run_command(app, user, 'search', ['freddy'])

out, err = capsys.readouterr()
assert "Hashtags:\n\033[32m#foo\033[0m, \033[32m#bar\033[0m, \033[32m#baz\033[0m" in out
Expand Down Expand Up @@ -200,7 +200,7 @@ def mock_send(*args, **kwargs):
monkeypatch.setattr(requests.Session, 'send', mock_send)
monkeypatch.setattr(requests, 'get', mock_get)

console.cmd_follow(app, user, ['blixa'])
console.run_command(app, user, 'follow', ['blixa'])

out, err = capsys.readouterr()
assert "You are now following blixa" in out
Expand All @@ -218,7 +218,7 @@ def mock_get(url, params, headers):

monkeypatch.setattr(requests, 'get', mock_get)

console.cmd_follow(app, user, ['blixa'])
console.run_command(app, user, 'follow', ['blixa'])

out, err = capsys.readouterr()
assert "Account not found" in err
Expand Down Expand Up @@ -247,7 +247,7 @@ def mock_send(*args, **kwargs):
monkeypatch.setattr(requests.Session, 'send', mock_send)
monkeypatch.setattr(requests, 'get', mock_get)

console.cmd_unfollow(app, user, ['blixa'])
console.run_command(app, user, 'unfollow', ['blixa'])

out, err = capsys.readouterr()
assert "You are no longer following blixa" in out
Expand All @@ -265,7 +265,7 @@ def mock_get(url, params, headers):

monkeypatch.setattr(requests, 'get', mock_get)

console.cmd_unfollow(app, user, ['blixa'])
console.run_command(app, user, 'unfollow', ['blixa'])

out, err = capsys.readouterr()
assert "Account not found" in err
Expand Down Expand Up @@ -297,7 +297,7 @@ def mock_get(url, params, headers=None):

monkeypatch.setattr(requests, 'get', mock_get)

console.cmd_whoami(app, user, [])
console.run_command(app, user, 'whoami', [])

out, err = capsys.readouterr()
out = uncolorize(out)
Expand Down
8 changes: 7 additions & 1 deletion toot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import print_function

from collections import namedtuple

Expand All @@ -7,5 +9,9 @@

DEFAULT_INSTANCE = 'mastodon.social'

CLIENT_NAME = 'toot - Mastodon CLI Interface'
CLIENT_NAME = 'toot - a Mastodon CLI client'
CLIENT_WEBSITE = 'https://github.com/ihabunek/toot'


class ConsoleError(Exception):
pass
2 changes: 1 addition & 1 deletion toot/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from requests import Request, Session

from toot import App, User, CLIENT_NAME, CLIENT_WEBSITE
from toot import CLIENT_NAME, CLIENT_WEBSITE

SCOPES = 'read write follow'

Expand Down
Loading

0 comments on commit 373f264

Please sign in to comment.