mirror of
https://github.com/hanzoai/sentinel.git
synced 2026-09-29 22:34:33 +00:00
133 lines
5.2 KiB
Python
133 lines
5.2 KiB
Python
import pytest
|
|
import responses
|
|
|
|
from sentry.integrations.slack.utils import get_channel_id
|
|
from sentry.integrations.slack.utils.channel import CHANNEL_PREFIX, MEMBER_PREFIX
|
|
from sentry.shared_integrations.exceptions import ApiRateLimitedError, DuplicateDisplayNameError
|
|
from sentry.testutils.cases import TestCase
|
|
from sentry.testutils.helpers import install_slack
|
|
from sentry.testutils.skips import requires_snuba
|
|
from sentry.utils import json
|
|
|
|
pytestmark = [requires_snuba]
|
|
|
|
|
|
class GetChannelIdTest(TestCase):
|
|
def setUp(self):
|
|
self.resp = responses.mock
|
|
self.resp.__enter__()
|
|
|
|
self.integration = install_slack(self.event.project.organization)
|
|
|
|
def tearDown(self):
|
|
self.resp.__exit__(None, None, None)
|
|
|
|
def add_list_response(self, list_type, channels, result_name="channels"):
|
|
self.resp = responses.mock
|
|
self.resp.add(
|
|
method=responses.GET,
|
|
url="https://slack.com/api/%s.list" % list_type,
|
|
status=200,
|
|
content_type="application/json",
|
|
body=json.dumps({"ok": "true", result_name: channels}),
|
|
)
|
|
|
|
def add_msg_response(self, channel_id, result_name="channel"):
|
|
if channel_id == "channel_not_found":
|
|
bodydict = {"ok": False, "error": "channel_not_found"}
|
|
else:
|
|
bodydict = {"ok": True, result_name: channel_id, "scheduled_message_id": "Q1298393284"}
|
|
|
|
self.resp.add(
|
|
method=responses.POST,
|
|
url="https://slack.com/api/chat.scheduleMessage",
|
|
status=200,
|
|
content_type="application/json",
|
|
body=json.dumps(bodydict),
|
|
)
|
|
|
|
def run_valid_test(self, channel, expected_prefix, expected_id, timed_out):
|
|
assert (expected_prefix, expected_id, timed_out) == get_channel_id(
|
|
self.organization, self.integration, channel
|
|
)
|
|
|
|
def test_valid_channel_selected(self):
|
|
self.add_msg_response("m-c")
|
|
self.resp.add(
|
|
method=responses.POST,
|
|
url="https://slack.com/api/chat.deleteScheduledMessage",
|
|
status=200,
|
|
content_type="application/json",
|
|
body=json.dumps({"ok": True}),
|
|
)
|
|
self.run_valid_test("#My-Channel", CHANNEL_PREFIX, "m-c", False)
|
|
|
|
def test_valid_private_channel_selected(self):
|
|
self.add_msg_response("m-p-c")
|
|
self.resp.add(
|
|
method=responses.POST,
|
|
url="https://slack.com/api/chat.deleteScheduledMessage",
|
|
status=200,
|
|
content_type="application/json",
|
|
body=json.dumps({"ok": True}),
|
|
)
|
|
self.run_valid_test("#my-private-channel", CHANNEL_PREFIX, "m-p-c", False)
|
|
|
|
def test_valid_member_selected(self):
|
|
self.add_msg_response("channel_not_found")
|
|
self.add_list_response(
|
|
"users",
|
|
[
|
|
{"name": "first-morty", "id": "m", "profile": {"display_name": "Morty"}},
|
|
{"name": "other-user", "id": "o-u", "profile": {"display_name": "Jimbob"}},
|
|
{"name": "better_morty", "id": "bm", "profile": {"display_name": "Morty"}},
|
|
],
|
|
result_name="members",
|
|
)
|
|
self.run_valid_test("@first-morty", MEMBER_PREFIX, "m", False)
|
|
|
|
def test_valid_member_selected_display_name(self):
|
|
self.add_msg_response("channel_not_found")
|
|
self.add_list_response(
|
|
"users",
|
|
[
|
|
{"name": "first-morty", "id": "m", "profile": {"display_name": "Morty"}},
|
|
{"name": "other-user", "id": "o-u", "profile": {"display_name": "Jimbob"}},
|
|
{"name": "better_morty", "id": "bm", "profile": {"display_name": "Morty"}},
|
|
],
|
|
result_name="members",
|
|
)
|
|
self.run_valid_test("@Jimbob", MEMBER_PREFIX, "o-u", False)
|
|
|
|
def test_invalid_member_selected_display_name(self):
|
|
self.add_msg_response("channel_not_found")
|
|
self.add_list_response(
|
|
"users",
|
|
[
|
|
{"name": "first-morty", "id": "m", "profile": {"display_name": "Morty"}},
|
|
{"name": "other-user", "id": "o-u", "profile": {"display_name": "Jimbob"}},
|
|
{"name": "better_morty", "id": "bm", "profile": {"display_name": "Morty"}},
|
|
],
|
|
result_name="members",
|
|
)
|
|
with pytest.raises(DuplicateDisplayNameError):
|
|
get_channel_id(self.organization, self.integration, "@Morty")
|
|
|
|
def test_invalid_channel_selected(self):
|
|
self.add_msg_response("channel_not_found")
|
|
assert get_channel_id(self.organization, self.integration, "#fake-channel")[1] is None
|
|
assert get_channel_id(self.organization, self.integration, "@fake-user")[1] is None
|
|
|
|
def test_rate_limiting(self):
|
|
"""Should handle 429 from Slack when searching for users"""
|
|
self.add_msg_response("channel_not_found")
|
|
self.resp.add(
|
|
method=responses.GET,
|
|
url="https://slack.com/api/users.list",
|
|
status=429,
|
|
content_type="application/json",
|
|
body=json.dumps({"ok": False, "error": "ratelimited"}),
|
|
)
|
|
with pytest.raises(ApiRateLimitedError):
|
|
get_channel_id(self.organization, self.integration, "@user")
|