Skip to content

Commit

Permalink
PA-752: Handle errors 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 7593ba6 commit 9a9b3b4
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 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
2 changes: 1 addition & 1 deletion pythonanywhere/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.15.3"
__version__ = "0.15.4"
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pytest==8.3.3
pytest-cov==5.0.0
pytest-mock==3.14.0
pytest-mypy==0.10.3
pythonanywhere_core==0.2.2
pythonanywhere_core==0.2.3
requests==2.32.3
responses==0.25.3
schema==0.7.2
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name="pythonanywhere",
version="0.15.3",
version="0.15.4",
description="PythonAnywhere helper tools for users",
long_description=long_description,
long_description_content_type="text/markdown",
Expand All @@ -36,7 +36,7 @@
"docopt",
"packaging",
"python-dateutil",
"pythonanywhere_core==0.2.0",
"pythonanywhere_core==0.2.3",
"requests",
"schema",
"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 9a9b3b4

Please sign in to comment.