From c0eb76751fc4bcd6817bb0b9039d7a9561f6285f Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Tue, 28 Nov 2023 16:56:53 +0100 Subject: [PATCH] Migrate update_account command --- tests/integration/test_auth.py | 93 +++++++++++++++++------------- toot/cli/accounts.py | 101 ++++++++++++++++++++++++++++++++- 2 files changed, 154 insertions(+), 40 deletions(-) diff --git a/tests/integration/test_auth.py b/tests/integration/test_auth.py index 8a5c06f1..83343d79 100644 --- a/tests/integration/test_auth.py +++ b/tests/integration/test_auth.py @@ -1,56 +1,60 @@ -import pytest - +from uuid import uuid4 from tests.integration.conftest import TRUMPET -from toot import api +from toot import api, cli from toot.entities import Account, from_dict from toot.utils import get_text -pytest.skip("TODO", allow_module_level=True) - - def test_update_account_no_options(run): - out = run("update_account") - assert out == "Please specify at least one option to update the account" + result = run(cli.update_account) + assert result.exit_code == 1 + assert result.stderr.strip() == "Error: Please specify at least one option to update the account" def test_update_account_display_name(run, app, user): - out = run("update_account", "--display-name", "elwood") - assert out == "✓ Account updated" + name = str(uuid4())[:10] + + result = run(cli.update_account, "--display-name", name) + assert result.exit_code == 0 + assert result.stdout.strip() == "✓ Account updated" account = api.verify_credentials(app, user).json() - assert account["display_name"] == "elwood" + assert account["display_name"] == name def test_update_account_json(run_json, app, user): - out = run_json("update_account", "--display-name", "elwood", "--json") + name = str(uuid4())[:10] + out = run_json(cli.update_account, "--display-name", name, "--json") account = from_dict(Account, out) assert account.acct == user.username - assert account.display_name == "elwood" + assert account.display_name == name def test_update_account_note(run, app, user): note = ("It's 106 miles to Chicago, we got a full tank of gas, half a pack " "of cigarettes, it's dark... and we're wearing sunglasses.") - out = run("update_account", "--note", note) - assert out == "✓ Account updated" + result = run(cli.update_account, "--note", note) + assert result.exit_code == 0 + assert result.stdout.strip() == "✓ Account updated" account = api.verify_credentials(app, user).json() assert get_text(account["note"]) == note def test_update_account_language(run, app, user): - out = run("update_account", "--language", "hr") - assert out == "✓ Account updated" + result = run(cli.update_account, "--language", "hr") + assert result.exit_code == 0 + assert result.stdout.strip() == "✓ Account updated" account = api.verify_credentials(app, user).json() assert account["source"]["language"] == "hr" def test_update_account_privacy(run, app, user): - out = run("update_account", "--privacy", "private") - assert out == "✓ Account updated" + result = run(cli.update_account, "--privacy", "private") + assert result.exit_code == 0 + assert result.stdout.strip() == "✓ Account updated" account = api.verify_credentials(app, user).json() assert account["source"]["privacy"] == "private" @@ -60,8 +64,9 @@ def test_update_account_avatar(run, app, user): account = api.verify_credentials(app, user).json() old_value = account["avatar"] - out = run("update_account", "--avatar", TRUMPET) - assert out == "✓ Account updated" + result = run(cli.update_account, "--avatar", TRUMPET) + assert result.exit_code == 0 + assert result.stdout.strip() == "✓ Account updated" account = api.verify_credentials(app, user).json() assert account["avatar"] != old_value @@ -71,64 +76,74 @@ def test_update_account_header(run, app, user): account = api.verify_credentials(app, user).json() old_value = account["header"] - out = run("update_account", "--header", TRUMPET) - assert out == "✓ Account updated" + result = run(cli.update_account, "--header", TRUMPET) + assert result.exit_code == 0 + assert result.stdout.strip() == "✓ Account updated" account = api.verify_credentials(app, user).json() assert account["header"] != old_value def test_update_account_locked(run, app, user): - out = run("update_account", "--locked") - assert out == "✓ Account updated" + result = run(cli.update_account, "--locked") + assert result.exit_code == 0 + assert result.stdout.strip() == "✓ Account updated" account = api.verify_credentials(app, user).json() assert account["locked"] is True - out = run("update_account", "--no-locked") - assert out == "✓ Account updated" + result = run(cli.update_account, "--no-locked") + assert result.exit_code == 0 + assert result.stdout.strip() == "✓ Account updated" account = api.verify_credentials(app, user).json() assert account["locked"] is False def test_update_account_bot(run, app, user): - out = run("update_account", "--bot") - assert out == "✓ Account updated" + result = run(cli.update_account, "--bot") + + assert result.exit_code == 0 + assert result.stdout.strip() == "✓ Account updated" account = api.verify_credentials(app, user).json() assert account["bot"] is True - out = run("update_account", "--no-bot") - assert out == "✓ Account updated" + result = run(cli.update_account, "--no-bot") + assert result.exit_code == 0 + assert result.stdout.strip() == "✓ Account updated" account = api.verify_credentials(app, user).json() assert account["bot"] is False def test_update_account_discoverable(run, app, user): - out = run("update_account", "--discoverable") - assert out == "✓ Account updated" + result = run(cli.update_account, "--discoverable") + assert result.exit_code == 0 + assert result.stdout.strip() == "✓ Account updated" account = api.verify_credentials(app, user).json() assert account["discoverable"] is True - out = run("update_account", "--no-discoverable") - assert out == "✓ Account updated" + result = run(cli.update_account, "--no-discoverable") + assert result.exit_code == 0 + assert result.stdout.strip() == "✓ Account updated" account = api.verify_credentials(app, user).json() assert account["discoverable"] is False def test_update_account_sensitive(run, app, user): - out = run("update_account", "--sensitive") - assert out == "✓ Account updated" + result = run(cli.update_account, "--sensitive") + assert result.exit_code == 0 + assert result.stdout.strip() == "✓ Account updated" account = api.verify_credentials(app, user).json() assert account["source"]["sensitive"] is True - out = run("update_account", "--no-sensitive") - assert out == "✓ Account updated" + result = run(cli.update_account, "--no-sensitive") + assert result.exit_code == 0 + assert result.stdout.strip() == "✓ Account updated" account = api.verify_credentials(app, user).json() assert account["source"]["sensitive"] is False diff --git a/toot/cli/accounts.py b/toot/cli/accounts.py index 5bed3109..a8c63c13 100644 --- a/toot/cli/accounts.py +++ b/toot/cli/accounts.py @@ -1,13 +1,112 @@ import click import json as pyjson -from typing import Optional +from typing import BinaryIO, Optional from toot import api from toot.cli.base import cli, json_option, Context, pass_context +from toot.cli.validators import validate_language +from toot.console import PRIVACY_CHOICES from toot.output import print_acct_list +@cli.command(name="update_account") +@click.option("--display-name", help="The display name to use for the profile.") +@click.option("--note", help="The account bio.") +@click.option( + "--avatar", + type=click.File(mode="rb"), + help="Path to the avatar image to set.", +) +@click.option( + "--header", + type=click.File(mode="rb"), + help="Path to the header image to set.", +) +@click.option( + "--bot/--no-bot", + default=None, + help="Whether the account has a bot flag.", +) +@click.option( + "--discoverable/--no-discoverable", + default=None, + help="Whether the account should be shown in the profile directory.", +) +@click.option( + "--locked/--no-locked", + default=None, + help="Whether manual approval of follow requests is required.", +) +@click.option( + "--privacy", + type=click.Choice(PRIVACY_CHOICES), + help="Default post privacy for authored statuses.", +) +@click.option( + "--sensitive/--no-sensitive", + default=None, + help="Whether to mark authored statuses as sensitive by default.", +) +@click.option( + "--language", + callback=validate_language, + help="Default language to use for authored statuses (ISO 639-1).", +) +@json_option +@pass_context +def update_account( + ctx: Context, + display_name: Optional[str], + note: Optional[str], + avatar: Optional[BinaryIO], + header: Optional[BinaryIO], + bot: Optional[bool], + discoverable: Optional[bool], + locked: Optional[bool], + privacy: Optional[bool], + sensitive: Optional[bool], + language: Optional[bool], + json: bool, +): + """Update your account details""" + options = [ + avatar, + bot, + discoverable, + display_name, + header, + language, + locked, + note, + privacy, + sensitive, + ] + + if all(option is None for option in options): + raise click.ClickException("Please specify at least one option to update the account") + + response = api.update_account( + ctx.app, + ctx.user, + avatar=avatar, + bot=bot, + discoverable=discoverable, + display_name=display_name, + header=header, + language=language, + locked=locked, + note=note, + privacy=privacy, + sensitive=sensitive, + ) + + if json: + click.echo(response.text) + else: + click.secho("✓ Account updated", fg="green") + + @cli.command() @click.argument("account") @json_option