Skip to content

Commit 2533134

Browse files
committed
Test coverage
1 parent f8ba3b3 commit 2533134

4 files changed

Lines changed: 137 additions & 3 deletions

File tree

tests/unit_tests/client/test_client.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
)
2121
from blueapi.client.event_bus import AnyEvent, EventBusClient
2222
from blueapi.client.rest import BlueapiRestClient, BlueskyRemoteControlError
23-
from blueapi.config import MissingStompConfigurationError
23+
from blueapi.config import MissingStompConfigurationError, StompConfig, TcpUrl
2424
from blueapi.core import DataEvent
2525
from blueapi.service.model import (
2626
DeviceModel,
@@ -385,6 +385,27 @@ def test_cannot_run_task_without_message_bus(client: BlueapiClient, mock_rest: M
385385
client.run_task(TaskRequest(name="foo", instrument_session="cm12345-1"))
386386

387387

388+
@patch("blueapi.client.client.EventBusClient")
389+
def test_run_task_with_stomp_config_from_server(
390+
ebc: Mock, client: BlueapiClient, mock_rest: Mock
391+
):
392+
mock_rest.get_stomp_config.return_value = StompConfig(
393+
enabled=True, url=TcpUrl("tcp://localhost:9876"), auth=None
394+
)
395+
mock_rest.create_task.return_value = TaskResponse(task_id="foo")
396+
mock_rest.update_worker_task.return_value = TaskResponse(task_id="foo")
397+
events = MagicMock(spec=EventBusClient, name="EventBusClient")
398+
ctx = Mock(correlation_id="foo")
399+
events.subscribe_to_all_events.side_effect = lambda on_event: on_event(
400+
COMPLETE_EVENT, ctx
401+
)
402+
ebc.from_stomp_config.return_value = events
403+
404+
client.run_task(TaskRequest(name="foo", instrument_session="cm12345-1"))
405+
406+
mock_rest.get_stomp_config.assert_called_once()
407+
408+
388409
def test_run_task_sets_up_control(
389410
client_with_events: BlueapiClient,
390411
mock_rest: Mock,

tests/unit_tests/client/test_rest.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
UnknownPlanError,
1717
_create_task_exceptions,
1818
)
19-
from blueapi.config import OIDCConfig
19+
from blueapi.config import OIDCConfig, TcpUrl
2020
from blueapi.service.authentication import SessionCacheManager, SessionManager
2121
from blueapi.service.model import EnvironmentResponse
2222

@@ -196,3 +196,47 @@ def test_parameter_error_other_string():
196196
input=34,
197197
)
198198
assert str(p1) == "Invalid value 34 for field field_one.0: error_message"
199+
200+
201+
@responses.activate
202+
def test_get_stomp_config(rest: BlueapiRestClient):
203+
responses.add(
204+
responses.GET,
205+
"http://localhost:8000/config/stomp",
206+
json={
207+
"enabled": True,
208+
"url": "tcp://messagebus.example.com",
209+
"auth": {"username": "foo", "password": "bar"},
210+
},
211+
status=200,
212+
)
213+
stomp = rest.get_stomp_config()
214+
assert stomp is not None
215+
assert stomp.enabled
216+
assert stomp.url == TcpUrl("tcp://messagebus.example.com")
217+
assert stomp.auth is not None
218+
assert stomp.auth.username == "foo"
219+
assert stomp.auth.password.get_secret_value() == "bar"
220+
221+
222+
@responses.activate
223+
def test_get_no_stomp_config(rest: BlueapiRestClient):
224+
responses.add(
225+
responses.GET,
226+
"http://localhost:8000/config/stomp",
227+
status=204,
228+
)
229+
stomp = rest.get_stomp_config()
230+
assert stomp is None
231+
232+
233+
@responses.activate
234+
def test_get_stomp_config_from_old_server(rest: BlueapiRestClient):
235+
responses.add(
236+
responses.GET,
237+
"http://localhost:8000/config/stomp",
238+
json={}, # Weird default handling for 404 - See #1409
239+
status=404,
240+
)
241+
stomp = rest.get_stomp_config()
242+
assert stomp is None

tests/unit_tests/service/test_rest_api.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414
from pydantic_core import InitErrorDetails
1515
from 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+
StompConfig,
23+
)
1824
from blueapi.core.bluesky_types import Plan
1925
from blueapi.service import main
2026
from blueapi.service.interface import (
@@ -709,6 +715,27 @@ def test_get_without_authentication(mock_runner: Mock, client: TestClient) -> No
709715
assert response.json() == {"detail": "Not authenticated"}
710716

711717

718+
def test_stomp_config_not_found_when_stomp_is_disabled(
719+
mock_runner: Mock, client: TestClient
720+
):
721+
mock_runner.run.return_value = None
722+
response = client.get("/config/stomp")
723+
assert response.status_code == status.HTTP_204_NO_CONTENT
724+
assert response.text == ""
725+
726+
727+
def test_get_stomp_config(
728+
mock_runner: Mock,
729+
mock_authn_server,
730+
client: TestClient,
731+
):
732+
stomp_config = StompConfig(enabled=False)
733+
mock_runner.run.return_value = stomp_config
734+
response = client.get("/config/stomp")
735+
assert response.status_code == status.HTTP_200_OK
736+
assert response.json() == stomp_config.model_dump(mode="json")
737+
738+
712739
def test_oidc_config_not_found_when_auth_is_disabled(
713740
mock_runner: Mock, client: TestClient
714741
):

tests/unit_tests/test_cli.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,3 +1404,45 @@ def test_config_schema(
14041404
def test_task_parameter_type(value, result):
14051405
t = ParametersType()
14061406
assert t.convert(value, None, None) == result
1407+
1408+
1409+
@responses.activate
1410+
def test_host_option(runner: CliRunner):
1411+
response = responses.add(
1412+
responses.GET,
1413+
"http://override.example.com:5678/plans",
1414+
json={"plans": []},
1415+
status=200,
1416+
)
1417+
1418+
res = runner.invoke(
1419+
main,
1420+
["--host", "http://override.example.com:5678", "controller", "plans"],
1421+
)
1422+
assert response.call_count == 1
1423+
assert res.exit_code == 0
1424+
1425+
1426+
@responses.activate
1427+
def test_host_overrides_config(runner: CliRunner):
1428+
config_path = "tests/unit_tests/example_yaml/rest_config.yaml"
1429+
response = responses.add(
1430+
responses.GET,
1431+
"http://override.example.com:5678/plans",
1432+
json={"plans": []},
1433+
status=200,
1434+
)
1435+
1436+
res = runner.invoke(
1437+
main,
1438+
[
1439+
"--host",
1440+
"http://override.example.com:5678",
1441+
"--config",
1442+
config_path,
1443+
"controller",
1444+
"plans",
1445+
],
1446+
)
1447+
assert response.call_count == 1
1448+
assert res.exit_code == 0

0 commit comments

Comments
 (0)