Skip to content

Commit

Permalink
Merge pull request #4 from joshkel/master
Browse files Browse the repository at this point in the history
Better support for custom user models
  • Loading branch information
mattrobenolt committed Oct 5, 2015
2 parents 48ddaf7 + 0eff41a commit 68741a5
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 8 deletions.
7 changes: 5 additions & 2 deletions sudo/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ def __init__(self, user, *args, **kwargs):
super(SudoForm, self).__init__(*args, **kwargs)

def clean_password(self):
if auth.authenticate(username=self.user.username,
password=self.data['password']):
try:
username = self.user.get_username() # Django 1.5 and above
except AttributeError: # pragma: nocover
username = self.user.username # Django 1.4
if auth.authenticate(username=username, password=self.data['password']):
return self.data['password']
raise forms.ValidationError(_('Incorrect password'))
5 changes: 4 additions & 1 deletion sudo/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, QueryDict
from django.template.response import TemplateResponse
from django.utils import importlib
try:
import importlib
except ImportError: # pragma: nocover
from django.utils import importlib
from django.utils.http import is_safe_url
from django.views.decorators.debug import sensitive_post_parameters
from django.views.decorators.cache import never_cache
Expand Down
4 changes: 2 additions & 2 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ def post(self, *args, **kwargs):
def setUser(self, user):
self.user = self.request.user = user

def login(self):
user = User()
def login(self, user_class=User):
user = user_class()
self.setUser(user)
7 changes: 7 additions & 0 deletions tests/forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .base import BaseTestCase
from .models import EmailUser

from django.forms import ValidationError
from sudo.forms import SudoForm
Expand Down Expand Up @@ -44,3 +45,9 @@ def test_clean_password_secondary_auth_valid_password(self):
SudoForm(self.user, {'password': password}).clean_password(),
password
)

def test_integration_custom_user(self):
self.login(EmailUser)
self.assertTrue(
SudoForm(self.user, {'password': 'foo'}).is_valid()
)
20 changes: 20 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import django
from django.db import models
try:
from django.contrib.auth.models import AbstractBaseUser
except ImportError:
# Django 1.4 doesn't properly support custom user models, but a User is
# close enough to AbstractBaseUser for our tests to work in 1.4.
from django.contrib.auth.models import User as AbstractBaseUser


class EmailUser(AbstractBaseUser):
if django.VERSION >= (1, 5):
# Skip on Django 1.4, since we're inheriting from User, which already
# has an email address.
email = models.CharField(max_length=254, unique=True)

USERNAME_FIELD = 'email'

def get_username(self):
return self.email
8 changes: 5 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
[tox]
envlist = py26, py27, py32, py33, py34, pypy
envlist = py26-django14, py27, py32, py33, py34, pypy

[testenv]
deps = pytest
deps =
django14: django >=1.4.2,<1.5
pytest
pytest-cov
pytest-django-lite
pytest-django
commands = py.test --cov sudo --cov-report term-missing

0 comments on commit 68741a5

Please sign in to comment.