1414from pydantic_core import InitErrorDetails
1515from super_state_machine .errors import TransitionError
1616
17- from blueapi .config import ApplicationConfig , CORSConfig , OIDCConfig , RestConfig
17+ from blueapi .config import (
18+ ApplicationConfig ,
19+ CORSConfig ,
20+ OIDCConfig ,
21+ RestConfig ,
22+ )
1823from blueapi .core .bluesky_types import Plan
1924from blueapi .service import main
25+ from blueapi .service .authorization import OpaUserClient , opa
2026from blueapi .service .interface import (
2127 cancel_active_task ,
2228 get_device ,
@@ -54,6 +60,11 @@ def mock_runner() -> Mock:
5460 return Mock (spec = WorkerDispatcher )
5561
5662
63+ @pytest .fixture
64+ def mock_opa_client () -> Mock :
65+ return Mock (spec = OpaUserClient )
66+
67+
5768@pytest .fixture
5869def client (mock_runner : Mock ) -> Iterator [TestClient ]:
5970 with patch ("blueapi.service.interface.worker" ):
@@ -79,6 +90,27 @@ def client_with_auth(
7990 main .teardown_runner ()
8091
8192
93+ @pytest .fixture
94+ def access_token (valid_token_with_jwt : dict [str , Any ]) -> str :
95+ return valid_token_with_jwt ["access_token" ]
96+
97+
98+ @pytest .fixture
99+ def client_with_opa (
100+ mock_runner : Mock ,
101+ oidc_config : OIDCConfig ,
102+ mock_opa_client : Mock ,
103+ mock_authn_server ,
104+ ):
105+ with patch ("blueapi.service.interface.worker" ):
106+ main .setup_runner (runner = mock_runner )
107+ app = main .get_app (ApplicationConfig (oidc = oidc_config ))
108+ app .dependency_overrides [opa ] = lambda : mock_opa_client
109+ client = TestClient (app )
110+ yield client
111+ main .teardown_runner ()
112+
113+
82114@pytest .fixture
83115def rest_config_with_cors () -> RestConfig :
84116 cors_config = CORSConfig (
@@ -416,6 +448,28 @@ def test_get_tasks_by_status_invalid(client: TestClient) -> None:
416448 assert response .status_code == status .HTTP_400_BAD_REQUEST
417449
418450
451+ def test_get_tasks_filters_by_user (
452+ mock_runner : Mock ,
453+ client_with_opa : TestClient ,
454+ access_token : str ,
455+ mock_opa_client : Mock ,
456+ ):
457+
458+ print ("Start of test" )
459+ mock_runner .run .return_value = [
460+ TrackableTask (task_id = "foo" , task = Task (name = "f1" , metadata = {"user" : "jd1" })),
461+ TrackableTask (task_id = "bar" , task = Task (name = "f2" , metadata = {"user" : "jd2" })),
462+ ]
463+ print (f"in test: { mock_opa_client = } " )
464+ mock_opa_client .admin .return_value = False
465+ client_with_opa .headers ["Authorization" ] = f"Bearer { access_token } "
466+ tasks = client_with_opa .get ("/tasks" ).json ().get ("tasks" )
467+ print (tasks )
468+
469+ assert len (tasks ) == 1
470+ assert tasks [0 ]["task_id" ] == "foo"
471+
472+
419473def test_delete_submitted_task (mock_runner : Mock , client : TestClient ) -> None :
420474 task_id = str (uuid .uuid4 ())
421475 mock_runner .run .return_value = task_id
0 commit comments