-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_cli_link.py
More file actions
191 lines (152 loc) · 5.77 KB
/
test_cli_link.py
File metadata and controls
191 lines (152 loc) · 5.77 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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 fastapi_cloud_cli.utils.apps import AppConfig
from tests.conftest import ConfiguredApp
from tests.utils import Keys, changing_dir
runner = CliRunner()
settings = Settings.get()
def test_shows_a_message_if_not_logged_in(logged_out_cli: None) -> None:
result = runner.invoke(app, ["link"])
assert result.exit_code == 1
assert "You need to be logged in to link an app." in result.output
def test_shows_a_message_if_already_linked(
logged_in_cli: None, configured_app: ConfiguredApp
) -> None:
with changing_dir(configured_app.path):
result = runner.invoke(app, ["link"])
assert result.exit_code == 1
assert "This directory is already linked to an app." in result.output
@pytest.mark.respx(base_url=settings.base_api_url)
def test_shows_a_message_if_no_teams(
logged_in_cli: None, respx_mock: respx.MockRouter, tmp_path: Path
) -> None:
respx_mock.get("/teams/").mock(return_value=Response(200, json={"data": []}))
with changing_dir(tmp_path):
result = runner.invoke(app, ["link"])
assert result.exit_code == 1
assert "No teams found" in result.output
@pytest.mark.respx(base_url=settings.base_api_url)
def test_shows_a_message_if_no_apps(
logged_in_cli: None, respx_mock: respx.MockRouter, tmp_path: Path
) -> None:
steps = [Keys.ENTER]
respx_mock.get("/teams/").mock(
return_value=Response(
200, json={"data": [{"id": "team-1", "name": "My Team", "slug": "my-team"}]}
)
)
respx_mock.get("/apps/", params={"team_id": "team-1"}).mock(
return_value=Response(200, json={"data": []})
)
with (
changing_dir(tmp_path),
patch("rich_toolkit.container.getchar") as mock_getchar,
):
mock_getchar.side_effect = steps
result = runner.invoke(app, ["link"])
assert result.exit_code == 1
assert "No apps found in this team." in result.output
@pytest.mark.respx(base_url=settings.base_api_url)
def test_links_successfully(
logged_in_cli: None, respx_mock: respx.MockRouter, tmp_path: Path
) -> None:
steps = [Keys.ENTER, Keys.ENTER]
respx_mock.get("/teams/").mock(
return_value=Response(
200, json={"data": [{"id": "team-1", "name": "My Team", "slug": "my-team"}]}
)
)
respx_mock.get("/apps/", params={"team_id": "team-1"}).mock(
return_value=Response(200, json={"data": [{"id": "app-1", "slug": "my-app"}]})
)
with (
changing_dir(tmp_path),
patch("rich_toolkit.container.getchar") as mock_getchar,
):
mock_getchar.side_effect = steps
result = runner.invoke(app, ["link"])
assert result.exit_code == 0
assert "Successfully linked to app" in result.output
assert "my-app" in result.output
config_path = tmp_path / ".fastapicloud" / "cloud.json"
assert config_path.exists()
config = AppConfig.model_validate_json(config_path.read_text())
assert config.app_id == "app-1"
assert config.team_id == "team-1"
@pytest.mark.respx(base_url=settings.base_api_url)
def test_shows_error_on_teams_api_failure(
logged_in_cli: None, respx_mock: respx.MockRouter, tmp_path: Path
) -> None:
respx_mock.get("/teams/").mock(return_value=Response(500))
with changing_dir(tmp_path):
result = runner.invoke(app, ["link"])
assert result.exit_code == 1
assert "Error fetching teams" in result.output
@pytest.mark.respx(base_url=settings.base_api_url)
def test_shows_error_on_apps_api_failure(
logged_in_cli: None, respx_mock: respx.MockRouter, tmp_path: Path
) -> None:
steps = [Keys.ENTER]
respx_mock.get("/teams/").mock(
return_value=Response(
200, json={"data": [{"id": "team-1", "name": "My Team", "slug": "my-team"}]}
)
)
respx_mock.get("/apps/", params={"team_id": "team-1"}).mock(
return_value=Response(500)
)
with (
changing_dir(tmp_path),
patch("rich_toolkit.container.getchar") as mock_getchar,
):
mock_getchar.side_effect = steps
result = runner.invoke(app, ["link"])
assert result.exit_code == 1
assert "Error fetching apps" in result.output
@pytest.mark.respx(base_url=settings.base_api_url)
def test_links_with_multiple_teams_and_apps(
logged_in_cli: None, respx_mock: respx.MockRouter, tmp_path: Path
) -> None:
steps = [Keys.DOWN_ARROW, Keys.ENTER, Keys.DOWN_ARROW, Keys.ENTER]
respx_mock.get("/teams/").mock(
return_value=Response(
200,
json={
"data": [
{"id": "team-1", "name": "Team One", "slug": "team-one"},
{"id": "team-2", "name": "Team Two", "slug": "team-two"},
]
},
)
)
respx_mock.get("/apps/", params={"team_id": "team-2"}).mock(
return_value=Response(
200,
json={
"data": [
{"id": "app-1", "slug": "first-app"},
{"id": "app-2", "slug": "second-app"},
]
},
)
)
with (
changing_dir(tmp_path),
patch("rich_toolkit.container.getchar") as mock_getchar,
):
mock_getchar.side_effect = steps
result = runner.invoke(app, ["link"])
assert result.exit_code == 0
assert "Successfully linked to app" in result.output
assert "second-app" in result.output
config_path = tmp_path / ".fastapicloud" / "cloud.json"
assert config_path.exists()
config = AppConfig.model_validate_json(config_path.read_text())
assert config.app_id == "app-2"
assert config.team_id == "team-2"