Skip to content

Commit c07e130

Browse files
committed
Add backend test for invoke url mapping
1 parent 7d0ce72 commit c07e130

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

api/PclusterApiHandler.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,9 @@ def ec2_action():
242242
def get_cluster_config_text(cluster_name, region=None):
243243
url = f"/v3/clusters/{cluster_name}"
244244
if region:
245-
info_resp = sigv4_request("GET", API_BASE_URL_MAPPING[_get_version(request)], url, params={"region": region})
245+
info_resp = sigv4_request("GET", get_base_url(request.args.get("version")), url, params={"region": region})
246246
else:
247-
info_resp = sigv4_request("GET", API_BASE_URL_MAPPING[_get_version(request)], url)
247+
info_resp = sigv4_request("GET", get_base_url(request.args.get("version")), url)
248248
if info_resp.status_code != 200:
249249
abort(info_resp.status_code)
250250

@@ -493,7 +493,7 @@ def get_dcv_session():
493493

494494

495495
def get_custom_image_config():
496-
image_info = sigv4_request("GET", API_BASE_URL_MAPPING[_get_version(request)], f"/v3/images/custom/{request.args.get('image_id')}").json()
496+
image_info = sigv4_request("GET", get_base_url(request.args.get("version")), f"/v3/images/custom/{request.args.get('image_id')}").json()
497497
configuration = requests.get(image_info["imageConfiguration"]["url"])
498498
return configuration.text
499499

@@ -744,10 +744,10 @@ def _get_params(_request):
744744
params.pop("path")
745745
return params
746746

747-
def _get_version(v):
747+
def get_base_url(v):
748748
if v and str(v) in API_VERSION:
749-
return str(v)
750-
return DEFAULT_API_VERSION
749+
return API_BASE_URL_MAPPING[str(v)]
750+
return API_BASE_URL_MAPPING[DEFAULT_API_VERSION]
751751

752752

753753
pc = Blueprint('pc', __name__)
@@ -756,7 +756,7 @@ def _get_version(v):
756756
@authenticated({'admin'})
757757
@validated(params=PCProxyArgs)
758758
def pc_proxy_get():
759-
response = sigv4_request(request.method, API_BASE_URL_MAPPING[_get_version(request.args.get("version"))], request.args.get("path"), _get_params(request))
759+
response = sigv4_request(request.method, get_base_url(request.args.get("version")), request.args.get("path"), _get_params(request))
760760
return response.json(), response.status_code
761761

762762
@pc.route('/', methods=['POST','PUT','PATCH','DELETE'], strict_slashes=False)
@@ -770,5 +770,5 @@ def pc_proxy():
770770
except:
771771
pass
772772

773-
response = sigv4_request(request.method, API_BASE_URL_MAPPING[_get_version(request.args.get("version"))], request.args.get("path"), _get_params(request), body=body)
773+
response = sigv4_request(request.method, get_base_url(request.args.get("version")), request.args.get("path"), _get_params(request), body=body)
774774
return response.json(), response.status_code

api/tests/test_pcluster_api_handler.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from unittest import mock
2-
from api.PclusterApiHandler import login
2+
from api.PclusterApiHandler import login, get_base_url
33

44

55
@mock.patch("api.PclusterApiHandler.requests.post")
@@ -27,3 +27,11 @@ def test_login_with_no_access_token_returns_401(mocker, app):
2727
login()
2828

2929
mock_abort.assert_called_once_with(401)
30+
31+
def test_get_base_url(monkeypatch):
32+
monkeypatch.setattr('api.PclusterApiHandler.API_VERSION', ['3.12.0', '3.11.0'])
33+
monkeypatch.setattr('api.PclusterApiHandler.API_BASE_URL', '3.12.0=https://example.com,3.11.0=https://example1.com,')
34+
monkeypatch.setattr('api.PclusterApiHandler.API_BASE_URL_MAPPING', {'3.12.0': 'https://example.com', '3.11.0': 'https://example1.com'})
35+
36+
assert 'https://example.com' == get_base_url('3.12.0')
37+

0 commit comments

Comments
 (0)