From b755404518dfc195a1e37873b602478e22e8ca86 Mon Sep 17 00:00:00 2001 From: Yulya Artyukhina Date: Tue, 6 Aug 2024 17:34:08 +0200 Subject: [PATCH] Don't send request for permalink if slack token has been revoked (#4777) # What this PR does Don't send request for permalink if slack token has been revoked ## Which issue(s) this PR closes Related to https://github.com/grafana/oncall-private/issues/2843 ## Checklist - [x] Unit, integration, and e2e (if applicable) tests updated - [x] Documentation added (or `pr:no public docs` PR label added if not required) - [x] Added the relevant release notes label (see labels prefixed w/ `release:`). These labels dictate how your PR will show up in the autogenerated release notes. --- engine/apps/slack/models/slack_message.py | 3 ++- engine/apps/slack/test_slack_message.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/engine/apps/slack/models/slack_message.py b/engine/apps/slack/models/slack_message.py index c6bbcff6ff..1e1c8f7f7d 100644 --- a/engine/apps/slack/models/slack_message.py +++ b/engine/apps/slack/models/slack_message.py @@ -82,7 +82,8 @@ def slack_team_identity(self): @property def permalink(self) -> typing.Optional[str]: - if self.cached_permalink or not self.slack_team_identity: + # Don't send request for permalink if there is no slack_team_identity or slack token has been revoked + if self.cached_permalink or not self.slack_team_identity or self.slack_team_identity.detected_token_revoked: return self.cached_permalink try: diff --git a/engine/apps/slack/test_slack_message.py b/engine/apps/slack/test_slack_message.py index 7067e60fdd..cd68aca486 100644 --- a/engine/apps/slack/test_slack_message.py +++ b/engine/apps/slack/test_slack_message.py @@ -1,6 +1,7 @@ from unittest.mock import patch import pytest +from django.utils import timezone from apps.slack.client import SlackClient from apps.slack.errors import SlackAPIError @@ -60,3 +61,18 @@ def test_slack_message_permalink_cache(mock_slack_api_call, slack_message_setup) slack_message = slack_message_setup(cached_permalink="cached_permalink") assert slack_message.permalink == "cached_permalink" mock_slack_api_call.assert_not_called() + + +@patch.object( + SlackClient, + "chat_getPermalink", + return_value=build_slack_response({"ok": False, "error": "account_inactive"}), +) +@pytest.mark.django_db +def test_slack_message_permalink_token_revoked(mock_slack_api_call, slack_message_setup): + slack_message = slack_message_setup(cached_permalink=None) + slack_message._slack_team_identity.detected_token_revoked = timezone.now() + slack_message._slack_team_identity.save() + assert slack_message._slack_team_identity is not None + assert slack_message.permalink is None + mock_slack_api_call.assert_not_called()