oncall-engine/engine/apps/chatops_proxy/tests/test_events.py
Innokentii Konstantinov 17f448c506
Prepare OnCall for Unified Slack App (#4232)
This PR does a bunch of changes to prepare OnCall for Unified Slack App:
1. Install Slack via Chatops-Proxy. This change contains two parts:
getting a Slack install link from chatops-proxy
([code](https://github.com/grafana/oncall/pull/4232/files#diff-437a77d49fc04b92d315651b3df5991000b1ab74cf60aabb21aa77cb2823bf52R46))
and receiving a "slack installed" event from chatops-proxy
([code](https://github.com/grafana/oncall/pull/4232/files#diff-976d106f0962be5c1de5e35582193f68435ed0c17f2defd6bd2857bf6e27f65d)).
Also it means that OnCall doesn't need to register slack_links anymore
when slack is connected/disconnected. These changes are behind
UNIFIED_SLACK_APP_ENABLED flag and should be no-op if flag is not
enabled.
2. Get rid of Multiregionatily restrictions - instrument all slack
interactions with a ProxyMeta - json data telling chatops-proxy where to
route the interaction. Note, that it doesn't apply for "Add to
resolution notes" message action - it will be handled differently in
following PR.
3. Move all chatops-proxy related stuff from common/oncall-gateway to
apps/chatops-proxy

Minor changes:
1. Remove usage of **CHATOPS_V3** flag. Chatops v3 is already released
(It's a refactoring from previous quarter)

---------

Co-authored-by: Vadim Stepanov <vadimkerr@gmail.com>
Co-authored-by: Rares Mardare <rares.mardare@grafana.com>
2024-06-03 09:07:10 +00:00

66 lines
1.9 KiB
Python

from unittest.mock import patch
import pytest
from django.test import override_settings
from apps.chatops_proxy.events import ChatopsEventsHandler
from apps.chatops_proxy.events.handlers import SlackInstallationHandler
from common.constants.slack_auth import SLACK_OAUTH_ACCESS_RESPONSE
installation_event = {
"event_type": "integration_installed",
"data": {
"provider_type": "slack",
"stack_id": "stack_id",
"grafana_user_id": "grafana_user_id",
"payload": SLACK_OAUTH_ACCESS_RESPONSE,
},
}
unknown_event = {
"event_type": "unknown_event",
"data": {
"provider_type": "slack",
"stack_id": "stack_id",
"grafana_user_id": "grafana_user_id",
"payload": {},
},
}
invalid_schema_event = {
"a": "b",
"c": "d",
}
@patch.object(ChatopsEventsHandler, "_exec", return_value=None)
@pytest.mark.parametrize(
"payload,is_handled",
[
(installation_event, True),
(unknown_event, False),
(invalid_schema_event, False),
],
)
@pytest.mark.django_db
@override_settings(UNIFIED_SLACK_APP_ENABLED=True)
def test_root_event_handler(mock_exec, payload, is_handled):
h = ChatopsEventsHandler()
assert h.handle(payload) is is_handled
@patch("apps.chatops_proxy.events.handlers.install_slack_integration", return_value=None)
@pytest.mark.django_db
def test_slack_installation_handler(mock_install_slack_integration, make_organization_and_user):
organization, user = make_organization_and_user()
installation_event["data"].update({"stack_id": organization.stack_id, "grafana_user_id": user.user_id})
h = SlackInstallationHandler()
assert h.match(unknown_event) is False
assert h.match(invalid_schema_event) is False
assert h.match(installation_event) is True
h.handle(installation_event["data"])
assert mock_install_slack_integration.call_args.args == (organization, user, installation_event["data"]["payload"])