Skip to content

Commit

Permalink
Don't send request for permalink if slack token has been revoked (gra…
Browse files Browse the repository at this point in the history
…fana#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 grafana/oncall-private#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.
  • Loading branch information
Ferril authored Aug 6, 2024
1 parent c057c09 commit b755404
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion engine/apps/slack/models/slack_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 16 additions & 0 deletions engine/apps/slack/test_slack_message.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()

0 comments on commit b755404

Please sign in to comment.