-
Notifications
You must be signed in to change notification settings - Fork 6k
/
Copy pathtest_secret_store.py
306 lines (254 loc) Β· 12.2 KB
/
test_secret_store.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
from __future__ import annotations
from types import MappingProxyType
from typing import Any
from pydantic import SecretStr
from openhands.integrations.provider import ProviderToken, ProviderType, SecretStore
class TestSecretStore:
def test_adding_only_provider_tokens(self):
"""Test adding only provider tokens to the SecretStore."""
# Create provider tokens
github_token = ProviderToken(
token=SecretStr('github-token-123'), user_id='user1'
)
gitlab_token = ProviderToken(
token=SecretStr('gitlab-token-456'), user_id='user2'
)
# Create a store with only provider tokens
provider_tokens = {
ProviderType.GITHUB: github_token,
ProviderType.GITLAB: gitlab_token,
}
# Initialize the store with a dict that will be converted to MappingProxyType
store = SecretStore(provider_tokens=provider_tokens)
# Verify the tokens were added correctly
assert isinstance(store.provider_tokens, MappingProxyType)
assert len(store.provider_tokens) == 2
assert (
store.provider_tokens[ProviderType.GITHUB].token.get_secret_value()
== 'github-token-123'
)
assert store.provider_tokens[ProviderType.GITHUB].user_id == 'user1'
assert (
store.provider_tokens[ProviderType.GITLAB].token.get_secret_value()
== 'gitlab-token-456'
)
assert store.provider_tokens[ProviderType.GITLAB].user_id == 'user2'
# Verify custom_secrets is empty
assert isinstance(store.custom_secrets, MappingProxyType)
assert len(store.custom_secrets) == 0
def test_adding_only_custom_secrets(self):
"""Test adding only custom secrets to the SecretStore."""
# Create custom secrets
custom_secrets = {
'API_KEY': 'api-key-123',
'DATABASE_PASSWORD': 'db-pass-456',
}
# Initialize the store with custom secrets
store = SecretStore(custom_secrets=custom_secrets)
# Verify the custom secrets were added correctly
assert isinstance(store.custom_secrets, MappingProxyType)
assert len(store.custom_secrets) == 2
assert store.custom_secrets['API_KEY'].get_secret_value() == 'api-key-123'
assert (
store.custom_secrets['DATABASE_PASSWORD'].get_secret_value()
== 'db-pass-456'
)
# Verify provider_tokens is empty
assert isinstance(store.provider_tokens, MappingProxyType)
assert len(store.provider_tokens) == 0
def test_initializing_with_mixed_types(self):
"""Test initializing the store with mixed types (dict and MappingProxyType)."""
# Create provider tokens as a dict
provider_tokens_dict = {
ProviderType.GITHUB: {'token': 'github-token-123', 'user_id': 'user1'},
}
# Create custom secrets as a MappingProxyType
custom_secrets_dict = {'API_KEY': 'api-key-123'}
custom_secrets_proxy = MappingProxyType(
{key: SecretStr(value) for key, value in custom_secrets_dict.items()}
)
# Test with dict for provider_tokens and MappingProxyType for custom_secrets
store1 = SecretStore(
provider_tokens=provider_tokens_dict, custom_secrets=custom_secrets_proxy
)
assert isinstance(store1.provider_tokens, MappingProxyType)
assert isinstance(store1.custom_secrets, MappingProxyType)
assert (
store1.provider_tokens[ProviderType.GITHUB].token.get_secret_value()
== 'github-token-123'
)
assert store1.custom_secrets['API_KEY'].get_secret_value() == 'api-key-123'
# Test with MappingProxyType for provider_tokens and dict for custom_secrets
provider_token = ProviderToken(
token=SecretStr('gitlab-token-456'), user_id='user2'
)
provider_tokens_proxy = MappingProxyType({ProviderType.GITLAB: provider_token})
store2 = SecretStore(
provider_tokens=provider_tokens_proxy, custom_secrets=custom_secrets_dict
)
assert isinstance(store2.provider_tokens, MappingProxyType)
assert isinstance(store2.custom_secrets, MappingProxyType)
assert (
store2.provider_tokens[ProviderType.GITLAB].token.get_secret_value()
== 'gitlab-token-456'
)
assert store2.custom_secrets['API_KEY'].get_secret_value() == 'api-key-123'
def test_model_copy_update_fields(self):
"""Test using model_copy to update fields without affecting other fields."""
# Create initial store
github_token = ProviderToken(
token=SecretStr('github-token-123'), user_id='user1'
)
custom_secret = {'API_KEY': SecretStr('api-key-123')}
initial_store = SecretStore(
provider_tokens=MappingProxyType({ProviderType.GITHUB: github_token}),
custom_secrets=MappingProxyType(custom_secret),
)
# Update only provider_tokens
gitlab_token = ProviderToken(
token=SecretStr('gitlab-token-456'), user_id='user2'
)
updated_provider_tokens = MappingProxyType(
{ProviderType.GITHUB: github_token, ProviderType.GITLAB: gitlab_token}
)
updated_store1 = initial_store.model_copy(
update={'provider_tokens': updated_provider_tokens}
)
# Verify provider_tokens was updated but custom_secrets remains the same
assert len(updated_store1.provider_tokens) == 2
assert (
updated_store1.provider_tokens[ProviderType.GITHUB].token.get_secret_value()
== 'github-token-123'
)
assert (
updated_store1.provider_tokens[ProviderType.GITLAB].token.get_secret_value()
== 'gitlab-token-456'
)
assert len(updated_store1.custom_secrets) == 1
assert (
updated_store1.custom_secrets['API_KEY'].get_secret_value() == 'api-key-123'
)
# Update only custom_secrets
updated_custom_secrets = MappingProxyType(
{
'API_KEY': SecretStr('api-key-123'),
'DATABASE_PASSWORD': SecretStr('db-pass-456'),
}
)
updated_store2 = initial_store.model_copy(
update={'custom_secrets': updated_custom_secrets}
)
# Verify custom_secrets was updated but provider_tokens remains the same
assert len(updated_store2.provider_tokens) == 1
assert (
updated_store2.provider_tokens[ProviderType.GITHUB].token.get_secret_value()
== 'github-token-123'
)
assert len(updated_store2.custom_secrets) == 2
assert (
updated_store2.custom_secrets['API_KEY'].get_secret_value() == 'api-key-123'
)
assert (
updated_store2.custom_secrets['DATABASE_PASSWORD'].get_secret_value()
== 'db-pass-456'
)
def test_serialization_with_expose_secrets(self):
"""Test serializing the SecretStore with expose_secrets=True."""
# Create a store with both provider tokens and custom secrets
github_token = ProviderToken(
token=SecretStr('github-token-123'), user_id='user1'
)
custom_secrets = {'API_KEY': SecretStr('api-key-123')}
store = SecretStore(
provider_tokens=MappingProxyType({ProviderType.GITHUB: github_token}),
custom_secrets=MappingProxyType(custom_secrets),
)
# Test serialization with expose_secrets=True
serialized_provider_tokens = store.provider_tokens_serializer(
store.provider_tokens, SerializationInfo(context={'expose_secrets': True})
)
serialized_custom_secrets = store.custom_secrets_serializer(
store.custom_secrets, SerializationInfo(context={'expose_secrets': True})
)
# Verify provider tokens are exposed
assert serialized_provider_tokens['github']['token'] == 'github-token-123'
assert serialized_provider_tokens['github']['user_id'] == 'user1'
# Verify custom secrets are exposed
assert serialized_custom_secrets['API_KEY'] == 'api-key-123'
# Test serialization with expose_secrets=False (default)
hidden_provider_tokens = store.provider_tokens_serializer(
store.provider_tokens, SerializationInfo(context={'expose_secrets': False})
)
hidden_custom_secrets = store.custom_secrets_serializer(
store.custom_secrets, SerializationInfo(context={'expose_secrets': False})
)
# Verify provider tokens are hidden
assert hidden_provider_tokens['github']['token'] != 'github-token-123'
assert '**' in hidden_provider_tokens['github']['token']
# Verify custom secrets are hidden
assert hidden_custom_secrets['API_KEY'] != 'api-key-123'
assert '**' in hidden_custom_secrets['API_KEY']
def test_initializing_provider_tokens_with_mixed_value_types(self):
"""Test initializing provider tokens with both plain strings and SecretStr objects."""
# Create provider tokens with mixed value types
# Note: The ProviderToken.from_value method only accepts plain strings in the token field
# when passed as a dictionary, not SecretStr objects
provider_tokens_dict = {
ProviderType.GITHUB: {
'token': 'github-token-123', # Plain string
'user_id': 'user1',
},
ProviderType.GITLAB: {
'token': 'gitlab-token-456', # Also using plain string
'user_id': 'user2',
},
}
# For the second provider, create a ProviderToken directly
gitlab_token = ProviderToken(
token=SecretStr('gitlab-token-456'), user_id='user2'
)
# Create a mixed dictionary with both a dict and a ProviderToken object
mixed_provider_tokens = {
ProviderType.GITHUB: provider_tokens_dict[ProviderType.GITHUB], # Dict
ProviderType.GITLAB: gitlab_token, # ProviderToken object
}
# Initialize the store
store = SecretStore(provider_tokens=mixed_provider_tokens)
# Verify all tokens are converted to SecretStr
assert isinstance(store.provider_tokens, MappingProxyType)
assert len(store.provider_tokens) == 2
# Check GitHub token (was plain string in a dict)
github_token = store.provider_tokens[ProviderType.GITHUB]
assert isinstance(github_token.token, SecretStr)
assert github_token.token.get_secret_value() == 'github-token-123'
assert github_token.user_id == 'user1'
# Check GitLab token (was a ProviderToken object)
gitlab_token_result = store.provider_tokens[ProviderType.GITLAB]
assert isinstance(gitlab_token_result.token, SecretStr)
assert gitlab_token_result.token.get_secret_value() == 'gitlab-token-456'
assert gitlab_token_result.user_id == 'user2'
def test_initializing_custom_secrets_with_mixed_value_types(self):
"""Test initializing custom secrets with both plain strings and SecretStr objects."""
# Create custom secrets with mixed value types
custom_secrets_dict = {
'API_KEY': 'api-key-123', # Plain string
'DATABASE_PASSWORD': SecretStr('db-pass-456'), # SecretStr
}
# Initialize the store
store = SecretStore(custom_secrets=custom_secrets_dict)
# Verify all secrets are converted to SecretStr
assert isinstance(store.custom_secrets, MappingProxyType)
assert len(store.custom_secrets) == 2
# Check API_KEY (was plain string)
assert isinstance(store.custom_secrets['API_KEY'], SecretStr)
assert store.custom_secrets['API_KEY'].get_secret_value() == 'api-key-123'
# Check DATABASE_PASSWORD (was SecretStr)
assert isinstance(store.custom_secrets['DATABASE_PASSWORD'], SecretStr)
assert (
store.custom_secrets['DATABASE_PASSWORD'].get_secret_value()
== 'db-pass-456'
)
# Mock class for SerializationInfo since it's not directly importable
class SerializationInfo:
def __init__(self, context: dict[str, Any] | None = None):
self.context = context or {}