|
| 1 | +import pytest |
| 2 | + |
| 3 | +from deepset_mcp.api.exceptions import ResourceNotFoundError, UnexpectedAPIError |
| 4 | +from deepset_mcp.api.protocols import SecretResourceProtocol |
| 5 | +from deepset_mcp.api.secrets.models import Secret, SecretList |
| 6 | +from deepset_mcp.api.shared_models import NoContentResponse |
| 7 | +from deepset_mcp.tools.secrets import get_secret, list_secrets |
| 8 | +from test.unit.conftest import BaseFakeClient |
| 9 | + |
| 10 | + |
| 11 | +class FakeSecretResource(SecretResourceProtocol): |
| 12 | + def __init__( |
| 13 | + self, |
| 14 | + list_response: SecretList | None = None, |
| 15 | + get_response: Secret | None = None, |
| 16 | + list_exception: Exception | None = None, |
| 17 | + get_exception: Exception | None = None, |
| 18 | + ) -> None: |
| 19 | + self.list_response = list_response |
| 20 | + self.get_response = get_response |
| 21 | + self.list_exception = list_exception |
| 22 | + self.get_exception = get_exception |
| 23 | + |
| 24 | + async def list(self, limit: int = 10, field: str = "created_at", order: str = "DESC") -> SecretList: |
| 25 | + if self.list_exception: |
| 26 | + raise self.list_exception |
| 27 | + if self.list_response is None: |
| 28 | + raise ValueError("No list response configured") |
| 29 | + return self.list_response |
| 30 | + |
| 31 | + async def get(self, secret_id: str) -> Secret: |
| 32 | + if self.get_exception: |
| 33 | + raise self.get_exception |
| 34 | + if self.get_response is None: |
| 35 | + raise ValueError("No get response configured") |
| 36 | + return self.get_response |
| 37 | + |
| 38 | + async def create(self, name: str, secret: str) -> NoContentResponse: |
| 39 | + """Not used in tests, but required by protocol.""" |
| 40 | + return NoContentResponse(message="Created") |
| 41 | + |
| 42 | + async def delete(self, secret_id: str) -> NoContentResponse: |
| 43 | + """Not used in tests, but required by protocol.""" |
| 44 | + return NoContentResponse(message="Deleted") |
| 45 | + |
| 46 | + |
| 47 | +class FakeClientWithSecrets(BaseFakeClient): |
| 48 | + def __init__(self, secret_resource: FakeSecretResource) -> None: |
| 49 | + super().__init__() |
| 50 | + self._secret_resource = secret_resource |
| 51 | + |
| 52 | + def secrets(self) -> FakeSecretResource: |
| 53 | + return self._secret_resource |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.asyncio |
| 57 | +async def test_list_secrets_success() -> None: |
| 58 | + """Test successful listing of secrets.""" |
| 59 | + secrets_data = [ |
| 60 | + Secret(name="api-key", secret_id="secret-1"), |
| 61 | + Secret(name="database-password", secret_id="secret-2"), |
| 62 | + ] |
| 63 | + secret_list = SecretList(data=secrets_data, has_more=False, total=2) |
| 64 | + fake_resource = FakeSecretResource(list_response=secret_list) |
| 65 | + client = FakeClientWithSecrets(fake_resource) |
| 66 | + |
| 67 | + result = await list_secrets(client, limit=10) |
| 68 | + |
| 69 | + expected = "Found 2 secret(s):\nName: api-key, ID: secret-1\nName: database-password, ID: secret-2" |
| 70 | + assert result == expected |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.asyncio |
| 74 | +async def test_list_secrets_with_pagination() -> None: |
| 75 | + """Test listing secrets with pagination info.""" |
| 76 | + secrets_data = [ |
| 77 | + Secret(name="api-key", secret_id="secret-1"), |
| 78 | + ] |
| 79 | + secret_list = SecretList(data=secrets_data, has_more=True, total=5) |
| 80 | + fake_resource = FakeSecretResource(list_response=secret_list) |
| 81 | + client = FakeClientWithSecrets(fake_resource) |
| 82 | + |
| 83 | + result = await list_secrets(client, limit=1) |
| 84 | + |
| 85 | + expected = ( |
| 86 | + "Found 1 secret(s):\nName: api-key, ID: secret-1\n\n" |
| 87 | + "Showing 1 of 5 total secrets. Use a higher limit to see more." |
| 88 | + ) |
| 89 | + assert result == expected |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.asyncio |
| 93 | +async def test_list_secrets_empty() -> None: |
| 94 | + """Test listing when no secrets exist.""" |
| 95 | + secret_list = SecretList(data=[], has_more=False, total=0) |
| 96 | + fake_resource = FakeSecretResource(list_response=secret_list) |
| 97 | + client = FakeClientWithSecrets(fake_resource) |
| 98 | + |
| 99 | + result = await list_secrets(client) |
| 100 | + |
| 101 | + assert result == "No secrets found in this workspace." |
| 102 | + |
| 103 | + |
| 104 | +@pytest.mark.asyncio |
| 105 | +async def test_list_secrets_unexpected_api_error() -> None: |
| 106 | + """Test handling of UnexpectedAPIError during list.""" |
| 107 | + fake_resource = FakeSecretResource(list_exception=UnexpectedAPIError(500, "Internal server error")) |
| 108 | + client = FakeClientWithSecrets(fake_resource) |
| 109 | + |
| 110 | + result = await list_secrets(client) |
| 111 | + |
| 112 | + assert result == "API Error: Internal server error (Status Code: 500)" |
| 113 | + |
| 114 | + |
| 115 | +@pytest.mark.asyncio |
| 116 | +async def test_list_secrets_generic_exception() -> None: |
| 117 | + """Test handling of generic exceptions during list.""" |
| 118 | + fake_resource = FakeSecretResource(list_exception=ValueError("Generic error")) |
| 119 | + client = FakeClientWithSecrets(fake_resource) |
| 120 | + |
| 121 | + result = await list_secrets(client) |
| 122 | + |
| 123 | + assert result == "Unexpected error: Generic error" |
| 124 | + |
| 125 | + |
| 126 | +@pytest.mark.asyncio |
| 127 | +async def test_get_secret_success() -> None: |
| 128 | + """Test successful retrieval of a specific secret.""" |
| 129 | + secret = Secret(name="api-key", secret_id="secret-1") |
| 130 | + fake_resource = FakeSecretResource(get_response=secret) |
| 131 | + client = FakeClientWithSecrets(fake_resource) |
| 132 | + |
| 133 | + result = await get_secret(client, "secret-1") |
| 134 | + |
| 135 | + expected = "Secret Details:\nName: api-key\nID: secret-1" |
| 136 | + assert result == expected |
| 137 | + |
| 138 | + |
| 139 | +@pytest.mark.asyncio |
| 140 | +async def test_get_secret_not_found() -> None: |
| 141 | + """Test handling when secret is not found.""" |
| 142 | + fake_resource = FakeSecretResource(get_exception=ResourceNotFoundError("Secret 'nonexistent' not found.")) |
| 143 | + client = FakeClientWithSecrets(fake_resource) |
| 144 | + |
| 145 | + result = await get_secret(client, "nonexistent") |
| 146 | + |
| 147 | + assert result == "Error: Secret 'nonexistent' not found. (Status Code: 404)" |
| 148 | + |
| 149 | + |
| 150 | +@pytest.mark.asyncio |
| 151 | +async def test_get_secret_unexpected_api_error() -> None: |
| 152 | + """Test handling of UnexpectedAPIError during get.""" |
| 153 | + fake_resource = FakeSecretResource(get_exception=UnexpectedAPIError(500, "Server error")) |
| 154 | + client = FakeClientWithSecrets(fake_resource) |
| 155 | + |
| 156 | + result = await get_secret(client, "secret-1") |
| 157 | + |
| 158 | + assert result == "API Error: Server error (Status Code: 500)" |
| 159 | + |
| 160 | + |
| 161 | +@pytest.mark.asyncio |
| 162 | +async def test_get_secret_generic_exception() -> None: |
| 163 | + """Test handling of generic exceptions during get.""" |
| 164 | + fake_resource = FakeSecretResource(get_exception=ValueError("Something went wrong")) |
| 165 | + client = FakeClientWithSecrets(fake_resource) |
| 166 | + |
| 167 | + result = await get_secret(client, "secret-1") |
| 168 | + |
| 169 | + assert result == "Unexpected error: Something went wrong" |
0 commit comments