-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_integration.py
More file actions
588 lines (424 loc) · 21 KB
/
test_integration.py
File metadata and controls
588 lines (424 loc) · 21 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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
"""Tests for the Integration class — config loading, handler registration, and execution."""
import json
from datetime import timedelta
import pytest
from autohive_integrations_sdk import (
Integration,
ExecutionContext,
ActionHandler,
ActionResult,
ActionError,
ConnectedAccountHandler,
ConnectedAccountInfo,
IntegrationResult,
ResultType,
ValidationError,
)
from autohive_integrations_sdk.integration import (
ConfigurationError,
PollingTriggerHandler,
)
# ── Config loading ───────────────────────────────────────────────────────────
def test_load_config(integration, config_dict):
assert integration.config.name == config_dict["name"]
assert integration.config.version == config_dict["version"]
assert integration.config.description == config_dict["description"]
assert "test_action" in integration.config.actions
assert "test_trigger" in integration.config.polling_triggers
def test_load_config_missing_file(tmp_path):
with pytest.raises(ConfigurationError, match="not found"):
Integration.load(tmp_path / "nonexistent.json")
def test_load_config_invalid_json(tmp_path):
bad_file = tmp_path / "config.json"
bad_file.write_text("{invalid json")
with pytest.raises(ConfigurationError, match="Invalid JSON"):
Integration.load(bad_file)
# ── Action handler registration ─────────────────────────────────────────────
def test_register_action_handler(integration):
@integration.action("test_action")
class Handler(ActionHandler):
async def execute(self, inputs, context):
return ActionResult(data={})
assert "test_action" in integration._action_handlers
def test_register_action_not_in_config(integration):
with pytest.raises(ConfigurationError, match="not defined in config"):
@integration.action("nonexistent_action")
class Handler(ActionHandler):
async def execute(self, inputs, context):
return ActionResult(data={})
# ── Action execution ────────────────────────────────────────────────────────
async def test_execute_action_success(integration):
@integration.action("test_action")
class Handler(ActionHandler):
async def execute(self, inputs, context):
return ActionResult(data={"greeting": f"Hello {inputs['name']}"})
ctx = ExecutionContext(auth={"api_key": "k"})
result = await integration.execute_action("test_action", {"name": "World"}, ctx)
assert result.type == ResultType.ACTION
assert result.result.data == {"greeting": "Hello World"}
assert result.version is not None
async def test_execute_action_with_cost(integration):
@integration.action("test_action")
class Handler(ActionHandler):
async def execute(self, inputs, context):
return ActionResult(data={"greeting": "hi"}, cost_usd=0.05)
ctx = ExecutionContext(auth={"api_key": "k"})
result = await integration.execute_action("test_action", {"name": "x"}, ctx)
assert result.type == ResultType.ACTION
assert result.result.cost_usd == 0.05
async def test_execute_action_invalid_inputs(integration):
@integration.action("test_action")
class Handler(ActionHandler):
async def execute(self, inputs, context):
return ActionResult(data={"greeting": "hi"})
ctx = ExecutionContext(auth={"api_key": "k"})
result = await integration.execute_action("test_action", {}, ctx)
assert result.type == ResultType.VALIDATION_ERROR
async def test_execute_action_invalid_output(integration):
@integration.action("test_action")
class Handler(ActionHandler):
async def execute(self, inputs, context):
return ActionResult(data={"wrong_key": 123})
ctx = ExecutionContext(auth={"api_key": "k"})
result = await integration.execute_action("test_action", {"name": "x"}, ctx)
assert result.type == ResultType.VALIDATION_ERROR
async def test_execute_action_wrong_return_type(integration):
@integration.action("test_action")
class Handler(ActionHandler):
async def execute(self, inputs, context):
return {"greeting": "plain dict"}
ctx = ExecutionContext(auth={"api_key": "k"})
result = await integration.execute_action("test_action", {"name": "x"}, ctx)
assert result.type == ResultType.VALIDATION_ERROR
async def test_execute_action_not_registered(integration):
ctx = ExecutionContext(auth={"api_key": "k"})
result = await integration.execute_action("test_action", {"name": "x"}, ctx)
assert result.type == ResultType.VALIDATION_ERROR
async def test_execute_action_error(integration):
@integration.action("test_action")
class Handler(ActionHandler):
async def execute(self, inputs, context):
return ActionError(message="something went wrong", cost_usd=0.01)
ctx = ExecutionContext(auth={"api_key": "k"})
result = await integration.execute_action("test_action", {"name": "x"}, ctx)
assert result.type == ResultType.ACTION_ERROR
assert result.result.message == "something went wrong"
assert result.result.cost_usd == 0.01
# ── Polling trigger ─────────────────────────────────────────────────────────
def test_register_polling_trigger(integration):
@integration.polling_trigger("test_trigger")
class Handler(PollingTriggerHandler):
async def poll(self, inputs, last_poll_ts, context):
return []
assert "test_trigger" in integration._polling_handlers
async def test_execute_polling_trigger_success(integration):
@integration.polling_trigger("test_trigger")
class Handler(PollingTriggerHandler):
async def poll(self, inputs, last_poll_ts, context):
return [{"id": "1", "data": {"message": "hello"}}]
ctx = ExecutionContext(auth={"api_key": "k"})
records = await integration.execute_polling_trigger(
"test_trigger", {"channel": "general"}, None, ctx
)
assert len(records) == 1
assert records[0]["id"] == "1"
assert records[0]["data"]["message"] == "hello"
async def test_execute_polling_trigger_missing_id(integration):
@integration.polling_trigger("test_trigger")
class Handler(PollingTriggerHandler):
async def poll(self, inputs, last_poll_ts, context):
return [{"data": {"message": "no id"}}]
ctx = ExecutionContext(auth={"api_key": "k"})
with pytest.raises(ValidationError, match="id"):
await integration.execute_polling_trigger(
"test_trigger", {"channel": "general"}, None, ctx
)
# ── Connected account ───────────────────────────────────────────────────────
def test_register_connected_account(integration):
@integration.connected_account()
class Handler(ConnectedAccountHandler):
async def get_account_info(self, context):
return ConnectedAccountInfo()
assert integration._connected_account_handler is not None
async def test_get_connected_account_success(integration):
@integration.connected_account()
class Handler(ConnectedAccountHandler):
async def get_account_info(self, context):
return ConnectedAccountInfo(email="a@b.com", first_name="Alice")
ctx = ExecutionContext(auth={"api_key": "k"})
result = await integration.get_connected_account(ctx)
assert result.type == ResultType.CONNECTED_ACCOUNT
assert result.result.email == "a@b.com"
assert result.result.first_name == "Alice"
async def test_get_connected_account_wrong_type(integration):
@integration.connected_account()
class Handler(ConnectedAccountHandler):
async def get_account_info(self, context):
return {"email": "raw dict"}
ctx = ExecutionContext(auth={"api_key": "k"})
with pytest.raises(ValidationError, match="ConnectedAccountInfo"):
await integration.get_connected_account(ctx)
# ── Interval parsing ────────────────────────────────────────────────────────
def test_parse_interval():
assert Integration._parse_interval("30s") == timedelta(seconds=30)
assert Integration._parse_interval("5m") == timedelta(minutes=5)
assert Integration._parse_interval("2h") == timedelta(hours=2)
assert Integration._parse_interval("1d") == timedelta(days=1)
def test_parse_interval_invalid():
with pytest.raises(ConfigurationError, match="Invalid interval"):
Integration._parse_interval("10x")
# ── Auth validation in actions ──────────────────────────────────────────────
async def test_execute_action_auth_validation_failure(integration):
"""Invalid auth credentials (missing required api_key) → VALIDATION_ERROR."""
@integration.action("test_action")
class Handler(ActionHandler):
async def execute(self, inputs, context):
return ActionResult(data={"greeting": "hi"})
ctx = ExecutionContext(auth={}) # missing required api_key
result = await integration.execute_action("test_action", {"name": "x"}, ctx)
assert result.type == ResultType.VALIDATION_ERROR
async def test_execute_action_no_auth_fields_in_config(tmp_path):
"""Config without auth.fields skips auth validation entirely."""
config = {
"name": "no-auth",
"version": "1.0.0",
"description": "No auth fields",
"auth": {},
"actions": {
"simple": {
"description": "A simple action",
"input_schema": {
"type": "object",
"properties": {"x": {"type": "string"}},
"required": ["x"],
},
"output_schema": {
"type": "object",
"properties": {"y": {"type": "string"}},
"required": ["y"],
},
}
},
}
config_file = tmp_path / "config.json"
config_file.write_text(json.dumps(config))
intg = Integration.load(config_file)
@intg.action("simple")
class Handler(ActionHandler):
async def execute(self, inputs, context):
return ActionResult(data={"y": "ok"})
ctx = ExecutionContext() # no auth at all
result = await intg.execute_action("simple", {"x": "test"}, ctx)
assert result.type == ResultType.ACTION
# ── Polling trigger edge cases ──────────────────────────────────────────────
async def test_execute_polling_trigger_not_registered(integration):
ctx = ExecutionContext(auth={"api_key": "k"})
with pytest.raises(ValidationError, match="not registered"):
await integration.execute_polling_trigger(
"test_trigger", {"channel": "general"}, None, ctx
)
async def test_execute_polling_trigger_missing_data(integration):
"""Record has 'id' but no 'data' field → ValidationError."""
@integration.polling_trigger("test_trigger")
class Handler(PollingTriggerHandler):
async def poll(self, inputs, last_poll_ts, context):
return [{"id": "1"}] # missing 'data'
ctx = ExecutionContext(auth={"api_key": "k"})
with pytest.raises(ValidationError, match="data"):
await integration.execute_polling_trigger(
"test_trigger", {"channel": "general"}, None, ctx
)
async def test_execute_polling_trigger_output_schema_mismatch(integration):
"""Record data doesn't match output_schema → ValidationError."""
@integration.polling_trigger("test_trigger")
class Handler(PollingTriggerHandler):
async def poll(self, inputs, last_poll_ts, context):
return [{"id": "1", "data": {"wrong_field": 123}}]
ctx = ExecutionContext(auth={"api_key": "k"})
with pytest.raises(ValidationError):
await integration.execute_polling_trigger(
"test_trigger", {"channel": "general"}, None, ctx
)
async def test_execute_polling_trigger_multiple_records(integration):
@integration.polling_trigger("test_trigger")
class Handler(PollingTriggerHandler):
async def poll(self, inputs, last_poll_ts, context):
return [
{"id": "1", "data": {"message": "first"}},
{"id": "2", "data": {"message": "second"}},
{"id": "3", "data": {"message": "third"}},
]
ctx = ExecutionContext(auth={"api_key": "k"})
records = await integration.execute_polling_trigger(
"test_trigger", {"channel": "general"}, None, ctx
)
assert len(records) == 3
assert [r["id"] for r in records] == ["1", "2", "3"]
# ── Connected account edge cases ────────────────────────────────────────────
async def test_get_connected_account_no_handler(integration):
ctx = ExecutionContext(auth={"api_key": "k"})
with pytest.raises(ValidationError, match="No connected account handler"):
await integration.get_connected_account(ctx)
async def test_get_connected_account_auth_validation_failure(integration):
@integration.connected_account()
class Handler(ConnectedAccountHandler):
async def get_account_info(self, context):
return ConnectedAccountInfo(email="a@b.com")
ctx = ExecutionContext(auth={}) # missing required api_key
with pytest.raises(ValidationError):
await integration.get_connected_account(ctx)
async def test_get_connected_account_no_auth_fields(tmp_path):
"""Config without auth.fields skips auth validation for connected accounts."""
config = {
"name": "no-auth",
"version": "1.0.0",
"description": "No auth",
"auth": {},
"actions": {},
}
config_file = tmp_path / "config.json"
config_file.write_text(json.dumps(config))
intg = Integration.load(config_file)
@intg.connected_account()
class Handler(ConnectedAccountHandler):
async def get_account_info(self, context):
return ConnectedAccountInfo(email="a@b.com")
ctx = ExecutionContext()
result = await intg.get_connected_account(ctx)
assert result.type == ResultType.CONNECTED_ACCOUNT
assert result.result.email == "a@b.com"
# ── Config loading edge cases ───────────────────────────────────────────────
def test_load_config_default_path():
"""Integration.load() with no args uses __file__-relative default path."""
with pytest.raises(ConfigurationError, match="not found"):
Integration.load() # no config.json at the default location
def test_load_config_no_actions_no_triggers(tmp_path):
config = {
"name": "empty",
"version": "1.0.0",
"description": "No actions or triggers",
"auth": {},
"actions": {},
}
config_file = tmp_path / "config.json"
config_file.write_text(json.dumps(config))
intg = Integration.load(config_file)
assert intg.config.name == "empty"
assert intg.config.actions == {}
assert intg.config.polling_triggers == {}
# ── Multiple actions / re-registration ──────────────────────────────────────
async def test_multiple_actions(tmp_path):
"""Register and execute two different actions independently."""
config = {
"name": "multi",
"version": "1.0.0",
"description": "Multi-action",
"auth": {},
"actions": {
"greet": {
"description": "Greet",
"input_schema": {
"type": "object",
"properties": {"name": {"type": "string"}},
"required": ["name"],
},
"output_schema": {
"type": "object",
"properties": {"msg": {"type": "string"}},
"required": ["msg"],
},
},
"add": {
"description": "Add",
"input_schema": {
"type": "object",
"properties": {"a": {"type": "integer"}, "b": {"type": "integer"}},
"required": ["a", "b"],
},
"output_schema": {
"type": "object",
"properties": {"sum": {"type": "integer"}},
"required": ["sum"],
},
},
},
}
config_file = tmp_path / "config.json"
config_file.write_text(json.dumps(config))
intg = Integration.load(config_file)
@intg.action("greet")
class GreetHandler(ActionHandler):
async def execute(self, inputs, context):
return ActionResult(data={"msg": f"Hi {inputs['name']}"})
@intg.action("add")
class AddHandler(ActionHandler):
async def execute(self, inputs, context):
return ActionResult(data={"sum": inputs["a"] + inputs["b"]})
ctx = ExecutionContext()
r1 = await intg.execute_action("greet", {"name": "Alice"}, ctx)
assert r1.type == ResultType.ACTION
assert r1.result.data == {"msg": "Hi Alice"}
r2 = await intg.execute_action("add", {"a": 2, "b": 3}, ctx)
assert r2.type == ResultType.ACTION
assert r2.result.data == {"sum": 5}
def test_register_polling_trigger_not_in_config(integration):
with pytest.raises(ConfigurationError, match="not defined in config"):
@integration.polling_trigger("nonexistent_trigger")
class Handler(PollingTriggerHandler):
async def poll(self, inputs, last_poll_ts, context):
return []
async def test_execute_polling_trigger_invalid_inputs(integration):
"""Invalid inputs against trigger input_schema → ValidationError."""
@integration.polling_trigger("test_trigger")
class Handler(PollingTriggerHandler):
async def poll(self, inputs, last_poll_ts, context):
return []
ctx = ExecutionContext(auth={"api_key": "k"})
with pytest.raises(ValidationError):
await integration.execute_polling_trigger(
"test_trigger", {}, None, ctx # missing required 'channel'
)
async def test_execute_polling_trigger_auth_failure(integration):
"""Invalid auth credentials for polling trigger → ValidationError."""
@integration.polling_trigger("test_trigger")
class Handler(PollingTriggerHandler):
async def poll(self, inputs, last_poll_ts, context):
return []
ctx = ExecutionContext(auth={}) # missing required api_key
with pytest.raises(ValidationError):
await integration.execute_polling_trigger(
"test_trigger", {"channel": "general"}, None, ctx
)
def test_re_register_handler_overwrites(integration):
"""Decorating the same action name twice overwrites the first handler."""
@integration.action("test_action")
class First(ActionHandler):
async def execute(self, inputs, context):
return ActionResult(data={"greeting": "first"})
@integration.action("test_action")
class Second(ActionHandler):
async def execute(self, inputs, context):
return ActionResult(data={"greeting": "second"})
assert integration._action_handlers["test_action"] is Second
# ── Abstract base class pass-through ───────────────────────────────────────
async def test_action_handler_abstract_execute_returns_none():
"""super().execute() on the ABC body executes the `pass` and returns None."""
class Concrete(ActionHandler):
async def execute(self, inputs, context):
return await super().execute(inputs, context)
result = await Concrete().execute({}, None)
assert result is None
async def test_polling_trigger_handler_abstract_poll_returns_none():
"""super().poll() on the ABC body executes the `pass` and returns None."""
class Concrete(PollingTriggerHandler):
async def poll(self, inputs, last_poll_ts, context):
return await super().poll(inputs, last_poll_ts, context)
result = await Concrete().poll({}, None, None)
assert result is None
async def test_connected_account_handler_abstract_get_account_info_returns_none():
"""super().get_account_info() on the ABC body executes the `pass` and returns None."""
class Concrete(ConnectedAccountHandler):
async def get_account_info(self, context):
return await super().get_account_info(context)
result = await Concrete().get_account_info(None)
assert result is None