|
| 1 | +import pytest |
| 2 | + |
| 3 | +from onyx.connectors.slack.connector import _bot_inclusive_msg_filter |
| 4 | +from onyx.connectors.slack.connector import default_msg_filter |
| 5 | +from onyx.connectors.slack.connector import SlackConnector |
| 6 | +from onyx.connectors.slack.connector import SlackMessageFilterReason |
| 7 | +from onyx.connectors.slack.models import MessageType |
| 8 | + |
| 9 | + |
| 10 | +# -- default_msg_filter tests -- |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.parametrize( |
| 14 | + "message,expected_reason", |
| 15 | + [ |
| 16 | + # Regular user message: not filtered |
| 17 | + ( |
| 18 | + {"text": "hello", "user": "U123", "ts": "1.0"}, |
| 19 | + None, |
| 20 | + ), |
| 21 | + # Bot message with bot_id: filtered as BOT |
| 22 | + ( |
| 23 | + {"text": "automated update", "bot_id": "B123", "ts": "1.0"}, |
| 24 | + SlackMessageFilterReason.BOT, |
| 25 | + ), |
| 26 | + # App message with app_id: filtered as BOT |
| 27 | + ( |
| 28 | + {"text": "app notification", "app_id": "A123", "ts": "1.0"}, |
| 29 | + SlackMessageFilterReason.BOT, |
| 30 | + ), |
| 31 | + # Bot message with both bot_id and app_id: filtered as BOT |
| 32 | + ( |
| 33 | + {"text": "bot+app", "bot_id": "B1", "app_id": "A1", "ts": "1.0"}, |
| 34 | + SlackMessageFilterReason.BOT, |
| 35 | + ), |
| 36 | + # DanswerBot Testing is explicitly allowed through |
| 37 | + ( |
| 38 | + { |
| 39 | + "text": "danswer test", |
| 40 | + "bot_id": "B999", |
| 41 | + "bot_profile": {"name": "DanswerBot Testing"}, |
| 42 | + "ts": "1.0", |
| 43 | + }, |
| 44 | + None, |
| 45 | + ), |
| 46 | + # channel_join subtype: filtered as DISALLOWED |
| 47 | + ( |
| 48 | + {"text": "joined", "subtype": "channel_join", "ts": "1.0"}, |
| 49 | + SlackMessageFilterReason.DISALLOWED, |
| 50 | + ), |
| 51 | + # channel_leave subtype: filtered as DISALLOWED |
| 52 | + ( |
| 53 | + {"text": "left", "subtype": "channel_leave", "ts": "1.0"}, |
| 54 | + SlackMessageFilterReason.DISALLOWED, |
| 55 | + ), |
| 56 | + # pinned_item subtype: filtered as DISALLOWED |
| 57 | + ( |
| 58 | + {"text": "pinned", "subtype": "pinned_item", "ts": "1.0"}, |
| 59 | + SlackMessageFilterReason.DISALLOWED, |
| 60 | + ), |
| 61 | + # Empty subtype: not filtered |
| 62 | + ( |
| 63 | + {"text": "normal", "subtype": "", "ts": "1.0"}, |
| 64 | + None, |
| 65 | + ), |
| 66 | + ], |
| 67 | + ids=[ |
| 68 | + "regular_user_message", |
| 69 | + "bot_id_message", |
| 70 | + "app_id_message", |
| 71 | + "bot_and_app_id", |
| 72 | + "danswerbot_testing_allowed", |
| 73 | + "channel_join", |
| 74 | + "channel_leave", |
| 75 | + "pinned_item", |
| 76 | + "empty_subtype", |
| 77 | + ], |
| 78 | +) |
| 79 | +def test_default_msg_filter( |
| 80 | + message: MessageType, |
| 81 | + expected_reason: SlackMessageFilterReason | None, |
| 82 | +) -> None: |
| 83 | + assert default_msg_filter(message) == expected_reason |
| 84 | + |
| 85 | + |
| 86 | +# -- _bot_inclusive_msg_filter tests -- |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.parametrize( |
| 90 | + "message,expected_reason", |
| 91 | + [ |
| 92 | + # Regular user message: not filtered |
| 93 | + ( |
| 94 | + {"text": "hello", "user": "U123", "ts": "1.0"}, |
| 95 | + None, |
| 96 | + ), |
| 97 | + # Bot message: NOT filtered (this is the whole point) |
| 98 | + ( |
| 99 | + {"text": "automated update", "bot_id": "B123", "ts": "1.0"}, |
| 100 | + None, |
| 101 | + ), |
| 102 | + # App message: NOT filtered |
| 103 | + ( |
| 104 | + {"text": "app notification", "app_id": "A123", "ts": "1.0"}, |
| 105 | + None, |
| 106 | + ), |
| 107 | + # channel_join subtype: still filtered as DISALLOWED |
| 108 | + ( |
| 109 | + {"text": "joined", "subtype": "channel_join", "ts": "1.0"}, |
| 110 | + SlackMessageFilterReason.DISALLOWED, |
| 111 | + ), |
| 112 | + # channel_leave subtype: still filtered as DISALLOWED |
| 113 | + ( |
| 114 | + {"text": "left", "subtype": "channel_leave", "ts": "1.0"}, |
| 115 | + SlackMessageFilterReason.DISALLOWED, |
| 116 | + ), |
| 117 | + ], |
| 118 | + ids=[ |
| 119 | + "regular_user_message", |
| 120 | + "bot_message_allowed", |
| 121 | + "app_message_allowed", |
| 122 | + "channel_join_still_filtered", |
| 123 | + "channel_leave_still_filtered", |
| 124 | + ], |
| 125 | +) |
| 126 | +def test_bot_inclusive_msg_filter( |
| 127 | + message: MessageType, |
| 128 | + expected_reason: SlackMessageFilterReason | None, |
| 129 | +) -> None: |
| 130 | + assert _bot_inclusive_msg_filter(message) == expected_reason |
| 131 | + |
| 132 | + |
| 133 | +# -- SlackConnector config tests -- |
| 134 | + |
| 135 | + |
| 136 | +def test_default_filter_when_include_bot_messages_false() -> None: |
| 137 | + """When include_bot_messages is False (default), the default filter is used.""" |
| 138 | + connector = SlackConnector(use_redis=False) |
| 139 | + assert connector.msg_filter_func is default_msg_filter |
| 140 | + |
| 141 | + |
| 142 | +def test_bot_inclusive_filter_when_include_bot_messages_true() -> None: |
| 143 | + """When include_bot_messages is True, the bot-inclusive filter is used.""" |
| 144 | + connector = SlackConnector(include_bot_messages=True, use_redis=False) |
| 145 | + assert connector.msg_filter_func is _bot_inclusive_msg_filter |
| 146 | + |
| 147 | + |
| 148 | +def test_include_bot_messages_defaults_to_false() -> None: |
| 149 | + """The include_bot_messages config defaults to False for backward compatibility.""" |
| 150 | + connector = SlackConnector(use_redis=False) |
| 151 | + assert connector.include_bot_messages is False |
0 commit comments