Skip to content

Commit e46c276

Browse files
committed
refactor: update tests to set no_auth configuration and improve proxy handling
- Adjust test cases in `test_check.py` and `test_check_multi_account.py` to set `no_auth` to True, ensuring consistent behavior during proxy checks. - Enhance proxy request assertions to include both HTTP and HTTPS protocols. - Update GCP provider tests to mock `get_client` for better isolation and reliability in tests. - Modify assertions in `test_gcp_start` to use `ANY` for more flexible verification of deployment calls.
1 parent ebb2fad commit e46c276

File tree

4 files changed

+65
-47
lines changed

4 files changed

+65
-47
lines changed

tests/test_check.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,21 @@ def test_check_alive_success(mock_get):
114114
mock_response.status_code = 200
115115
mock_get.return_value = mock_response
116116

117-
# Execute
118-
result = check_alive("10.0.0.1")
119-
120-
# Verify
121-
assert result is True
122-
mock_get.assert_called_once_with(
123-
"http://ipecho.net/plain",
124-
proxies={'http': "http://10.0.0.1:8899"},
125-
timeout=10
126-
)
117+
# Set no_auth to True
118+
original_no_auth = settings.config["no_auth"]
119+
settings.config["no_auth"] = True
120+
try:
121+
# Execute
122+
result = check_alive("10.0.0.1")
123+
# Verify
124+
assert result is True
125+
mock_get.assert_called_once_with(
126+
"http://ipecho.net/plain",
127+
proxies={'http': "http://10.0.0.1:8899", 'https': "http://10.0.0.1:8899"},
128+
timeout=10
129+
)
130+
finally:
131+
settings.config["no_auth"] = original_no_auth
127132

128133
@patch('cloudproxy.check.requests.get')
129134
def test_check_alive_auth_required(mock_get):

tests/test_check_multi_account.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,27 @@ def test_check_alive_for_different_instances(mock_requests_get, mock_proxy_data)
7272
mock_response.status_code = 200
7373
mock_requests_get.return_value = mock_response
7474

75-
# Test check_alive for each proxy
76-
for proxy in mock_proxy_data:
77-
# Call function under test
78-
result = check_alive(proxy["ip"])
79-
80-
# Verify result
81-
assert result is True, f"check_alive for {proxy['ip']} from {proxy['provider']}/{proxy['instance']} should return True"
82-
83-
# Verify correct proxy was used in the request
84-
expected_proxy = {'http': f'http://{proxy["ip"]}:8899'}
85-
mock_requests_get.assert_called_with(
86-
"http://ipecho.net/plain",
87-
proxies=expected_proxy,
88-
timeout=10
89-
)
90-
91-
# Reset mock for next iteration
92-
mock_requests_get.reset_mock()
75+
# Set no_auth to True
76+
original_no_auth = settings.config["no_auth"]
77+
settings.config["no_auth"] = True
78+
try:
79+
# Test check_alive for each proxy
80+
for proxy in mock_proxy_data:
81+
# Call function under test
82+
result = check_alive(proxy["ip"])
83+
# Verify result
84+
assert result is True, f"check_alive for {proxy['ip']} from {proxy['provider']}/{proxy['instance']} should return True"
85+
# Verify correct proxy was used in the request
86+
expected_proxy = {'http': f'http://{proxy["ip"]}:8899', 'https': f'http://{proxy["ip"]}:8899'}
87+
mock_requests_get.assert_called_with(
88+
"http://ipecho.net/plain",
89+
proxies=expected_proxy,
90+
timeout=10
91+
)
92+
# Reset mock for next iteration
93+
mock_requests_get.reset_mock()
94+
finally:
95+
settings.config["no_auth"] = original_no_auth
9396

9497

9598
@patch('cloudproxy.check.requests_retry_session')

tests/test_providers_gcp_functions.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ def mock_gcp_environment():
3636
)
3737
from cloudproxy.providers.settings import config
3838

39+
@patch('cloudproxy.providers.gcp.functions.get_client')
3940
@patch('uuid.uuid4')
40-
def test_create_proxy(mock_uuid, mock_gcp_environment):
41+
def test_create_proxy(mock_uuid, mock_get_client, mock_gcp_environment):
42+
mock_get_client.return_value = (None, mock_gcp_environment)
4143
"""Test create_proxy function"""
4244
# Setup
4345
mock_uuid.return_value = "test-uuid"
@@ -78,8 +80,9 @@ def test_create_proxy(mock_uuid, mock_gcp_environment):
7880
assert body["networkInterfaces"][0]["accessConfigs"][0]["type"] == "ONE_TO_ONE_NAT"
7981
assert "startup-script" in body["metadata"]["items"][0]["key"]
8082

81-
def test_delete_proxy_success(mock_gcp_environment):
82-
"""Test delete_proxy function successful case"""
83+
@patch('cloudproxy.providers.gcp.functions.get_client')
84+
def test_delete_proxy_success(mock_get_client, mock_gcp_environment):
85+
mock_get_client.return_value = (None, mock_gcp_environment)
8386
# Setup
8487
mock_compute = mock_gcp_environment
8588
name = "cloudproxy-123"
@@ -101,8 +104,9 @@ def test_delete_proxy_success(mock_gcp_environment):
101104
)
102105
assert result == {"status": "RUNNING"}
103106

104-
def test_delete_proxy_http_error(mock_gcp_environment):
105-
"""Test delete_proxy function when HTTP error occurs"""
107+
@patch('cloudproxy.providers.gcp.functions.get_client')
108+
def test_delete_proxy_http_error(mock_get_client, mock_gcp_environment):
109+
mock_get_client.return_value = (None, mock_gcp_environment)
106110
# Setup
107111
mock_compute = mock_gcp_environment
108112
name = "cloudproxy-123"
@@ -121,8 +125,9 @@ def test_delete_proxy_http_error(mock_gcp_environment):
121125
# Verify
122126
assert result is None
123127

124-
def test_stop_proxy_success(mock_gcp_environment):
125-
"""Test stop_proxy function successful case"""
128+
@patch('cloudproxy.providers.gcp.functions.get_client')
129+
def test_stop_proxy_success(mock_get_client, mock_gcp_environment):
130+
mock_get_client.return_value = (None, mock_gcp_environment)
126131
# Setup
127132
mock_compute = mock_gcp_environment
128133
name = "cloudproxy-123"
@@ -144,8 +149,9 @@ def test_stop_proxy_success(mock_gcp_environment):
144149
)
145150
assert result == {"status": "STOPPING"}
146151

147-
def test_stop_proxy_http_error(mock_gcp_environment):
148-
"""Test stop_proxy function when HTTP error occurs"""
152+
@patch('cloudproxy.providers.gcp.functions.get_client')
153+
def test_stop_proxy_http_error(mock_get_client, mock_gcp_environment):
154+
mock_get_client.return_value = (None, mock_gcp_environment)
149155
# Setup
150156
mock_compute = mock_gcp_environment
151157
name = "cloudproxy-123"
@@ -164,8 +170,9 @@ def test_stop_proxy_http_error(mock_gcp_environment):
164170
# Verify
165171
assert result is None
166172

167-
def test_start_proxy_success(mock_gcp_environment):
168-
"""Test start_proxy function successful case"""
173+
@patch('cloudproxy.providers.gcp.functions.get_client')
174+
def test_start_proxy_success(mock_get_client, mock_gcp_environment):
175+
mock_get_client.return_value = (None, mock_gcp_environment)
169176
# Setup
170177
mock_compute = mock_gcp_environment
171178
name = "cloudproxy-123"
@@ -187,8 +194,9 @@ def test_start_proxy_success(mock_gcp_environment):
187194
)
188195
assert result == {"status": "RUNNING"}
189196

190-
def test_start_proxy_http_error(mock_gcp_environment):
191-
"""Test start_proxy function when HTTP error occurs"""
197+
@patch('cloudproxy.providers.gcp.functions.get_client')
198+
def test_start_proxy_http_error(mock_get_client, mock_gcp_environment):
199+
mock_get_client.return_value = (None, mock_gcp_environment)
192200
# Setup
193201
mock_compute = mock_gcp_environment
194202
name = "cloudproxy-123"
@@ -207,8 +215,9 @@ def test_start_proxy_http_error(mock_gcp_environment):
207215
# Verify
208216
assert result is None
209217

210-
def test_list_instances_with_instances(mock_gcp_environment):
211-
"""Test list_instances function when instances exist"""
218+
@patch('cloudproxy.providers.gcp.functions.get_client')
219+
def test_list_instances_with_instances(mock_get_client, mock_gcp_environment):
220+
mock_get_client.return_value = (None, mock_gcp_environment)
212221
# Setup
213222
mock_compute = mock_gcp_environment
214223

@@ -238,8 +247,9 @@ def test_list_instances_with_instances(mock_gcp_environment):
238247
assert len(result) == 1
239248
assert result[0]["name"] == "cloudproxy-123"
240249

241-
def test_list_instances_no_instances(mock_gcp_environment):
242-
"""Test list_instances function when no instances exist"""
250+
@patch('cloudproxy.providers.gcp.functions.get_client')
251+
def test_list_instances_no_instances(mock_get_client, mock_gcp_environment):
252+
mock_get_client.return_value = (None, mock_gcp_environment)
243253
# Setup
244254
mock_compute = mock_gcp_environment
245255

tests/test_providers_gcp_main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pytest
2-
from unittest.mock import patch, Mock
2+
from unittest.mock import patch, Mock, ANY
33
import datetime
44
from datetime import timezone
55
import itertools
@@ -336,7 +336,7 @@ def test_gcp_start(mock_gcp_deployment, mock_gcp_check_alive, mock_gcp_check_sto
336336
# Verify
337337
mock_gcp_check_delete.assert_called_once()
338338
mock_gcp_check_stop.assert_called_once()
339-
mock_gcp_deployment.assert_called_once_with(3)
339+
mock_gcp_deployment.assert_called_once_with(3, ANY)
340340
mock_gcp_check_alive.assert_called_once()
341341

342342
assert result == ["1.2.3.4", "5.6.7.8"] # Should return IPs from check_alive

0 commit comments

Comments
 (0)