roksta 0.3.2__cp311-cp311-macosx_10_9_universal2.whl → 0.3.8__cp311-cp311-macosx_10_9_universal2.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. roksta/__init__.cpython-311-darwin.so +0 -0
  2. roksta/ai/__init__.cpython-311-darwin.so +0 -0
  3. roksta/ai/call_ai.cpython-311-darwin.so +0 -0
  4. roksta/ai/gemini.cpython-311-darwin.so +0 -0
  5. roksta/ai/generic.cpython-311-darwin.so +0 -0
  6. roksta/ai/llm.cpython-311-darwin.so +0 -0
  7. roksta/ai/openai.cpython-311-darwin.so +0 -0
  8. roksta/ai/tools/__init__.cpython-311-darwin.so +0 -0
  9. roksta/ai/tools/delete_file.cpython-311-darwin.so +0 -0
  10. roksta/ai/tools/edit_file.cpython-311-darwin.so +0 -0
  11. roksta/ai/tools/final_response.cpython-311-darwin.so +0 -0
  12. roksta/ai/tools/get_file_summaries.cpython-311-darwin.so +0 -0
  13. roksta/ai/tools/read_file.cpython-311-darwin.so +0 -0
  14. roksta/ai/tools/regex_replace.cpython-311-darwin.so +0 -0
  15. roksta/ai/tools/shell_any.cpython-311-darwin.so +0 -0
  16. roksta/ai/tools/shell_limited.cpython-311-darwin.so +0 -0
  17. roksta/ai/tools/tool_defs.cpython-311-darwin.so +0 -0
  18. roksta/ai/tools/tool_utils.cpython-311-darwin.so +0 -0
  19. roksta/ai/tools/web_fetch.cpython-311-darwin.so +0 -0
  20. roksta/ai/tools/write_file.cpython-311-darwin.so +0 -0
  21. roksta/analytics.cpython-311-darwin.so +0 -0
  22. roksta/balance.cpython-311-darwin.so +0 -0
  23. roksta/build_project.cpython-311-darwin.so +0 -0
  24. roksta/chat_workflow.cpython-311-darwin.so +0 -0
  25. roksta/check_for_updates.cpython-311-darwin.so +0 -0
  26. roksta/check_subtask_sequence.cpython-311-darwin.so +0 -0
  27. roksta/checkpoints.cpython-311-darwin.so +0 -0
  28. roksta/clarify_goal.cpython-311-darwin.so +0 -0
  29. roksta/codebase_listing.cpython-311-darwin.so +0 -0
  30. roksta/command_handlers/__init__.cpython-311-darwin.so +0 -0
  31. roksta/command_handlers/handle_activate_command.cpython-311-darwin.so +0 -0
  32. roksta/command_handlers/handle_add_funds_command.cpython-311-darwin.so +0 -0
  33. roksta/command_handlers/handle_auto_charge_command.cpython-311-darwin.so +0 -0
  34. roksta/command_handlers/handle_auto_commit_command.cpython-311-darwin.so +0 -0
  35. roksta/command_handlers/handle_building_command.cpython-311-darwin.so +0 -0
  36. roksta/command_handlers/handle_chat_command.cpython-311-darwin.so +0 -0
  37. roksta/command_handlers/handle_dev_rate_command.cpython-311-darwin.so +0 -0
  38. roksta/command_handlers/handle_feedback_command.cpython-311-darwin.so +0 -0
  39. roksta/command_handlers/handle_goal_command.cpython-311-darwin.so +0 -0
  40. roksta/command_handlers/handle_help_command.cpython-311-darwin.so +0 -0
  41. roksta/command_handlers/handle_init_command.cpython-311-darwin.so +0 -0
  42. roksta/command_handlers/handle_linting_command.cpython-311-darwin.so +0 -0
  43. roksta/command_handlers/handle_login_command.cpython-311-darwin.so +0 -0
  44. roksta/command_handlers/handle_logout_command.cpython-311-darwin.so +0 -0
  45. roksta/command_handlers/handle_payment_details_command.cpython-311-darwin.so +0 -0
  46. roksta/command_handlers/handle_quit_command.cpython-311-darwin.so +0 -0
  47. roksta/command_handlers/handle_redeem_command.cpython-311-darwin.so +0 -0
  48. roksta/command_handlers/handle_request_activation_command.cpython-311-darwin.so +0 -0
  49. roksta/command_handlers/handle_testing_command.cpython-311-darwin.so +0 -0
  50. roksta/command_handlers/handle_usage_command.cpython-311-darwin.so +0 -0
  51. roksta/create_default_config.cpython-311-darwin.so +0 -0
  52. roksta/create_default_ignore_file.cpython-311-darwin.so +0 -0
  53. roksta/default_config.cpython-311-darwin.so +0 -0
  54. roksta/default_ignores.cpython-311-darwin.so +0 -0
  55. roksta/discover_test_command.cpython-311-darwin.so +0 -0
  56. roksta/enums.cpython-311-darwin.so +0 -0
  57. roksta/env.cpython-311-darwin.so +0 -0
  58. roksta/extended_text_area.cpython-311-darwin.so +0 -0
  59. roksta/firebase.cpython-311-darwin.so +0 -0
  60. roksta/firebase_auth_web.cpython-311-darwin.so +0 -0
  61. roksta/firebase_config.cpython-311-darwin.so +0 -0
  62. roksta/fix_tests.cpython-311-darwin.so +0 -0
  63. roksta/gen_codebase_summaries.cpython-311-darwin.so +0 -0
  64. roksta/gen_one_line_goal.cpython-311-darwin.so +0 -0
  65. roksta/gen_subtasks.cpython-311-darwin.so +0 -0
  66. roksta/get_codebase_structure.cpython-311-darwin.so +0 -0
  67. roksta/get_failing_tests.cpython-311-darwin.so +0 -0
  68. roksta/goal_workflow.cpython-311-darwin.so +0 -0
  69. roksta/init_codebase.cpython-311-darwin.so +0 -0
  70. roksta/lint_code.cpython-311-darwin.so +0 -0
  71. roksta/logger.cpython-311-darwin.so +0 -0
  72. roksta/main.cpython-311-darwin.so +0 -0
  73. roksta/make_issue.cpython-311-darwin.so +0 -0
  74. roksta/new_features.cpython-311-darwin.so +0 -0
  75. roksta/parse_directive_cli_tokens.cpython-311-darwin.so +0 -0
  76. roksta/parse_readme.cpython-311-darwin.so +0 -0
  77. roksta/propose_solution.cpython-311-darwin.so +0 -0
  78. roksta/response_formats.cpython-311-darwin.so +0 -0
  79. roksta/rewrite_goal.cpython-311-darwin.so +0 -0
  80. roksta/roksta.cpython-311-darwin.so +0 -0
  81. roksta/run_cli_goal.cpython-311-darwin.so +0 -0
  82. roksta/save_chat_transcript.cpython-311-darwin.so +0 -0
  83. roksta/select_files.cpython-311-darwin.so +0 -0
  84. roksta/tips.cpython-311-darwin.so +0 -0
  85. roksta/utils.cpython-311-darwin.so +0 -0
  86. roksta/write_code.cpython-311-darwin.so +0 -0
  87. roksta-0.3.8.dist-info/METADATA +471 -0
  88. roksta-0.3.8.dist-info/RECORD +91 -0
  89. {roksta-0.3.2.dist-info → roksta-0.3.8.dist-info}/top_level.txt +0 -1
  90. roksta-0.3.2.dist-info/METADATA +0 -40
  91. roksta-0.3.2.dist-info/RECORD +0 -121
  92. tests/__init__.py +0 -2
  93. tests/conftest.py +0 -211
  94. tests/functions/__init__.py +0 -2
  95. tests/functions/api_v1_00/__init__.py +0 -2
  96. tests/functions/api_v1_00/test__analytics.py +0 -416
  97. tests/functions/api_v1_00/test__gemini_proxy.py +0 -352
  98. tests/functions/api_v1_00/test__generic_proxy.py +0 -428
  99. tests/functions/api_v1_00/test__get_payment_details.py +0 -356
  100. tests/functions/api_v1_00/test__openai_proxy.py +0 -449
  101. tests/functions/api_v1_00/test__redeem_credit_code.py +0 -167
  102. tests/functions/api_v1_00/test__sync_emails.py +0 -325
  103. tests/functions/api_v1_00/test__take_payment.py +0 -491
  104. tests/functions/api_v1_00/test__use_activation_code.py +0 -438
  105. tests/functions/api_v1_01/__init__.py +0 -2
  106. tests/functions/api_v1_01/test__analytics.py +0 -416
  107. tests/functions/api_v1_01/test__gemini_proxy.py +0 -352
  108. tests/functions/api_v1_01/test__generic_proxy.py +0 -428
  109. tests/functions/api_v1_01/test__get_payment_details.py +0 -356
  110. tests/functions/api_v1_01/test__openai_proxy.py +0 -449
  111. tests/functions/api_v1_01/test__redeem_credit_code.py +0 -167
  112. tests/functions/api_v1_01/test__sync_emails.py +0 -325
  113. tests/functions/api_v1_01/test__take_payment.py +0 -491
  114. tests/functions/api_v1_01/test__use_activation_code.py +0 -438
  115. tests/functions/api_v1_02/__init__.py +0 -2
  116. tests/functions/api_v1_02/test__analytics.py +0 -416
  117. tests/functions/api_v1_02/test__gemini_proxy.py +0 -352
  118. tests/functions/api_v1_02/test__generic_proxy.py +0 -428
  119. tests/functions/api_v1_02/test__get_payment_details.py +0 -356
  120. tests/functions/api_v1_02/test__openai_proxy.py +0 -449
  121. tests/functions/api_v1_02/test__redeem_credit_code.py +0 -167
  122. tests/functions/api_v1_02/test__sync_emails.py +0 -325
  123. tests/functions/api_v1_02/test__take_payment.py +0 -491
  124. tests/functions/api_v1_02/test__use_activation_code.py +0 -438
  125. tests/functions/api_v1_02/test_proxy_keyword_replacement.py +0 -557
  126. tests/functions/api_v1_02/test_replace_keywords.py +0 -74
  127. tests/functions/test_auth.py +0 -24
  128. tests/functions/test_main.py +0 -73
  129. tests/functions/test_utils.py +0 -484
  130. {roksta-0.3.2.dist-info → roksta-0.3.8.dist-info}/WHEEL +0 -0
  131. {roksta-0.3.2.dist-info → roksta-0.3.8.dist-info}/entry_points.txt +0 -0
@@ -1,73 +0,0 @@
1
- import importlib
2
- import sys
3
- import types
4
-
5
-
6
- def test_import_initializes_firebase_and_stripe_webhook_delegates():
7
- # Save originals
8
- orig = {}
9
- for name in ('firebase_admin', 'firebase_functions', '_stripe_webhook', 'env', 'main'):
10
- orig[name] = sys.modules.get(name)
11
-
12
- try:
13
- # Stub env so DEPLOY_VERSIONS is empty and main won't try to dynamically import endpoints
14
- env_mod = types.ModuleType('env')
15
- env_mod.DEPLOY_VERSIONS = []
16
- sys.modules['env'] = env_mod
17
-
18
- # Stub firebase_admin.initialize_app and record invocation
19
- init_called = {'called': False}
20
-
21
- def fake_initialize_app():
22
- init_called['called'] = True
23
-
24
- firebase_admin_mod = types.ModuleType('firebase_admin')
25
- firebase_admin_mod.initialize_app = fake_initialize_app
26
- sys.modules['firebase_admin'] = firebase_admin_mod
27
-
28
- # Stub firebase_functions.https_fn.on_request decorator used by main
29
- def on_request(memory=None, timeout_sec=None):
30
- def decorator(fn):
31
- # return the function unchanged; this keeps behavior simple for testing
32
- return fn
33
- return decorator
34
-
35
- firebase_functions_mod = types.ModuleType('firebase_functions')
36
- firebase_functions_mod.https_fn = types.SimpleNamespace(on_request=on_request, Request=object, Response=object)
37
- sys.modules['firebase_functions'] = firebase_functions_mod
38
-
39
- # Stub the _stripe_webhook implementation that main imports
40
- sentinel = {'sentinel': True}
41
-
42
- def _stripe_webhook(req):
43
- return sentinel
44
-
45
- stripe_mod = types.ModuleType('_stripe_webhook')
46
- stripe_mod._stripe_webhook = _stripe_webhook
47
- sys.modules['_stripe_webhook'] = stripe_mod
48
-
49
- # Ensure main is imported fresh
50
- if 'main' in sys.modules:
51
- del sys.modules['main']
52
-
53
- main = importlib.import_module('main')
54
-
55
- # initialize_app should have been called during import
56
- assert init_called['called'] is True
57
-
58
- # The stripe_webhook exported function should delegate to our stub
59
- class DummyRequest:
60
- def __init__(self, headers=None):
61
- self.headers = headers or {}
62
-
63
- resp = main.stripe_webhook(DummyRequest())
64
- assert resp == sentinel
65
-
66
- finally:
67
- # Restore originals
68
- for name, val in orig.items():
69
- if val is None:
70
- if name in sys.modules:
71
- del sys.modules[name]
72
- else:
73
- sys.modules[name] = val
@@ -1,484 +0,0 @@
1
- import importlib.util
2
- import os
3
- import sys
4
- import types
5
- import json
6
- import base64
7
- import uuid
8
- import pytest
9
-
10
-
11
- # Helper to create simple module stubs
12
- def _make_fake_firebase_functions():
13
- fake_https = types.ModuleType('firebase_functions.https_fn')
14
-
15
- class FakeResponse:
16
- def __init__(self, response=None, mimetype=None, status=200, **kwargs):
17
- # Mirror the small subset of the real Response API used by the code/tests
18
- self.status_code = status
19
- if isinstance(response, bytes):
20
- self._text = response.decode('utf-8')
21
- else:
22
- # If a non-string is passed (unlikely), coerce to JSON text for assertions
23
- self._text = response if isinstance(response, str) else (json.dumps(response) if response is not None else '')
24
- self.mimetype = mimetype
25
- self.headers = {'Content-Type': mimetype} if mimetype else {}
26
-
27
- def get_data(self, as_text=False):
28
- return self._text if as_text else self._text.encode('utf-8')
29
-
30
- fake_https.Response = FakeResponse
31
- # Provide a Request attribute used in type annotations in the module under test
32
- fake_https.Request = types.SimpleNamespace
33
-
34
- fake_root = types.ModuleType('firebase_functions')
35
- fake_root.https_fn = fake_https
36
- return fake_root, fake_https
37
-
38
-
39
- def _make_fake_firebase_admin():
40
- fake = types.ModuleType('firebase_admin')
41
- # Minimal auth object so import succeeds; tests here don't rely on actual auth behaviour
42
- fake.auth = types.SimpleNamespace(verify_id_token=lambda token: {'uid': 'test'})
43
- return fake
44
-
45
-
46
- def _make_fake_google_secretmanager():
47
- google = types.ModuleType('google')
48
- google_cloud = types.ModuleType('google.cloud')
49
- secretmanager = types.ModuleType('google.cloud.secretmanager')
50
-
51
- class FakeClient:
52
- def access_secret_version(self, request):
53
- raise RuntimeError("SecretManager should not be called in these tests")
54
-
55
- secretmanager.SecretManagerServiceClient = FakeClient
56
- google_cloud.secretmanager = secretmanager
57
- google.cloud = google_cloud
58
- return google, google_cloud, secretmanager
59
-
60
-
61
- def _load_utils_module(monkeypatch):
62
- """Load a fresh copy of functions/utils.py as an isolated module.
63
-
64
- This helper inserts necessary fake modules into sys.modules via the
65
- provided monkeypatch fixture before loading the module so its import-time
66
- dependencies succeed and are controllable by the tests.
67
- """
68
- repo_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
69
- utils_path = os.path.join(repo_root, 'functions', 'utils.py')
70
- assert os.path.exists(utils_path), f"Expected functions/utils.py to exist at {utils_path}"
71
-
72
- # Prepare and register lightweight stubs needed at import time
73
- fake_firebase_root, fake_https = _make_fake_firebase_functions()
74
- monkeypatch.setitem(sys.modules, 'firebase_functions', fake_firebase_root)
75
- monkeypatch.setitem(sys.modules, 'firebase_functions.https_fn', fake_https)
76
-
77
- fake_firebase_admin = _make_fake_firebase_admin()
78
- monkeypatch.setitem(sys.modules, 'firebase_admin', fake_firebase_admin)
79
-
80
- google, google_cloud, secretmanager = _make_fake_google_secretmanager()
81
- monkeypatch.setitem(sys.modules, 'google', google)
82
- monkeypatch.setitem(sys.modules, 'google.cloud', google_cloud)
83
- monkeypatch.setitem(sys.modules, 'google.cloud.secretmanager', secretmanager)
84
-
85
- # Create a unique module name so each test gets a fresh module object
86
- module_name = f"tests.utils_isolated_{uuid.uuid4().hex}"
87
- spec = importlib.util.spec_from_file_location(module_name, utils_path)
88
- module = importlib.util.module_from_spec(spec)
89
- # Ensure the new module is discoverable under its name during execution
90
- monkeypatch.setitem(sys.modules, module_name, module)
91
- spec.loader.exec_module(module)
92
- return module
93
-
94
-
95
- def test_create_json_response_returns_expected_json_and_status(monkeypatch):
96
- mod = _load_utils_module(monkeypatch)
97
-
98
- resp = mod.create_json_response(True, {"alpha": 1}, 201)
99
-
100
- assert hasattr(resp, 'status_code')
101
- assert resp.status_code == 201
102
-
103
- body_text = resp.get_data(as_text=True)
104
- parsed = json.loads(body_text)
105
- assert parsed == {"success": True, "payload": {"alpha": 1}}
106
-
107
-
108
- def test_get_sealedbox_private_key_raises_when_pynacl_privatekey_missing(monkeypatch):
109
- # Ensure a nacl.public module exists but does NOT expose PrivateKey so the
110
- # import inside get_sealedbox_private_key raises and the function reports
111
- # that encryption support is unavailable.
112
- nacl_mod = types.ModuleType('nacl')
113
- nacl_public = types.ModuleType('nacl.public')
114
-
115
- # Provide PublicKey/SealedBox but omit PrivateKey to trigger the import error
116
- class PublicKeyStub:
117
- def __init__(self, data):
118
- self._data = bytes(data)
119
-
120
- class SealedBoxStub:
121
- def __init__(self, key):
122
- pass
123
-
124
- nacl_public.PublicKey = PublicKeyStub
125
- nacl_public.SealedBox = SealedBoxStub
126
-
127
- monkeypatch.setitem(sys.modules, 'nacl', nacl_mod)
128
- monkeypatch.setitem(sys.modules, 'nacl.public', nacl_public)
129
-
130
- mod = _load_utils_module(monkeypatch)
131
-
132
- with pytest.raises(Exception) as excinfo:
133
- mod.get_sealedbox_private_key()
134
-
135
- assert 'Encryption support is unavailable' in str(excinfo.value)
136
-
137
-
138
- def test_get_sealedbox_private_key_initializes_and_caches_private_key(monkeypatch):
139
- # Provide a PrivateKey implementation and stub get_secret_key to return
140
- # a valid 32-byte base64-encoded secret. Verify the value is cached and
141
- # the secret fetch is only performed once.
142
- key_bytes = b"\x02" * 32
143
-
144
- nacl_mod = types.ModuleType('nacl')
145
- nacl_public = types.ModuleType('nacl.public')
146
-
147
- class PrivateKeyStub:
148
- def __init__(self, data):
149
- # Accept bytes-like input
150
- self._data = bytes(data)
151
-
152
- def __repr__(self):
153
- return f"PrivateKeyStub(len={len(self._data)})"
154
-
155
- nacl_public.PrivateKey = PrivateKeyStub
156
- monkeypatch.setitem(sys.modules, 'nacl', nacl_mod)
157
- monkeypatch.setitem(sys.modules, 'nacl.public', nacl_public)
158
-
159
- mod = _load_utils_module(monkeypatch)
160
-
161
- calls = {'count': 0}
162
-
163
- def fake_get_secret_key(name):
164
- calls['count'] += 1
165
- return base64.b64encode(key_bytes).decode('ascii')
166
-
167
- # Replace the module-level get_secret_key with our fake
168
- mod.get_secret_key = fake_get_secret_key
169
-
170
- # Ensure fresh start
171
- mod._SEALEDBOX_PRIVATE_KEY = None
172
-
173
- first = mod.get_sealedbox_private_key()
174
- second = mod.get_sealedbox_private_key()
175
-
176
- assert first is second
177
- assert calls['count'] == 1
178
- assert hasattr(first, '_data') and first._data == key_bytes
179
-
180
-
181
- def test_get_sealedbox_private_key_invalid_length_does_not_cache_and_raises(monkeypatch):
182
- # Provide PrivateKey but have the secret manager return an invalid-length key
183
- invalid_key = b"\x03" * 16
184
-
185
- nacl_mod = types.ModuleType('nacl')
186
- nacl_public = types.ModuleType('nacl.public')
187
-
188
- class PrivateKeyStub:
189
- def __init__(self, data):
190
- self._data = bytes(data)
191
-
192
- nacl_public.PrivateKey = PrivateKeyStub
193
- monkeypatch.setitem(sys.modules, 'nacl', nacl_mod)
194
- monkeypatch.setitem(sys.modules, 'nacl.public', nacl_public)
195
-
196
- mod = _load_utils_module(monkeypatch)
197
-
198
- def fake_get_secret_key(name):
199
- return base64.b64encode(invalid_key).decode('ascii')
200
-
201
- mod.get_secret_key = fake_get_secret_key
202
-
203
- # Ensure fresh start
204
- mod._SEALEDBOX_PRIVATE_KEY = None
205
-
206
- with pytest.raises(Exception) as excinfo:
207
- mod.get_sealedbox_private_key()
208
-
209
- assert 'Failed to initialize sealed-box private key' in str(excinfo.value)
210
- # Failure should not be cached
211
- assert mod._SEALEDBOX_PRIVATE_KEY is None
212
-
213
-
214
- # ------------------------- Additional tests to improve coverage -------------------------
215
-
216
- def test_get_secret_key_returns_secret_on_success(monkeypatch):
217
- mod = _load_utils_module(monkeypatch)
218
-
219
- fake_payload = types.SimpleNamespace(data=b'secret-value')
220
- fake_response = types.SimpleNamespace(payload=fake_payload)
221
-
222
- class FakeClient:
223
- def access_secret_version(self, request):
224
- return fake_response
225
-
226
- # Patch the SecretManager client on the imported module
227
- monkeypatch.setattr(mod.secretmanager, 'SecretManagerServiceClient', FakeClient, raising=True)
228
-
229
- result = mod.get_secret_key('my-secret')
230
- assert result == 'secret-value'
231
-
232
-
233
- def test_get_secret_key_raises_on_backend_error(monkeypatch):
234
- mod = _load_utils_module(monkeypatch)
235
-
236
- class FakeClient:
237
- def access_secret_version(self, request):
238
- raise RuntimeError('boom')
239
-
240
- monkeypatch.setattr(mod.secretmanager, 'SecretManagerServiceClient', FakeClient, raising=True)
241
-
242
- with pytest.raises(Exception) as excinfo:
243
- mod.get_secret_key('does-not-matter')
244
- assert 'Failed to retrieve secret key from Secret Manager' in str(excinfo.value)
245
-
246
-
247
- def test_verify_firebase_token_success(monkeypatch):
248
- mod = _load_utils_module(monkeypatch)
249
- req = types.SimpleNamespace(headers={'Authorization': 'Bearer SOME_TOKEN'})
250
-
251
- res = mod.verify_firebase_token(req)
252
- assert res == {'uid': 'test'}
253
-
254
-
255
- def test_verify_firebase_token_missing_or_invalid_header_raises(monkeypatch):
256
- mod = _load_utils_module(monkeypatch)
257
- req = types.SimpleNamespace(headers={})
258
-
259
- with pytest.raises(Exception) as excinfo:
260
- mod.verify_firebase_token(req)
261
- # The implementation wraps the underlying message, but the cause should be present
262
- assert 'Missing or invalid Authorization' in str(excinfo.value)
263
-
264
-
265
- def test_verify_firebase_token_verify_id_token_failure_raises(monkeypatch):
266
- mod = _load_utils_module(monkeypatch)
267
-
268
- def fake_verify(token):
269
- raise ValueError('invalid token')
270
-
271
- # Replace the verifier with one that raises
272
- mod.auth.verify_id_token = fake_verify
273
-
274
- req = types.SimpleNamespace(headers={'Authorization': 'Bearer x'})
275
- with pytest.raises(Exception) as excinfo:
276
- mod.verify_firebase_token(req)
277
- assert 'invalid token' in str(excinfo.value)
278
-
279
-
280
- def test_get_api_key_delegates_to_get_secret_key(monkeypatch):
281
- mod = _load_utils_module(monkeypatch)
282
-
283
- called = {}
284
-
285
- def fake_get_secret_key(name):
286
- called['name'] = name
287
- return 'X'
288
-
289
- mod.get_secret_key = fake_get_secret_key
290
-
291
- result = mod.get_api_key(mod.LlmFamily.OPENAI)
292
- assert result == 'X'
293
- assert called['name'] == 'openai-api-key'
294
-
295
-
296
- def test_get_request_json_plaintext_returns_parsed_json(monkeypatch):
297
- mod = _load_utils_module(monkeypatch)
298
-
299
- def get_json(silent=False):
300
- return {'a': 1}
301
-
302
- req = types.SimpleNamespace(headers={}, get_json=get_json)
303
- result = mod.get_request_json(req, strict=False)
304
- assert result == {'a': 1}
305
-
306
-
307
- def test_get_request_json_plaintext_strict_raises_on_get_json_error(monkeypatch):
308
- mod = _load_utils_module(monkeypatch)
309
-
310
- def get_json(silent=False):
311
- raise ValueError('bad json')
312
-
313
- req = types.SimpleNamespace(headers={}, get_json=get_json)
314
- with pytest.raises(Exception):
315
- mod.get_request_json(req, strict=True)
316
-
317
-
318
- def test_get_request_json_plaintext_non_strict_returns_none_on_get_json_error(monkeypatch):
319
- mod = _load_utils_module(monkeypatch)
320
-
321
- def get_json(silent=False):
322
- raise ValueError('bad json')
323
-
324
- req = types.SimpleNamespace(headers={}, get_json=get_json)
325
- result = mod.get_request_json(req, strict=False)
326
- assert result is None
327
-
328
-
329
- def _install_sealedbox_stub(monkeypatch, decrypt_behavior):
330
- """Helper: install a nacl.public.SealedBox stub whose decrypt returns or throws.
331
-
332
- decrypt_behavior may be either:
333
- - a bytes object to return from decrypt(), or
334
- - a callable that accepts ciphertext and returns bytes / raises.
335
- """
336
- nacl_mod = types.ModuleType('nacl')
337
- nacl_public = types.ModuleType('nacl.public')
338
-
339
- class SealedBox:
340
- def __init__(self, key):
341
- self._key = key
342
-
343
- def decrypt(self, ciphertext):
344
- if callable(decrypt_behavior):
345
- return decrypt_behavior(ciphertext)
346
- return decrypt_behavior
347
-
348
- nacl_public.SealedBox = SealedBox
349
- nacl_mod.public = nacl_public
350
-
351
- # Ensure both module paths exist in sys.modules so `from nacl.public import SealedBox` works
352
- monkeypatch.setitem(sys.modules, 'nacl', nacl_mod)
353
- monkeypatch.setitem(sys.modules, 'nacl.public', nacl_public)
354
-
355
-
356
- def test_get_request_json_encrypted_success_using_get_data(monkeypatch):
357
- mod = _load_utils_module(monkeypatch)
358
-
359
- plaintext = b'{"ok": true}'
360
- ciphertext = b'FAKECIPHERTEXT'
361
- body_b64 = base64.b64encode(ciphertext)
362
-
363
- # Request exposes get_data(as_text=False)
364
- def get_data(as_text=False):
365
- return body_b64
366
-
367
- req = types.SimpleNamespace(headers={mod.ENCRYPTION_HEADER_NAME: 'true'}, get_data=get_data)
368
-
369
- # SealedBox.decrypt will return the plaintext regardless of ciphertext
370
- _install_sealedbox_stub(monkeypatch, plaintext)
371
- # Stub the private key loader so it doesn't call Secret Manager
372
- monkeypatch.setattr(mod, 'get_sealedbox_private_key', lambda: 'DUMMY', raising=False)
373
-
374
- res = mod.get_request_json(req, strict=True)
375
- assert res == {"ok": True}
376
-
377
-
378
- def test_get_request_json_encrypted_fallback_when_get_data_rejects_as_text_arg(monkeypatch):
379
- mod = _load_utils_module(monkeypatch)
380
-
381
- plaintext = b'{"ok": 1}'
382
- ciphertext = b'CT'
383
- body_b64 = base64.b64encode(ciphertext)
384
-
385
- def get_data(*args, **kwargs):
386
- # Simulate a stub that raises when given the as_text kwarg
387
- if 'as_text' in kwargs:
388
- raise TypeError('no as_text')
389
- return body_b64
390
-
391
- req = types.SimpleNamespace(headers={mod.ENCRYPTION_HEADER_NAME: 'true'}, get_data=get_data)
392
-
393
- _install_sealedbox_stub(monkeypatch, plaintext)
394
- monkeypatch.setattr(mod, 'get_sealedbox_private_key', lambda: 'DUMMY', raising=False)
395
-
396
- res = mod.get_request_json(req, strict=True)
397
- assert res == {"ok": 1}
398
-
399
-
400
- def test_get_request_json_encrypted_uses_data_attribute_if_no_get_data(monkeypatch):
401
- mod = _load_utils_module(monkeypatch)
402
-
403
- plaintext = b'{"v": 42}'
404
- ciphertext = b'CT2'
405
- body_b64 = base64.b64encode(ciphertext)
406
-
407
- req = types.SimpleNamespace(headers={mod.ENCRYPTION_HEADER_NAME: 'true'}, data=body_b64)
408
-
409
- _install_sealedbox_stub(monkeypatch, plaintext)
410
- monkeypatch.setattr(mod, 'get_sealedbox_private_key', lambda: 'DUMMY', raising=False)
411
-
412
- res = mod.get_request_json(req, strict=True)
413
- assert res == {"v": 42}
414
-
415
-
416
- def test_get_request_json_encrypted_invalid_base64(monkeypatch):
417
- mod = _load_utils_module(monkeypatch)
418
-
419
- # Make b64 decoder throw to exercise that error path deterministically
420
- def fake_b64decode(_):
421
- raise Exception('bad base64')
422
-
423
- monkeypatch.setattr(mod.base64, 'b64decode', fake_b64decode, raising=True)
424
-
425
- def get_data(as_text=False):
426
- return b'NOT-BASE64'
427
-
428
- req = types.SimpleNamespace(headers={mod.ENCRYPTION_HEADER_NAME: 'true'}, get_data=get_data)
429
-
430
- # Non-strict should return None
431
- res = mod.get_request_json(req, strict=False)
432
- assert res is None
433
-
434
- # Strict should raise
435
- with pytest.raises(Exception):
436
- mod.get_request_json(req, strict=True)
437
-
438
-
439
- def test_get_request_json_encrypted_decrypt_failure(monkeypatch):
440
- mod = _load_utils_module(monkeypatch)
441
-
442
- ciphertext = b'CT'
443
- body_b64 = base64.b64encode(ciphertext)
444
-
445
- # SealedBox.decrypt will raise to simulate decryption failure
446
- def raise_on_decrypt(_):
447
- raise RuntimeError('bad decrypt')
448
-
449
- _install_sealedbox_stub(monkeypatch, raise_on_decrypt)
450
- monkeypatch.setattr(mod, 'get_sealedbox_private_key', lambda: 'DUMMY', raising=False)
451
-
452
- def get_data(as_text=False):
453
- return body_b64
454
-
455
- req = types.SimpleNamespace(headers={mod.ENCRYPTION_HEADER_NAME: 'true'}, get_data=get_data)
456
-
457
- # Non-strict returns None
458
- assert mod.get_request_json(req, strict=False) is None
459
-
460
- # Strict raises
461
- with pytest.raises(Exception):
462
- mod.get_request_json(req, strict=True)
463
-
464
-
465
- def test_get_request_json_encrypted_invalid_json_after_decrypt(monkeypatch):
466
- mod = _load_utils_module(monkeypatch)
467
-
468
- # Decrypt returns bytes that are not valid JSON
469
- _install_sealedbox_stub(monkeypatch, b'not-json')
470
- monkeypatch.setattr(mod, 'get_sealedbox_private_key', lambda: 'DUMMY', raising=False)
471
-
472
- body_b64 = base64.b64encode(b'CT')
473
-
474
- def get_data(as_text=False):
475
- return body_b64
476
-
477
- req = types.SimpleNamespace(headers={mod.ENCRYPTION_HEADER_NAME: 'true'}, get_data=get_data)
478
-
479
- assert mod.get_request_json(req, strict=False) is None
480
- with pytest.raises(Exception):
481
- mod.get_request_json(req, strict=True)
482
-
483
-
484
- # End of additional tests
File without changes