Skip to content

Commit e7c0ebb

Browse files
committed
✅ Refactor tests
1 parent 342dcd3 commit e7c0ebb

3 files changed

Lines changed: 62 additions & 8 deletions

File tree

tests/conftest.py

Lines changed: 16 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) -> Generator[None, None, None]:
29+
# Create auth file with test token
30+
temp_auth_config.write_text('{"access_token": "test_token_12345"}')
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) -> Generator[None, None, None]:
36+
# Ensure no auth file exists
37+
if temp_auth_config.exists():
38+
temp_auth_config.unlink()
39+
yield
3740

3841

3942
@dataclass
@@ -54,3 +57,10 @@ 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+
with patch("fastapi_cloud_cli.utils.config.get_config_folder", return_value=tmp_path):
66+
yield tmp_path / "auth.json"

tests/test_cli_login.py

Lines changed: 10 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) -> 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,19 @@ 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+
auth_content = temp_auth_config.read_text()
73+
assert '"access_token":"test_token_1234"' in auth_content

tests/test_cli_logout.py

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

0 commit comments

Comments
 (0)