Skip to content

Commit

Permalink
Test refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
DiamondJoseph committed Nov 6, 2024
1 parent 9705a1b commit bf18693
Show file tree
Hide file tree
Showing 6 changed files with 351 additions and 325 deletions.
73 changes: 33 additions & 40 deletions src/blueapi/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections.abc import Mapping
from enum import Enum
from functools import cached_property
from pathlib import Path
from typing import Any, Generic, Literal, TypeVar

Expand Down Expand Up @@ -87,48 +88,40 @@ class OAuthServerConfig(BlueapiBaseModel):
oidc_config_url: str = Field(
description="URL to fetch OIDC config from the provider"
)
# Initialized post-init
device_auth_url: str = Field(exclude=True, default="")
pkce_auth_url: str = Field(exclude=True, default="")
token_url: str = Field(exclude=True, default="")
issuer: str = Field(exclude=True, default="")
jwks_uri: str = Field(exclude=True, default="")
logout_url: str = Field(exclude=True, default="")
signing_algos: list[str] = Field(exclude=True, default_factory=list)

def model_post_init(self, __context: Any) -> None:

@cached_property
def _config_from_oidc_url(self) -> dict[str, Any]:
response: requests.Response = requests.get(self.oidc_config_url)
response.raise_for_status()
config_data: dict[str, Any] = response.json()

device_auth_url: str | None = config_data.get("device_authorization_endpoint")
pkce_auth_url: str | None = config_data.get("authorization_endpoint")
token_url: str | None = config_data.get("token_endpoint")
issuer: str | None = config_data.get("issuer")
jwks_uri: str | None = config_data.get("jwks_uri")
logout_url: str | None = config_data.get("end_session_endpoint")
signing_algos: list[str] | None = config_data.get(
"id_token_signing_alg_values_supported"
)
# post this we need to check if all the values are present
if (
device_auth_url
and pkce_auth_url
and token_url
and issuer
and jwks_uri
and logout_url
and signing_algos
):
self.device_auth_url = device_auth_url
self.pkce_auth_url = pkce_auth_url
self.token_url = token_url
self.issuer = issuer
self.jwks_uri = jwks_uri
self.logout_url = logout_url
self.signing_algos = signing_algos
else:
raise ValueError("OIDC config is missing required fields")
return response.json()

@cached_property
def device_auth_url(self) -> str:
return self._config_from_oidc_url.get("device_authorization_endpoint")

@cached_property
def pkce_auth_url(self) -> str:
return self._config_from_oidc_url.get("authorization_endpoint")

@cached_property
def token_url(self) -> str:
return self._config_from_oidc_url.get("token_endpoint")

@cached_property
def issuer(self) -> str:
return self._config_from_oidc_url.get("issuer")

@cached_property
def jwks_uri(self) -> str:
return self._config_from_oidc_url.get("jwks_uri")

@cached_property
def logout_url(self) -> str:
return self._config_from_oidc_url.get("end_session_endpoint")

@cached_property
def signing_algos(self) -> list[str]:
return self._config_from_oidc_url.get("id_token_signing_alg_values_supported")

Check warning on line 124 in src/blueapi/config.py

View check run for this annotation

Codecov / codecov/patch

src/blueapi/config.py#L124

Added line #L124 was not covered by tests


class OAuthClientConfig(BlueapiBaseModel):
Expand Down
8 changes: 4 additions & 4 deletions src/blueapi/service/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from enum import Enum
from http import HTTPStatus
from pathlib import Path
from typing import Any
from typing import Any, cast

import jwt
import requests
Expand Down Expand Up @@ -187,9 +187,9 @@ def start_device_flow(self) -> None:

if response.status_code == HTTPStatus.OK:
response_json: dict[str, Any] = response.json()
device_code: str = response_json.get("device_code")
interval: float = response_json.get("interval")
expires_in: float = response_json.get("expires_in")
device_code = cast(str, response_json.get("device_code"))
interval = cast(float, response_json.get("interval"))
expires_in = cast(float, response_json.get("expires_in"))
print(
"Please login from this URL:- "
f"{response_json['verification_uri_complete']}"
Expand Down
20 changes: 10 additions & 10 deletions tests/system_tests/test_blueapi_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test_cannot_access_endpoints(
for get_method in blueapi_client_get_methods:
with pytest.raises(BlueskyRemoteControlError) as exception:
getattr(client_without_auth, get_method)()
assert str(exception) == "<Response [401]>"
assert str(exception) == "<Response [401]>"


def test_get_plans(client: BlueapiClient, expected_plans: PlanResponse):
Expand All @@ -147,7 +147,7 @@ def test_get_plans_by_name(client: BlueapiClient, expected_plans: PlanResponse):
def test_get_non_existent_plan(client: BlueapiClient):
with pytest.raises(KeyError) as exception:
client.get_plan("Not exists")
assert str(exception) == ("{'detail': 'Item not found'}")
assert str(exception) == ("{'detail': 'Item not found'}")


def test_get_devices(client: BlueapiClient, expected_devices: DeviceResponse):
Expand All @@ -161,8 +161,8 @@ def test_get_device_by_name(client: BlueapiClient, expected_devices: DeviceRespo

def test_get_non_existent_device(client: BlueapiClient):
with pytest.raises(KeyError) as exception:
assert client.get_device("Not exists")
assert str(exception) == ("{'detail': 'Item not found'}")
client.get_device("Not exists")
assert str(exception) == ("{'detail': 'Item not found'}")


def test_create_task_and_delete_task_by_id(client: BlueapiClient):
Expand All @@ -173,7 +173,7 @@ def test_create_task_and_delete_task_by_id(client: BlueapiClient):
def test_create_task_validation_error(client: BlueapiClient):
with pytest.raises(KeyError) as exception:
client.create_task(Task(name="Not-exists", params={"Not-exists": 0.0}))
assert str(exception) == ("{'detail': 'Item not found'}")
assert str(exception) == ("{'detail': 'Item not found'}")


def test_get_all_tasks(client: BlueapiClient):
Expand Down Expand Up @@ -209,13 +209,13 @@ def test_get_task_by_id(client: BlueapiClient):
def test_get_non_existent_task(client: BlueapiClient):
with pytest.raises(KeyError) as exception:
client.get_task("Not-exists")
assert str(exception) == "{'detail': 'Item not found'}"
assert str(exception) == "{'detail': 'Item not found'}"


def test_delete_non_existent_task(client: BlueapiClient):
with pytest.raises(KeyError) as exception:
client.clear_task("Not-exists")
assert str(exception) == "{'detail': 'Item not found'}"
assert str(exception) == "{'detail': 'Item not found'}"


def test_put_worker_task(client: BlueapiClient):
Expand All @@ -236,7 +236,7 @@ def test_put_worker_task_fails_if_not_idle(client: BlueapiClient):

with pytest.raises(BlueskyRemoteControlError) as exception:
client.start_task(WorkerTask(task_id=small_task.task_id))
assert str(exception) == "<Response [409]>"
assert str(exception) == "<Response [409]>"
client.abort()
client.clear_task(small_task.task_id)
client.clear_task(long_task.task_id)
Expand All @@ -249,11 +249,11 @@ def test_get_worker_state(client: BlueapiClient):
def test_set_state_transition_error(client: BlueapiClient):
with pytest.raises(BlueskyRemoteControlError) as exception:
client.resume()
assert str(exception) == "<Response [400]>"
assert str(exception) == "<Response [400]>"

with pytest.raises(BlueskyRemoteControlError) as exception:
client.pause()
assert str(exception) == "<Response [400]>"
assert str(exception) == "<Response [400]>"


def test_get_task_by_status(client: BlueapiClient):
Expand Down
Loading

0 comments on commit bf18693

Please sign in to comment.