Skip to content

Commit

Permalink
Fix #25 - User Manager doesn't set last_login (#27)
Browse files Browse the repository at this point in the history
Ensure that last_login and date_joined defaults are properly set in User
Model, and ensure that User Manager does not override the defaults.

Thanks to @jschneier for reporting the issue.
  • Loading branch information
jambonrose authored Aug 18, 2017
1 parent ec3f6fd commit db3e06a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ AUTHORS

- Andrew Pinkham http://andrewsforge.com
- Jim Illback [email protected]
- Josh Schneier https://github.com/jschneier
- Russell Keith-Magee http://cecinestpasun.com
3 changes: 1 addition & 2 deletions src/improved_user/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ def _create_user(
raise ValueError(
'The Improved User model does not have a username; '
'it uses only email')
now = timezone.now()
user = self.model(
email=self.normalize_email(email),
is_staff=is_staff, is_superuser=is_superuser,
last_login=now, date_joined=now, **extra_fields)
**extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
Expand Down
23 changes: 23 additions & 0 deletions tests/test_managers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Test User model manager"""
from datetime import datetime

from django.test import TestCase

from improved_user.models import User, UserManager
Expand Down Expand Up @@ -93,3 +95,24 @@ def test_make_random_password(self):
self.assertEqual(len(password), 5)
for char in password:
self.assertIn(char, allowed_chars)

def test_last_login_is_none(self):
"""Check that last login is unset when created
https://github.com/jambonsw/django-improved-user/issues/25
"""
user1 = User.objects.create_user('[email protected]', 'password1')
self.assertIsNone(user1.last_login)

user2 = User.objects.create_superuser('[email protected]', 'password1')
self.assertIsNone(user2.last_login)

def test_date_joined_default(self):
"""Check date joined set upon creation"""
user1 = User.objects.create_user('[email protected]', 'password1')
self.assertIsNotNone(user1.date_joined)
self.assertIsInstance(user1.date_joined, datetime)

user2 = User.objects.create_superuser('[email protected]', 'password1')
self.assertIsNotNone(user2.date_joined)
self.assertIsInstance(user2.date_joined, datetime)
11 changes: 11 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test Improved User Model"""
from datetime import datetime
from types import MethodType
from unittest import skipUnless
from unittest.mock import patch
Expand Down Expand Up @@ -78,6 +79,16 @@ def test_last_login_default(self):
user2 = User.objects.create(email='[email protected]')
self.assertIsNone(user2.last_login)

def test_date_joined_default(self):
"""Check date joined set upon creation"""
user1 = User.objects.create(email='[email protected]')
self.assertIsNotNone(user1.date_joined)
self.assertIsInstance(user1.date_joined, datetime)

user2 = User.objects.create(email='[email protected]')
self.assertIsNotNone(user2.date_joined)
self.assertIsInstance(user2.date_joined, datetime)

def test_user_clean_normalize_email(self):
"""User email/username is normalized upon creation"""
user = User(email='[email protected]', password='foo')
Expand Down

0 comments on commit db3e06a

Please sign in to comment.