Skip to content

Commit

Permalink
Merge pull request #59 from incuna/checks
Browse files Browse the repository at this point in the history
Add check for misconfigured User model
  • Loading branch information
meshy committed Jun 30, 2014
2 parents 840b8ad + d9059d4 commit 6dd99a3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ env:
- DATABASE_URL='postgres://postgres@localhost/user_management'
matrix:
- DJANGO='django>=1.6,<1.7'
- DJANGO='https://github.com/django/django/archive/1.7b3.zip'
- DJANGO='https://github.com/django/django/archive/1.7c1.zip'
install:
- psql -c 'CREATE DATABASE user_management' -U postgres;
- pip install -r test_requirements.txt
Expand Down
20 changes: 20 additions & 0 deletions user_management/models/mixins.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib.auth.models import BaseUserManager
from django.contrib.auth.tokens import default_token_generator
from django.contrib.sites.models import Site
from django.core import checks
from django.db import models
from django.utils import timezone
from django.utils.encoding import force_bytes, python_2_unicode_compatible
Expand Down Expand Up @@ -121,6 +122,25 @@ def send_validation_email(self):
context=context,
)

@classmethod
def check(cls, **kwargs):
errors = super(VerifyEmailMixin, cls).check(**kwargs)
errors.extend(cls._check_manager(**kwargs))
return errors

@classmethod
def _check_manager(cls, **kwargs):
if isinstance(cls.objects, VerifyEmailManager):
return []

return [
checks.Warning(
"Manager should be an instance of 'VerifyEmailManager'",
hint="Subclass a custom manager from 'VerifyEmailManager'",
obj=cls,
id='user_management.W001',
),
]

class AvatarMixin(models.Model):
avatar = models.ImageField(upload_to='user_avatar', null=True, blank=True)
Expand Down
32 changes: 32 additions & 0 deletions user_management/models/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import unittest

import django
from django.contrib.auth.tokens import default_token_generator
from django.contrib.sites.models import Site
from django.core import checks
from django.db.utils import IntegrityError
from django.test import TestCase
from django.utils import timezone
Expand All @@ -8,10 +12,17 @@
from django.utils.http import urlsafe_base64_encode
from mock import patch

from .. import mixins
from . import models
from .factories import UserFactory


skip_if_checks_unavailable = unittest.skipIf(
django.VERSION < (1, 7),
'Checks only available in django>=1.7',
)


class TestUser(TestCase):
"""Test the "User" model"""
model = models.User
Expand Down Expand Up @@ -179,3 +190,24 @@ def test_verified_email(self):
user.send_validation_email()

self.assertFalse(send.called)

@skip_if_checks_unavailable
def test_manager_check_valid(self):
errors = self.model.check()
self.assertEqual(errors, [])

@skip_if_checks_unavailable
def test_manager_check_invalid(self):
class InvalidUser(self.model):
objects = mixins.UserManager()

expected = [
checks.Warning(
"Manager should be an instance of 'VerifyEmailManager'",
hint="Subclass a custom manager from 'VerifyEmailManager'",
obj=InvalidUser,
id='user_management.W001',
),
]
errors = InvalidUser.check()
self.assertEqual(errors, expected)

0 comments on commit 6dd99a3

Please sign in to comment.