Skip to content

Commit 1b17785

Browse files
author
Nina Kahr
committed
PA-655: Add website create and get happy paths to cli. by Piotr, Nina
1 parent 785e202 commit 1b17785

File tree

2 files changed

+64
-7
lines changed

2 files changed

+64
-7
lines changed

cli/website.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/python3
22
import typer
3+
from pprint import pformat
34
from typing_extensions import Annotated
4-
5+
from pythonanywhere_core.website import Website
6+
from snakesay import snakesay
57

68
app = typer.Typer(no_args_is_help=True)
79

@@ -25,7 +27,13 @@ def create(
2527
],
2628
):
2729
"""Create an ASGI website"""
28-
pass
30+
Website().create(domain_name=domain_name, command=command)
31+
# TODO: do some basic checks
32+
typer.echo(
33+
snakesay(
34+
f"All done! Your site is now live at {domain_name}. "
35+
)
36+
)
2937

3038

3139
@app.command()
@@ -38,7 +46,15 @@ def get(
3846
)
3947
):
4048
"""If no domain name is specified, list all domains. Otherwise get details for specified domain"""
41-
pass
49+
websites = Website().get()
50+
typer.echo(
51+
snakesay(
52+
f"You have {len(websites)} website(s). "
53+
)
54+
)
55+
typer.echo(
56+
pformat(websites)
57+
)
4258

4359

4460
@app.command()

tests/test_cli_website.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
1+
import getpass
2+
import pytest
13
from typer.testing import CliRunner
24

35
from cli.website import app
46

57
runner = CliRunner()
68

9+
@pytest.fixture
10+
def domain_name():
11+
return "foo.bar.com"
12+
13+
14+
@pytest.fixture
15+
def command():
16+
return "/usr/local/bin/uvicorn --uds $DOMAIN_SOCKET main:app"
17+
18+
19+
@pytest.fixture
20+
def website_info(domain_name, command):
21+
return {
22+
"domain_name": domain_name,
23+
"enabled": True,
24+
"id": 42,
25+
"user": getpass.getuser(),
26+
"webapp": {
27+
"command": command,
28+
"domains": [
29+
{
30+
"domain_name": domain_name,
31+
"enabled": True
32+
}
33+
],
34+
"id": 42
35+
}
36+
}
37+
738

839
def test_main_subcommand_without_args_prints_help():
940
result = runner.invoke(
@@ -40,7 +71,8 @@ def test_create_without_command_barfs():
4071
assert "Missing option" in result.stdout
4172

4273

43-
def test_create_with_domain_and_command_creates_it():
74+
def test_create_with_domain_and_command_creates_it(mocker):
75+
mock_website = mocker.patch("cli.website.Website")
4476
result = runner.invoke(
4577
app,
4678
[
@@ -52,18 +84,27 @@ def test_create_with_domain_and_command_creates_it():
5284
],
5385
)
5486
assert result.exit_code == 0
55-
assert False, "TODO"
87+
mock_website.return_value.create.assert_called_once_with(
88+
domain_name="www.something.com",
89+
command="some kind of server"
90+
)
91+
assert "All done!" in result.stdout
5692

5793

58-
def test_get_with_no_domain_lists_websites():
94+
def test_get_with_no_domain_lists_websites(mocker, website_info):
95+
mock_website = mocker.patch("cli.website.Website")
96+
mock_website.return_value.get.return_value = [website_info]
97+
5998
result = runner.invoke(
6099
app,
61100
[
62101
"get",
63102
],
64103
)
65104
assert result.exit_code == 0
66-
assert False, "TODO"
105+
mock_website.return_value.get.assert_called_once()
106+
assert "You have 1 website(s). " in result.stdout
107+
assert "foo.bar.com" in result.stdout
67108

68109

69110
def test_get_with_domain_gives_details_for_domain():

0 commit comments

Comments
 (0)