diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 2164d5a8..417bd83d 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -6,6 +6,8 @@ Brewtils Changelog TBD - Expanded PublishClient to support Registering and Unregistering commands to Topics after a plugin has been initialized +- Fixed bug where threading a SystemClient within a plugin lost current_request context and failed to map `requester`, + SystemClient will still drop the current_request context,but the requester field can be provided via `_requester` - Expanded Garden model to support tracking the version of Beer Garden running 3.27.1 diff --git a/brewtils/rest/system_client.py b/brewtils/rest/system_client.py index bfcd216b..cb2a1b3f 100644 --- a/brewtils/rest/system_client.py +++ b/brewtils/rest/system_client.py @@ -595,13 +595,16 @@ def _construct_bg_request(self, **kwargs): publish = kwargs.pop("_publish", None) topic = kwargs.pop("_topic", None) propagate = kwargs.pop("_propagate", None) + requester = kwargs.pop("_requester", None) - if parent: + if ( + not requester + and parent + and getattr(brewtils.plugin.request_context, "current_request", None) + ): requester = getattr( brewtils.plugin.request_context.current_request, "requester", None ) - else: - requester = None if system_display: metadata["system_display_name"] = system_display diff --git a/test/rest/system_client_test.py b/test/rest/system_client_test.py index 5bdacdbc..b1411caf 100644 --- a/test/rest/system_client_test.py +++ b/test/rest/system_client_test.py @@ -304,6 +304,52 @@ def test_missing_field(self, monkeypatch, client, remove_kwarg): with pytest.raises(ValidationError): client._construct_bg_request(**kwargs) + def test_requester_field(self, monkeypatch, client): + monkeypatch.setattr(client, "_resolve_parameters", Mock()) + + kwargs = { + "_command": "", + "_system_name": "", + "_system_version": "", + "_instance_name": "", + "_requester": "test", + } + + request = client._construct_bg_request(**kwargs) + assert request.requester == "test" + + def test_requester_from_parent_field(self, monkeypatch, client): + monkeypatch.setattr(client, "_resolve_parameters", Mock()) + monkeypatch.setattr( + brewtils.plugin, + "request_context", + Mock(current_request=Mock(id="1", requester="test")), + ) + + kwargs = { + "_command": "", + "_system_name": "", + "_system_version": "", + "_instance_name": "", + } + + request = client._construct_bg_request(**kwargs) + assert request.requester == "test" + + def test_no_requester_from_provided_parent(self, monkeypatch, client): + monkeypatch.setattr(client, "_resolve_parameters", Mock()) + + kwargs = { + "_command": "", + "_system_name": "", + "_system_version": "", + "_instance_name": "", + "_parent": Mock(id="1"), + } + + request = client._construct_bg_request(**kwargs) + assert request.requester is None + def test_positional_parameter(self, client, easy_client, mock_success): easy_client.create_request.return_value = mock_success