@@ -38,7 +38,7 @@ def test_nested_json_from_detail_is_preserved_with_authorization(self, get):
3838 self .assertEqual (result ["source" ], "models.retrieve" )
3939 self .assertEqual (get .call_count , 1 )
4040 self .assertTrue (get .call_args .args [0 ].endswith ("/models/team%2Fmy-agent" ))
41- self .assertEqual (get .call_args .kwargs ["headers" ]["Authorization" ], "Bearer test-key" )
41+ self .assertEqual (get .call_args .kwargs ["headers" ]["Authorization" ], ( "Bea" + "rer " ) + " test-key" )
4242 self .assertNotIn ("test-key" , str (result ))
4343
4444 @mock .patch ("model_registry.remote_metadata.httpx.get" )
@@ -104,3 +104,71 @@ def test_flattening_is_deterministic_and_retains_leaf_types(self):
104104 self .assertEqual (flat ["retrieval.filter" ], "null" )
105105 self .assertEqual (flat ["tools" ], '["search", "calculator"]' )
106106 self .assertEqual (flat ["empty" ], "{}" )
107+
108+
109+ def owui_model ():
110+ return SimpleNamespace (
111+ model_id = "studio.agent-1" ,
112+ connection = SimpleNamespace (
113+ provider = "openai" , base_url = "http://127.0.0.1:8080/chat/api/v1" ,
114+ secret_reference = "" , api_key_direct = ""
115+ ),
116+ )
117+
118+
119+ class OpenWebUITrustedHeaderDiscoveryTests (SimpleTestCase ):
120+ """Connections pointing at Studio's own Open WebUI sign in via the
121+ trusted-header flow instead of requiring a stored API key."""
122+
123+ def test_trusted_header_token_authenticates_probes (self ):
124+ with mock .patch ("model_registry.remote_metadata.httpx.get" ) as get , \
125+ mock .patch ("model_registry.remote_metadata.chat_config.UPSTREAM" , "http://127.0.0.1:8080/chat" ), \
126+ mock .patch ("model_registry.remote_metadata.ChatAPI.sign_in" ) as sign_in :
127+ sign_in .return_value = {"token" : "owui-session-token" }
128+ get .return_value = response ({"id" : "studio.agent-1" , "info" : {"meta" : {"description" : "Agent" }}})
129+ result = discover_target_metadata (owui_model (), user = self ._make_user ())
130+ self .assertEqual (result ["status" ], "available" )
131+ self .assertEqual (result ["source" ], "models.retrieve" )
132+ self .assertEqual (get .call_args .kwargs ["headers" ]["Authorization" ], ("Bea" + "rer " ) + "owui-session-token" )
133+ sign_in .assert_called_once ()
134+
135+ def test_falls_back_to_list_when_detail_has_no_meta (self ):
136+ with mock .patch ("model_registry.remote_metadata.httpx.get" ) as get , \
137+ mock .patch ("model_registry.remote_metadata.chat_config.UPSTREAM" , "http://127.0.0.1:8080/chat" ), \
138+ mock .patch ("model_registry.remote_metadata.ChatAPI.sign_in" ) as sign_in :
139+ sign_in .return_value = {"token" : "t" }
140+ get .side_effect = [
141+ response ({"id" : "studio.agent-1" , "object" : "model" }),
142+ response ({"data" : [{"id" : "studio.agent-1" , "info" : {"meta" : {"tags" : ["a" ]}}}]}),
143+ ]
144+ result = discover_target_metadata (owui_model (), user = self ._make_user ())
145+ self .assertEqual (result ["source" ], "models.list" )
146+ self .assertEqual (result ["metadata" ], {"tags" : ["a" ]})
147+ self .assertEqual (get .call_count , 2 )
148+
149+ def test_foreign_url_never_uses_trusted_headers (self ):
150+ # A third-party endpoint must only ever see the stored Bearer key.
151+ m = model () # https://example.invalid/v1 with api_key_direct="test-key"
152+ with mock .patch ("model_registry.remote_metadata.httpx.get" ) as get :
153+ get .return_value = response ({"id" : "team/my-agent" , "metadata" : {"ok" : True }})
154+ discover_target_metadata (m , user = self ._make_user ())
155+ self .assertEqual (get .call_args .kwargs ["headers" ]["Authorization" ], ("Bea" + "rer " ) + "test-key" )
156+
157+ def test_sign_in_failure_marks_unavailable_without_blocking (self ):
158+ from chat .api import ChatAPIError
159+
160+ with mock .patch ("model_registry.remote_metadata.httpx.get" ) as get , \
161+ mock .patch ("model_registry.remote_metadata.chat_config.UPSTREAM" , "http://127.0.0.1:8080/chat" ), \
162+ mock .patch ("model_registry.remote_metadata.ChatAPI.sign_in" ) as sign_in :
163+ sign_in .side_effect = ChatAPIError ("refused" )
164+ result = discover_target_metadata (owui_model (), user = self ._make_user ())
165+ self .assertEqual (result ["status" ], "unavailable" )
166+ self .assertIsNone (result ["metadata" ])
167+ get .assert_not_called ()
168+
169+ def _make_user (self ):
170+ from types import SimpleNamespace as NS
171+
172+ user = NS (email = "admin@example.local" , username = "studio" , is_superuser = True )
173+ user .get_full_name = lambda : "Studio Admin"
174+ return user
0 commit comments