@@ -110,101 +110,66 @@ def test_enrich_headers_injects_context_id(set_context_id):
110110 assert result [mlrun .common .schemas .HeaderNames .igz_ctx ] == context_id
111111
112112
113- def test_enrich_headers_adds_projects_role_when_leader_is_mlrun (set_context_id ):
114- set_context_id (None )
115- original_leader = mlrun .mlconf .httpdb .projects .leader
116- mlrun .mlconf .httpdb .projects .leader = "mlrun"
117-
118- try :
119- result = clients_helpers .enrich_headers (
120- headers = {}, path = "/api/v1/projects/my-project"
121- )
122- assert result [mlrun .common .schemas .HeaderNames .projects_role ] == "mlrun"
123- finally :
124- mlrun .mlconf .httpdb .projects .leader = original_leader
125-
126-
127- def test_enrich_headers_does_not_add_projects_role_when_leader_is_not_mlrun (
128- set_context_id ,
129- ):
113+ def test_enrich_headers_adds_projects_role (set_context_id ):
114+ """
115+ Test that the projects role header is always added when the path contains "projects"
116+ """
130117 set_context_id (None )
131- original_leader = mlrun .mlconf .httpdb .projects .leader
132- mlrun .mlconf .httpdb .projects .leader = "iguazio"
133-
134- try :
135- result = clients_helpers .enrich_headers (
136- headers = {}, path = "/api/v1/projects/my-project"
137- )
138- assert mlrun .common .schemas .HeaderNames .projects_role not in result
139- finally :
140- mlrun .mlconf .httpdb .projects .leader = original_leader
118+ result = clients_helpers .enrich_headers (
119+ headers = {}, path = "/api/v1/projects/my-project"
120+ )
121+ assert (
122+ result [mlrun .common .schemas .HeaderNames .projects_role ]
123+ == mlrun .mlconf .httpdb .projects .leader
124+ )
141125
142126
143127def test_enrich_headers_does_not_add_projects_role_when_path_is_none (set_context_id ):
144128 set_context_id (None )
145- original_leader = mlrun .mlconf .httpdb .projects .leader
146- mlrun .mlconf .httpdb .projects .leader = "mlrun"
147-
148- try :
149- result = clients_helpers .enrich_headers (headers = {}, path = None )
150- assert mlrun .common .schemas .HeaderNames .projects_role not in result
151- finally :
152- mlrun .mlconf .httpdb .projects .leader = original_leader
129+ result = clients_helpers .enrich_headers (headers = {}, path = None )
130+ assert mlrun .common .schemas .HeaderNames .projects_role not in result
153131
154132
155133def test_enrich_headers_does_not_add_projects_role_when_path_has_no_projects (
156134 set_context_id ,
157135):
158136 set_context_id (None )
159- original_leader = mlrun .mlconf .httpdb .projects .leader
160- mlrun .mlconf .httpdb .projects .leader = "mlrun"
161-
162- try :
163- result = clients_helpers .enrich_headers (
164- headers = {}, path = "/api/v1/functions/my-function"
165- )
166- assert mlrun .common .schemas .HeaderNames .projects_role not in result
167- finally :
168- mlrun .mlconf .httpdb .projects .leader = original_leader
137+ result = clients_helpers .enrich_headers (
138+ headers = {}, path = "/api/v1/functions/my-function"
139+ )
140+ assert mlrun .common .schemas .HeaderNames .projects_role not in result
169141
170142
171143def test_enrich_headers_does_not_override_existing_projects_role (set_context_id ):
172144 set_context_id (None )
173- original_leader = mlrun .mlconf .httpdb .projects .leader
174- mlrun .mlconf .httpdb .projects .leader = "mlrun"
175145 existing_role = "custom-role"
176146
177- try :
178- result = clients_helpers .enrich_headers (
179- headers = {mlrun .common .schemas .HeaderNames .projects_role : existing_role },
180- path = "/api/v1/projects/my-project" ,
181- )
182- assert result [mlrun .common .schemas .HeaderNames .projects_role ] == existing_role
183- finally :
184- mlrun .mlconf .httpdb .projects .leader = original_leader
147+ result = clients_helpers .enrich_headers (
148+ headers = {mlrun .common .schemas .HeaderNames .projects_role : existing_role },
149+ path = "/api/v1/projects/my-project" ,
150+ )
151+ assert result [mlrun .common .schemas .HeaderNames .projects_role ] == existing_role
185152
186153
187154def test_enrich_headers_preserves_existing_headers (set_context_id ):
188155 context_id = "test-context"
189156 set_context_id (context_id )
190- original_leader = mlrun .mlconf .httpdb .projects .leader
191- mlrun .mlconf .httpdb .projects .leader = "mlrun"
192-
193- try :
194- headers = {
195- "Authorization" : "Bearer token" ,
196- "X-Custom-Header" : "custom-value" ,
197- }
198- result = clients_helpers .enrich_headers (
199- headers = headers , path = "/api/v1/projects/my-project"
200- )
201-
202- # Should preserve existing headers
203- assert result ["Authorization" ] == "Bearer token"
204- assert result ["X-Custom-Header" ] == "custom-value"
205- # Should add context id
206- assert result [mlrun .common .schemas .HeaderNames .igz_ctx ] == context_id
207- # Should add projects role
208- assert result [mlrun .common .schemas .HeaderNames .projects_role ] == "mlrun"
209- finally :
210- mlrun .mlconf .httpdb .projects .leader = original_leader
157+
158+ headers = {
159+ "Authorization" : "Bearer token" ,
160+ "X-Custom-Header" : "custom-value" ,
161+ }
162+ result = clients_helpers .enrich_headers (
163+ headers = headers , path = "/api/v1/projects/my-project"
164+ )
165+
166+ # Should preserve existing headers
167+ assert result ["Authorization" ] == "Bearer token"
168+ assert result ["X-Custom-Header" ] == "custom-value"
169+ # Should add context id
170+ assert result [mlrun .common .schemas .HeaderNames .igz_ctx ] == context_id
171+ # Should add projects role
172+ assert (
173+ result [mlrun .common .schemas .HeaderNames .projects_role ]
174+ == mlrun .mlconf .httpdb .projects .leader
175+ )
0 commit comments