Skip to content

Commit

Permalink
#29 adds path share and unshare commands
Browse files Browse the repository at this point in the history
  • Loading branch information
caseneuve authored and filiplajszczak committed May 21, 2021
1 parent 90563a1 commit 7e8133c
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 4 deletions.
23 changes: 19 additions & 4 deletions cli/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,25 @@ def delete(


@app.command()
def share(path: str = typer.Argument(..., help="Path to PythonAnywhere file or directory")):
pass
def share(
path: str = typer.Argument(..., help="Path to PythonAnywhere file to be shared"),
check: bool = typer.Option(False, "-c", "--check", help="Check sharing status")
):
path = standarize_path(path)
pa_path = PAPath(path)

link = pa_path.get_sharing_url() if check else pa_path.share()

if not link:
sys.exit(1)
typer.echo(link)


@app.command()
def unshare(path: str = typer.Argument(..., help="Path to PythonAnywhere file or directory")):
pass
def unshare(path: str = typer.Argument(..., help="Path to PythonAnywhere file to be unshared")):
path = standarize_path(path)
pa_path = PAPath(path)

success = pa_path.unshare()

sys.exit(0 if success else 1)
76 changes: 76 additions & 0 deletions tests/test_cli_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ def test_prints_tree_for_empty_directory(self, mock_path, home_dir):
class TestUpload:
file = NamedTemporaryFile()

def test_creates_pa_path_with_provided_path(self, mock_path, home_dir):
runner.invoke(app, ["upload", "~/hello.txt", "-c", self.file.name])
mock_path.assert_called_once_with(f"{home_dir}/hello.txt")

def test_exits_with_success_when_successful_upload(self, mock_path):
mock_path.return_value.upload.return_value = True

Expand All @@ -206,6 +210,10 @@ def test_exits_with_error_when_unsuccessful_upload(self, mock_path):


class TestDelete:
def test_creates_pa_path_with_provided_path(self, mock_path, home_dir):
runner.invoke(app, ["delete", "~/hello.txt"])
mock_path.assert_called_once_with(f"{home_dir}/hello.txt")

def test_exits_with_success_when_successful_delete(self, mock_path):
mock_path.return_value.delete.return_value = True

Expand All @@ -221,3 +229,71 @@ def test_exits_with_error_when_unsuccessful_delete(self, mock_path):

assert mock_path.return_value.delete.called
assert result.exit_code == 1


class TestShare:
def test_creates_pa_path_with_provided_path(self, mock_path, home_dir):
runner.invoke(app, ["share", "~/hello.txt"])
mock_path.assert_called_once_with(f"{home_dir}/hello.txt")

def test_exits_with_success_and_prints_sharing_url_when_successful_share(self, mock_path):
mock_path.return_value.share.return_value = "link"

result = runner.invoke(app, ["share", "~/hello.txt"])

assert "link" in result.stdout
assert mock_path.return_value.share.called
assert not mock_path.return_value.get_sharing_url.called
assert result.exit_code == 0

def test_exits_with_error_when_unsuccessful_share(self, mock_path):
mock_path.return_value.share.return_value = ""

result = runner.invoke(app, ["share", "~/hello.txt"])

assert mock_path.return_value.share.called
assert not mock_path.return_value.get_sharing_url.called
assert result.stdout == ""
assert result.exit_code == 1

def test_exits_with_success_and_prints_sharing_url_when_path_already_shared(self, mock_path):
mock_path.return_value.get_sharing_url.return_value = "link"

result = runner.invoke(app, ["share", "--check", "~/hello.txt"])

assert mock_path.return_value.get_sharing_url.called
assert not mock_path.return_value.share.called
assert "link" in result.stdout
assert result.exit_code == 0

def test_exits_with_error_when_path_was_not_shared(self, mock_path):
mock_path.return_value.get_sharing_url.return_value = ""

result = runner.invoke(app, ["share", "--check", "~/hello.txt"])

assert mock_path.return_value.get_sharing_url.called
assert not mock_path.return_value.share.called
assert result.stdout == ""
assert result.exit_code == 1


class TestUnshare:
def test_creates_pa_path_with_provided_path(self, mock_path, home_dir):
runner.invoke(app, ["unshare", "~/hello.txt"])
mock_path.assert_called_once_with(f"{home_dir}/hello.txt")

def test_exits_with_success_when_successful_unshare_or_file_not_shared(self, mock_path):
mock_path.return_value.unshare.return_value = True

result = runner.invoke(app, ["unshare", "~/hello.txt"])

assert mock_path.return_value.unshare.called
assert result.exit_code == 0

def test_exits_with_error_when_unsuccessful_unshare(self, mock_path):
mock_path.return_value.unshare.return_value = False

result = runner.invoke(app, ["unshare", "~/hello.txt"])

assert mock_path.return_value.unshare.called
assert result.exit_code == 1

0 comments on commit 7e8133c

Please sign in to comment.