def setUp(self): self.user = self.create_user() self.org = self.create_organization(owner=self.user) self.project = self.create_project(organization=self.org) self.creator = InternalCreator( name="nulldb", user=self.user, organization=self.org, scopes=("project:read", ), webhook_url="http://example.com", schema={"elements": [self.create_issue_link_schema()]}, )
def setUp(self): self.user = self.create_user() self.org = self.create_organization(owner=self.user) self.project = self.create_project(organization=self.org) self.creator = InternalCreator( name='nulldb', user=self.user, author='Sentry', organization=self.org, scopes=('project:read',), webhook_url='http://example.com', schema={'elements': [self.create_issue_link_schema()]}, )
def test_audits(self, create_audit_entry): InternalCreator.run( name="nulldb", user=self.user, author="Sentry", organization=self.org, scopes=("project:read",), webhook_url="http://example.com", schema={"elements": [self.create_issue_link_schema()]}, request=MagicMock(), ) call = faux(create_audit_entry) assert call.kwarg_equals("organization", self.org) assert call.kwarg_equals("target_object", self.org.id) assert call.kwarg_equals("event", AuditLogEntryEvent.INTERNAL_INTEGRATION_ADD)
def post_install(self, integration, organization, extra=None): # check if we have an Vercel internal installation already if SentryAppInstallationForProvider.objects.filter( organization=organization, provider="vercel").exists(): logger.info( "vercel.post_install.installation_exists", extra={"organization_id": organization.id}, ) return user = User.objects.get(id=extra.get("user_id")) data = { "name": "Vercel Internal Integration", "author": "Auto-generated by Sentry", "organization": organization, "overview": internal_integration_overview.strip(), "user": user, "scopes": ["project:releases", "project:read", "project:write"], } # create the internal integration and link it to the join table sentry_app = InternalCreator.run(**data) sentry_app_installation = SentryAppInstallation.objects.get( sentry_app=sentry_app) SentryAppInstallationForProvider.objects.create( sentry_app_installation=sentry_app_installation, organization=organization, provider="vercel", )
def test_audits(self, create_audit_entry): InternalCreator.run( name='nulldb', user=self.user, author='Sentry', organization=self.org, scopes=('project:read',), webhook_url='http://example.com', schema={'elements': [self.create_issue_link_schema()]}, request=MagicMock(), ) call = faux(create_audit_entry) assert call.kwarg_equals('organization', self.org) assert call.kwarg_equals('target_object', self.org.id) assert call.kwarg_equals('event', AuditLogEntryEvent.INTERNAL_INTEGRATION_ADD)
def run_creator(self, **kwargs): return InternalCreator.run( name="nulldb", user=self.user, organization=self.org, scopes=("project:read", ), webhook_url="http://example.com", schema={"elements": [self.create_issue_link_schema()]}, **kwargs)
def enable_scim(self, user): from sentry.mediators.sentry_apps import InternalCreator from sentry.models import SentryAppInstallation, SentryAppInstallationForProvider if (not self.get_provider().can_use_scim(self.organization, user) or self.flags.scim_enabled is True): logger.warning( "SCIM already enabled", extra={"organization_id": self.organization.id}, ) return # check if we have a scim app already if SentryAppInstallationForProvider.objects.filter( organization=self.organization, provider="okta_scim").exists(): logger.warning( "SCIM installation already exists", extra={"organization_id": self.organization.id}, ) return data = { "name": "SCIM Internal Integration", "author": "Auto-generated by Sentry", "organization": self.organization, "overview": SCIM_INTERNAL_INTEGRATION_OVERVIEW, "user": user, "scopes": [ "member:read", "member:write", "member:admin", "team:write", "team:admin", ], } # create the internal integration and link it to the join table sentry_app = InternalCreator.run(**data) sentry_app_installation = SentryAppInstallation.objects.get( sentry_app=sentry_app) SentryAppInstallationForProvider.objects.create( sentry_app_installation=sentry_app_installation, organization=self.organization, provider=f"{self.provider}_scim", ) self.flags.scim_enabled = True
def test_records_analytics(self, create_audit_entry, record): sentry_app = InternalCreator.run( name="nulldb", user=self.user, author="Sentry", organization=self.org, scopes=("project:read", ), webhook_url="http://example.com", schema={"elements": [self.create_issue_link_schema()]}, request=MagicMock(), ) assert faux(record).args_equals("internal_integration.created") assert faux(record).kwargs == { "user_id": self.user.id, "organization_id": self.org.id, "sentry_app": sentry_app.slug, }
def test_records_analytics(self, create_audit_entry, record): sentry_app = InternalCreator.run( name='nulldb', user=self.user, author='Sentry', organization=self.org, scopes=('project:read',), webhook_url='http://example.com', schema={'elements': [self.create_issue_link_schema()]}, request=MagicMock(), ) assert faux(record).args_equals('internal_integration.created') assert faux(record).kwargs == { 'user_id': self.user.id, 'organization_id': self.org.id, 'sentry_app': sentry_app.slug, }
def post_install(self, integration, organization, extra=None): # add new configuration information to metadata configurations = integration.metadata.get("configurations") or {} configurations[integration.metadata["installation_id"]] = { "access_token": integration.metadata["access_token"], "webhook_id": integration.metadata["webhook_id"], "organization_id": organization.id, } integration.metadata["configurations"] = configurations integration.save() # check if we have an installation already if SentryAppInstallationForProvider.objects.filter( organization=organization, provider="vercel").exists(): logger.info( "vercel.post_install.installation_exists", extra={"organization_id": organization.id}, ) return user = User.objects.get(id=extra.get("user_id")) data = { "name": "Vercel Internal Integration", "author": "Auto-generated by Sentry", "organization": organization, "overview": internal_integration_overview.strip(), "user": user, "scopes": ["project:releases", "project:read", "project:write"], } # create the internal integration and link it to the join table sentry_app = InternalCreator.run(**data) sentry_app_installation = SentryAppInstallation.objects.get( sentry_app=sentry_app) SentryAppInstallationForProvider.objects.create( sentry_app_installation=sentry_app_installation, organization=organization, provider="vercel", )
class TestInternalCreator(TestCase): def setUp(self): self.user = self.create_user() self.org = self.create_organization(owner=self.user) self.project = self.create_project(organization=self.org) self.creator = InternalCreator( name="nulldb", user=self.user, author="Sentry", organization=self.org, scopes=("project:read", ), webhook_url="http://example.com", schema={"elements": [self.create_issue_link_schema()]}, ) def test_slug(self): sentry_app = self.creator.call() # test slug is the name + a UUID assert sentry_app.slug[:7] == "nulldb-" assert len(sentry_app.slug) == 13 def test_creates_internal_sentry_app(self): sentry_app = self.creator.call() assert SentryApp.objects.filter(slug=sentry_app.slug).exists() def test_installs_to_org(self): sentry_app = self.creator.call() assert SentryAppInstallation.objects.filter( organization=self.org, sentry_app=sentry_app).exists() @patch("sentry.tasks.sentry_apps.installation_webhook.delay") def test_does_not_notify_service(self, delay): self.creator.call() assert not len(delay.mock_calls) def test_creates_access_token(self): sentry_app = self.creator.call() install = SentryAppInstallation.objects.get(organization=self.org, sentry_app=sentry_app) assert install.api_token @patch("sentry.utils.audit.create_audit_entry") def test_audits(self, create_audit_entry): InternalCreator.run( name="nulldb", user=self.user, author="Sentry", organization=self.org, scopes=("project:read", ), webhook_url="http://example.com", schema={"elements": [self.create_issue_link_schema()]}, request=MagicMock(), ) call = faux(create_audit_entry) assert call.kwarg_equals("organization", self.org) assert call.kwarg_equals("target_object", self.org.id) assert call.kwarg_equals("event", AuditLogEntryEvent.INTERNAL_INTEGRATION_ADD) @patch("sentry.analytics.record") @patch("sentry.utils.audit.create_audit_entry") def test_records_analytics(self, create_audit_entry, record): sentry_app = InternalCreator.run( name="nulldb", user=self.user, author="Sentry", organization=self.org, scopes=("project:read", ), webhook_url="http://example.com", schema={"elements": [self.create_issue_link_schema()]}, request=MagicMock(), ) assert faux(record).args_equals("internal_integration.created") assert faux(record).kwargs == { "user_id": self.user.id, "organization_id": self.org.id, "sentry_app": sentry_app.slug, }
class TestInternalCreator(TestCase): def setUp(self): self.user = self.create_user() self.org = self.create_organization(owner=self.user) self.project = self.create_project(organization=self.org) self.creator = InternalCreator( name='nulldb', user=self.user, author='Sentry', organization=self.org, scopes=('project:read',), webhook_url='http://example.com', schema={'elements': [self.create_issue_link_schema()]}, ) def test_creates_internal_sentry_app(self): sentry_app = self.creator.call() assert SentryApp.objects.filter(slug=sentry_app.slug).exists() def test_installs_to_org(self): sentry_app = self.creator.call() assert SentryAppInstallation.objects.filter( organization=self.org, sentry_app=sentry_app, ).exists() @patch('sentry.tasks.sentry_apps.installation_webhook.delay') def test_does_not_notify_service(self, delay): self.creator.call() assert not len(delay.mock_calls) def test_creates_access_token(self): sentry_app = self.creator.call() install = SentryAppInstallation.objects.get( organization=self.org, sentry_app=sentry_app, ) assert install.api_token @patch('sentry.utils.audit.create_audit_entry') def test_audits(self, create_audit_entry): InternalCreator.run( name='nulldb', user=self.user, author='Sentry', organization=self.org, scopes=('project:read',), webhook_url='http://example.com', schema={'elements': [self.create_issue_link_schema()]}, request=MagicMock(), ) call = faux(create_audit_entry) assert call.kwarg_equals('organization', self.org) assert call.kwarg_equals('target_object', self.org.id) assert call.kwarg_equals('event', AuditLogEntryEvent.INTERNAL_INTEGRATION_ADD) @patch('sentry.analytics.record') @patch('sentry.utils.audit.create_audit_entry') def test_records_analytics(self, create_audit_entry, record): sentry_app = InternalCreator.run( name='nulldb', user=self.user, author='Sentry', organization=self.org, scopes=('project:read',), webhook_url='http://example.com', schema={'elements': [self.create_issue_link_schema()]}, request=MagicMock(), ) assert faux(record).args_equals('internal_integration.created') assert faux(record).kwargs == { 'user_id': self.user.id, 'organization_id': self.org.id, 'sentry_app': sentry_app.slug, }