def installation_webhook(installation_id, user_id, *args, **kwargs): from sentry.mediators.sentry_app_installations import InstallationNotifier extra = { 'installation_id': installation_id, 'user_id': user_id, } try: install = SentryAppInstallation.objects.get(id=installation_id) except SentryAppInstallation.DoesNotExist: logger.info('installation_webhook.missing_installation', extra=extra) return try: user = User.objects.get(id=user_id) except User.DoesNotExist: logger.info('installation_webhook.missing_user', extra=extra) return InstallationNotifier.run( install=install, user=user, action='created', )
def test_uninstallation_enqueued(self, safe_urlopen): InstallationNotifier.run(install=self.install, user=self.user, action="deleted") data = faux(safe_urlopen).kwargs["data"] assert data == json.dumps( { "action": "deleted", "installation": {"uuid": self.install.uuid}, "data": { "installation": { "app": {"uuid": self.sentry_app.uuid, "slug": self.sentry_app.slug}, "organization": {"slug": self.org.slug}, "uuid": self.install.uuid, "code": self.install.api_grant.code, "status": "pending", } }, "actor": {"id": self.user.id, "name": self.user.name, "type": "user"}, } ) assert faux(safe_urlopen).kwarg_equals( "headers", DictContaining( "Content-Type", "Request-ID", "Sentry-Hook-Resource", "Sentry-Hook-Timestamp", "Sentry-Hook-Signature", ), )
def test_invalid_installation_action(self, safe_urlopen): with self.assertRaises(APIUnauthorized): InstallationNotifier.run(install=self.install, user=self.user, action="updated") assert not safe_urlopen.called
def test_task_enqueued(self, safe_urlopen): InstallationNotifier.run( install=self.install, user=self.user, ) safe_urlopen.assert_called_once_with( 'https://example.com', json={ 'action': 'installation', 'installation': { 'uuid': self.install.uuid, }, 'data': { 'app': { 'uuid': self.sentry_app.uuid, 'slug': self.sentry_app.slug, }, 'organization': { 'slug': self.org.slug, }, 'uuid': self.install.uuid, 'code': self.install.api_grant.code, }, }, timeout=5, )
def installation_webhook(installation_id, user_id): from sentry.mediators.sentry_app_installations import InstallationNotifier try: install = SentryAppInstallation.objects.get(id=installation_id) except SentryAppInstallation.DoesNotExist: logger.info( 'installation_webhook.missing_installation', extra={ 'installation_id': installation_id, 'user_id': user_id, }, ) return try: user = User.objects.get(id=user_id) except User.DoesNotExist: logger.info( 'installation_webhook.missing_user', extra={ 'installation_id': installation_id, 'user_id': user_id, }, ) return InstallationNotifier.run( install=install, user=user, )
def test_webhook_request_saved(self, safe_urlopen): InstallationNotifier.run(install=self.install, user=self.user, action="created") InstallationNotifier.run(install=self.install, user=self.user, action="deleted") buffer = SentryAppWebhookRequestsBuffer(self.sentry_app) requests = buffer.get_requests() assert len(requests) == 2 assert requests[0]["event_type"] == "installation.deleted" assert requests[1]["event_type"] == "installation.created"
def installation_webhook(installation_id, user_id, *args, **kwargs): from sentry.mediators.sentry_app_installations import InstallationNotifier extra = {"installation_id": installation_id, "user_id": user_id} try: # we should send the webhook for pending installations on the install event in case that's part of the workflow install = SentryAppInstallation.objects.get(id=installation_id) except SentryAppInstallation.DoesNotExist: logger.info("installation_webhook.missing_installation", extra=extra) return try: user = User.objects.get(id=user_id) except User.DoesNotExist: logger.info("installation_webhook.missing_user", extra=extra) return InstallationNotifier.run(install=install, user=user, action="created")
def test_uninstallation_enqueued(self, safe_urlopen): InstallationNotifier.run( install=self.install, user=self.user, action='deleted', ) data = faux(safe_urlopen).kwargs['data'] assert data == json.dumps({ 'action': 'deleted', 'installation': { 'uuid': self.install.uuid, }, 'data': { 'installation': { 'app': { 'uuid': self.sentry_app.uuid, 'slug': self.sentry_app.slug, }, 'organization': { 'slug': self.org.slug, }, 'uuid': self.install.uuid, 'code': self.install.api_grant.code, 'status': 'pending' }, }, 'actor': { 'id': self.user.id, 'name': self.user.name, 'type': 'user', }, }) assert faux(safe_urlopen).kwarg_equals( 'headers', DictContaining( 'Content-Type', 'Request-ID', 'Sentry-Hook-Resource', 'Sentry-Hook-Timestamp', 'Sentry-Hook-Signature', ))
def test_task_enqueued(self, safe_urlopen): InstallationNotifier.run( install=self.install, user=self.user, ) data = faux(safe_urlopen).kwargs['data'] assert data == { 'action': 'created', 'installation': { 'uuid': self.install.uuid, }, 'data': { 'app': { 'uuid': self.sentry_app.uuid, 'slug': self.sentry_app.slug, }, 'organization': { 'slug': self.org.slug, }, 'uuid': self.install.uuid, 'code': self.install.api_grant.code, }, 'actor': { 'id': self.user.id, 'name': self.user.name, 'type': 'user', }, } assert faux(safe_urlopen).kwarg_equals('headers', DictContaining( 'Content-Type', 'Request-ID', 'Sentry-Hook-Resource', 'Sentry-Hook-Timestamp', 'Sentry-Hook-Signature', ))