|
| 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 pytest |
| 16 | +from unittest.mock import patch, MagicMock |
| 17 | +from veadk.auth.veauth.speech_veauth import get_speech_token |
| 18 | + |
| 19 | + |
| 20 | +# Test cases |
| 21 | + |
| 22 | + |
| 23 | +def test_get_speech_token_with_env_vars(monkeypatch): |
| 24 | + """Test when credentials are available in environment variables""" |
| 25 | + # Setup |
| 26 | + monkeypatch.setenv("VOLCENGINE_ACCESS_KEY", "test_access_key") |
| 27 | + monkeypatch.setenv("VOLCENGINE_SECRET_KEY", "test_secret_key") |
| 28 | + |
| 29 | + mock_response = {"Result": {"APIKeys": [{"APIKey": "test_api_key"}]}} |
| 30 | + |
| 31 | + with patch("veadk.auth.veauth.speech_veauth.ve_request") as mock_ve_request: |
| 32 | + mock_ve_request.return_value = mock_response |
| 33 | + |
| 34 | + # Execute |
| 35 | + result = get_speech_token() |
| 36 | + |
| 37 | + # Verify |
| 38 | + assert result == "test_api_key" |
| 39 | + mock_ve_request.assert_called_once_with( |
| 40 | + request_body={ |
| 41 | + "ProjectName": "default", |
| 42 | + "OnlyAvailable": True, |
| 43 | + }, |
| 44 | + header={"X-Security-Token": ""}, |
| 45 | + action="ListAPIKeys", |
| 46 | + ak="test_access_key", |
| 47 | + sk="test_secret_key", |
| 48 | + service="speech_saas_prod", |
| 49 | + version="2025-05-20", |
| 50 | + region="cn-beijing", |
| 51 | + host="open.volcengineapi.com", |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +def test_get_speech_token_with_vefaas_iam(monkeypatch): |
| 56 | + """Test when credentials are obtained from vefaas iam""" |
| 57 | + # Setup |
| 58 | + monkeypatch.delenv("VOLCENGINE_ACCESS_KEY", raising=False) |
| 59 | + monkeypatch.delenv("VOLCENGINE_SECRET_KEY", raising=False) |
| 60 | + |
| 61 | + mock_cred = MagicMock() |
| 62 | + mock_cred.access_key_id = "vefaas_access_key" |
| 63 | + mock_cred.secret_access_key = "vefaas_secret_key" |
| 64 | + mock_cred.session_token = "vefaas_session_token" |
| 65 | + |
| 66 | + mock_response = {"Result": {"APIKeys": [{"APIKey": "vefaas_api_key"}]}} |
| 67 | + |
| 68 | + with ( |
| 69 | + patch( |
| 70 | + "veadk.auth.veauth.speech_veauth.get_credential_from_vefaas_iam" |
| 71 | + ) as mock_get_cred, |
| 72 | + patch("veadk.auth.veauth.speech_veauth.ve_request") as mock_ve_request, |
| 73 | + ): |
| 74 | + mock_get_cred.return_value = mock_cred |
| 75 | + mock_ve_request.return_value = mock_response |
| 76 | + |
| 77 | + # Execute |
| 78 | + result = get_speech_token(region="cn-shanghai") |
| 79 | + |
| 80 | + # Verify |
| 81 | + assert result == "vefaas_api_key" |
| 82 | + mock_get_cred.assert_called_once() |
| 83 | + mock_ve_request.assert_called_once_with( |
| 84 | + request_body={ |
| 85 | + "ProjectName": "default", |
| 86 | + "OnlyAvailable": True, |
| 87 | + }, |
| 88 | + header={"X-Security-Token": "vefaas_session_token"}, |
| 89 | + action="ListAPIKeys", |
| 90 | + ak="vefaas_access_key", |
| 91 | + sk="vefaas_secret_key", |
| 92 | + service="speech_saas_prod", |
| 93 | + version="2025-05-20", |
| 94 | + region="cn-shanghai", |
| 95 | + host="open.volcengineapi.com", |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +def test_get_speech_token_invalid_response(): |
| 100 | + """Test when API response is invalid""" |
| 101 | + # Setup |
| 102 | + monkeypatch = pytest.MonkeyPatch() |
| 103 | + monkeypatch.setenv("VOLCENGINE_ACCESS_KEY", "test_access_key") |
| 104 | + monkeypatch.setenv("VOLCENGINE_SECRET_KEY", "test_secret_key") |
| 105 | + |
| 106 | + mock_response = {"Error": {"Message": "Invalid request"}} |
| 107 | + |
| 108 | + with patch("veadk.auth.veauth.speech_veauth.ve_request") as mock_ve_request: |
| 109 | + mock_ve_request.return_value = mock_response |
| 110 | + |
| 111 | + # Execute & Verify |
| 112 | + with pytest.raises(ValueError, match="Failed to get speech api key list"): |
| 113 | + get_speech_token() |
0 commit comments