Skip to content

Commit

Permalink
PA-655: Add website create and get happy paths to cli. by Piotr, Nina
Browse files Browse the repository at this point in the history
  • Loading branch information
ninakahr committed Jul 8, 2024
1 parent 785e202 commit 1b17785
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
22 changes: 19 additions & 3 deletions cli/website.py
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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()
Expand All @@ -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()
Expand Down
49 changes: 45 additions & 4 deletions tests/test_cli_website.py
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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,
[
Expand All @@ -52,18 +84,27 @@ 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,
[
"get",
],
)
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():
Expand Down

0 comments on commit 1b17785

Please sign in to comment.