@@ -201,8 +201,8 @@ def test_cloudflare_get_dnssec_status(self, tools_registry, monkeypatch):
201201 ):
202202 fn = tools_registry ["cloudflare_get_dnssec_status" ].fn
203203 result = fn (zone_id = "z_test" )
204- assert isinstance (result , dict )
205- assert "error" not in result
204+ assert isinstance (result , dict )
205+ assert "error" not in result
206206
207207 def test_cloudflare_update_zone_setting (self , tools_registry , monkeypatch ):
208208 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -516,8 +516,8 @@ def test_cloudflare_get_performance_analytics(self, tools_registry, monkeypatch)
516516 ):
517517 fn = tools_registry ["cloudflare_get_performance_analytics" ].fn
518518 result = fn (zone_id = "z_test" )
519- if isinstance (result , dict ):
520- assert "error" not in result
519+ assert isinstance (result , dict )
520+ assert "error" not in result
521521
522522 def test_cloudflare_get_http_analytics_report (self , tools_registry , monkeypatch ):
523523 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -537,8 +537,8 @@ def test_cloudflare_get_http_analytics_report(self, tools_registry, monkeypatch)
537537 ):
538538 fn = tools_registry ["cloudflare_get_http_analytics_report" ].fn
539539 result = fn (zone_id = "z_test" )
540- if isinstance (result , dict ):
541- assert "error" not in result
540+ assert isinstance (result , dict )
541+ assert "error" not in result
542542
543543 def test_cloudflare_list_firewall_events (self , tools_registry , monkeypatch ):
544544 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -579,8 +579,8 @@ def test_cloudflare_get_security_settings(self, tools_registry, monkeypatch):
579579 ):
580580 fn = tools_registry ["cloudflare_get_security_settings" ].fn
581581 result = fn (zone_id = "z_test" )
582- if isinstance (result , dict ):
583- assert "error" not in result
582+ assert isinstance (result , dict )
583+ assert "error" not in result
584584
585585 def test_cloudflare_list_page_rules (self , tools_registry , monkeypatch ):
586586 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -600,8 +600,8 @@ def test_cloudflare_list_page_rules(self, tools_registry, monkeypatch):
600600 ):
601601 fn = tools_registry ["cloudflare_list_page_rules" ].fn
602602 result = fn (zone_id = "z_test" )
603- if isinstance (result , dict ):
604- assert "error" not in result
603+ assert isinstance (result , dict )
604+ assert "error" not in result
605605
606606 def test_cloudflare_list_waf_rulesets (self , tools_registry , monkeypatch ):
607607 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -621,8 +621,8 @@ def test_cloudflare_list_waf_rulesets(self, tools_registry, monkeypatch):
621621 ):
622622 fn = tools_registry ["cloudflare_list_waf_rulesets" ].fn
623623 result = fn (zone_id = "z_test" )
624- if isinstance (result , dict ):
625- assert "error" not in result
624+ assert isinstance (result , dict )
625+ assert "error" not in result
626626
627627 def test_cloudflare_get_bot_management_settings (self , tools_registry , monkeypatch ):
628628 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -642,8 +642,8 @@ def test_cloudflare_get_bot_management_settings(self, tools_registry, monkeypatc
642642 ):
643643 fn = tools_registry ["cloudflare_get_bot_management_settings" ].fn
644644 result = fn (zone_id = "z_test" )
645- if isinstance (result , dict ):
646- assert "error" not in result
645+ assert isinstance (result , dict )
646+ assert "error" not in result
647647
648648 def test_cloudflare_list_managed_transforms (self , tools_registry , monkeypatch ):
649649 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -663,8 +663,8 @@ def test_cloudflare_list_managed_transforms(self, tools_registry, monkeypatch):
663663 ):
664664 fn = tools_registry ["cloudflare_list_managed_transforms" ].fn
665665 result = fn (zone_id = "z_test" )
666- if isinstance (result , dict ):
667- assert "error" not in result
666+ assert isinstance (result , dict )
667+ assert "error" not in result
668668
669669 def test_cloudflare_get_ddos_protection_settings (self , tools_registry , monkeypatch ):
670670 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -684,8 +684,8 @@ def test_cloudflare_get_ddos_protection_settings(self, tools_registry, monkeypat
684684 ):
685685 fn = tools_registry ["cloudflare_get_ddos_protection_settings" ].fn
686686 result = fn (zone_id = "z_test" )
687- if isinstance (result , dict ):
688- assert "error" not in result
687+ assert isinstance (result , dict )
688+ assert "error" not in result
689689
690690 def test_cloudflare_create_firewall_rule (self , tools_registry , monkeypatch ):
691691 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -705,8 +705,8 @@ def test_cloudflare_create_firewall_rule(self, tools_registry, monkeypatch):
705705 ):
706706 fn = tools_registry ["cloudflare_create_firewall_rule" ].fn
707707 result = fn (zone_id = "z_test" , action = "block" , expression = "ip.src eq 1.2.3.4" )
708- if isinstance (result , dict ):
709- assert "error" not in result
708+ assert isinstance (result , dict )
709+ assert "error" not in result
710710
711711 def test_cloudflare_delete_firewall_rule (self , tools_registry , monkeypatch ):
712712 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -726,8 +726,8 @@ def test_cloudflare_delete_firewall_rule(self, tools_registry, monkeypatch):
726726 ):
727727 fn = tools_registry ["cloudflare_delete_firewall_rule" ].fn
728728 result = fn (zone_id = "z_test" , rule_id = "rule1" )
729- if isinstance (result , dict ):
730- assert "error" not in result
729+ assert isinstance (result , dict )
730+ assert "error" not in result
731731
732732 def test_cloudflare_get_speed_settings (self , tools_registry , monkeypatch ):
733733 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -852,8 +852,8 @@ def test_cloudflare_purge_cache_files(self, tools_registry, monkeypatch):
852852 ):
853853 fn = tools_registry ["cloudflare_purge_cache_files" ].fn
854854 result = fn (zone_id = "z_test" , urls = ["https://example.com/a" ])
855- if isinstance (result , dict ):
856- assert "error" not in result
855+ assert isinstance (result , dict )
856+ assert "error" not in result
857857
858858 def test_cloudflare_list_advanced_services (self , tools_registry , monkeypatch ):
859859 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -873,8 +873,8 @@ def test_cloudflare_list_advanced_services(self, tools_registry, monkeypatch):
873873 ):
874874 fn = tools_registry ["cloudflare_list_advanced_services" ].fn
875875 result = fn (zone_id = "z_test" )
876- if isinstance (result , dict ):
877- assert "error" not in result
876+ assert isinstance (result , dict )
877+ assert "error" not in result
878878
879879 def test_cloudflare_list_accounts (self , tools_registry , monkeypatch ):
880880 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -894,8 +894,8 @@ def test_cloudflare_list_accounts(self, tools_registry, monkeypatch):
894894 ):
895895 fn = tools_registry ["cloudflare_list_accounts" ].fn
896896 result = fn ()
897- if isinstance (result , dict ):
898- assert "error" not in result
897+ assert isinstance (result , dict )
898+ assert "error" not in result
899899
900900 def test_cloudflare_get_account_details (self , tools_registry , monkeypatch ):
901901 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -915,8 +915,8 @@ def test_cloudflare_get_account_details(self, tools_registry, monkeypatch):
915915 ):
916916 fn = tools_registry ["cloudflare_get_account_details" ].fn
917917 result = fn (account_id = "acct1" )
918- if isinstance (result , dict ):
919- assert "error" not in result
918+ assert isinstance (result , dict )
919+ assert "error" not in result
920920
921921 def test_cloudflare_list_account_members (self , tools_registry , monkeypatch ):
922922 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -936,8 +936,8 @@ def test_cloudflare_list_account_members(self, tools_registry, monkeypatch):
936936 ):
937937 fn = tools_registry ["cloudflare_list_account_members" ].fn
938938 result = fn (account_id = "acct1" )
939- if isinstance (result , dict ):
940- assert "error" not in result
939+ assert isinstance (result , dict )
940+ assert "error" not in result
941941
942942 def test_cloudflare_invite_account_member (self , tools_registry , monkeypatch ):
943943 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -957,8 +957,8 @@ def test_cloudflare_invite_account_member(self, tools_registry, monkeypatch):
957957 ):
958958 fn = tools_registry ["cloudflare_invite_account_member" ].fn
959959 result = fn (account_id = "acct1" , email = "a@b.com" , roles = ["admin" ])
960- if isinstance (result , dict ):
961- assert "error" not in result
960+ assert isinstance (result , dict )
961+ assert "error" not in result
962962
963963 def test_cloudflare_delete_account_member (self , tools_registry , monkeypatch ):
964964 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -978,8 +978,8 @@ def test_cloudflare_delete_account_member(self, tools_registry, monkeypatch):
978978 ):
979979 fn = tools_registry ["cloudflare_delete_account_member" ].fn
980980 result = fn (account_id = "acct1" , member_id = "m1" )
981- if isinstance (result , dict ):
982- assert "error" not in result
981+ assert isinstance (result , dict )
982+ assert "error" not in result
983983
984984 def test_cloudflare_list_custom_hostnames (self , tools_registry , monkeypatch ):
985985 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -999,8 +999,8 @@ def test_cloudflare_list_custom_hostnames(self, tools_registry, monkeypatch):
999999 ):
10001000 fn = tools_registry ["cloudflare_list_custom_hostnames" ].fn
10011001 result = fn (zone_id = "z_test" )
1002- if isinstance (result , dict ):
1003- assert "error" not in result
1002+ assert isinstance (result , dict )
1003+ assert "error" not in result
10041004
10051005 def test_cloudflare_list_audit_logs (self , tools_registry , monkeypatch ):
10061006 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -1020,8 +1020,8 @@ def test_cloudflare_list_audit_logs(self, tools_registry, monkeypatch):
10201020 ):
10211021 fn = tools_registry ["cloudflare_list_audit_logs" ].fn
10221022 result = fn (account_id = "acct1" )
1023- if isinstance (result , dict ):
1024- assert "error" not in result
1023+ assert isinstance (result , dict )
1024+ assert "error" not in result
10251025
10261026 def test_cloudflare_list_firewall_rules (self , tools_registry , monkeypatch ):
10271027 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -1041,8 +1041,8 @@ def test_cloudflare_list_firewall_rules(self, tools_registry, monkeypatch):
10411041 ):
10421042 fn = tools_registry ["cloudflare_list_firewall_rules" ].fn
10431043 result = fn (zone_id = "z_test" )
1044- if isinstance (result , dict ):
1045- assert "error" not in result
1044+ assert isinstance (result , dict )
1045+ assert "error" not in result
10461046
10471047 def test_cloudflare_list_access_applications (self , tools_registry , monkeypatch ):
10481048 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -1062,8 +1062,8 @@ def test_cloudflare_list_access_applications(self, tools_registry, monkeypatch):
10621062 ):
10631063 fn = tools_registry ["cloudflare_list_access_applications" ].fn
10641064 result = fn (account_id = "acct1" )
1065- if isinstance (result , dict ):
1066- assert "error" not in result
1065+ assert isinstance (result , dict )
1066+ assert "error" not in result
10671067
10681068 def test_cloudflare_list_r2_buckets (self , tools_registry , monkeypatch ):
10691069 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -1083,8 +1083,8 @@ def test_cloudflare_list_r2_buckets(self, tools_registry, monkeypatch):
10831083 ):
10841084 fn = tools_registry ["cloudflare_list_r2_buckets" ].fn
10851085 result = fn (account_id = "acct1" )
1086- if isinstance (result , dict ):
1087- assert "error" not in result
1086+ assert isinstance (result , dict )
1087+ assert "error" not in result
10881088
10891089 def test_cloudflare_list_pages_projects (self , tools_registry , monkeypatch ):
10901090 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -1104,8 +1104,8 @@ def test_cloudflare_list_pages_projects(self, tools_registry, monkeypatch):
11041104 ):
11051105 fn = tools_registry ["cloudflare_list_pages_projects" ].fn
11061106 result = fn (account_id = "acct1" )
1107- if isinstance (result , dict ):
1108- assert "error" not in result
1107+ assert isinstance (result , dict )
1108+ assert "error" not in result
11091109
11101110 def test_cloudflare_create_access_policy (self , tools_registry , monkeypatch ):
11111111 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -1127,8 +1127,8 @@ def test_cloudflare_create_access_policy(self, tools_registry, monkeypatch):
11271127 result = fn (
11281128 account_id = "acct1" , application_id = "app1" , name = "p1" , decision = "allow" , include = []
11291129 )
1130- if isinstance (result , dict ):
1131- assert "error" not in result
1130+ assert isinstance (result , dict )
1131+ assert "error" not in result
11321132
11331133 def test_cloudflare_create_worker_route (self , tools_registry , monkeypatch ):
11341134 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -1148,8 +1148,8 @@ def test_cloudflare_create_worker_route(self, tools_registry, monkeypatch):
11481148 ):
11491149 fn = tools_registry ["cloudflare_create_worker_route" ].fn
11501150 result = fn (zone_id = "z_test" , pattern = "api.example.com/*" , script_name = "s1" )
1151- if isinstance (result , dict ):
1152- assert "error" not in result
1151+ assert isinstance (result , dict )
1152+ assert "error" not in result
11531153
11541154 def test_cloudflare_set_ssl_mode (self , tools_registry , monkeypatch ):
11551155 monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "test-key" )
@@ -1174,6 +1174,31 @@ def test_cloudflare_set_ssl_mode(self, tools_registry, monkeypatch):
11741174
11751175
11761176class TestCloudflareEdgeCases :
1177+ def test_credentials_dict_preferred_over_env_var (self , mcp , monkeypatch ):
1178+ # Set environment variable
1179+ monkeypatch .setenv ("CLOUDFLARE_API_TOKEN" , "env-key" )
1180+
1181+ # Register with credentials dict that has a different key
1182+ # We must call register_tools directly instead of using the tools_registry fixture
1183+ # which doesn't pass a credentials dictionary.
1184+ register_tools (mcp , credentials = {"cloudflare" : "dict-key" })
1185+ tools = mcp ._tool_manager ._tools
1186+ fn = tools ["cloudflare_list_zones" ].fn
1187+
1188+ with patch ("aden_tools.tools.cloudflare_tool.cloudflare_tool.httpx.request" ) as mock_req :
1189+ mock_req .return_value = _mock_success_response ()
1190+ result = fn ()
1191+
1192+ # Assert success
1193+ assert isinstance (result , dict )
1194+ assert "error" not in result
1195+
1196+ # Verify the dict-key was used, not the env-key
1197+ mock_req .assert_called_once ()
1198+ args , kwargs = mock_req .call_args
1199+ headers = kwargs .get ("headers" , {})
1200+ assert headers .get ("Authorization" ) == "Bearer dict-key"
1201+
11771202 def test_missing_or_invalid_token (self , tools_registry , monkeypatch ):
11781203 # Unset the environment variable
11791204 monkeypatch .delenv ("CLOUDFLARE_API_TOKEN" , raising = False )
0 commit comments