|
| 1 | +# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import json |
| 16 | + |
| 17 | +import pytest |
| 18 | +from pydantic import ValidationError |
| 19 | + |
| 20 | +from agentkit.platform.context import default_cloud_provider |
| 21 | +from agentkit.sdk.gateway import ( |
| 22 | + AgentkitGatewayClient, |
| 23 | + CreateGatewayPluginBindingRequest, |
| 24 | + CreateGatewayPluginBindingResponse, |
| 25 | + CreateGatewayPluginRequest, |
| 26 | + CreateGatewayPluginResponse, |
| 27 | +) |
| 28 | +from agentkit.toolkit.errors import ApiError |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +def client(): |
| 33 | + with default_cloud_provider("volcengine"): |
| 34 | + return AgentkitGatewayClient( |
| 35 | + access_key="AK_LOCAL_TEST_ONLY", |
| 36 | + secret_key="SK_LOCAL_TEST_ONLY", |
| 37 | + region="cn-beijing", |
| 38 | + session_token="TOKEN_LOCAL_TEST_ONLY", |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +def test_create_plugin_sends_agentkit_request_and_parses_id(client, monkeypatch): |
| 43 | + def fake_json(api, params, body): |
| 44 | + assert api == "CreateGatewayPlugin" |
| 45 | + assert params == {} |
| 46 | + assert json.loads(body) == { |
| 47 | + "GatewayInstanceId": "g-test", |
| 48 | + "GatewayPluginName": "IdentityAuthorization", |
| 49 | + "GatewayPluginConfig": "{}", |
| 50 | + } |
| 51 | + return json.dumps({"Result": {"GatewayPluginId": "gpl-test"}}) |
| 52 | + |
| 53 | + monkeypatch.setattr(client, "json", fake_json) |
| 54 | + assert client.service == "agentkit" |
| 55 | + assert client.session_token == "TOKEN_LOCAL_TEST_ONLY" |
| 56 | + for action, info in client.api_info.items(): |
| 57 | + assert info.method == "POST" |
| 58 | + assert info.path == "/" |
| 59 | + assert info.query == {"Action": action, "Version": "2025-10-30"} |
| 60 | + |
| 61 | + response = client.create_gateway_plugin( |
| 62 | + CreateGatewayPluginRequest( |
| 63 | + gateway_instance_id="g-test", |
| 64 | + gateway_plugin_name="IdentityAuthorization", |
| 65 | + gateway_plugin_config="{}", |
| 66 | + ) |
| 67 | + ) |
| 68 | + assert isinstance(response, CreateGatewayPluginResponse) |
| 69 | + assert response.gateway_plugin_id == "gpl-test" |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.parametrize("enable", [None, False, True]) |
| 73 | +def test_create_binding_preserves_config_and_optional_values( |
| 74 | + client, monkeypatch, enable |
| 75 | +): |
| 76 | + config = json.dumps( |
| 77 | + { |
| 78 | + "Namespace": "default", |
| 79 | + "TokenPosition": {"Header": "x-ve-tip-token", "Prefix": ""}, |
| 80 | + } |
| 81 | + ) |
| 82 | + expected = { |
| 83 | + "GatewayPluginId": "gpl-test", |
| 84 | + "TargetId": "m-test", |
| 85 | + "GatewayPluginBindingConfig": config, |
| 86 | + } |
| 87 | + if enable is not None: |
| 88 | + expected.update(Enable=enable, Comments="") |
| 89 | + |
| 90 | + def fake_json(api, params, body): |
| 91 | + assert api == "CreateGatewayPluginBinding" |
| 92 | + assert params == {} |
| 93 | + assert json.loads(body) == expected |
| 94 | + return json.dumps({"Result": {"GatewayPluginBindingId": "gpb-test"}}) |
| 95 | + |
| 96 | + monkeypatch.setattr(client, "json", fake_json) |
| 97 | + response = client.create_gateway_plugin_binding( |
| 98 | + CreateGatewayPluginBindingRequest( |
| 99 | + GatewayPluginId="gpl-test", |
| 100 | + TargetId="m-test", |
| 101 | + GatewayPluginBindingConfig=config, |
| 102 | + Enable=enable, |
| 103 | + Comments="" if enable is not None else None, |
| 104 | + ) |
| 105 | + ) |
| 106 | + assert isinstance(response, CreateGatewayPluginBindingResponse) |
| 107 | + assert response.gateway_plugin_binding_id == "gpb-test" |
| 108 | + |
| 109 | + |
| 110 | +@pytest.mark.parametrize( |
| 111 | + "model,values,config_field", |
| 112 | + [ |
| 113 | + ( |
| 114 | + CreateGatewayPluginRequest, |
| 115 | + { |
| 116 | + "gateway_instance_id": "g-test", |
| 117 | + "gateway_plugin_name": "IdentityAuthorization", |
| 118 | + }, |
| 119 | + "gateway_plugin_config", |
| 120 | + ), |
| 121 | + ( |
| 122 | + CreateGatewayPluginBindingRequest, |
| 123 | + {"gateway_plugin_id": "gpl-test", "target_id": "m-test"}, |
| 124 | + "gateway_plugin_binding_config", |
| 125 | + ), |
| 126 | + ], |
| 127 | +) |
| 128 | +def test_config_is_required_and_must_be_a_string(model, values, config_field): |
| 129 | + with pytest.raises(ValidationError): |
| 130 | + model(**values) |
| 131 | + with pytest.raises(ValidationError): |
| 132 | + model(**values, **{config_field: {"Namespace": "default"}}) |
| 133 | + |
| 134 | + |
| 135 | +def test_duplicate_binding_error_is_preserved(client, monkeypatch): |
| 136 | + calls = [] |
| 137 | + |
| 138 | + def fake_json(api, params, body): |
| 139 | + calls.append(api) |
| 140 | + return json.dumps( |
| 141 | + { |
| 142 | + "ResponseMetadata": { |
| 143 | + "Error": { |
| 144 | + "Code": "DuplicatedResource", |
| 145 | + "Message": "The specified resource already exists.", |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + ) |
| 150 | + |
| 151 | + monkeypatch.setattr(client, "json", fake_json) |
| 152 | + with pytest.raises(ApiError) as error: |
| 153 | + client.create_gateway_plugin_binding( |
| 154 | + CreateGatewayPluginBindingRequest( |
| 155 | + gateway_plugin_id="gpl-test", |
| 156 | + target_id="m-test", |
| 157 | + gateway_plugin_binding_config="{}", |
| 158 | + ) |
| 159 | + ) |
| 160 | + assert error.value.error_code == "DuplicatedResource" |
| 161 | + assert calls == ["CreateGatewayPluginBinding"] |
0 commit comments