Skip to content

Commit

Permalink
mock signing secret
Browse files Browse the repository at this point in the history
  • Loading branch information
MeredithAnya committed Feb 10, 2021
1 parent 05d858a commit cfb2739
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
14 changes: 5 additions & 9 deletions tests/sentry/integrations/slack/test_action_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,10 @@ def test_slack_bad_payload(self, check_signing_secret_mock):
resp = self.client.post("/extensions/slack/action/", data={"nopayload": 0})
assert resp.status_code == 400

def test_sentry_docs_link_clicked(self):
@patch(
"sentry.integrations.slack.requests.SlackRequest._check_signing_secret", return_value=True
)
def test_sentry_docs_link_clicked(self, check_signing_secret_mock):
payload = {
"team": {"id": "TXXXXXXX1", "domain": "example.com"},
"user": {"id": self.identity.external_id, "domain": "example"},
Expand All @@ -421,13 +424,6 @@ def test_sentry_docs_link_clicked(self):
}

payload = {"payload": json.dumps(payload)}
from tests.sentry.integrations.slack.test_requests import set_signature
from sentry import options
from urllib.parse import urlencode

headers = set_signature(
options.get("slack.signing-secret"), urlencode(payload).encode("utf-8")
)

resp = self.client.post("/extensions/slack/action/", data=payload, **headers)
resp = self.client.post("/extensions/slack/action/", data=payload)
assert resp.status_code == 200
30 changes: 16 additions & 14 deletions tests/sentry/integrations/slack/test_event_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import responses
from urllib.parse import parse_qsl
from sentry.utils.compat.mock import patch

from sentry import options
from sentry.utils import json
from sentry.integrations.slack.utils import build_group_attachment, build_incident_attachment
from sentry.models import Integration, OrganizationIntegration
Expand Down Expand Up @@ -79,13 +79,19 @@ def setUp(self):
)
OrganizationIntegration.objects.create(organization=self.org, integration=self.integration)

@patch(
"sentry.integrations.slack.requests.SlackRequest._check_signing_secret", return_value=True
)
def post_webhook(
self, event_data=None, type="event_callback", data=None, token=UNSET, team_id="TXXXXXXX1"
self,
check_signing_secret_mock,
event_data=None,
type="event_callback",
data=None,
token=UNSET,
team_id="TXXXXXXX1",
):
if token is UNSET:
token = options.get("slack.verification-token")
payload = {
"token": token,
"team_id": team_id,
"api_app_id": "AXXXXXXXX1",
"type": type,
Expand All @@ -97,31 +103,27 @@ def post_webhook(
payload.update(data)
if event_data:
payload.setdefault("event", {}).update(event_data)

return self.client.post("/extensions/slack/event/", payload)


class UrlVerificationEventTest(BaseEventTest):
challenge = "3eZbrw1aBm2rZgRNFdxV2595E9CY3gmdALWMmHkvFXO7tYXAYM8P"

def test_valid_token(self):
@patch(
"sentry.integrations.slack.requests.SlackRequest._check_signing_secret", return_value=True
)
def test_valid_event(self, check_signing_secret_mock):
resp = self.client.post(
"/extensions/slack/event/",
{
"type": "url_verification",
"challenge": self.challenge,
"token": options.get("slack.verification-token"),
},
)
assert resp.status_code == 200, resp.content
assert resp.data["challenge"] == self.challenge

def test_invalid_token(self):
resp = self.client.post(
"/extensions/slack/event/",
{"type": "url_verification", "challenge": self.challenge, "token": "fizzbuzz"},
)
assert resp.status_code == 401, resp.content


class LinkSharedEventTest(BaseEventTest):
@responses.activate
Expand Down

0 comments on commit cfb2739

Please sign in to comment.