Skip to content

ensure that username captured from Django is a string #398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions elasticapm/contrib/django/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ def get_user_info(self, request):
if hasattr(user, "id"):
user_info["id"] = encoding.keyword_field(user.id)
if hasattr(user, "get_username"):
user_info["username"] = encoding.keyword_field(user.get_username())
user_info["username"] = encoding.keyword_field(encoding.force_text(user.get_username()))
elif hasattr(user, "username"):
user_info["username"] = encoding.keyword_field(user.username)
user_info["username"] = encoding.keyword_field(encoding.force_text(user.username))

if hasattr(user, "email"):
user_info["email"] = user.email
user_info["email"] = encoding.force_text(user.email)
except DatabaseError:
# If the connection is closed or similar, we'll just skip this
return {}
Expand Down
22 changes: 22 additions & 0 deletions tests/contrib/django/django_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,28 @@ def test_user_info_with_custom_user(django_elasticapm_client, client):
assert "email" not in user_info


@pytest.mark.django_db
def test_user_info_with_custom_user_non_string_username(django_elasticapm_client, client):
with override_settings(AUTH_USER_MODEL="testapp.MyIntUser"):
from django.contrib.auth import get_user_model

MyIntUser = get_user_model()
user = MyIntUser(my_username=1)
user.set_password("admin")
user.save()
assert client.login(username=1, password="admin")
with pytest.raises(Exception):
client.get(reverse("elasticapm-raise-exc"))

assert len(django_elasticapm_client.events) == 1
event = django_elasticapm_client.events[ERROR][0]
assert "user" in event["context"]
user_info = event["context"]["user"]
assert "username" in user_info
assert isinstance(user_info["username"], compat.text_type)
assert user_info["username"] == "1"


@pytest.mark.skipif(django.VERSION > (1, 9), reason="MIDDLEWARE_CLASSES removed in Django 2.0")
def test_user_info_with_non_django_auth(django_elasticapm_client, client):
with override_settings(
Expand Down
27 changes: 18 additions & 9 deletions tests/contrib/django/testapp/models.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import

from django import VERSION as DJANGO_VERSION
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.db import models

if DJANGO_VERSION >= (1, 5):
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager

class MyUser(AbstractBaseUser):
USERNAME_FIELD = "my_username"
my_username = models.CharField(max_length=30)
class MyUser(AbstractBaseUser):
USERNAME_FIELD = "my_username"
my_username = models.CharField(max_length=30)

objects = BaseUserManager()
objects = BaseUserManager()

class Meta:
abstract = False
class Meta:
abstract = False


class MyIntUser(AbstractBaseUser):
USERNAME_FIELD = "my_username"

my_username = models.IntegerField()

objects = BaseUserManager()

class Meta:
abstract = False