Repository navigation
Expand file tree
/
Copy pathtest_integrations.py
More file actions
461 lines (349 loc) · 17.8 KB
/
Copy pathtest_integrations.py
File metadata and controls
461 lines (349 loc) · 17.8 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
"""Deterministic coverage for the shared Connections registry."""
from __future__ import annotations
import os
import socket
import pytest
from prometheus import integrations
from prometheus.integrations import IntegrationState, IntegrationStatus
from prometheus.loop.models import PROVIDERS
from prometheus.ops import browser_agent
from prometheus.tools import apple, calendar
def _isolate(monkeypatch, tmp_path):
monkeypatch.setenv("PROMETHEUS_HOME", str(tmp_path))
monkeypatch.chdir(tmp_path)
integrations._HEALTH = None
integrations._reset_import_cache()
def test_registry_contract():
items = integrations.registry()
# waku-platform is hidden_unless_env: it's in the registry only when its
# PROMETHEUS_PLATFORM_BASE_URL is set (see test_platform_provider.py for the
# row's own visibility contract) — so the expected count is DERIVED from
# the live environment, not hardcoded. A hardcoded 24 passes on a laptop
# and fails inside a tenant container, where the row is visible and the
# true count is 25.
visible_providers = {name for name, p in PROVIDERS.items() if p.is_visible()}
assert len(items) == len(visible_providers) + len(integrations.INTEGRATIONS)
assert len({item.key for item in items}) == len(items)
assert {item.key for item in items if item.group == "AI Providers"} == visible_providers
for item in items:
assert callable(item.enabled)
for field in item.env:
if field.kind is integrations.FieldKind.CHOICE:
assert field.options
if field.secret:
assert field.kind is integrations.FieldKind.TEXT
def test_status_masking_health_persistence_and_invalidation(monkeypatch, tmp_path):
_isolate(monkeypatch, tmp_path)
monkeypatch.setenv("OPENAI_API_KEY", "super-secret-1234")
view = next(view for view in integrations.list_integrations() if view.key == "openai")
# A key is present and nothing required is missing, so this is CONFIGURED —
# not INSTALLED_BUT_UNCONFIGURED, which the dashboard renders as "needs
# setup". See test_configured_is_not_confused_with_needing_setup below.
assert view.status.state is IntegrationState.CONFIGURED
assert view.fields[0].value == ""
assert view.fields[0].last4 == "1234"
integrations.record_health("openai", IntegrationStatus(IntegrationState.CONNECTED))
integrations._HEALTH = None
assert next(view for view in integrations.list_integrations() if view.key == "openai").status.state is IntegrationState.CONNECTED
integrations.invalidate_health("openai")
after = next(view for view in integrations.list_integrations() if view.key == "openai")
assert after.status.state is IntegrationState.CONFIGURED
assert after.status.message == "configured — not tested yet"
def test_otel_probe_checks_configured_collector_tcp_port(monkeypatch):
captured = {}
class Connection:
def close(self):
captured["closed"] = True
def connect(address, timeout):
captured.update(address=address, timeout=timeout)
return Connection()
monkeypatch.setattr(socket, "create_connection", connect)
integrations._otel_probe({"OTEL_EXPORTER_OTLP_ENDPOINT": "localhost:4317"})
assert captured == {"address": ("localhost", 4317), "timeout": 3, "closed": True}
def test_otel_probe_rejects_endpoint_without_tcp_port():
with pytest.raises(ValueError, match="host:port"):
integrations._otel_probe({"OTEL_EXPORTER_OTLP_ENDPOINT": "localhost"})
def test_otel_probe_rejects_endpoint_with_a_signal_path(monkeypatch):
monkeypatch.setattr(socket, "create_connection", lambda address, timeout: None)
with pytest.raises(ValueError, match="host:port"):
integrations._otel_probe({"OTEL_EXPORTER_OTLP_ENDPOINT": "localhost:4317/v1/traces"})
def test_otel_test_connection_records_connected_after_tcp_probe(monkeypatch, tmp_path):
_isolate(monkeypatch, tmp_path)
monkeypatch.setenv("OTEL_EXPORTER_OTLP_ENDPOINT", "localhost:4317")
monkeypatch.setattr(integrations, "_extra_installed", lambda name: True)
monkeypatch.setattr(integrations, "_otel_probe", lambda values: None)
view = integrations.test_integration("otel")
assert view.status.state is IntegrationState.CONNECTED
def test_apply_rejects_unknown_and_secret_clear(monkeypatch, tmp_path):
_isolate(monkeypatch, tmp_path)
(tmp_path / ".env").write_text("TAVILY_API_KEY=old-secret\n")
monkeypatch.setenv("TAVILY_API_KEY", "old-secret")
assert not integrations.apply_integration("tavily", {"NOPE": "x"}).ok
# Force avoids the remote Tavily probe; clear is explicit and removes both stores.
result = integrations.apply_integration("tavily", {}, ("TAVILY_API_KEY",), force=True)
assert result.ok
assert "TAVILY_API_KEY" not in os.environ
assert "TAVILY_API_KEY" not in (tmp_path / ".env").read_text()
def _configure_google(monkeypatch, tmp_path):
_isolate(monkeypatch, tmp_path)
monkeypatch.setenv("PROMETHEUS_GOOGLE_CALENDAR", "1")
monkeypatch.setenv("PROMETHEUS_GOOGLE_CALENDAR_ID", "team@example.com")
monkeypatch.setattr(integrations, "_extra_installed", lambda name: True)
def test_google_test_connection_records_connected(monkeypatch, tmp_path):
_configure_google(monkeypatch, tmp_path)
captured = {}
monkeypatch.setattr(
calendar,
"probe_google_calendar",
lambda home, calendar_id: captured.update(home=home, calendar_id=calendar_id),
)
view = integrations.test_integration("google_calendar")
assert captured == {"home": tmp_path, "calendar_id": "team@example.com"}
assert view.status.state is IntegrationState.CONNECTED
assert view.status.checked_at is not None
def _configure_apple(monkeypatch, tmp_path):
_isolate(monkeypatch, tmp_path)
monkeypatch.setattr(integrations.sys, "platform", "darwin")
monkeypatch.setenv("PROMETHEUS_APPLE_CALENDAR", "1")
def test_apple_test_connection_records_connected(monkeypatch, tmp_path):
_configure_apple(monkeypatch, tmp_path)
called = []
monkeypatch.setattr(calendar, "probe_apple_calendar", lambda: called.append(True))
view = integrations.test_integration("apple_calendar")
assert called == [True]
assert view.status.state is IntegrationState.CONNECTED
assert view.status.checked_at is not None
def test_apple_save_probes_before_recording_connected(monkeypatch, tmp_path):
_isolate(monkeypatch, tmp_path)
monkeypatch.setattr(integrations.sys, "platform", "darwin")
monkeypatch.setattr(browser_agent, "rebuild", lambda: None)
called = []
monkeypatch.setattr(calendar, "probe_apple_calendar", lambda: called.append(True))
result = integrations.apply_integration(
"apple_calendar", {"PROMETHEUS_APPLE_CALENDAR": "1", "PROMETHEUS_APPLE_CALENDARS": ""}
)
assert result.ok
assert called == [True]
assert result.view is not None
assert result.view.status.state is IntegrationState.CONNECTED
def test_apple_probe_failure_records_error_and_can_recover(monkeypatch, tmp_path):
_configure_apple(monkeypatch, tmp_path)
monkeypatch.setattr(
calendar,
"probe_apple_calendar",
lambda: (_ for _ in ()).throw(RuntimeError("automation denied")),
)
view = integrations.test_integration("apple_calendar")
assert view.status.state is IntegrationState.ERROR
assert view.status.message == "automation denied"
monkeypatch.setattr(calendar, "probe_apple_calendar", lambda: None)
view = integrations.test_integration("apple_calendar")
assert view.status.state is IntegrationState.CONNECTED
def test_apple_force_save_records_error_without_probe(monkeypatch, tmp_path):
_isolate(monkeypatch, tmp_path)
monkeypatch.setattr(integrations.sys, "platform", "darwin")
monkeypatch.setattr(browser_agent, "rebuild", lambda: None)
def unexpected_probe():
raise AssertionError("force save must skip the probe")
monkeypatch.setattr(calendar, "probe_apple_calendar", unexpected_probe)
result = integrations.apply_integration(
"apple_calendar", {"PROMETHEUS_APPLE_CALENDAR": "1"}, force=True
)
assert result.ok
assert result.view is not None
assert result.view.status.state is IntegrationState.ERROR
assert result.view.status.message == "Saved without a successful test"
def _configure_apple_tools(monkeypatch, tmp_path):
_isolate(monkeypatch, tmp_path)
monkeypatch.setattr(integrations.sys, "platform", "darwin")
monkeypatch.setenv("PROMETHEUS_APPLE_TOOLS", "1")
def test_apple_tools_save_probes_and_records_connected(monkeypatch, tmp_path):
_isolate(monkeypatch, tmp_path)
monkeypatch.setattr(integrations.sys, "platform", "darwin")
monkeypatch.setattr(browser_agent, "rebuild", lambda: None)
called = []
monkeypatch.setattr(apple, "probe_apple_tools", lambda: called.append(True))
result = integrations.apply_integration(
"apple_tools", {"PROMETHEUS_APPLE_TOOLS": "1"}
)
assert result.ok
assert called == [True]
assert os.environ["PROMETHEUS_APPLE_TOOLS"] == "1"
assert result.view is not None
assert result.view.status.state is IntegrationState.CONNECTED
assert result.view.status.checked_at is not None
def test_apple_tools_probe_failure_records_error_and_recovers(monkeypatch, tmp_path):
_configure_apple_tools(monkeypatch, tmp_path)
monkeypatch.setattr(
apple,
"probe_apple_tools",
lambda: (_ for _ in ()).throw(RuntimeError("Mail: Not authorized")),
)
view = integrations.test_integration("apple_tools")
assert view.status.state is IntegrationState.ERROR
assert view.status.message == "Mail: Not authorized"
assert view.status.checked_at is not None
monkeypatch.setattr(apple, "probe_apple_tools", lambda: None)
view = integrations.test_integration("apple_tools")
assert view.status.state is IntegrationState.CONNECTED
assert view.status.checked_at is not None
def test_apple_tools_force_save_skips_probe(monkeypatch, tmp_path):
_isolate(monkeypatch, tmp_path)
monkeypatch.setattr(integrations.sys, "platform", "darwin")
monkeypatch.setattr(browser_agent, "rebuild", lambda: None)
def unexpected_probe():
raise AssertionError("force save must skip the probe")
monkeypatch.setattr(apple, "probe_apple_tools", unexpected_probe)
result = integrations.apply_integration(
"apple_tools", {"PROMETHEUS_APPLE_TOOLS": "1"}, force=True
)
assert result.ok
assert result.view is not None
assert result.view.status.state is IntegrationState.ERROR
assert result.view.status.message == "Saved without a successful test"
def test_disabling_apple_clears_connected_health(monkeypatch, tmp_path):
_configure_apple(monkeypatch, tmp_path)
(tmp_path / ".env").write_text("PROMETHEUS_APPLE_CALENDAR=1\n")
monkeypatch.setattr(browser_agent, "rebuild", lambda: None)
integrations.record_health("apple_calendar", IntegrationStatus(IntegrationState.CONNECTED))
result = integrations.apply_integration(
"apple_calendar", {"PROMETHEUS_APPLE_CALENDAR": ""}
)
assert result.ok
assert result.view is not None
assert result.view.status.state is IntegrationState.NOT_CONFIGURED
def test_google_save_probes_candidate_and_records_connected(monkeypatch, tmp_path):
_isolate(monkeypatch, tmp_path)
monkeypatch.setattr(integrations, "_extra_installed", lambda name: True)
monkeypatch.setattr(browser_agent, "rebuild", lambda: None)
captured = {}
monkeypatch.setattr(
calendar,
"probe_google_calendar",
lambda home, calendar_id: captured.update(home=home, calendar_id=calendar_id),
)
result = integrations.apply_integration(
"google_calendar",
{
"PROMETHEUS_GOOGLE_CALENDAR": "1",
"PROMETHEUS_GOOGLE_CALENDAR_ID": "candidate@example.com",
},
)
assert result.ok
assert captured == {"home": tmp_path, "calendar_id": "candidate@example.com"}
assert result.view is not None
assert result.view.status.state is IntegrationState.CONNECTED
def test_google_probe_failure_records_error(monkeypatch, tmp_path):
_configure_google(monkeypatch, tmp_path)
monkeypatch.setattr(
calendar,
"probe_google_calendar",
lambda home, calendar_id: (_ for _ in ()).throw(RuntimeError("access denied")),
)
view = integrations.test_integration("google_calendar")
assert view.status.state is IntegrationState.ERROR
assert view.status.message == "access denied"
assert view.status.checked_at is not None
def test_google_save_failure_can_force_without_writing_first(monkeypatch, tmp_path):
_isolate(monkeypatch, tmp_path)
monkeypatch.setenv("PROMETHEUS_GOOGLE_CALENDAR", "")
monkeypatch.setenv("PROMETHEUS_GOOGLE_CALENDAR_ID", "old@example.com")
monkeypatch.setattr(integrations, "_extra_installed", lambda name: True)
monkeypatch.setattr(
calendar,
"probe_google_calendar",
lambda home, calendar_id: (_ for _ in ()).throw(RuntimeError("not authorized")),
)
result = integrations.apply_integration(
"google_calendar",
{
"PROMETHEUS_GOOGLE_CALENDAR": "1",
"PROMETHEUS_GOOGLE_CALENDAR_ID": "team@example.com",
},
)
assert not result.ok
assert result.can_force
assert result.error == "not authorized"
assert not (tmp_path / ".env").exists()
assert os.environ["PROMETHEUS_GOOGLE_CALENDAR"] == ""
assert os.environ["PROMETHEUS_GOOGLE_CALENDAR_ID"] == "old@example.com"
def test_google_force_save_skips_probe_and_records_error(monkeypatch, tmp_path):
_isolate(monkeypatch, tmp_path)
monkeypatch.setattr(integrations, "_extra_installed", lambda name: True)
monkeypatch.setattr(browser_agent, "rebuild", lambda: None)
def unexpected_probe(home, calendar_id):
raise AssertionError("force save must skip the probe")
monkeypatch.setattr(calendar, "probe_google_calendar", unexpected_probe)
result = integrations.apply_integration(
"google_calendar",
{
"PROMETHEUS_GOOGLE_CALENDAR": "1",
"PROMETHEUS_GOOGLE_CALENDAR_ID": "team@example.com",
},
force=True,
)
assert result.ok
assert result.view is not None
assert result.view.status.state is IntegrationState.ERROR
assert result.view.status.message == "Saved without a successful test"
def test_disabling_google_skips_probe(monkeypatch, tmp_path):
_configure_google(monkeypatch, tmp_path)
(tmp_path / ".env").write_text(
"PROMETHEUS_GOOGLE_CALENDAR=1\nWAKU_GOOGLE_CALENDAR_ID=team@example.com\n"
)
monkeypatch.setattr(browser_agent, "rebuild", lambda: None)
def unexpected_probe(home, calendar_id):
raise AssertionError("disabled integrations must not be probed")
monkeypatch.setattr(calendar, "probe_google_calendar", unexpected_probe)
result = integrations.apply_integration(
"google_calendar", {"PROMETHEUS_GOOGLE_CALENDAR": ""}
)
assert result.ok
def test_disabled_google_test_connection_skips_probe(monkeypatch, tmp_path):
_isolate(monkeypatch, tmp_path)
monkeypatch.setenv("PROMETHEUS_GOOGLE_CALENDAR", "")
monkeypatch.setenv("PROMETHEUS_GOOGLE_CALENDAR_ID", "team@example.com")
monkeypatch.setattr(integrations, "_extra_installed", lambda name: True)
def unexpected_probe(home, calendar_id):
raise AssertionError("disabled integrations must not be probed")
monkeypatch.setattr(calendar, "probe_google_calendar", unexpected_probe)
view = integrations.test_integration("google_calendar")
assert view.status.state is not IntegrationState.CONNECTED
assert view.status.message == "integration is disabled"
def test_configured_is_not_confused_with_needing_setup(monkeypatch, tmp_path):
"""THE regression. Both of these used to return INSTALLED_BUT_UNCONFIGURED,
which the dashboard renders as "needs setup" — so a working Tavily key
reported itself as broken. The health store is empty on a fresh checkout,
so EVERY user met that on their first visit to Connections, about
integrations that were already working.
The two situations are not the same and now cannot collapse:
missing a required value -> installed_but_unconfigured ("needs setup")
complete but never probed -> configured ("configured, not tested")
"""
_isolate(monkeypatch, tmp_path)
monkeypatch.setenv("TAVILY_API_KEY", "tvly-working-key")
tavily = next(v for v in integrations.list_integrations() if v.key == "tavily")
assert tavily.status.state is IntegrationState.CONFIGURED, (
"a filled-in integration must not tell the user it needs setting up"
)
monkeypatch.delenv("TAVILY_API_KEY")
integrations._HEALTH = None
empty = next(v for v in integrations.list_integrations() if v.key == "tavily")
assert empty.status.state is IntegrationState.NOT_CONFIGURED
def test_a_genuinely_missing_required_field_still_says_needs_setup(monkeypatch, tmp_path):
"""The other half: widening the states must not swallow a real problem.
Telegram with a token but no allowed-chat id is incomplete, and has to keep
saying so."""
_isolate(monkeypatch, tmp_path)
monkeypatch.setattr(integrations, "_extra_installed", lambda name: True)
# Notion, not Telegram: Telegram has a single required field, so "half
# filled in" is not expressible there and the test would prove nothing.
notion = next(i for i in integrations.registry() if i.key == "notion")
required = [f.name for f in notion.env if f.required]
assert len(required) > 1, "this test needs an integration with 2+ required fields"
monkeypatch.setenv(required[0], "set")
for name in required[1:]:
monkeypatch.delenv(name, raising=False)
view = next(v for v in integrations.list_integrations() if v.key == "notion")
assert view.status.state is IntegrationState.INSTALLED_BUT_UNCONFIGURED
assert required[1] in view.status.message