Skip to content

Commit 0f437a0

Browse files
committed
fix texts
1 parent aa01bf5 commit 0f437a0

2 files changed

Lines changed: 85 additions & 45 deletions

File tree

tests/conftest.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,15 @@ def isolated_session():
153153
def basic_session(isolated_session, test_host, api_token):
154154
"""Basic APISession with host and token configured"""
155155
isolated_session.set_cloud(test_host)
156-
isolated_session.set_api_token(api_token)
156+
# Mock token validation to prevent HTTP calls
157+
with patch("mistapi.__api_session.requests.get") as mock_get:
158+
mock_response = Mock()
159+
mock_response.status_code = 200
160+
mock_response.json.return_value = {
161+
"privileges": [{"scope": "org", "org_id": "test-org", "role": "admin"}]
162+
}
163+
mock_get.return_value = mock_response
164+
isolated_session.set_api_token(api_token)
157165
return isolated_session
158166

159167

tests/unit/test_api_session.py

Lines changed: 76 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"""
88

99
import os
10-
from unittest.mock import patch
10+
from unittest.mock import Mock, patch
1111

1212
import pytest
1313

@@ -40,52 +40,78 @@ def test_initialisation_with_parameters(self, api_token, test_host) -> None:
4040
# Mock environment to ensure clean state
4141
with patch.dict(os.environ, {}, clear=True):
4242
with patch("mistapi.__api_session.requests.session"):
43-
# Act
44-
session = APISession(
45-
email=email,
46-
password=password,
47-
apitoken=api_token,
48-
host=test_host,
49-
console_log_level=30,
50-
logging_log_level=20,
51-
show_cli_notif=False,
52-
https_proxy="http://proxy:8080",
53-
)
54-
55-
# Assert
56-
assert session.email == email
57-
assert session._password == password
58-
assert session._apitoken == [api_token] # Should be converted to list
59-
assert session._cloud_uri == test_host
60-
assert session._console_log_level == 30
61-
assert session._logging_log_level == 20
62-
assert session._show_cli_notif is False
63-
assert session._proxies["https"] == "http://proxy:8080"
43+
with patch("mistapi.__api_session.requests.get") as mock_get:
44+
# Mock successful token validation
45+
mock_response = Mock()
46+
mock_response.status_code = 200
47+
mock_response.json.return_value = {
48+
"privileges": [
49+
{"scope": "org", "org_id": "test-org", "role": "admin"}
50+
]
51+
}
52+
mock_get.return_value = mock_response
53+
54+
# Act
55+
session = APISession(
56+
email=email,
57+
password=password,
58+
apitoken=api_token,
59+
host=test_host,
60+
console_log_level=30,
61+
logging_log_level=20,
62+
show_cli_notif=False,
63+
https_proxy="http://proxy:8080",
64+
)
65+
66+
# Assert
67+
assert session.email == email
68+
assert session._password == password
69+
assert session._apitoken == [
70+
api_token
71+
] # Should be converted to list
72+
assert session._cloud_uri == test_host
73+
assert session._console_log_level == 30
74+
assert session._logging_log_level == 20
75+
assert session._show_cli_notif is False
76+
assert session._proxies["https"] == "http://proxy:8080"
6477

6578
def test_initialisation_with_env_file(self, tmp_env_file) -> None:
6679
"""Test APISession initialisation using environment file"""
6780
# Mock requests.session and ensure clean environment
6881
with patch("mistapi.__api_session.requests.session"):
69-
with patch.dict(
70-
os.environ,
71-
{
72-
"MIST_HOST": "api.mist.com",
73-
"MIST_APITOKEN": "abcdef0123456789abcdef0123456789abcdef01",
74-
"MIST_USER": "test@example.com",
75-
"MIST_PASSWORD": "test_password",
76-
"CONSOLE_LOG_LEVEL": "30",
77-
"LOGGING_LOG_LEVEL": "20",
78-
"HTTPS_PROXY": "http://proxy:8080",
79-
},
80-
):
81-
# Act
82-
session = APISession(env_file=tmp_env_file)
83-
84-
# Assert
85-
assert session._cloud_uri == "api.mist.com"
86-
assert session._apitoken == ["abcdef0123456789abcdef0123456789abcdef01"]
87-
assert session.email == "test@example.com"
88-
assert session._password == "test_password"
82+
with patch("mistapi.__api_session.requests.get") as mock_get:
83+
# Mock successful token validation
84+
mock_response = Mock()
85+
mock_response.status_code = 200
86+
mock_response.json.return_value = {
87+
"privileges": [
88+
{"scope": "org", "org_id": "test-org", "role": "admin"}
89+
]
90+
}
91+
mock_get.return_value = mock_response
92+
93+
with patch.dict(
94+
os.environ,
95+
{
96+
"MIST_HOST": "api.mist.com",
97+
"MIST_APITOKEN": "abcdef0123456789abcdef0123456789abcdef01",
98+
"MIST_USER": "test@example.com",
99+
"MIST_PASSWORD": "test_password",
100+
"CONSOLE_LOG_LEVEL": "30",
101+
"LOGGING_LOG_LEVEL": "20",
102+
"HTTPS_PROXY": "http://proxy:8080",
103+
},
104+
):
105+
# Act
106+
session = APISession(env_file=tmp_env_file)
107+
108+
# Assert
109+
assert session._cloud_uri == "api.mist.com"
110+
assert session._apitoken == [
111+
"abcdef0123456789abcdef0123456789abcdef01"
112+
]
113+
assert session.email == "test@example.com"
114+
assert session._password == "test_password"
89115

90116

91117
class TestCloudConfiguration:
@@ -187,7 +213,10 @@ def test_set_password_interactive(self, mock_getpass, isolated_session) -> None:
187213
def test_set_single_api_token(self, isolated_session, api_token) -> None:
188214
"""Test setting a single API token"""
189215
# Arrange
190-
with patch.object(isolated_session, "_check_api_tokens", return_value=True):
216+
isolated_session.set_cloud("api.mist.com") # Set cloud to enable validation
217+
with patch.object(
218+
isolated_session, "_check_api_tokens", return_value=[api_token]
219+
):
191220
# Act
192221
isolated_session.set_api_token(api_token)
193222

@@ -201,7 +230,10 @@ def test_set_multiple_api_tokens(self, isolated_session) -> None:
201230
tokens = "token1, token2, token3"
202231
expected_tokens = ["token1", "token2", "token3"]
203232

204-
with patch.object(isolated_session, "_check_api_tokens", return_value=True):
233+
isolated_session.set_cloud("api.mist.com") # Set cloud to enable validation
234+
with patch.object(
235+
isolated_session, "_check_api_tokens", return_value=expected_tokens
236+
):
205237
# Act
206238
isolated_session.set_api_token(tokens)
207239

0 commit comments

Comments
 (0)