support custom username fields

This commit is contained in:
Brad Pitcher 2015-12-20 09:40:08 -08:00
parent 25236d524a
commit 5af731ab9d
3 changed files with 31 additions and 2 deletions

View file

@ -1,16 +1,29 @@
"""
Tests for the analytical.utils module.
"""
import django
from django.conf import settings
from django.db import models
from django.http import HttpRequest
from django.template import Context
from django.test.utils import override_settings
from analytical.utils import (
get_domain, is_internal_ip, get_required_setting, AnalyticalException)
get_domain, get_identity, is_internal_ip, get_required_setting,
AnalyticalException)
from analytical.tests.utils import TestCase
try:
from unittest import skipIf
except ImportError: # Python 2.6 fallback
from unittest2 import skipIf
try:
from django.contrib.auth.models import AbstractBaseUser
except ImportError: # Django < 1.5 fallback
AbstractBaseUser = models.Model
class SettingDeletedTestCase(TestCase):
@ -33,6 +46,18 @@ class SettingDeletedTestCase(TestCase):
get_required_setting, "USER_ID", "\d+", "invalid USER_ID")
class MyUser(AbstractBaseUser):
identity = models.CharField()
USERNAME_FIELD = 'identity'
class GetIdentityTestCase(TestCase):
@skipIf(django.VERSION < (1, 5,), 'Custom usernames not supported in Django < 1.5')
def test_custom_username_field(self):
get_id = get_identity(Context({}), user=MyUser(identity='fake_id'))
self.assertEqual(get_id, 'fake_id')
@override_settings(ANALYTICAL_DOMAIN="example.org")
class GetDomainTestCase(TestCase):
def test_get_service_domain_from_context(self):

View file

@ -76,7 +76,10 @@ def get_identity(context, prefix=None, identity_func=None, user=None):
if identity_func is not None:
return identity_func(user)
else:
return user.username
try:
return user.get_username()
except AttributeError: # Django < 1.5 fallback
return user.username
except (KeyError, AttributeError):
pass
return None

View file

@ -18,6 +18,7 @@ basepython =
deps =
coverage==3.7.1
coveralls
py26: unittest2
django14: Django>=1.4,<1.5
django15: Django>=1.5,<1.6
django16: Django>=1.6,<1.7