mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-05-02 15:52:34 +00:00
fix: use anon id for ephemeral traces
This commit is contained in:
@@ -28,7 +28,19 @@ class TestPlusAPI(unittest.TestCase):
|
||||
response = self.api.login_to_tool_repository()
|
||||
|
||||
mock_make_request.assert_called_once_with(
|
||||
"POST", "/crewai_plus/api/v1/tools/login"
|
||||
"POST", "/crewai_plus/api/v1/tools/login", json={}
|
||||
)
|
||||
self.assertEqual(response, mock_response)
|
||||
|
||||
@patch("crewai.cli.plus_api.PlusAPI._make_request")
|
||||
def test_login_to_tool_repository_with_user_identifier(self, mock_make_request):
|
||||
mock_response = MagicMock()
|
||||
mock_make_request.return_value = mock_response
|
||||
|
||||
response = self.api.login_to_tool_repository(user_identifier="test-hash-123")
|
||||
|
||||
mock_make_request.assert_called_once_with(
|
||||
"POST", "/crewai_plus/api/v1/tools/login", json={"user_identifier": "test-hash-123"}
|
||||
)
|
||||
self.assertEqual(response, mock_response)
|
||||
|
||||
@@ -67,7 +79,7 @@ class TestPlusAPI(unittest.TestCase):
|
||||
response = self.api.login_to_tool_repository()
|
||||
|
||||
self.assert_request_with_org_id(
|
||||
mock_client_instance, "POST", "/crewai_plus/api/v1/tools/login"
|
||||
mock_client_instance, "POST", "/crewai_plus/api/v1/tools/login", json={}
|
||||
)
|
||||
self.assertEqual(response, mock_response)
|
||||
|
||||
|
||||
@@ -840,3 +840,87 @@ class TestTraceListenerSetup:
|
||||
mock_mark_failed.assert_called_once_with(
|
||||
"test_batch_id_12345", "Internal Server Error"
|
||||
)
|
||||
|
||||
def test_ephemeral_batch_includes_anon_id(self):
|
||||
"""Test that ephemeral batch initialization sends anon_id from get_user_id()"""
|
||||
fake_user_id = "abc123def456"
|
||||
|
||||
with (
|
||||
patch(
|
||||
"crewai.events.listeners.tracing.trace_batch_manager.is_tracing_enabled_in_context",
|
||||
return_value=True,
|
||||
),
|
||||
patch(
|
||||
"crewai.events.listeners.tracing.trace_batch_manager.get_user_id",
|
||||
return_value=fake_user_id,
|
||||
),
|
||||
patch(
|
||||
"crewai.events.listeners.tracing.trace_batch_manager.should_auto_collect_first_time_traces",
|
||||
return_value=False,
|
||||
),
|
||||
):
|
||||
batch_manager = TraceBatchManager()
|
||||
|
||||
mock_response = MagicMock(
|
||||
status_code=201,
|
||||
json=MagicMock(return_value={
|
||||
"ephemeral_trace_id": "test-trace-id",
|
||||
"access_code": "TRACE-abc123",
|
||||
}),
|
||||
)
|
||||
|
||||
with patch.object(
|
||||
batch_manager.plus_api,
|
||||
"initialize_ephemeral_trace_batch",
|
||||
return_value=mock_response,
|
||||
) as mock_init:
|
||||
batch_manager.initialize_batch(
|
||||
user_context={"privacy_level": "standard"},
|
||||
execution_metadata={
|
||||
"execution_type": "crew",
|
||||
"crew_name": "test_crew",
|
||||
},
|
||||
use_ephemeral=True,
|
||||
)
|
||||
|
||||
mock_init.assert_called_once()
|
||||
payload = mock_init.call_args[0][0]
|
||||
assert payload["user_identifier"] == fake_user_id
|
||||
assert "ephemeral_trace_id" in payload
|
||||
|
||||
def test_non_ephemeral_batch_does_not_include_anon_id(self):
|
||||
"""Test that non-ephemeral batch initialization does not send anon_id"""
|
||||
with (
|
||||
patch(
|
||||
"crewai.events.listeners.tracing.trace_batch_manager.is_tracing_enabled_in_context",
|
||||
return_value=True,
|
||||
),
|
||||
patch(
|
||||
"crewai.events.listeners.tracing.trace_batch_manager.should_auto_collect_first_time_traces",
|
||||
return_value=False,
|
||||
),
|
||||
):
|
||||
batch_manager = TraceBatchManager()
|
||||
|
||||
mock_response = MagicMock(
|
||||
status_code=201,
|
||||
json=MagicMock(return_value={"trace_id": "test-trace-id"}),
|
||||
)
|
||||
|
||||
with patch.object(
|
||||
batch_manager.plus_api,
|
||||
"initialize_trace_batch",
|
||||
return_value=mock_response,
|
||||
) as mock_init:
|
||||
batch_manager.initialize_batch(
|
||||
user_context={"privacy_level": "standard"},
|
||||
execution_metadata={
|
||||
"execution_type": "crew",
|
||||
"crew_name": "test_crew",
|
||||
},
|
||||
use_ephemeral=False,
|
||||
)
|
||||
|
||||
mock_init.assert_called_once()
|
||||
payload = mock_init.call_args[0][0]
|
||||
assert "user_identifier" not in payload
|
||||
|
||||
Reference in New Issue
Block a user