-
Notifications
You must be signed in to change notification settings - Fork 228
Resolve @handle owner/subscriber mentions in Slack web integration #2371
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
Open
devin-ai-integration
wants to merge
3
commits into
master
Choose a base branch
from
devin/1790693198-slack-handle-mentions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
d693cc8
Resolve @handle owner/subscriber mentions in Slack web integration
devin-ai-integration[bot] 5e94b1d
Stop Slack user pagination when the cursor does not advance
devin-ai-integration[bot] cd29c7c
Harden Slack handle resolution against non-Slack errors and guest col…
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
tests/unit/messages/messaging_integrations/test_slack_web.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| from unittest.mock import MagicMock | ||
|
|
||
| import pytest | ||
| from slack_sdk.errors import SlackApiError | ||
|
|
||
| from elementary.messages.blocks import LineBlock, LinesBlock, MentionBlock | ||
| from elementary.messages.message_body import MessageBody | ||
| from elementary.messages.messaging_integrations.slack_web import ( | ||
| SlackWebMessageContext, | ||
| SlackWebMessagingIntegration, | ||
| ) | ||
|
|
||
| USERS_PAGE_1 = { | ||
| "members": [ | ||
| { | ||
| "id": "U_JESSICA", | ||
| "name": "jjones", | ||
| "profile": {"email": "jessica.jones@marvel.com"}, | ||
| }, | ||
| {"id": "U_BOT", "name": "bot", "is_bot": True, "profile": {}}, | ||
| ], | ||
| "response_metadata": {"next_cursor": "page2"}, | ||
| } | ||
| USERS_PAGE_2 = { | ||
| "members": [ | ||
| {"id": "U_LUKE", "name": "luke", "profile": {"email": "lcage@marvel.com"}}, | ||
| { | ||
| "id": "U_DELETED", | ||
| "name": "deleted", | ||
| "deleted": True, | ||
| "profile": {"email": "deleted@marvel.com"}, | ||
| }, | ||
| ], | ||
| "response_metadata": {"next_cursor": ""}, | ||
| } | ||
|
|
||
|
|
||
| def _build_integration() -> SlackWebMessagingIntegration: | ||
| client = MagicMock() | ||
| client.users_list.side_effect = lambda cursor=None, limit=None: ( | ||
| USERS_PAGE_2 if cursor == "page2" else USERS_PAGE_1 | ||
| ) | ||
| client.users_lookupByEmail.side_effect = lambda email: { | ||
| "user": {"id": f"U_EMAIL_{email}"} | ||
| } | ||
| return SlackWebMessagingIntegration(client) | ||
|
|
||
|
|
||
| def test_resolve_user_id_by_email_prefix_handle(): | ||
| integration = _build_integration() | ||
| assert integration.resolve_user_id("@jessica.jones") == "U_JESSICA" | ||
| assert integration.resolve_user_id("@Jessica.Jones") == "U_JESSICA" | ||
|
|
||
|
|
||
| def test_resolve_user_id_by_username_handle(): | ||
| integration = _build_integration() | ||
| assert integration.resolve_user_id("@jjones") == "U_JESSICA" | ||
| assert integration.resolve_user_id("@luke") == "U_LUKE" | ||
|
|
||
|
|
||
| def test_resolve_user_id_skips_bots_deleted_and_unknown_handles(): | ||
| integration = _build_integration() | ||
| assert integration.resolve_user_id("@bot") is None | ||
| assert integration.resolve_user_id("@deleted") is None | ||
| assert integration.resolve_user_id("@unknown") is None | ||
|
|
||
|
|
||
| def test_users_are_listed_once(): | ||
| integration = _build_integration() | ||
| integration.resolve_user_id("@jessica.jones") | ||
| integration.resolve_user_id("@luke") | ||
| assert integration.client.users_list.call_count == 2 | ||
|
|
||
|
|
||
| def test_resolve_user_id_by_email(): | ||
| integration = _build_integration() | ||
| assert ( | ||
| integration.resolve_user_id("jessica.jones@marvel.com") | ||
| == "U_EMAIL_jessica.jones@marvel.com" | ||
| ) | ||
| integration.client.users_list.assert_not_called() | ||
|
|
||
|
|
||
| def test_resolve_user_id_when_users_list_fails(): | ||
|
devin-ai-integration[bot] marked this conversation as resolved.
|
||
| integration = _build_integration() | ||
| integration.client.users_list.side_effect = SlackApiError( | ||
| "error", MagicMock(data={"error": "missing_scope"}) | ||
| ) | ||
| assert integration.resolve_user_id("@jessica.jones") is None | ||
|
|
||
|
|
||
| HANDLE_MENTION_BODY = MessageBody( | ||
| blocks=[ | ||
| LinesBlock(lines=[LineBlock(inlines=[MentionBlock(user="@jessica.jones")])]) | ||
| ] | ||
| ) | ||
|
|
||
|
|
||
| def _send(integration: SlackWebMessagingIntegration, reply: bool) -> str: | ||
| integration.client.chat_postMessage.return_value = { | ||
| "ts": "123.456", | ||
| "channel": "C1", | ||
| } | ||
| if reply: | ||
| context = SlackWebMessageContext(id="111.222", channel="C1") | ||
| integration.reply_to_message("C1", context, HANDLE_MENTION_BODY) | ||
| else: | ||
| integration.send_message("C1", HANDLE_MENTION_BODY) | ||
| sent = integration.client.chat_postMessage.call_args.kwargs | ||
| return sent["blocks"] + sent["attachments"] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("reply", [False, True]) | ||
| def test_send_resolves_handle_mentions(reply): | ||
| integration = _build_integration() | ||
| assert "<@U_JESSICA>" in _send(integration, reply) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("reply", [False, True]) | ||
| def test_send_keeps_plain_handle_when_users_list_raises(reply): | ||
| integration = _build_integration() | ||
| integration.client.users_list.side_effect = ConnectionError("connection reset") | ||
| sent = _send(integration, reply) | ||
| assert "@jessica.jones" in sent | ||
| assert "<@" not in sent | ||
|
|
||
|
|
||
| def test_resolve_user_id_collisions(): | ||
| integration = _build_integration() | ||
| integration.client.users_list.side_effect = lambda cursor=None, limit=None: { | ||
| "members": [ | ||
| { | ||
| "id": "U_GUEST", | ||
| "name": "john", | ||
| "is_restricted": True, | ||
| "profile": {"email": "john@partner.com"}, | ||
| }, | ||
| {"id": "U_JOHN", "name": "jdoe", "profile": {"email": "john@company.com"}}, | ||
| {"id": "U_JOHN_2", "name": "jd", "profile": {"email": "john@other.com"}}, | ||
| { | ||
| "id": "U_JANE", | ||
| "name": "jane.doe", | ||
| "profile": {"email": "jd@company.com"}, | ||
| }, | ||
| ], | ||
| } | ||
| # Full members win over guests, then the first listed user wins. | ||
| assert integration.resolve_user_id("@john") == "U_JOHN" | ||
| # Email prefix matches win over username matches. | ||
| assert integration.resolve_user_id("@jd") == "U_JANE" | ||
|
|
||
|
|
||
| def test_resolve_user_id_when_users_list_cursor_does_not_advance(): | ||
| integration = _build_integration() | ||
| integration.client.users_list.side_effect = lambda cursor=None, limit=None: { | ||
| "members": USERS_PAGE_1["members"], | ||
| "response_metadata": {"next_cursor": "stuck"}, | ||
| } | ||
| assert integration.resolve_user_id("@jessica.jones") == "U_JESSICA" | ||
| assert integration.client.users_list.call_count == 2 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.