11import uuid
22from collections .abc import Iterator
33from dataclasses import dataclass
4+ from typing import Any
45from unittest .mock import MagicMock , Mock , patch
56
67import jwt
78import pytest
89from bluesky .protocols import Stoppable
910from fastapi import status
10- from fastapi .routing import APIRoute
1111from fastapi .testclient import TestClient
12+ from httpx import Headers
1213from pydantic import BaseModel , ValidationError
1314from pydantic_core import InitErrorDetails
1415from super_state_machine .errors import TransitionError
@@ -63,34 +64,15 @@ def client(mock_runner: Mock) -> Iterator[TestClient]:
6364
6465@pytest .fixture
6566def client_with_auth (
66- mock_runner : Mock , oidc_config : OIDCConfig
67+ mock_runner : Mock , oidc_config : OIDCConfig , valid_token_with_jwt : dict [ str , Any ]
6768) -> Iterator [TestClient ]:
6869 with patch ("blueapi.service.interface.worker" ):
6970 main .setup_runner (runner = mock_runner )
70- yield TestClient (main .get_app (ApplicationConfig (oidc = oidc_config )))
71- main .teardown_runner ()
72-
73-
74- @pytest .fixture
75- def client_authenticated (
76- mock_runner : Mock , oidc_config : OIDCConfig
77- ) -> Iterator [TestClient ]:
78- with patch ("blueapi.service.interface.worker" ):
79- main .setup_runner (runner = mock_runner )
80- app = main .get_app (ApplicationConfig (oidc = oidc_config ))
81- dependant_dependencies = []
82- for route in app .routes :
83- if isinstance (route , APIRoute ) and route .path == "/config/oidc" :
84- dependant_dependencies = route .dependant .dependencies
85- break
86-
87- for route in app .routes :
88- if isinstance (route , APIRoute ):
89- route .dependencies = []
90- temp = dependant_dependencies
91- temp [0 ].path = route .path
92- route .dependant .dependencies = temp
93- yield TestClient (app )
71+ access_token = valid_token_with_jwt .get ("access_token" )
72+ assert access_token is not None
73+ client = TestClient (main .get_app (ApplicationConfig (oidc = oidc_config )))
74+ client .headers = Headers (headers = {"Authorization" : f"Bearer { access_token } " })
75+ yield client
9476 main .teardown_runner ()
9577
9678
@@ -750,12 +732,12 @@ def test_logout(
750732 mock_runner : Mock ,
751733 mock_authn_server ,
752734 oidc_config : OIDCConfig ,
753- client_authenticated : TestClient ,
735+ client_with_auth : TestClient ,
754736):
755737 oidc_config .logout_redirect_endpoint = "/oauth2/logout"
756738 mock_runner .run .return_value = oidc_config
757- client_authenticated .follow_redirects = False
758- response = client_authenticated .get ("/logout" )
739+ client_with_auth .follow_redirects = False
740+ response = client_with_auth .get ("/logout" )
759741 assert response .status_code == status .HTTP_308_PERMANENT_REDIRECT
760742 assert (
761743 response .headers .get ("X-Auth-Request-Redirect" )
@@ -770,13 +752,13 @@ def test_logout_when_oidc_config_invalid(
770752 mock_runner : Mock ,
771753 oidc_config : OIDCConfig ,
772754 mock_authn_server ,
773- client_authenticated : TestClient ,
755+ client_with_auth : TestClient ,
774756):
775757 if has_oidc_config :
776758 oidc_config .logout_redirect_endpoint = ""
777759 mock_runner .run .return_value = oidc_config
778760 else :
779761 mock_runner .run .return_value = None
780762
781- response = client_authenticated .get ("/logout" )
763+ response = client_with_auth .get ("/logout" )
782764 assert response .status_code == status .HTTP_205_RESET_CONTENT
0 commit comments