Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ihabunek committed Nov 30, 2023
1 parent 6c9b939 commit fda4e30
Show file tree
Hide file tree
Showing 11 changed files with 253 additions and 101 deletions.
6 changes: 6 additions & 0 deletions changelog.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
0.40.0:
date: TBA
changes:
- "Migrated to `click` for commandline arguments. BC should be mostly preserved, please report any issues."
- "Removed the deprecated `--disable-https` option for `login` and `login_cli`, pass the base URL instead"

0.39.0:
date: 2023-11-23
changes:
Expand Down
10 changes: 3 additions & 7 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
from pathlib import Path
from toot import api, App, User
from toot.cli import Context
from toot.console import run_command
from toot.exceptions import ApiError, ConsoleError
from toot.output import print_out


def pytest_configure(config):
Expand All @@ -36,6 +33,7 @@ def pytest_configure(config):

# Mastodon database name, used to confirm user registration without having to click the link
DATABASE_DSN = os.getenv("TOOT_TEST_DATABASE_DSN")
TOOT_TEST_BASE_URL = os.getenv("TOOT_TEST_BASE_URL")

# Toot logo used for testing image upload
TRUMPET = str(Path(__file__).parent.parent.parent / "trumpet.png")
Expand Down Expand Up @@ -74,12 +72,10 @@ def confirm_user(email):
# DO NOT USE PUBLIC INSTANCES!!!
@pytest.fixture(scope="session")
def base_url():
base_url = os.getenv("TOOT_TEST_BASE_URL")

if not base_url:
if not TOOT_TEST_BASE_URL:
pytest.skip("Skipping integration tests, TOOT_TEST_BASE_URL not set")

return base_url
return TOOT_TEST_BASE_URL


@pytest.fixture(scope="session")
Expand Down
74 changes: 74 additions & 0 deletions tests/integration/test_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from unittest import mock

from toot import User, cli


# Saving config is mocked so we don't mess up our local config
# TODO: could this be implemented using an auto-use fixture so we have it always
# mocked?
@mock.patch("toot.config.load_app")
@mock.patch("toot.config.save_app")
@mock.patch("toot.config.save_user")
def test_login_cli(
save_user: mock.MagicMock,
save_app: mock.MagicMock,
load_app: mock.MagicMock,
user: User,
run
):
load_app.return_value = None

result = run(
cli.login_cli,
"--instance", "http://localhost:3000",
"--email", f"{user.username}@example.com",
"--password", "password",
)
assert result.exit_code == 0
assert "✓ Successfully logged in." in result.stdout

save_app.assert_called_once()
(app,) = save_app.call_args.args
assert app.instance == "localhost:3000"
assert app.base_url == "http://localhost:3000"
assert app.client_id
assert app.client_secret

save_user.assert_called_once()
(new_user,) = save_user.call_args.args
assert new_user.instance == "localhost:3000"
assert new_user.username == user.username
# access token will be different since this is a new login
assert new_user.access_token and new_user.access_token != user.access_token
assert save_user.call_args.kwargs == {"activate": True}


@mock.patch("toot.config.load_app")
@mock.patch("toot.config.save_app")
@mock.patch("toot.config.save_user")
def test_login_cli_wrong_password(
save_user: mock.MagicMock,
save_app: mock.MagicMock,
load_app: mock.MagicMock,
user: User,
run
):
load_app.return_value = None

result = run(
cli.login_cli,
"--instance", "http://localhost:3000",
"--email", f"{user.username}@example.com",
"--password", "wrong password",
)
assert result.exit_code == 1
assert result.stderr.strip() == "Error: Login failed"

save_app.assert_called_once()
(app,) = save_app.call_args.args
assert app.instance == "localhost:3000"
assert app.base_url == "http://localhost:3000"
assert app.client_id
assert app.client_secret

save_user.assert_not_called()
12 changes: 11 additions & 1 deletion tests/integration/test_read.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import json
import re

from tests.integration.conftest import TOOT_TEST_BASE_URL
from toot import api, cli
from toot.entities import Account, Status, from_dict, from_dict_list
from uuid import uuid4


def test_instance(app, run):
def test_instance_default(app, run):
result = run(cli.instance)
assert result.exit_code == 0

Expand All @@ -15,6 +16,15 @@ def test_instance(app, run):
assert "running Mastodon" in result.stdout


def test_instance_with_url(app, run):
result = run(cli.instance, TOOT_TEST_BASE_URL)
assert result.exit_code == 0

assert "Mastodon" in result.stdout
assert app.instance in result.stdout
assert "running Mastodon" in result.stdout


def test_instance_json(app, run):
result = run(cli.instance, "--json")
assert result.exit_code == 0
Expand Down
73 changes: 0 additions & 73 deletions tests/test_api.py

This file was deleted.

8 changes: 1 addition & 7 deletions toot/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,7 @@ def login(app, username, password):
'scope': SCOPES,
}

response = http.anon_post(url, data=data, allow_redirects=False)

# If auth fails, it redirects to the login page
if response.is_redirect:
raise AuthenticationError()

return response.json()
return http.anon_post(url, data=data)


def get_browser_login_url(app):
Expand Down
12 changes: 2 additions & 10 deletions toot/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@

def register_app(domain, base_url):
try:
print_out("Registering application...")
response = api.create_app(base_url)
except ApiError:
raise ConsoleError("Registration failed.")

app = App(domain, base_url, response['client_id'], response['client_secret'])
config.save_app(app)

print_out("Application tokens saved.")

return app


Expand All @@ -38,8 +35,6 @@ def create_app_interactive(base_url):


def get_instance_domain(base_url):
print_out("Looking up instance info...")

instance = api.get_instance(base_url).json()

print_out(
Expand All @@ -63,12 +58,9 @@ def create_user(app, access_token):
user = User(app.instance, None, access_token)
creds = api.verify_credentials(app, user).json()

user = User(app.instance, creds['username'], access_token)
user = User(app.instance, creds["username"], access_token)
config.save_user(user, activate=True)

print_out("Access token saved to config at: <green>{}</green>".format(
config.get_config_file_path()))

return user


Expand All @@ -91,7 +83,7 @@ def login_interactive(app, email=None):

try:
print_out("Authenticating...")
response = api.login(app, email, password)
response = api.login(app, email, password).json()
except ApiError:
raise ConsoleError("Login failed")

Expand Down
1 change: 1 addition & 0 deletions toot/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from toot.cli.base import cli, Context # noqa

from toot.cli.auth import *
from toot.cli.accounts import *
from toot.cli.lists import *
from toot.cli.post import *
Expand Down
Loading

0 comments on commit fda4e30

Please sign in to comment.