6
6
7
7
from marshmallow import EXCLUDE
8
8
9
+ from ...cache .base import BaseCache
10
+ from ...cache .in_memory import InMemoryCache
9
11
from ...config .injection_context import InjectionContext
10
12
from ...core .event_bus import EventBus
11
13
from ...core .in_memory import InMemoryProfile
@@ -413,12 +415,85 @@ async def test_create_send_outbound(self):
413
415
profile ,
414
416
settings = {"timing.enabled" : True },
415
417
)
418
+ registry = profile .inject (ProtocolRegistry )
419
+ registry .register_message_types (
420
+ {
421
+ pfx .qualify (StubAgentMessage .Meta .message_type ): StubAgentMessage
422
+ for pfx in DIDCommPrefix
423
+ }
424
+ )
416
425
message = StubAgentMessage ()
417
426
responder = test_module .DispatcherResponder (context , message , None )
418
- outbound_message = await responder .create_outbound (message )
419
- with async_mock .patch .object (responder , "_send" , async_mock .AsyncMock ()):
427
+ outbound_message = await responder .create_outbound (
428
+ json .dumps (message .serialize ())
429
+ )
430
+ with async_mock .patch .object (
431
+ responder , "_send" , async_mock .AsyncMock ()
432
+ ), async_mock .patch .object (
433
+ test_module .BaseResponder ,
434
+ "conn_rec_active_state_check" ,
435
+ async_mock .AsyncMock (return_value = True ),
436
+ ):
420
437
await responder .send_outbound (outbound_message )
421
438
439
+ async def test_create_send_outbound_with_msg_attrs (self ):
440
+ profile = make_profile ()
441
+ context = RequestContext (
442
+ profile ,
443
+ settings = {"timing.enabled" : True },
444
+ )
445
+ registry = profile .inject (ProtocolRegistry )
446
+ registry .register_message_types (
447
+ {
448
+ pfx .qualify (StubAgentMessage .Meta .message_type ): StubAgentMessage
449
+ for pfx in DIDCommPrefix
450
+ }
451
+ )
452
+ message = StubAgentMessage ()
453
+ responder = test_module .DispatcherResponder (context , message , None )
454
+ outbound_message = await responder .create_outbound (message )
455
+ with async_mock .patch .object (
456
+ responder , "_send" , async_mock .AsyncMock ()
457
+ ), async_mock .patch .object (
458
+ test_module .BaseResponder ,
459
+ "conn_rec_active_state_check" ,
460
+ async_mock .AsyncMock (return_value = True ),
461
+ ):
462
+ await responder .send_outbound (
463
+ message = outbound_message ,
464
+ message_type = message ._message_type ,
465
+ message_id = message ._id ,
466
+ )
467
+
468
+ async def test_create_send_outbound_with_msg_attrs_x (self ):
469
+ profile = make_profile ()
470
+ context = RequestContext (
471
+ profile ,
472
+ settings = {"timing.enabled" : True },
473
+ )
474
+ registry = profile .inject (ProtocolRegistry )
475
+ registry .register_message_types (
476
+ {
477
+ pfx .qualify (StubAgentMessage .Meta .message_type ): StubAgentMessage
478
+ for pfx in DIDCommPrefix
479
+ }
480
+ )
481
+ message = StubAgentMessage ()
482
+ responder = test_module .DispatcherResponder (context , message , None )
483
+ outbound_message = await responder .create_outbound (message )
484
+ outbound_message .connection_id = "123"
485
+ with async_mock .patch .object (
486
+ test_module .BaseResponder ,
487
+ "conn_rec_active_state_check" ,
488
+ async_mock .AsyncMock (return_value = False ),
489
+ ):
490
+ with self .assertRaises (RuntimeError ):
491
+ await responder .send_outbound (
492
+ message = outbound_message ,
493
+ message_type = message ._message_type ,
494
+ message_id = message ._id ,
495
+ )
496
+
422
497
async def test_create_send_webhook (self ):
423
498
profile = make_profile ()
424
499
context = RequestContext (profile )
@@ -427,16 +502,81 @@ async def test_create_send_webhook(self):
427
502
with pytest .deprecated_call ():
428
503
await responder .send_webhook ("topic" , {"pay" : "load" })
429
504
505
+ async def test_conn_rec_active_state_check_a (self ):
506
+ profile = make_profile ()
507
+ profile .context .injector .bind_instance (BaseCache , InMemoryCache ())
508
+ context = RequestContext (profile )
509
+ message = StubAgentMessage ()
510
+ responder = test_module .DispatcherResponder (context , message , None )
511
+ with async_mock .patch .object (
512
+ test_module .ConnRecord , "retrieve_by_id" , async_mock .AsyncMock ()
513
+ ) as mock_conn_ret_by_id :
514
+ conn_rec = test_module .ConnRecord ()
515
+ conn_rec .state = test_module .ConnRecord .State .COMPLETED
516
+ mock_conn_ret_by_id .return_value = conn_rec
517
+ check_flag = await responder .conn_rec_active_state_check (
518
+ profile ,
519
+ "conn-id" ,
520
+ )
521
+ assert check_flag
522
+ check_flag = await responder .conn_rec_active_state_check (
523
+ profile ,
524
+ "conn-id" ,
525
+ )
526
+ assert check_flag
527
+
528
+ async def test_conn_rec_active_state_check_b (self ):
529
+ profile = make_profile ()
530
+ profile .context .injector .bind_instance (BaseCache , InMemoryCache ())
531
+ profile .context .injector .bind_instance (
532
+ EventBus , async_mock .MagicMock (notify = async_mock .AsyncMock ())
533
+ )
534
+ context = RequestContext (profile )
535
+ message = StubAgentMessage ()
536
+ responder = test_module .DispatcherResponder (context , message , None )
537
+ with async_mock .patch .object (
538
+ test_module .ConnRecord , "retrieve_by_id" , async_mock .AsyncMock ()
539
+ ) as mock_conn_ret_by_id :
540
+ conn_rec_a = test_module .ConnRecord ()
541
+ conn_rec_a .state = test_module .ConnRecord .State .REQUEST
542
+ conn_rec_b = test_module .ConnRecord ()
543
+ conn_rec_b .state = test_module .ConnRecord .State .COMPLETED
544
+ mock_conn_ret_by_id .side_effect = [conn_rec_a , conn_rec_b ]
545
+ check_flag = await responder .conn_rec_active_state_check (
546
+ profile ,
547
+ "conn-id" ,
548
+ )
549
+ assert check_flag
550
+
430
551
async def test_create_enc_outbound (self ):
431
552
profile = make_profile ()
432
553
context = RequestContext (profile )
433
- message = b"abc123xyz7890000"
554
+ message = StubAgentMessage ()
434
555
responder = test_module .DispatcherResponder (context , message , None )
435
556
with async_mock .patch .object (
436
557
responder , "send_outbound" , async_mock .AsyncMock ()
437
558
) as mock_send_outbound :
438
559
await responder .send (message )
439
560
assert mock_send_outbound .called_once ()
561
+ msg_json = json .dumps (StubAgentMessage ().serialize ())
562
+ message = msg_json .encode ("utf-8" )
563
+ with async_mock .patch .object (
564
+ responder , "send_outbound" , async_mock .AsyncMock ()
565
+ ) as mock_send_outbound :
566
+ await responder .send (message )
567
+
568
+ message = StubAgentMessage ()
569
+ with async_mock .patch .object (
570
+ responder , "send_outbound" , async_mock .AsyncMock ()
571
+ ) as mock_send_outbound :
572
+ await responder .send_reply (message )
573
+ assert mock_send_outbound .called_once ()
574
+
575
+ message = json .dumps (StubAgentMessage ().serialize ())
576
+ with async_mock .patch .object (
577
+ responder , "send_outbound" , async_mock .AsyncMock ()
578
+ ) as mock_send_outbound :
579
+ await responder .send_reply (message )
440
580
441
581
async def test_expired_context_x (self ):
442
582
def _smaller_scope ():
0 commit comments