Skip to content

Commit 1283d01

Browse files
committed
Update test to use vaild token instead of dependency overrides
1 parent 8ee2c0b commit 1283d01

1 file changed

Lines changed: 13 additions & 31 deletions

File tree

tests/unit_tests/service/test_rest_api.py

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import uuid
22
from collections.abc import Iterator
33
from dataclasses import dataclass
4+
from typing import Any
45
from unittest.mock import MagicMock, Mock, patch
56

67
import jwt
78
import pytest
89
from bluesky.protocols import Stoppable
910
from fastapi import status
10-
from fastapi.routing import APIRoute
1111
from fastapi.testclient import TestClient
12+
from httpx import Headers
1213
from pydantic import BaseModel, ValidationError
1314
from pydantic_core import InitErrorDetails
1415
from super_state_machine.errors import TransitionError
@@ -63,34 +64,15 @@ def client(mock_runner: Mock) -> Iterator[TestClient]:
6364

6465
@pytest.fixture
6566
def 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

Comments
 (0)