284
client.connected_accounts.update_status = Mock()
285
client.connected_accounts.refresh = Mock()
286
client.connected_accounts.create = Mock()
287
+ client.connected_accounts.patch = Mock()
288
client.link.create = Mock()
289
return client
290
526
assert result == "connected"
527
528
529
+def _make_bad_request_error(message: str):
530
+ """Build a BadRequestError instance for testing the error mapper.
531
+
532
+ The composio_client BadRequestError constructor signature is internal —
533
+ rather than depend on it, we stub a minimal object that ``str(error)``
534
+ surfaces the message and which is recognized as the BadRequestError type
535
+ by ``isinstance``. This mirrors how production responses arrive: the
536
+ error class with a message body.
537
+ """
538
+ from composio_client import BadRequestError
539
+
540
+ error = BadRequestError.__new__(BadRequestError)
541
+ Exception.__init__(error, message)
542
+ return error
543
+
544
+
545
+class TestConnectedAccountsAcl:
546
+ """Tests for accountType + per-user ACL surfacing (Hermes #9860, #9902).
547
+
548
+ Mirrors the 12 behaviour tests in
549
+ ``ts/packages/core/test/connectedAccounts/connectedAccounts.test.ts``.
550
+ """
551
+
552
+ @pytest.fixture
553
+ def mock_client(self):
554
+ client = Mock()
555
+ client.connected_accounts.retrieve = Mock()
556
+ client.connected_accounts.list = Mock()
557
+ client.connected_accounts.delete = Mock()
558
+ client.connected_accounts.update_status = Mock()
559
+ client.connected_accounts.refresh = Mock()
560
+ client.connected_accounts.create = Mock()
561
+ client.connected_accounts.patch = Mock()
562
+ client.link.create = Mock()
563
+ # Default: no existing connections, so link() doesn't trip the guard.
564
+ no_accounts = Mock()
565
+ no_accounts.items = []
566
+ client.connected_accounts.list.return_value = no_accounts
567
+ # Default link.create response — overridden per test where the
568
+ # response shape matters.
569
+ default_link = Mock()
570
+ default_link.connected_account_id = "ca_test_shared"
571
+ default_link.redirect_url = "https://redirect"
572
+ client.link.create.return_value = default_link
573
+ return client
574
+
575
+ @pytest.fixture
576
+ def connected_accounts(self, mock_client):
577
+ return ConnectedAccounts(client=mock_client)
578
+
579
+ # -- link() with accountType + ACL forwarding ---------------------------
580
+
581
+ def test_link_forwards_account_type_and_nested_acl(
582
+ self, connected_accounts, mock_client
583
+ ):
584
+ connected_accounts.link(
585
+ user_id="user_creator",
586
+ auth_config_id="auth_config_123",
587
+ account_type="SHARED",
588
+ acl_config_for_shared={
589
+ "allow_all_users": True,
590
+ "not_allowed_user_ids": ["user_bob"],
591
+ },
592
+ )
593
+
594
+ call_kwargs = mock_client.link.create.call_args.kwargs
595
+ assert call_kwargs["account_type"] == "SHARED"
596
+ assert call_kwargs["acl_config_for_shared"] == {
597
+ "allow_all_users": True,
598
+ "not_allowed_user_ids": ["user_bob"],
599
+ }
600
+
601
+ def test_link_omits_acl_when_not_provided(
602
+ self, connected_accounts, mock_client
603
+ ):
604
+ connected_accounts.link(
605
+ user_id="user_creator",
606
+ auth_config_id="auth_config_123",
607
+ account_type="SHARED",
608
+ )
609
+
610
+ call_kwargs = mock_client.link.create.call_args.kwargs
611
+ assert call_kwargs["account_type"] == "SHARED"
612
+ assert call_kwargs["acl_config_for_shared"] is omit
613
+
614
+ def test_link_forwards_only_provided_acl_fields(
615
+ self, connected_accounts, mock_client
616
+ ):
617
+ connected_accounts.link(
618
+ user_id="user_creator",
619
+ auth_config_id="auth_config_123",
620
+ account_type="SHARED",
621
+ acl_config_for_shared={"allowed_user_ids": ["user_alice"]},
622
+ )
623
+
624
+ call_kwargs = mock_client.link.create.call_args.kwargs
625
+ assert call_kwargs["acl_config_for_shared"] == {
626
+ "allowed_user_ids": ["user_alice"]
627
+ }
628
+
629
+ def test_link_preserves_explicit_empty_lists(
630
+ self, connected_accounts, mock_client
631
+ ):
632
+ """An empty list is meaningful (clear the allow/deny list)."""
633
+ connected_accounts.link(
634
+ user_id="user_creator",
635
+ auth_config_id="auth_config_123",
636
+ account_type="SHARED",
637
+ acl_config_for_shared={
638
+ "allowed_user_ids": [],
639
+ "not_allowed_user_ids": [],
640
+ },
641
+ )
642
+
643
+ call_kwargs = mock_client.link.create.call_args.kwargs
644
+ assert call_kwargs["acl_config_for_shared"] == {
645
+ "allowed_user_ids": [],
646
+ "not_allowed_user_ids": [],
647
+ }
648
+
649
+ def test_link_maps_acl_only_for_shared_to_typed_error(
650
+ self, connected_accounts, mock_client
651
+ ):
652
+ mock_client.link.create.side_effect = _make_bad_request_error(
653
+ "acl_config_for_shared is only valid on SHARED connections."
654
+ )
655
+
656
+ with pytest.raises(exceptions.ComposioAclOnlyForSharedError):
657
+ connected_accounts.link(
658
+ user_id="user_creator",
659
+ auth_config_id="auth_config_123",
660
+ account_type="PRIVATE",
661
+ acl_config_for_shared={"allow_all_users": True},
662
+ )
663
+
664
+ def test_link_rethrows_non_acl_bad_request_errors(
665
+ self, connected_accounts, mock_client
666
+ ):
667
+ unrelated = _make_bad_request_error("auth_config_id is required")
668
+ mock_client.link.create.side_effect = unrelated
669
+
670
+ from composio_client import BadRequestError
671
+
672
+ with pytest.raises(BadRequestError):
673
+ connected_accounts.link(
674
+ user_id="user_creator", auth_config_id="auth_config_123"
675
+ )
676
+
677
+ # -- update_acl() body construction + mapper ----------------------------
678
+
679
+ def test_update_acl_serializes_nested_body(
680
+ self, connected_accounts, mock_client
681
+ ):
682
+ response = Mock()
683
+ response.id = "ca_abc"
684
+ response.status = "ACTIVE"
685
+ response.success = True
686
+ mock_client.connected_accounts.patch.return_value = response
687
+
688
+ result = connected_accounts.update_acl(
689
+ "ca_abc",
690
+ allow_all_users=True,
691
+ not_allowed_user_ids=["user_bob"],
692
+ )
693
+
694
+ mock_client.connected_accounts.patch.assert_called_once_with(
695
+ "ca_abc",
696
+ acl_config_for_shared={
697
+ "allow_all_users": True,
698
+ "not_allowed_user_ids": ["user_bob"],
699
+ },
700
+ )
701
+ assert result is response
702
+
703
+ def test_update_acl_omits_absent_fields(self, connected_accounts, mock_client):
704
+ connected_accounts.update_acl("ca_abc", allowed_user_ids=["user_alice"])
705
+
706
+ mock_client.connected_accounts.patch.assert_called_once_with(
707
+ "ca_abc",
708
+ acl_config_for_shared={"allowed_user_ids": ["user_alice"]},
709
+ )
710
+
711
+ def test_update_acl_preserves_empty_array(self, connected_accounts, mock_client):
712
+ connected_accounts.update_acl("ca_abc", allowed_user_ids=[])
713
+
714
+ mock_client.connected_accounts.patch.assert_called_once_with(
715
+ "ca_abc",
716
+ acl_config_for_shared={"allowed_user_ids": []},
717
+ )
718
+
719
+ def test_update_acl_rejects_all_none(self, connected_accounts, mock_client):
720
+ with pytest.raises(exceptions.ValidationError):
721
+ connected_accounts.update_acl("ca_abc")
722
+ mock_client.connected_accounts.patch.assert_not_called()
723
+
724
+ def test_update_acl_maps_acl_only_for_shared_to_typed_error(
725
+ self, connected_accounts, mock_client
726
+ ):
727
+ mock_client.connected_accounts.patch.side_effect = _make_bad_request_error(
728
+ "acl_config_for_shared is only valid on SHARED connections."
729
+ )
730
+
731
+ with pytest.raises(exceptions.ComposioAclOnlyForSharedError):
732
+ connected_accounts.update_acl("ca_abc", allow_all_users=True)
733
+
734
+ def test_update_acl_rethrows_non_acl_bad_request_errors(
735
+ self, connected_accounts, mock_client
736
+ ):
737
+ unrelated = _make_bad_request_error("some other 400")
738
+ mock_client.connected_accounts.patch.side_effect = unrelated
739
+
740
+ from composio_client import BadRequestError
741
+
742
+ with pytest.raises(BadRequestError):
743
+ connected_accounts.update_acl("ca_abc", allow_all_users=True)
744
+
745
+
746
# SEC-339: initiate() must gate its DeprecationWarning on the response
747
# `Deprecation` HTTP header (RFC 9745) that apollo emits only on the
748
# retiring branch (Composio-managed + redirectable OAuth). These tests pin