|
| 1 | +# Copyright (c) 2025 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 sys |
| 16 | +from types import SimpleNamespace |
| 17 | +from unittest.mock import MagicMock |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
| 21 | +from agentkit.auth.admin import CliAccessCoords, publish_discovery |
| 22 | +from agentkit.auth.errors import AuthError |
| 23 | + |
| 24 | + |
| 25 | +class TosError(Exception): |
| 26 | + def __init__(self, code: str, message: str): |
| 27 | + super().__init__(message) |
| 28 | + self.code = code |
| 29 | + |
| 30 | + |
| 31 | +def install_fake_tos(monkeypatch, create_bucket_error: Exception): |
| 32 | + client = MagicMock() |
| 33 | + client.create_bucket.side_effect = create_bucket_error |
| 34 | + tos_module = SimpleNamespace( |
| 35 | + ACLType=SimpleNamespace(ACL_Public_Read="public-read"), |
| 36 | + TosClientV2=MagicMock(return_value=client), |
| 37 | + ) |
| 38 | + monkeypatch.setitem(sys.modules, "tos", tos_module) |
| 39 | + return client |
| 40 | + |
| 41 | + |
| 42 | +def make_coords() -> CliAccessCoords: |
| 43 | + return CliAccessCoords( |
| 44 | + account_id="account", |
| 45 | + region="cn-beijing", |
| 46 | + user_pool_uid="pool", |
| 47 | + issuer="https://issuer.example", |
| 48 | + client_id="client", |
| 49 | + role_trn="role", |
| 50 | + provider_trn="provider", |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.parametrize( |
| 55 | + "error_code", |
| 56 | + ["BucketAlreadyExists", "BucketAlreadyOwnedByYou"], |
| 57 | +) |
| 58 | +def test_publish_discovery_ignores_explicit_bucket_conflicts(monkeypatch, error_code): |
| 59 | + client = install_fake_tos( |
| 60 | + monkeypatch, |
| 61 | + TosError(error_code, "bucket is already owned"), |
| 62 | + ) |
| 63 | + |
| 64 | + url = publish_discovery( |
| 65 | + make_coords(), |
| 66 | + bucket="existing-bucket", |
| 67 | + custom_domain="agent.example", |
| 68 | + access_key="ak", |
| 69 | + secret_key="sk", |
| 70 | + ) |
| 71 | + |
| 72 | + assert url == "https://agent.example" |
| 73 | + client.put_object.assert_called_once() |
| 74 | + |
| 75 | + |
| 76 | +def test_publish_discovery_does_not_swallow_authentication_errors(monkeypatch): |
| 77 | + client = install_fake_tos( |
| 78 | + monkeypatch, |
| 79 | + TosError( |
| 80 | + "InvalidAccessKeyId", |
| 81 | + "the specified access key does not exist", |
| 82 | + ), |
| 83 | + ) |
| 84 | + |
| 85 | + with pytest.raises(AuthError, match="could not create the TOS bucket"): |
| 86 | + publish_discovery( |
| 87 | + make_coords(), |
| 88 | + bucket="new-bucket", |
| 89 | + custom_domain="agent.example", |
| 90 | + access_key="invalid-ak", |
| 91 | + secret_key="invalid-sk", |
| 92 | + ) |
| 93 | + |
| 94 | + client.put_object.assert_not_called() |
0 commit comments