Coverage for tests/conftest.py: 100%

65 statements  

« prev     ^ index     » next       coverage.py v7.16.0, created at 2026-11-09 00:25 +0000

1import os 1abcdefg

2import sys 1abcdefg

3import tempfile 1abcdefg

4from collections.abc import Generator 1abcdefg

5from dataclasses import dataclass 1abcdefg

6from pathlib import Path 1abcdefg

7 

8import pytest 1abcdefg

9import respx 1abcdefg

10from typer import rich_utils 1abcdefg

11 

12from fastapi_cloud_cli.config import Settings 1abcdefg

13 

14from .utils import create_jwt_token 1abcdefg

15 

16 

17@pytest.fixture(autouse=True) 1abcdefg

18def unset_env_vars(monkeypatch: pytest.MonkeyPatch) -> Generator[None, None, None]: 1abcdefg

19 """Fixture to unset environment variables that might interfere with tests.""" 

20 monkeypatch.delenv("CI", raising=False) 1abcdefg

21 monkeypatch.delenv("FASTAPI_CLOUD_TOKEN", raising=False) 1abcdefg

22 monkeypatch.setenv("FASTAPI_CLOUD_DISABLE_VERSION_CHECK", "1") 1abcdefg

23 yield 1abcdefg

24 

25 

26@pytest.fixture(autouse=True) 1abcdefg

27def isolated_config_path() -> Generator[Path, None, None]: 1abcdefg

28 with tempfile.TemporaryDirectory() as tmpdir: 1abcdefg

29 os.environ["FASTAPI_CLOUD_CLI_CONFIG_DIR"] = tmpdir 1abcdefg

30 

31 yield Path(tmpdir) 1abcdefg

32 

33 

34@pytest.fixture 1abcdefg

35def temp_auth_config( 1abcdefg

36 isolated_config_path: Path, monkeypatch: pytest.MonkeyPatch 

37) -> Generator[Path, None, None]: 

38 yield isolated_config_path / "auth.json" 1abcdefg

39 

40 

41@pytest.fixture(autouse=True) 1abcdefg

42def reset_syspath() -> Generator[None, None, None]: 1abcdefg

43 initial_python_path = sys.path.copy() 1abcdefg

44 try: 1abcdefg

45 yield 1abcdefg

46 finally: 

47 sys.path = initial_python_path 1abcdefg

48 

49 

50@pytest.fixture(autouse=True, scope="session") 1abcdefg

51def setup_terminal() -> None: 1abcdefg

52 rich_utils.MAX_WIDTH = 3000 1abcdefg

53 rich_utils.FORCE_TERMINAL = False 1abcdefg

54 return 1abcdefg

55 

56 

57@pytest.fixture 1abcdefg

58def settings() -> Settings: 1abcdefg

59 return Settings.get() 1abcdefg

60 

61 

62@pytest.fixture 1abcdefg

63def respx_mock(settings: Settings) -> Generator[respx.MockRouter, None, None]: 1abcdefg

64 with respx.mock(base_url=settings.base_api_url) as mock_router: 1abcdefg

65 yield mock_router 1abcdefg

66 

67 

68@pytest.fixture 1abcdefg

69def logged_in_cli(temp_auth_config: Path) -> Generator[None, None, None]: 1abcdefg

70 valid_token = create_jwt_token({"sub": "test_user_12345"}) 1abcdefg

71 

72 temp_auth_config.write_text(f'{{"access_token": "{valid_token}"}}') 1abcdefg

73 

74 yield 1abcdefg

75 

76 

77@pytest.fixture 1abcdefg

78def logged_out_cli(temp_auth_config: Path) -> Generator[None, None, None]: 1abcdefg

79 assert not temp_auth_config.exists() 1abcdefg

80 

81 yield 1abcdefg

82 

83 

84@dataclass 1abcdefg

85class ConfiguredApp: 1abcdefg

86 app_id: str 1abcdefg

87 team_id: str 1abcdefg

88 path: Path 1abcdefg

89 

90 

91@pytest.fixture 1abcdefg

92def configured_app(tmp_path: Path) -> ConfiguredApp: 1abcdefg

93 app_id = "123" 1abcdefg

94 team_id = "456" 1abcdefg

95 

96 config_path = tmp_path / ".fastapicloud" / "cloud.json" 1abcdefg

97 

98 config_path.parent.mkdir(parents=True, exist_ok=True) 1abcdefg

99 config_path.write_text(f'{{"app_id": "{app_id}", "team_id": "{team_id}"}}') 1abcdefg

100 

101 return ConfiguredApp(app_id=app_id, team_id=team_id, path=tmp_path) 1abcdefg