forked from fastapilabs/fastapi-cloud-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_env_set.py
More file actions
95 lines (64 loc) · 2.85 KB
/
test_env_set.py
File metadata and controls
95 lines (64 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from pathlib import Path
from unittest.mock import patch
import pytest
import respx
from httpx import Response
from typer.testing import CliRunner
from fastapi_cloud_cli.cli import cloud_app as app
from fastapi_cloud_cli.config import Settings
from tests.utils import Keys, changing_dir
runner = CliRunner()
settings = Settings.get()
assets_path = Path(__file__).parent / "assets"
@pytest.fixture
def configured_app(tmp_path: Path) -> Path:
app_id = "123"
team_id = "456"
config_path = tmp_path / ".fastapicloud" / "cloud.json"
config_path.parent.mkdir(parents=True, exist_ok=True)
config_path.write_text(f'{{"app_id": "{app_id}", "team_id": "{team_id}"}}')
return tmp_path
def test_shows_a_message_if_not_logged_in(logged_out_cli: None) -> None:
result = runner.invoke(app, ["env", "set"])
assert result.exit_code == 1
assert "No credentials found." in result.output
def test_shows_a_message_if_app_is_not_configured(logged_in_cli: None) -> None:
result = runner.invoke(app, ["env", "set"])
assert result.exit_code == 1
assert "No app found" in result.output
@pytest.mark.respx(base_url=settings.base_api_url)
def test_shows_a_message_if_something_is_wrong(
logged_in_cli: None, respx_mock: respx.MockRouter, configured_app: Path
) -> None:
respx_mock.post("/apps/123/environment-variables/").mock(return_value=Response(500))
with changing_dir(configured_app):
result = runner.invoke(app, ["env", "set", "SOME_VAR", "secret"])
assert result.exit_code == 1
assert (
"Something went wrong while contacting the FastAPI Cloud server."
in result.output
)
@pytest.mark.respx(base_url=settings.base_api_url)
def test_shows_message_when_it_sets(
logged_in_cli: None, respx_mock: respx.MockRouter, configured_app: Path
) -> None:
respx_mock.post("/apps/123/environment-variables/").mock(return_value=Response(200))
with changing_dir(configured_app):
result = runner.invoke(app, ["env", "set", "SOME_VAR", "secret"])
assert result.exit_code == 0
assert "Environment variable SOME_VAR set" in result.output
@pytest.mark.respx(base_url=settings.base_api_url)
def test_asks_for_name_and_value(
logged_in_cli: None, respx_mock: respx.MockRouter, configured_app: Path
) -> None:
steps = [*"SOME_VAR", Keys.ENTER, *"secret", Keys.ENTER]
respx_mock.post("/apps/123/environment-variables/").mock(return_value=Response(200))
with changing_dir(configured_app), patch(
"rich_toolkit.container.getchar", side_effect=steps
):
result = runner.invoke(app, ["env", "set"])
assert result.exit_code == 0
assert "Enter the name of the environment variable" in result.output
assert "Enter the value of the environment variable" in result.output
assert "Environment variable SOME_VAR set" in result.output
assert "*" * 6 in result.output