|
| 1 | +import sys |
| 2 | +from unittest.mock import Mock, patch |
| 3 | +from app.services.slack import get_friendly_channel_name |
| 4 | + |
| 5 | + |
| 6 | +def test_get_friendly_channel_name_returns_direct_message_for_im(): |
| 7 | + mock_client = Mock() |
| 8 | + mock_client.conversations_info.return_value = {"ok": True, "channel": {"is_im": True, "id": "D12345"}} |
| 9 | + |
| 10 | + result = get_friendly_channel_name("D12345", mock_client) |
| 11 | + assert result == "Direct Message" |
| 12 | + |
| 13 | + |
| 14 | +def test_get_friendly_channel_name_returns_name_for_public_channel(): |
| 15 | + mock_client = Mock() |
| 16 | + mock_client.conversations_info.return_value = { |
| 17 | + "ok": True, |
| 18 | + "channel": {"is_im": False, "name": "general", "id": "C12345"}, |
| 19 | + } |
| 20 | + |
| 21 | + result = get_friendly_channel_name("C12345", mock_client) |
| 22 | + assert result == "general" |
| 23 | + |
| 24 | + |
| 25 | +def test_log_query_stats_masks_dm_channel(mock_env, mock_get_parameter): |
| 26 | + # reload module to ensure clean state and correct patching |
| 27 | + if "app.slack.slack_events" in sys.modules: |
| 28 | + del sys.modules["app.slack.slack_events"] |
| 29 | + |
| 30 | + from app.slack.slack_events import log_query_stats |
| 31 | + import app.slack.slack_events |
| 32 | + |
| 33 | + mock_client = Mock() |
| 34 | + |
| 35 | + event = {"event_ts": "1234567890.123", "channel_type": "im"} |
| 36 | + |
| 37 | + with patch.object(app.slack.slack_events, "logger") as mock_logger: |
| 38 | + log_query_stats(user_query="test", event=event, channel="D123", client=mock_client, thread_ts="123") |
| 39 | + |
| 40 | + assert mock_logger.info.called |
| 41 | + |
| 42 | + _, kwargs = mock_logger.info.call_args |
| 43 | + reporting_info = kwargs["extra"]["reporting_info"] |
| 44 | + assert reporting_info["channel"] == "Direct Message" |
| 45 | + assert reporting_info["is_direct_message"] is True |
| 46 | + |
| 47 | + mock_client.conversations_info.assert_not_called() |
| 48 | + |
| 49 | + |
| 50 | +def test_process_slack_message_log_privacy(mock_env, mock_get_parameter): |
| 51 | + if "app.slack.slack_events" in sys.modules: |
| 52 | + del sys.modules["app.slack.slack_events"] |
| 53 | + |
| 54 | + from app.slack.slack_events import process_slack_message |
| 55 | + import app.slack.slack_events |
| 56 | + |
| 57 | + with patch.object(app.slack.slack_events, "logger") as mock_logger, patch.object( |
| 58 | + app.slack.slack_events, "conversation_key_and_root", return_value=("key", "ts") |
| 59 | + ), patch.object(app.slack.slack_events, "get_conversation_session_data", return_value={}), patch.object( |
| 60 | + app.slack.slack_events, "get_friendly_channel_name" |
| 61 | + ), patch.object( |
| 62 | + app.slack.slack_events, "process_ai_query" |
| 63 | + ) as mock_process_ai: |
| 64 | + |
| 65 | + mock_client = Mock() |
| 66 | + mock_client.chat_postMessage.return_value = {"ts": "123"} |
| 67 | + mock_process_ai.return_value = {"kb_response": {}, "text": "response", "session_id": "sid"} |
| 68 | + |
| 69 | + event = { |
| 70 | + "user": "U12345", |
| 71 | + "channel": "C123", |
| 72 | + "text": "hello", |
| 73 | + "ts": "123", |
| 74 | + "event_ts": "123", |
| 75 | + "channel_type": "channel", |
| 76 | + } |
| 77 | + |
| 78 | + process_slack_message(event, "evt1", mock_client) |
| 79 | + |
| 80 | + processing_call = None |
| 81 | + for call in mock_logger.info.call_args_list: |
| 82 | + args, _ = call |
| 83 | + if args and "Processing message" in args[0]: |
| 84 | + processing_call = call |
| 85 | + break |
| 86 | + |
| 87 | + assert processing_call is not None |
| 88 | + args, _ = processing_call |
| 89 | + assert "from user" not in args[0] |
| 90 | + assert "U12345" not in args[0] |
0 commit comments