-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_cli_login.py
More file actions
222 lines (180 loc) · 7.58 KB
/
test_cli_login.py
File metadata and controls
222 lines (180 loc) · 7.58 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import time
from pathlib import Path
from unittest.mock import patch
import httpx
import pytest
import respx
from httpx import Response
from typer.testing import CliRunner
from fastapi_cloud_cli.cli import app
from fastapi_cloud_cli.config import Settings
from tests.utils import create_jwt_token
runner = CliRunner()
settings = Settings.get()
assets_path = Path(__file__).parent / "assets"
@pytest.mark.respx(base_url=settings.base_api_url)
def test_shows_a_message_if_something_is_wrong(
logged_out_cli: None, respx_mock: respx.MockRouter
) -> None:
with patch("fastapi_cloud_cli.commands.login.typer.launch") as mock_open:
respx_mock.post(
"/login/device/authorization", data={"client_id": settings.client_id}
).mock(return_value=Response(500))
result = runner.invoke(app, ["login"])
assert result.exit_code == 1
assert (
"Something went wrong while contacting the FastAPI Cloud server."
in result.output
)
assert not mock_open.called
@pytest.mark.respx(base_url=settings.base_api_url)
def test_full_login(respx_mock: respx.MockRouter, temp_auth_config: Path) -> None:
with patch("fastapi_cloud_cli.commands.login.typer.launch") as mock_open:
respx_mock.post(
"/login/device/authorization", data={"client_id": settings.client_id}
).mock(
return_value=Response(
200,
json={
"verification_uri_complete": "http://test.com",
"verification_uri": "http://test.com",
"user_code": "1234",
"device_code": "5678",
},
)
)
respx_mock.post(
"/login/device/token",
data={
"device_code": "5678",
"client_id": settings.client_id,
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
},
).mock(return_value=Response(200, json={"access_token": "test_token_1234"}))
# Verify no auth file exists before login
assert not temp_auth_config.exists()
result = runner.invoke(app, ["login"])
assert result.exit_code == 0
assert mock_open.called
assert mock_open.call_args.args == ("http://test.com",)
assert "Now you are logged in!" in result.output
# Verify auth file was created with correct content
assert temp_auth_config.exists()
assert '"access_token":"test_token_1234"' in temp_auth_config.read_text()
@pytest.mark.respx(base_url=settings.base_api_url)
def test_fetch_access_token_success_immediately(respx_mock: respx.MockRouter) -> None:
from fastapi_cloud_cli.commands.login import _fetch_access_token
from fastapi_cloud_cli.utils.api import APIClient
respx_mock.post(
"/login/device/token",
data={
"device_code": "test_device_code",
"client_id": settings.client_id,
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
},
).mock(return_value=Response(200, json={"access_token": "test_token_success"}))
with APIClient() as client:
access_token = _fetch_access_token(client, "test_device_code", 5)
assert access_token == "test_token_success"
@pytest.mark.respx(base_url=settings.base_api_url)
def test_fetch_access_token_authorization_pending_then_success(
respx_mock: respx.MockRouter,
) -> None:
from fastapi_cloud_cli.commands.login import _fetch_access_token
from fastapi_cloud_cli.utils.api import APIClient
# First call returns authorization pending, second call succeeds
respx_mock.post(
"/login/device/token",
data={
"device_code": "test_device_code",
"client_id": settings.client_id,
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
},
).mock(
side_effect=[
Response(400, json={"error": "authorization_pending"}),
Response(200, json={"access_token": "test_token_after_pending"}),
]
)
with patch("fastapi_cloud_cli.commands.login.time.sleep") as mock_sleep:
with APIClient() as client:
access_token = _fetch_access_token(client, "test_device_code", 3)
assert access_token == "test_token_after_pending"
mock_sleep.assert_called_once_with(3)
@pytest.mark.respx(base_url=settings.base_api_url)
def test_fetch_access_token_handles_400_error_not_authorization_pending(
respx_mock: respx.MockRouter,
) -> None:
from fastapi_cloud_cli.commands.login import _fetch_access_token
from fastapi_cloud_cli.utils.api import APIClient
respx_mock.post(
"/login/device/token",
data={
"device_code": "test_device_code",
"client_id": settings.client_id,
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
},
).mock(return_value=Response(400, json={"error": "access_denied"}))
with APIClient() as client:
with pytest.raises(httpx.HTTPStatusError):
_fetch_access_token(client, "test_device_code", 5)
@pytest.mark.respx(base_url=settings.base_api_url)
def test_fetch_access_token_handles_500_error(respx_mock: respx.MockRouter) -> None:
from fastapi_cloud_cli.commands.login import _fetch_access_token
from fastapi_cloud_cli.utils.api import APIClient
respx_mock.post(
"/login/device/token",
data={
"device_code": "test_device_code",
"client_id": settings.client_id,
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
},
).mock(return_value=Response(500))
with APIClient() as client:
with pytest.raises(httpx.HTTPStatusError):
_fetch_access_token(client, "test_device_code", 5)
@pytest.mark.respx(base_url=settings.base_api_url)
def test_notify_already_logged_in_user(
respx_mock: respx.MockRouter, logged_in_cli: None
) -> None:
result = runner.invoke(app, ["login"])
assert result.exit_code == 0
assert "You are already logged in." in result.output
assert (
"Run fastapi cloud logout first if you want to switch accounts."
in result.output
)
@pytest.mark.respx(base_url=settings.base_api_url)
def test_notify_expired_token_user(
respx_mock: respx.MockRouter, temp_auth_config: Path
) -> None:
past_exp = int(time.time()) - 3600
expired_token = create_jwt_token({"sub": "test_user_12345", "exp": past_exp})
temp_auth_config.write_text(f'{{"access_token": "{expired_token}"}}')
with patch("fastapi_cloud_cli.commands.login.typer.launch") as mock_open:
respx_mock.post(
"/login/device/authorization", data={"client_id": settings.client_id}
).mock(
return_value=Response(
200,
json={
"verification_uri_complete": "http://test.com",
"verification_uri": "http://test.com",
"user_code": "1234",
"device_code": "5678",
},
)
)
respx_mock.post(
"/login/device/token",
data={
"device_code": "5678",
"client_id": settings.client_id,
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
},
).mock(return_value=Response(200, json={"access_token": "new_token_1234"}))
result = runner.invoke(app, ["login"])
assert result.exit_code == 0
assert "Your session has expired. Logging in again..." in result.output
assert "Now you are logged in!" in result.output
assert mock_open.called