Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_automatic_transaction(self):
class TransactionMediator(Mediator):
def call(self):
User.objects.create(username="beep")
raise Exception
with self.assertRaises(Exception):
TransactionMediator.run()
assert not User.objects.filter(username="beep").exists()
def test_removes_username_if_matches_email(self):
user = User(username='foo@example.com', email='foo@example.com')
form = AccountSettingsForm(user=user)
assert 'username' not in form.fields
def test_build_events_by_actor(self):
events = self.team1_events + self.team2_events + self.user4_events
events_by_actor = {
Actor(self.team1.id, Team): set(self.team1_events),
Actor(self.team2.id, Team): set(self.team2_events),
Actor(self.user3.id, User): set(self.team1_events),
Actor(self.user4.id, User): set(self.user4_events),
}
assert build_events_by_actor(self.project.id, events, self.user_ids) == events_by_actor
if not sig or sig.count(":") < 2:
return None
signed_data = "%s|%s|%s" % (request.build_absolute_uri("/").rstrip("/"), request.path, sig)
try:
data = get_signer().unsign(signed_data, max_age=max_age)
except signing.BadSignature:
return None
_, signed_path, user_id = data.rsplit("|", 2)
if signed_path != request.path:
return None
try:
return User.objects.get(pk=base36_decode(user_id))
except (ValueError, User.DoesNotExist):
return None
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,
)
from __future__ import absolute_import
from django.conf import settings
from sentry.api.serializers import Serializer, register
from sentry.models import User, UserOption
from sentry.utils.avatar import get_gravatar_url
@register(User)
class UserSerializer(Serializer):
def serialize(self, obj, attrs, user):
d = {
'id': str(obj.id),
'name': obj.get_display_name(),
'username': obj.username,
'email': obj.email,
'avatarUrl': get_gravatar_url(obj.email, size=32),
}
if obj == user:
options = {
o.key: o.value
for o in UserOption.objects.filter(
user=user,
project__isnull=True,
)
def _find_existing_user(self, email):
return User.objects.filter(
id__in=UserEmail.objects.filter(
email__iexact=email,
is_verified=True,
).values('user'),
is_active=True,
).first()
if superuser is None:
superuser = False
if not email:
raise click.ClickException("Invalid or missing email address.")
# TODO(mattrobenolt): Accept password over stdin?
if not no_password and not password:
raise click.ClickException("No password set and --no-password not passed.")
from sentry import roles
from sentry.models import User
from django.conf import settings
user = User(
email=email, username=email, is_superuser=superuser, is_staff=superuser, is_active=True
)
if password:
user.set_password(password)
user.save()
click.echo("User created: %s" % (email,))
# TODO(dcramer): kill this when we improve flows
if settings.SENTRY_SINGLE_ORGANIZATION:
from sentry.models import Organization, OrganizationMember, OrganizationMemberTeam, Team
org = Organization.get_default()
if superuser:
if commit_author is not None and not is_anonymous_email(
commit_author.email
):
author_email = commit_author.email
gh_username_cache[gh_username] = author_email
else:
try:
gh_user = client.request_no_auth("GET", "/users/%s" % gh_username)
except ApiError as exc:
logger.exception(six.text_type(exc))
else:
# even if we can't find a user, set to none so we
# don't re-query
gh_username_cache[gh_username] = None
try:
user = User.objects.filter(
social_auth__provider="github",
social_auth__uid=gh_user["id"],
org_memberships=organization,
)[0]
except IndexError:
pass
else:
author_email = user.email
gh_username_cache[gh_username] = author_email
if commit_author is not None:
try:
with transaction.atomic():
commit_author.update(
email=author_email, external_id=external_id
)
except IntegrityError:
def create_comment_attribution(self, user_id, comment_text):
# VSTS uses markdown or xml
# https://docs.microsoft.com/en-us/microsoftteams/platform/concepts/bots/bots-text-formats
user = User.objects.get(id=user_id)
attribution = "%s wrote:\n\n" % user.name
quoted_comment = "%s<blockquote>%s</blockquote>" % (attribution, comment_text)
return quoted_comment