|
| 1 | +from pathlib import Path |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +import pytest |
| 5 | +import respx |
| 6 | +from httpx import Response |
| 7 | +from typer.testing import CliRunner |
| 8 | + |
| 9 | +from fastapi_cloud_cli.cli import cloud_app as app |
| 10 | +from fastapi_cloud_cli.config import Settings |
| 11 | +from fastapi_cloud_cli.utils.apps import AppConfig |
| 12 | +from tests.conftest import ConfiguredApp |
| 13 | +from tests.utils import Keys, changing_dir |
| 14 | + |
| 15 | +runner = CliRunner() |
| 16 | +settings = Settings.get() |
| 17 | + |
| 18 | + |
| 19 | +def test_shows_a_message_if_not_logged_in(logged_out_cli: None) -> None: |
| 20 | + result = runner.invoke(app, ["link"]) |
| 21 | + |
| 22 | + assert result.exit_code == 1 |
| 23 | + assert "You need to be logged in to link an app." in result.output |
| 24 | + |
| 25 | + |
| 26 | +def test_shows_a_message_if_already_linked( |
| 27 | + logged_in_cli: None, configured_app: ConfiguredApp |
| 28 | +) -> None: |
| 29 | + with changing_dir(configured_app.path): |
| 30 | + result = runner.invoke(app, ["link"]) |
| 31 | + |
| 32 | + assert result.exit_code == 1 |
| 33 | + assert "This directory is already linked to an app." in result.output |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.respx(base_url=settings.base_api_url) |
| 37 | +def test_shows_a_message_if_no_teams( |
| 38 | + logged_in_cli: None, respx_mock: respx.MockRouter, tmp_path: Path |
| 39 | +) -> None: |
| 40 | + respx_mock.get("/teams/").mock(return_value=Response(200, json={"data": []})) |
| 41 | + |
| 42 | + with changing_dir(tmp_path): |
| 43 | + result = runner.invoke(app, ["link"]) |
| 44 | + |
| 45 | + assert result.exit_code == 1 |
| 46 | + assert "No teams found" in result.output |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.respx(base_url=settings.base_api_url) |
| 50 | +def test_shows_a_message_if_no_apps( |
| 51 | + logged_in_cli: None, respx_mock: respx.MockRouter, tmp_path: Path |
| 52 | +) -> None: |
| 53 | + steps = [Keys.ENTER] |
| 54 | + |
| 55 | + respx_mock.get("/teams/").mock( |
| 56 | + return_value=Response( |
| 57 | + 200, json={"data": [{"id": "team-1", "name": "My Team", "slug": "my-team"}]} |
| 58 | + ) |
| 59 | + ) |
| 60 | + respx_mock.get("/apps/", params={"team_id": "team-1"}).mock( |
| 61 | + return_value=Response(200, json={"data": []}) |
| 62 | + ) |
| 63 | + |
| 64 | + with ( |
| 65 | + changing_dir(tmp_path), |
| 66 | + patch("rich_toolkit.container.getchar") as mock_getchar, |
| 67 | + ): |
| 68 | + mock_getchar.side_effect = steps |
| 69 | + result = runner.invoke(app, ["link"]) |
| 70 | + |
| 71 | + assert result.exit_code == 1 |
| 72 | + assert "No apps found in this team." in result.output |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.respx(base_url=settings.base_api_url) |
| 76 | +def test_links_successfully( |
| 77 | + logged_in_cli: None, respx_mock: respx.MockRouter, tmp_path: Path |
| 78 | +) -> None: |
| 79 | + steps = [Keys.ENTER, Keys.ENTER] |
| 80 | + |
| 81 | + respx_mock.get("/teams/").mock( |
| 82 | + return_value=Response( |
| 83 | + 200, json={"data": [{"id": "team-1", "name": "My Team", "slug": "my-team"}]} |
| 84 | + ) |
| 85 | + ) |
| 86 | + respx_mock.get("/apps/", params={"team_id": "team-1"}).mock( |
| 87 | + return_value=Response(200, json={"data": [{"id": "app-1", "slug": "my-app"}]}) |
| 88 | + ) |
| 89 | + |
| 90 | + with ( |
| 91 | + changing_dir(tmp_path), |
| 92 | + patch("rich_toolkit.container.getchar") as mock_getchar, |
| 93 | + ): |
| 94 | + mock_getchar.side_effect = steps |
| 95 | + result = runner.invoke(app, ["link"]) |
| 96 | + |
| 97 | + assert result.exit_code == 0 |
| 98 | + assert "Successfully linked to app" in result.output |
| 99 | + assert "my-app" in result.output |
| 100 | + |
| 101 | + config_path = tmp_path / ".fastapicloud" / "cloud.json" |
| 102 | + assert config_path.exists() |
| 103 | + config = AppConfig.model_validate_json(config_path.read_text()) |
| 104 | + assert config.app_id == "app-1" |
| 105 | + assert config.team_id == "team-1" |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.respx(base_url=settings.base_api_url) |
| 109 | +def test_shows_error_on_teams_api_failure( |
| 110 | + logged_in_cli: None, respx_mock: respx.MockRouter, tmp_path: Path |
| 111 | +) -> None: |
| 112 | + respx_mock.get("/teams/").mock(return_value=Response(500)) |
| 113 | + |
| 114 | + with changing_dir(tmp_path): |
| 115 | + result = runner.invoke(app, ["link"]) |
| 116 | + |
| 117 | + assert result.exit_code == 1 |
| 118 | + assert "Error fetching teams" in result.output |
| 119 | + |
| 120 | + |
| 121 | +@pytest.mark.respx(base_url=settings.base_api_url) |
| 122 | +def test_shows_error_on_apps_api_failure( |
| 123 | + logged_in_cli: None, respx_mock: respx.MockRouter, tmp_path: Path |
| 124 | +) -> None: |
| 125 | + steps = [Keys.ENTER] |
| 126 | + |
| 127 | + respx_mock.get("/teams/").mock( |
| 128 | + return_value=Response( |
| 129 | + 200, json={"data": [{"id": "team-1", "name": "My Team", "slug": "my-team"}]} |
| 130 | + ) |
| 131 | + ) |
| 132 | + respx_mock.get("/apps/", params={"team_id": "team-1"}).mock( |
| 133 | + return_value=Response(500) |
| 134 | + ) |
| 135 | + |
| 136 | + with ( |
| 137 | + changing_dir(tmp_path), |
| 138 | + patch("rich_toolkit.container.getchar") as mock_getchar, |
| 139 | + ): |
| 140 | + mock_getchar.side_effect = steps |
| 141 | + result = runner.invoke(app, ["link"]) |
| 142 | + |
| 143 | + assert result.exit_code == 1 |
| 144 | + assert "Error fetching apps" in result.output |
| 145 | + |
| 146 | + |
| 147 | +@pytest.mark.respx(base_url=settings.base_api_url) |
| 148 | +def test_links_with_multiple_teams_and_apps( |
| 149 | + logged_in_cli: None, respx_mock: respx.MockRouter, tmp_path: Path |
| 150 | +) -> None: |
| 151 | + steps = [Keys.DOWN_ARROW, Keys.ENTER, Keys.DOWN_ARROW, Keys.ENTER] |
| 152 | + |
| 153 | + respx_mock.get("/teams/").mock( |
| 154 | + return_value=Response( |
| 155 | + 200, |
| 156 | + json={ |
| 157 | + "data": [ |
| 158 | + {"id": "team-1", "name": "Team One", "slug": "team-one"}, |
| 159 | + {"id": "team-2", "name": "Team Two", "slug": "team-two"}, |
| 160 | + ] |
| 161 | + }, |
| 162 | + ) |
| 163 | + ) |
| 164 | + respx_mock.get("/apps/", params={"team_id": "team-2"}).mock( |
| 165 | + return_value=Response( |
| 166 | + 200, |
| 167 | + json={ |
| 168 | + "data": [ |
| 169 | + {"id": "app-1", "slug": "first-app"}, |
| 170 | + {"id": "app-2", "slug": "second-app"}, |
| 171 | + ] |
| 172 | + }, |
| 173 | + ) |
| 174 | + ) |
| 175 | + |
| 176 | + with ( |
| 177 | + changing_dir(tmp_path), |
| 178 | + patch("rich_toolkit.container.getchar") as mock_getchar, |
| 179 | + ): |
| 180 | + mock_getchar.side_effect = steps |
| 181 | + result = runner.invoke(app, ["link"]) |
| 182 | + |
| 183 | + assert result.exit_code == 0 |
| 184 | + assert "Successfully linked to app" in result.output |
| 185 | + assert "second-app" in result.output |
| 186 | + |
| 187 | + config_path = tmp_path / ".fastapicloud" / "cloud.json" |
| 188 | + assert config_path.exists() |
| 189 | + config = AppConfig.model_validate_json(config_path.read_text()) |
| 190 | + assert config.app_id == "app-2" |
| 191 | + assert config.team_id == "team-2" |
0 commit comments