From 1b177856fd1682b3c3e69f08b3c20a05e79a47e6 Mon Sep 17 00:00:00 2001 From: Nina Kahr Date: Mon, 8 Jul 2024 17:04:42 +0100 Subject: [PATCH] PA-655: Add website create and get happy paths to cli. by Piotr, Nina --- cli/website.py | 22 +++++++++++++++--- tests/test_cli_website.py | 49 +++++++++++++++++++++++++++++++++++---- 2 files changed, 64 insertions(+), 7 deletions(-) diff --git a/cli/website.py b/cli/website.py index e661d1f..ab173a7 100644 --- a/cli/website.py +++ b/cli/website.py @@ -1,7 +1,9 @@ #!/usr/bin/python3 import typer +from pprint import pformat from typing_extensions import Annotated - +from pythonanywhere_core.website import Website +from snakesay import snakesay app = typer.Typer(no_args_is_help=True) @@ -25,7 +27,13 @@ def create( ], ): """Create an ASGI website""" - pass + Website().create(domain_name=domain_name, command=command) + # TODO: do some basic checks + typer.echo( + snakesay( + f"All done! Your site is now live at {domain_name}. " + ) + ) @app.command() @@ -38,7 +46,15 @@ def get( ) ): """If no domain name is specified, list all domains. Otherwise get details for specified domain""" - pass + websites = Website().get() + typer.echo( + snakesay( + f"You have {len(websites)} website(s). " + ) + ) + typer.echo( + pformat(websites) + ) @app.command() diff --git a/tests/test_cli_website.py b/tests/test_cli_website.py index b851d55..0aeb26a 100644 --- a/tests/test_cli_website.py +++ b/tests/test_cli_website.py @@ -1,9 +1,40 @@ +import getpass +import pytest from typer.testing import CliRunner from cli.website import app runner = CliRunner() +@pytest.fixture +def domain_name(): + return "foo.bar.com" + + +@pytest.fixture +def command(): + return "/usr/local/bin/uvicorn --uds $DOMAIN_SOCKET main:app" + + +@pytest.fixture +def website_info(domain_name, command): + return { + "domain_name": domain_name, + "enabled": True, + "id": 42, + "user": getpass.getuser(), + "webapp": { + "command": command, + "domains": [ + { + "domain_name": domain_name, + "enabled": True + } + ], + "id": 42 + } + } + def test_main_subcommand_without_args_prints_help(): result = runner.invoke( @@ -40,7 +71,8 @@ def test_create_without_command_barfs(): assert "Missing option" in result.stdout -def test_create_with_domain_and_command_creates_it(): +def test_create_with_domain_and_command_creates_it(mocker): + mock_website = mocker.patch("cli.website.Website") result = runner.invoke( app, [ @@ -52,10 +84,17 @@ def test_create_with_domain_and_command_creates_it(): ], ) assert result.exit_code == 0 - assert False, "TODO" + mock_website.return_value.create.assert_called_once_with( + domain_name="www.something.com", + command="some kind of server" + ) + assert "All done!" in result.stdout -def test_get_with_no_domain_lists_websites(): +def test_get_with_no_domain_lists_websites(mocker, website_info): + mock_website = mocker.patch("cli.website.Website") + mock_website.return_value.get.return_value = [website_info] + result = runner.invoke( app, [ @@ -63,7 +102,9 @@ def test_get_with_no_domain_lists_websites(): ], ) assert result.exit_code == 0 - assert False, "TODO" + mock_website.return_value.get.assert_called_once() + assert "You have 1 website(s). " in result.stdout + assert "foo.bar.com" in result.stdout def test_get_with_domain_gives_details_for_domain():