-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_env_set.py
More file actions
146 lines (104 loc) · 4.5 KB
/
test_env_set.py
File metadata and controls
146 lines (104 loc) · 4.5 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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/",
json={"name": "SOME_VAR", "value": "secret", "is_secret": False},
).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/",
json={"name": "SOME_VAR", "value": "secret", "is_secret": False},
).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/",
json={"name": "SOME_VAR", "value": "secret", "is_secret": False},
).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
@pytest.mark.respx(base_url=settings.base_api_url)
def test_asks_for_name_and_value_for_secret(
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/",
json={"name": "SOME_VAR", "value": "secret", "is_secret": True},
).mock(return_value=Response(200))
with (
changing_dir(configured_app),
patch("rich_toolkit.container.getchar", side_effect=steps),
):
result = runner.invoke(app, ["env", "set", "--secret"])
assert result.exit_code == 0
assert "Enter the name of the secret" in result.output
assert "Enter the secret value" in result.output
assert "Secret environment variable SOME_VAR set" in result.output
assert "*" * 6 in result.output
@pytest.mark.respx(base_url=settings.base_api_url)
def test_sets_secret_flag(
logged_in_cli: None, respx_mock: respx.MockRouter, configured_app: Path
) -> None:
respx_mock.post(
"/apps/123/environment-variables/",
json={"name": "SOME_VAR", "value": "secret", "is_secret": True},
).mock(return_value=Response(200))
with changing_dir(configured_app):
result = runner.invoke(app, ["env", "set", "SOME_VAR", "secret", "--secret"])
assert result.exit_code == 0
assert "Secret environment variable SOME_VAR set" in result.output