Skip to content

Commit

Permalink
PA-752: Return errors from API in website creation. by Filip, Nina
Browse files Browse the repository at this point in the history
  • Loading branch information
ninakahr committed Nov 18, 2024
1 parent db25bcb commit 25cfc9e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
11 changes: 7 additions & 4 deletions cli/website.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from tabulate import tabulate

from pythonanywhere_core.website import Website
from pythonanywhere_core.exceptions import SanityException
from pythonanywhere_core.exceptions import PythonAnywhereApiException, DomainAlreadyExistsException


app = typer.Typer(no_args_is_help=True)
Expand Down Expand Up @@ -35,9 +35,12 @@ def create(
"""Create an ASGI website"""
try:
Website().create(domain_name=domain_name, command=command)
except SanityException as e:
typer.echo(f"You already have a website for {domain_name}. Use the --nuke option to replace it.")
return
except DomainAlreadyExistsException:
typer.echo(f"You already have a website for {domain_name}.")
raise typer.Exit(code=1)
except PythonAnywhereApiException as e:
typer.echo(str(e))
raise typer.Exit(code=1)

typer.echo(
snakesay(
Expand Down
35 changes: 28 additions & 7 deletions tests/test_cli_website.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typer.testing import CliRunner

from cli.website import app
from pythonanywhere_core.exceptions import SanityException
from pythonanywhere_core.exceptions import PythonAnywhereApiException, DomainAlreadyExistsException


runner = CliRunner()
Expand Down Expand Up @@ -115,25 +115,46 @@ def test_create_with_domain_and_command_creates_it(mock_website):
assert "All done!" in result.stdout


def test_create_with_failing_sanity_check(mock_website):
mock_website.return_value.create.side_effect = SanityException("You already have a website for this domain. Use the --nuke option to replace it.")

def test_create_with_existing_domain(mock_website):
mock_website.return_value.create.side_effect = DomainAlreadyExistsException
domain_name = "www.something.com"
result = runner.invoke(
app,
[
"create",
"-d",
"www.something.com",
domain_name,
"-c",
"some kind of server",
],
)
assert result.exit_code == 0
assert result.exit_code != 0
mock_website.return_value.create.assert_called_once_with(
domain_name="www.something.com",
command="some kind of server"
)
assert "You already have a website for www.something.com." in result.stdout


def test_create_with_existing_domain(mock_website):
mock_website.return_value.create.side_effect = PythonAnywhereApiException("Something terrible has happened.")
domain_name = "www.something.com"
result = runner.invoke(
app,
[
"create",
"-d",
domain_name,
"-c",
"some kind of server",
],
)
assert result.exit_code != 0
mock_website.return_value.create.assert_called_once_with(
domain_name="www.something.com",
command="some kind of server"
)
assert "You already have a website for www.something.com. Use the --nuke option to replace it." in result.stdout
assert "Something terrible has happened." in result.stdout


def test_get_with_no_domain_lists_websites(mock_echo, mock_tabulate, mock_website, website_info):
Expand Down

0 comments on commit 25cfc9e

Please sign in to comment.