-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathtest_provider_immutability.py
373 lines (298 loc) Β· 11.8 KB
/
test_provider_immutability.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
from types import MappingProxyType
import pytest
from pydantic import SecretStr, ValidationError
from openhands.events.action.commands import CmdRunAction
from openhands.integrations.provider import (
ProviderHandler,
ProviderToken,
ProviderType,
SecretStore,
)
from openhands.server.routes.settings import convert_to_settings
from openhands.server.settings import POSTSettingsModel, Settings
def test_provider_token_immutability():
"""Test that ProviderToken is immutable"""
token = ProviderToken(token=SecretStr('test'), user_id='user1')
# Test direct attribute modification
with pytest.raises(ValidationError):
token.token = SecretStr('new')
with pytest.raises(ValidationError):
token.user_id = 'new_user'
# Test that __setattr__ is blocked
with pytest.raises(ValidationError):
setattr(token, 'token', SecretStr('new'))
# Verify original values are unchanged
assert token.token.get_secret_value() == 'test'
assert token.user_id == 'user1'
def test_secret_store_immutability():
"""Test that SecretStore is immutable"""
store = SecretStore(
provider_tokens={ProviderType.GITHUB: ProviderToken(token=SecretStr('test'))}
)
# Test direct attribute modification
with pytest.raises(ValidationError):
store.provider_tokens = {}
# Test dictionary mutation attempts
with pytest.raises((TypeError, AttributeError)):
store.provider_tokens[ProviderType.GITHUB] = ProviderToken(
token=SecretStr('new')
)
with pytest.raises((TypeError, AttributeError)):
store.provider_tokens.clear()
with pytest.raises((TypeError, AttributeError)):
store.provider_tokens.update(
{ProviderType.GITLAB: ProviderToken(token=SecretStr('test'))}
)
# Test nested immutability
github_token = store.provider_tokens[ProviderType.GITHUB]
with pytest.raises(ValidationError):
github_token.token = SecretStr('new')
# Verify original values are unchanged
assert store.provider_tokens[ProviderType.GITHUB].token.get_secret_value() == 'test'
def test_settings_immutability():
"""Test that Settings secrets_store is immutable"""
settings = Settings(
secrets_store=SecretStore(
provider_tokens={
ProviderType.GITHUB: ProviderToken(token=SecretStr('test'))
}
)
)
# Test direct modification of secrets_store
with pytest.raises(ValidationError):
settings.secrets_store = SecretStore()
# Test nested modification attempts
with pytest.raises((TypeError, AttributeError)):
settings.secrets_store.provider_tokens[ProviderType.GITHUB] = ProviderToken(
token=SecretStr('new')
)
# Test model_copy creates new instance
new_store = SecretStore(
provider_tokens={
ProviderType.GITHUB: ProviderToken(token=SecretStr('new_token'))
}
)
new_settings = settings.model_copy(update={'secrets_store': new_store})
# Verify original is unchanged and new has updated values
assert (
settings.secrets_store.provider_tokens[
ProviderType.GITHUB
].token.get_secret_value()
== 'test'
)
assert (
new_settings.secrets_store.provider_tokens[
ProviderType.GITHUB
].token.get_secret_value()
== 'new_token'
)
with pytest.raises(ValidationError):
new_settings.secrets_store.provider_tokens[
ProviderType.GITHUB
].token = SecretStr('')
def test_post_settings_conversion():
"""Test that POSTSettingsModel correctly converts to Settings"""
# Create POST model with token data
post_data = POSTSettingsModel(
provider_tokens={'github': 'test_token', 'gitlab': 'gitlab_token'}
)
# Convert to settings using convert_to_settings function
settings = convert_to_settings(post_data)
# Verify tokens were converted correctly
assert (
settings.secrets_store.provider_tokens[
ProviderType.GITHUB
].token.get_secret_value()
== 'test_token'
)
assert (
settings.secrets_store.provider_tokens[
ProviderType.GITLAB
].token.get_secret_value()
== 'gitlab_token'
)
assert settings.secrets_store.provider_tokens[ProviderType.GITLAB].user_id is None
# Verify immutability of converted settings
with pytest.raises(ValidationError):
settings.secrets_store = SecretStore()
def test_provider_handler_immutability():
"""Test that ProviderHandler maintains token immutability"""
# Create initial tokens
tokens = MappingProxyType(
{ProviderType.GITHUB: ProviderToken(token=SecretStr('test'))}
)
handler = ProviderHandler(provider_tokens=tokens)
# Try to modify tokens (should raise TypeError due to frozen dict)
with pytest.raises((TypeError, AttributeError)):
handler.provider_tokens[ProviderType.GITHUB] = ProviderToken(
token=SecretStr('new')
)
# Try to modify the handler's tokens property
with pytest.raises((ValidationError, TypeError, AttributeError)):
handler.provider_tokens = {}
# Original token should be unchanged
assert (
handler.provider_tokens[ProviderType.GITHUB].token.get_secret_value() == 'test'
)
def test_token_conversion():
"""Test token conversion in SecretStore.create"""
# Test with string token
store1 = Settings(
secrets_store=SecretStore(
provider_tokens={
ProviderType.GITHUB: ProviderToken(token=SecretStr('test_token'))
}
)
)
assert (
store1.secrets_store.provider_tokens[
ProviderType.GITHUB
].token.get_secret_value()
== 'test_token'
)
assert store1.secrets_store.provider_tokens[ProviderType.GITHUB].user_id is None
# Test with dict token
store2 = SecretStore(
provider_tokens={'github': {'token': 'test_token', 'user_id': 'user1'}}
)
assert (
store2.provider_tokens[ProviderType.GITHUB].token.get_secret_value()
== 'test_token'
)
assert store2.provider_tokens[ProviderType.GITHUB].user_id == 'user1'
# Test with ProviderToken
token = ProviderToken(token=SecretStr('test_token'), user_id='user2')
store3 = SecretStore(provider_tokens={ProviderType.GITHUB: token})
assert (
store3.provider_tokens[ProviderType.GITHUB].token.get_secret_value()
== 'test_token'
)
assert store3.provider_tokens[ProviderType.GITHUB].user_id == 'user2'
store4 = SecretStore(
provider_tokens={
ProviderType.GITHUB: 123 # Invalid type
}
)
assert ProviderType.GITHUB not in store4.provider_tokens
# Test with empty/None token
store5 = SecretStore(provider_tokens={ProviderType.GITHUB: None})
assert ProviderType.GITHUB not in store5.provider_tokens
store6 = SecretStore(
provider_tokens={
'invalid_provider': 'test_token' # Invalid provider type
}
)
assert len(store6.provider_tokens.keys()) == 0
def test_provider_handler_type_enforcement():
with pytest.raises((TypeError)):
ProviderHandler(provider_tokens={'a': 'b'})
def test_expose_env_vars():
"""Test that expose_env_vars correctly exposes secrets as strings"""
tokens = MappingProxyType(
{
ProviderType.GITHUB: ProviderToken(token=SecretStr('test_token')),
ProviderType.GITLAB: ProviderToken(token=SecretStr('gitlab_token')),
}
)
handler = ProviderHandler(provider_tokens=tokens)
# Test with specific provider tokens
env_secrets = {
ProviderType.GITHUB: SecretStr('gh_token'),
ProviderType.GITLAB: SecretStr('gl_token'),
}
exposed = handler.expose_env_vars(env_secrets)
assert exposed['github_token'] == 'gh_token'
assert exposed['gitlab_token'] == 'gl_token'
@pytest.mark.asyncio
async def test_get_env_vars():
"""Test get_env_vars with different configurations"""
tokens = MappingProxyType(
{
ProviderType.GITHUB: ProviderToken(token=SecretStr('test_token')),
ProviderType.GITLAB: ProviderToken(token=SecretStr('gitlab_token')),
}
)
handler = ProviderHandler(provider_tokens=tokens)
# Test getting all tokens unexposed
env_vars = await handler.get_env_vars(expose_secrets=False)
assert isinstance(env_vars, dict)
assert isinstance(env_vars[ProviderType.GITHUB], SecretStr)
assert env_vars[ProviderType.GITHUB].get_secret_value() == 'test_token'
assert env_vars[ProviderType.GITLAB].get_secret_value() == 'gitlab_token'
# Test getting specific providers
env_vars = await handler.get_env_vars(
expose_secrets=False, providers=[ProviderType.GITHUB]
)
assert len(env_vars) == 1
assert ProviderType.GITHUB in env_vars
assert ProviderType.GITLAB not in env_vars
# Test exposed secrets
exposed_vars = await handler.get_env_vars(expose_secrets=True)
assert isinstance(exposed_vars, dict)
assert exposed_vars['github_token'] == 'test_token'
assert exposed_vars['gitlab_token'] == 'gitlab_token'
# Test empty tokens
empty_handler = ProviderHandler(provider_tokens=MappingProxyType({}))
empty_vars = await empty_handler.get_env_vars()
assert empty_vars == {}
@pytest.fixture
def event_stream():
"""Fixture for event stream testing"""
class TestEventStream:
def __init__(self):
self.secrets = {}
def set_secrets(self, secrets):
self.secrets = secrets
return TestEventStream()
@pytest.mark.asyncio
async def test_set_event_stream_secrets(event_stream):
"""Test setting secrets in event stream"""
tokens = MappingProxyType(
{
ProviderType.GITHUB: ProviderToken(token=SecretStr('test_token')),
ProviderType.GITLAB: ProviderToken(token=SecretStr('gitlab_token')),
}
)
handler = ProviderHandler(provider_tokens=tokens)
# Test with provided env_vars
env_vars = {
ProviderType.GITHUB: SecretStr('new_token'),
ProviderType.GITLAB: SecretStr('new_gitlab_token'),
}
await handler.set_event_stream_secrets(event_stream, env_vars)
assert event_stream.secrets == {
'github_token': 'new_token',
'gitlab_token': 'new_gitlab_token',
}
# Test without env_vars (using existing tokens)
await handler.set_event_stream_secrets(event_stream)
assert event_stream.secrets == {
'github_token': 'test_token',
'gitlab_token': 'gitlab_token',
}
def test_check_cmd_action_for_provider_token_ref():
"""Test detection of provider tokens in command actions"""
# Test command with GitHub token
cmd = CmdRunAction(command='echo $GITHUB_TOKEN')
providers = ProviderHandler.check_cmd_action_for_provider_token_ref(cmd)
assert ProviderType.GITHUB in providers
assert len(providers) == 1
# Test command with multiple tokens
cmd = CmdRunAction(command='echo $GITHUB_TOKEN && echo $GITLAB_TOKEN')
providers = ProviderHandler.check_cmd_action_for_provider_token_ref(cmd)
assert ProviderType.GITHUB in providers
assert ProviderType.GITLAB in providers
assert len(providers) == 2
# Test command without tokens
cmd = CmdRunAction(command='echo "Hello"')
providers = ProviderHandler.check_cmd_action_for_provider_token_ref(cmd)
assert len(providers) == 0
# Test non-command action
from openhands.events.action import MessageAction
msg = MessageAction(content='test')
providers = ProviderHandler.check_cmd_action_for_provider_token_ref(msg)
assert len(providers) == 0
def test_get_provider_env_key():
"""Test provider environment key generation"""
assert ProviderHandler.get_provider_env_key(ProviderType.GITHUB) == 'github_token'
assert ProviderHandler.get_provider_env_key(ProviderType.GITLAB) == 'gitlab_token'