Skip to content

Commit 73bea4a

Browse files
Update: [AEA-6136] - change logging + tests (#360)
## Summary - Routine Change ### Details - removes user ID's which we were logging prior. --------- Co-authored-by: Beenyaa <bencegadanyi1@hotmail.com>
1 parent 5417129 commit 73bea4a

7 files changed

Lines changed: 102 additions & 8 deletions

File tree

packages/notifyS3UploadFunction/tests/conftest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from unittest.mock import MagicMock, Mock, patch
55
import os
66

7-
87
TEST_BOT_TOKEN = "test-bot-token"
98

109

packages/slackBotFunction/app/services/bedrock.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
from app.core.config import BedrockConfig, get_logger
99

10-
1110
logger = get_logger()
1211

1312

packages/slackBotFunction/app/services/slack.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from slack_sdk import WebClient
44
from app.core.config import bot_messages, get_logger
55

6-
76
logger = get_logger()
87

98

@@ -12,7 +11,10 @@ def get_friendly_channel_name(channel_id: str, client: WebClient) -> str:
1211
try:
1312
conversations_info_response = client.conversations_info(channel=channel_id)
1413
if conversations_info_response["ok"]:
15-
friendly_channel_name = conversations_info_response["channel"]["name"]
14+
channel_info = conversations_info_response["channel"]
15+
if channel_info.get("is_im"):
16+
return "Direct Message"
17+
friendly_channel_name = channel_info.get("name", channel_id)
1618
else:
1719
logger.warning(
1820
"There was a problem getting the friendly channel name",

packages/slackBotFunction/app/slack/slack_events.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141

4242
from app.services.ai_processor import process_ai_query
4343

44-
4544
logger = get_logger()
4645

4746
processing_error_message = "Error processing message"
@@ -513,7 +512,7 @@ def process_slack_message(event: Dict[str, Any], event_id: str, client: WebClien
513512
user_query = re.sub(r"<@[UW][A-Z0-9]+(\|[^>]+)?>", "", event["text"]).strip()
514513

515514
logger.info(
516-
f"Processing message from user {user_id}",
515+
"Processing message",
517516
extra={"user_query": user_query, "conversation_key": conversation_key, "event_id": event_id},
518517
)
519518

@@ -577,7 +576,11 @@ def log_query_stats(user_query: str, event: Dict[str, Any], channel: str, client
577576
end_time = time.time()
578577
duration = end_time - start_time
579578
is_direct_message = event.get("channel_type") == constants.CHANNEL_TYPE_IM
580-
friendly_channel_name = get_friendly_channel_name(channel_id=channel, client=client)
579+
if is_direct_message:
580+
friendly_channel_name = "Direct Message"
581+
else:
582+
friendly_channel_name = get_friendly_channel_name(channel_id=channel, client=client)
583+
581584
reporting_info = {
582585
"query_length": query_length,
583586
"start_time": start_time,
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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]

poetry.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/run_regression_tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Script to generate user defined unique ID which can be used to
55
check the status of the regression test run to be reported to the CI.
66
"""
7+
78
import argparse
89
from datetime import datetime, timedelta, timezone
910
import random

0 commit comments

Comments
 (0)