Skip to content
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

Allow kwargs on Slack API. #397

Merged
merged 5 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions spidermon/contrib/actions/slack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ def users(self):
return self._users

def send_message(
self, to, text, parse=None, link_names=1, attachments=None, use_mention=False
self,
to,
text,
parse=None,
link_names=1,
attachments=None,
use_mention=False,
**kwargs,
):
if self.fake:
logger.info(text)
Expand All @@ -56,6 +63,7 @@ def send_message(
link_names=link_names,
attachments=attachments,
use_mention=use_mention,
**kwargs,
)
for recipient in to
]
Expand All @@ -66,6 +74,7 @@ def send_message(
parse=parse,
link_names=link_names,
attachments=attachments,
**kwargs,
)
else:
if use_mention:
Expand All @@ -79,6 +88,7 @@ def send_message(
parse=parse,
link_names=link_names,
attachments=attachments,
**kwargs,
)

def _get_user_id(self, username):
Expand All @@ -95,7 +105,7 @@ def _get_users_info(self):
)

def _send_user_message(
self, username, text, parse="full", link_names=1, attachments=None
self, username, text, parse="full", link_names=1, attachments=None, **kwargs
):
user_id = self._get_user_id(username)
if user_id:
Expand All @@ -105,10 +115,11 @@ def _send_user_message(
parse=parse,
link_names=link_names,
attachments=attachments,
**kwargs,
)

def _send_channel_message(
self, channel, text, parse="full", link_names=1, attachments=None
self, channel, text, parse="full", link_names=1, attachments=None, **kwargs
):
self._client.chat_postMessage(
channel=channel,
Expand All @@ -118,6 +129,7 @@ def _send_channel_message(
attachments=self._parse_attachments(attachments),
username=self.sender_name,
icon_url=self._get_icon_url(),
**kwargs,
)

def _get_icon_url(self):
Expand Down Expand Up @@ -184,6 +196,7 @@ def __init__(
attachments_template=None,
include_attachments=None,
fake=None,
**kwargs,
):
super().__init__()

Expand All @@ -207,6 +220,8 @@ def __init__(
self.attachments = attachments or self.attachments
self.attachments_template = attachments_template or self.attachments_template

self.kwargs = kwargs or {}

if not self.fake and not self.recipients:
raise NotConfigured(
"You must provide a value for SPIDERMON_SLACK_RECIPIENTS setting."
Expand Down Expand Up @@ -238,6 +253,7 @@ def run_action(self):
to=self.recipients,
text=self.get_message(),
attachments=self.get_attachments(),
**self.kwargs,
)

def get_message(self):
Expand Down
56 changes: 55 additions & 1 deletion tests/contrib/actions/slack/test_slack_action.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import sys
import pytest

from spidermon.contrib.actions.slack import SlackMessageManager
from unittest.mock import MagicMock, patch
from spidermon.contrib.actions.slack import SendSlackMessage, SlackMessageManager


@pytest.fixture
Expand Down Expand Up @@ -58,3 +59,56 @@ def test_do_not_log_text_and_attach_when_fake_is_not_set(logger_info):
)

assert logger_info.call_count == 0


@patch("spidermon.contrib.actions.slack.WebClient")
def test_pass_arbitrary_args_to_manager_send_message_channel(_slack_mock):
manager = SlackMessageManager(
sender_token="anything",
sender_name="@someone",
)

manager.send_message(
to=["channel"], text="a message", use_mention=True, arbitrary_arg=True
)

_, kwargs = manager._client.chat_postMessage.call_args_list[0]
assert "arbitrary_arg" in kwargs


@patch("spidermon.contrib.actions.slack.WebClient")
def test_pass_arbitrary_args_to_manager_send_message_user(_slack_mock):
manager = SlackMessageManager(
sender_token="anything",
sender_name="@someone",
)

manager._users = {"user": {"id": 10}}

manager.send_message(
to=["@user"], text="a message", use_mention=True, arbitrary_arg=True
)

_, kwargs = manager._client.chat_postMessage.call_args_list[0]
assert "arbitrary_arg" in kwargs


def test_message_sender_pass_kwargs():
sender = SendSlackMessage(
sender_token="anything",
sender_name="@someone",
recipients=["user"],
a_new_arg="hello",
)

sender.manager._client = MagicMock()
sender.get_message = MagicMock()
sender.get_attachments = MagicMock()

sender.get_message.return_value = "a message"
sender.get_attachments.return_value = None

sender.run_action()

_, kwargs = sender.manager._client.chat_postMessage.call_args_list[0]
assert "a_new_arg" in kwargs