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.
This commit is contained in:
Yulya Artyukhina 2024-08-06 17:34:08 +02:00 committed by GitHub
parent c057c09938
commit b755404518
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 1 deletions

View file

@ -82,7 +82,8 @@ class SlackMessage(models.Model):
@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:

View file

@ -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()