Skip to content

Commit

Permalink
refactor tests to use response body as verification
Browse files Browse the repository at this point in the history
  • Loading branch information
Christinarlong committed Oct 8, 2024
1 parent a296640 commit 6c23936
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from sentry.sentry_apps.services.app import app_service
from sentry.testutils.cases import APITestCase
from sentry.testutils.silo import control_silo_test
from sentry.users.services.user.service import user_service
from sentry.utils import json


class SentryAppInstallationDetailsTest(APITestCase):
Expand Down Expand Up @@ -101,9 +103,10 @@ def test_no_access_outside_install_organization(self):
class DeleteSentryAppInstallationDetailsTest(SentryAppInstallationDetailsTest):
@responses.activate
@patch("sentry.analytics.record")
def test_delete_install(self, record, request):
def test_delete_install(self, record):
responses.add(url="https://example.com/webhook", method=responses.POST, body=b"")
self.login_as(user=self.user)
rpc_user = user_service.get_user(user_id=self.user.id)
response = self.client.delete(self.url, format="json")
assert AuditLogEntry.objects.filter(
event=audit_log.get_event_id("SENTRY_APP_UNINSTALL")
Expand All @@ -115,6 +118,12 @@ def test_delete_install(self, record, request):
sentry_app=self.orm_installation2.sentry_app.slug,
)

response_body = json.loads(responses.calls[0].request.body)

assert response_body.get("installation").get("uuid") == self.orm_installation2.uuid
assert response_body.get("action") == "deleted"
assert response_body.get("actor")["id"] == rpc_user.id

assert response.status_code == 204

def test_member_cannot_delete_install(self):
Expand Down
12 changes: 8 additions & 4 deletions tests/sentry/sentry_apps/test_sentry_app_installation_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,18 @@ def test_creates_audit_log_entry(self):
).exists()

@responses.activate
# @patch("sentry.utils.sentry_apps.webhooks.send_and_save_webhook_request")
def test_notifies_service(self):
responses.add(responses.POST, "https://example.com/webhook")

rpc_user = user_service.get_user(user_id=self.user.id)
with self.tasks():
self.run_creator()
assert json.loads(responses.calls[0].request.body).get("action") == "created"
assert json.loads(responses.calls[0].request.body).get("actor")["id"] == rpc_user.id
responses.add(responses.POST, "https://example.com/webhook")
install = self.run_creator()
response_body = json.loads(responses.calls[0].request.body)

assert response_body.get("installation").get("uuid") == install.uuid
assert response_body.get("action") == "created"
assert response_body.get("actor")["id"] == rpc_user.id

@responses.activate
def test_associations(self):
Expand Down
25 changes: 18 additions & 7 deletions tests/sentry/tasks/test_sentry_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from unittest.mock import ANY, patch

import pytest
import responses
from celery import Task
from django.core import mail
from django.test import override_settings
Expand Down Expand Up @@ -416,7 +417,6 @@ def test_sends_webhooks_to_all_installs(self, safe_urlopen):


@control_silo_test
@patch("sentry.sentry_apps.installations.SentryAppInstallationNotifier.run")
class TestInstallationWebhook(TestCase):
def setUp(self):
self.project = self.create_project()
Expand All @@ -429,18 +429,29 @@ def setUp(self):
organization=self.project.organization, slug=self.sentry_app.slug
)

def test_sends_installation_notification(self, run):
@responses.activate
def test_sends_installation_notification(self):
responses.add(responses.POST, "https://example.com/webhook")
installation_webhook(self.install.id, self.user.id)

run.assert_called_with(install=self.install, user=self.rpc_user, action="created")
response_body = json.loads(responses.calls[0].request.body)
assert response_body.get("installation").get("uuid") == self.install.uuid
assert response_body.get("action") == "created"
assert response_body.get("actor")["id"] == self.rpc_user.id

@responses.activate
def test_gracefully_handles_missing_install(self):
responses.add(responses.POST, "https://example.com/webhook")

def test_gracefully_handles_missing_install(self, run):
installation_webhook(999, self.user.id)
assert len(run.mock_calls) == 0
assert len(responses.calls) == 0

@responses.activate
def test_gracefully_handles_missing_user(self):
responses.add(responses.POST, "https://example.com/webhook")

def test_gracefully_handles_missing_user(self, run):
installation_webhook(self.install.id, 999)
assert len(run.mock_calls) == 0
assert len(responses.calls) == 0


@patch("sentry.utils.sentry_apps.webhooks.safe_urlopen", return_value=MockResponseInstance)
Expand Down

0 comments on commit 6c23936

Please sign in to comment.