-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_env_delete.py
More file actions
148 lines (108 loc) · 4.39 KB
/
test_env_delete.py
File metadata and controls
148 lines (108 loc) · 4.39 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
147
148
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 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", "delete"])
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", "delete"])
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.delete("/apps/123/environment-variables/SOME_VAR").mock(
return_value=Response(500)
)
with changing_dir(configured_app):
result = runner.invoke(app, ["env", "delete", "SOME_VAR"])
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_if_not_found(
logged_in_cli: None, respx_mock: respx.MockRouter, configured_app: Path
) -> None:
respx_mock.delete("/apps/123/environment-variables/SOME_VAR").mock(
return_value=Response(404)
)
with changing_dir(configured_app):
result = runner.invoke(app, ["env", "delete", "SOME_VAR"])
assert result.exit_code == 1
assert "Environment variable not found" in result.output
def test_shows_a_message_if_name_is_invalid(
logged_in_cli: None, configured_app: Path
) -> None:
with changing_dir(configured_app):
result = runner.invoke(app, ["env", "delete", "aaa-aaa"])
assert result.exit_code == 1
assert "The environment variable name aaa-aaa is invalid." in result.output
@pytest.mark.respx(base_url=settings.base_api_url)
def test_shows_message_when_it_deletes(
logged_in_cli: None, respx_mock: respx.MockRouter, configured_app: Path
) -> None:
respx_mock.delete("/apps/123/environment-variables/SOME_VAR").mock(
return_value=Response(204)
)
with changing_dir(configured_app):
result = runner.invoke(app, ["env", "delete", "SOME_VAR"])
assert result.exit_code == 0
assert "Environment variable SOME_VAR deleted" in result.output
@pytest.mark.respx(base_url=settings.base_api_url)
def test_shows_selector_for_environment_variables(
logged_in_cli: None, respx_mock: respx.MockRouter, configured_app: Path
) -> None:
steps = [Keys.ENTER]
respx_mock.get("/apps/123/environment-variables/").mock(
return_value=Response(
200,
json={
"data": [
{"name": "SECRET_KEY", "value": "123"},
{"name": "API_KEY", "value": "456"},
]
},
)
)
respx_mock.delete("/apps/123/environment-variables/SECRET_KEY").mock(
return_value=Response(204)
)
with changing_dir(configured_app), patch(
"rich_toolkit.container.getchar", side_effect=steps
):
result = runner.invoke(app, ["env", "delete"])
assert result.exit_code == 0
assert "Select the environment variable to delete" in result.output
assert "Environment variable SECRET_KEY deleted" in result.output
@pytest.mark.respx(base_url=settings.base_api_url)
def test_shows_message_if_no_environment_variable(
logged_in_cli: None, respx_mock: respx.MockRouter, configured_app: Path
) -> None:
respx_mock.get("/apps/123/environment-variables/").mock(
return_value=Response(200, json={"data": []})
)
with changing_dir(configured_app):
result = runner.invoke(app, ["env", "delete"])
assert result.exit_code == 0
assert "No environment variables found." in result.output