Skip to content

Commit 2387cda

Browse files
committed
✅ Refactor tests
1 parent 342dcd3 commit 2387cda

3 files changed

Lines changed: 59 additions & 8 deletions

File tree

tests/conftest.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@ def setup_terminal() -> None:
2525

2626

2727
@pytest.fixture
28-
def logged_in_cli() -> Generator[None, None, None]:
29-
with patch("fastapi_cloud_cli.utils.auth.get_auth_token", return_value=True):
30-
yield
28+
def logged_in_cli(temp_auth_config: Path) -> Generator[None, None, None]:
29+
temp_auth_config.write_text('{"access_token": "test_token_12345"}')
30+
31+
yield
3132

3233

3334
@pytest.fixture
34-
def logged_out_cli() -> Generator[None, None, None]:
35-
with patch("fastapi_cloud_cli.utils.auth.get_auth_token", return_value=None):
36-
yield
35+
def logged_out_cli(temp_auth_config: Path) -> Generator[None, None, None]:
36+
if temp_auth_config.exists():
37+
temp_auth_config.unlink()
38+
39+
yield
3740

3841

3942
@dataclass
@@ -54,3 +57,13 @@ def configured_app(tmp_path: Path) -> ConfiguredApp:
5457
config_path.write_text(f'{{"app_id": "{app_id}", "team_id": "{team_id}"}}')
5558

5659
return ConfiguredApp(app_id=app_id, team_id=team_id, path=tmp_path)
60+
61+
62+
@pytest.fixture
63+
def temp_auth_config(tmp_path: Path) -> Generator[Path, None, None]:
64+
"""Provides a temporary auth config setup for testing file operations."""
65+
66+
with patch(
67+
"fastapi_cloud_cli.utils.config.get_config_folder", return_value=tmp_path
68+
):
69+
yield tmp_path / "auth.json"

tests/test_cli_login.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_shows_a_message_if_something_is_wrong(respx_mock: respx.MockRouter) ->
3333

3434

3535
@pytest.mark.respx(base_url=settings.base_api_url)
36-
def test_full_login(respx_mock: respx.MockRouter) -> None:
36+
def test_full_login(respx_mock: respx.MockRouter, temp_auth_config: Path) -> None:
3737
with patch("fastapi_cloud_cli.commands.login.typer.launch") as mock_open:
3838
respx_mock.post(
3939
"/login/device/authorization", data={"client_id": settings.client_id}
@@ -55,11 +55,18 @@ def test_full_login(respx_mock: respx.MockRouter) -> None:
5555
"client_id": settings.client_id,
5656
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
5757
},
58-
).mock(return_value=Response(200, json={"access_token": "1234"}))
58+
).mock(return_value=Response(200, json={"access_token": "test_token_1234"}))
59+
60+
# Verify no auth file exists before login
61+
assert not temp_auth_config.exists()
5962

6063
result = runner.invoke(app, ["login"])
6164

6265
assert result.exit_code == 0
6366
assert mock_open.called
6467
assert mock_open.call_args.args == ("http://test.com",)
6568
assert "Now you are logged in!" in result.output
69+
70+
# Verify auth file was created with correct content
71+
assert temp_auth_config.exists()
72+
assert '"access_token":"test_token_1234"' in temp_auth_config.read_text()

tests/test_cli_logout.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from pathlib import Path
2+
3+
from typer.testing import CliRunner
4+
5+
from fastapi_cloud_cli.cli import app
6+
7+
runner = CliRunner()
8+
9+
10+
def test_logout_with_existing_auth_file(temp_auth_config: Path) -> None:
11+
temp_auth_config.write_text('{"access_token": "test_token"}')
12+
13+
assert temp_auth_config.exists()
14+
15+
result = runner.invoke(app, ["logout"])
16+
17+
assert result.exit_code == 0
18+
assert "You are now logged out! 🚀" in result.output
19+
20+
assert not temp_auth_config.exists()
21+
22+
23+
def test_logout_with_no_auth_file(temp_auth_config: Path) -> None:
24+
assert not temp_auth_config.exists()
25+
26+
result = runner.invoke(app, ["logout"])
27+
28+
assert result.exit_code == 0
29+
assert "You are now logged out! 🚀" in result.output
30+
31+
assert not temp_auth_config.exists()

0 commit comments

Comments
 (0)