@@ -621,6 +621,52 @@ def test_prepare_a2a_invocation_api_key_auth():
621621 assert prepared .headers ["Authorization" ] == "Bearer my-api-key"
622622
623623
624+ def test_prepare_a2a_invocation_api_key_auth_falls_back_to_raw_value_on_decode_error (monkeypatch ):
625+ monkeypatch .setattr ("mcpgateway.services.a2a_protocol.decode_auth" , MagicMock (side_effect = ValueError ("boom" )))
626+
627+ prepared = prepare_a2a_invocation (
628+ agent_type = "generic" ,
629+ endpoint_url = "https://example.com/" ,
630+ protocol_version = "1.0.0" ,
631+ parameters = {"query" : "hi" },
632+ interaction_type = "query" ,
633+ auth_type = "api_key" ,
634+ auth_value = "raw-api-key" , # pragma: allowlist secret
635+ )
636+
637+ assert prepared .headers ["Authorization" ] == "Bearer raw-api-key" # pragma: allowlist secret
638+
639+
640+ def test_prepare_a2a_invocation_api_key_auth_uses_decoded_scalar (monkeypatch ):
641+ monkeypatch .setattr ("mcpgateway.services.a2a_protocol.decode_auth" , lambda _val : "decoded-key" )
642+
643+ prepared = prepare_a2a_invocation (
644+ agent_type = "generic" ,
645+ endpoint_url = "https://example.com/" ,
646+ protocol_version = "1.0.0" ,
647+ parameters = {"query" : "hi" },
648+ interaction_type = "query" ,
649+ auth_type = "api_key" ,
650+ auth_value = "encrypted-api-key" , # pragma: allowlist secret
651+ )
652+
653+ assert prepared .headers ["Authorization" ] == "Bearer decoded-key" # pragma: allowlist secret
654+
655+
656+ def test_prepare_a2a_invocation_api_key_auth_from_mapping_uses_first_value ():
657+ prepared = prepare_a2a_invocation (
658+ agent_type = "generic" ,
659+ endpoint_url = "https://example.com/" ,
660+ protocol_version = "1.0.0" ,
661+ parameters = {"query" : "hi" },
662+ interaction_type = "query" ,
663+ auth_type = "api_key" ,
664+ auth_value = {"x-api-key" : "mapped-key" }, # pragma: allowlist secret
665+ )
666+
667+ assert prepared .headers ["Authorization" ] == "Bearer mapped-key" # pragma: allowlist secret
668+
669+
624670def test_prepare_a2a_invocation_rejects_non_mapping_decoded_auth (monkeypatch ):
625671 """A decoded auth value that is not a mapping should raise ValueError."""
626672 monkeypatch .setattr ("mcpgateway.services.a2a_protocol.decode_auth" , lambda _val : "not-a-mapping" )
0 commit comments