Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed missing requester for spawned threads of SystemClient #504

Merged
merged 4 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`

3.27.1
------
Expand Down
9 changes: 6 additions & 3 deletions brewtils/rest/system_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions test/rest/system_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading