Skip to content
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

Tz aware timestamp #32

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions useraudit/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import datetime
import logging
from django.db import models
from django.utils import timezone
from django.contrib.auth.signals import user_logged_in
from .signals import password_has_expired, account_has_expired, login_failure_limit_reached

Expand All @@ -20,14 +21,14 @@ class LoginAttemptLogger(object):
def reset(self, username):
defaults = {
'count': 0,
'timestamp': datetime.datetime.now()
'timestamp': timezone.now()
}
LoginAttempt.objects.update_or_create(username=username, defaults=defaults)

def increment(self, username):
obj, created = LoginAttempt.objects.get_or_create(username=username)
obj.count += 1
obj.timestamp = datetime.datetime.now()
obj.timestamp = timezone.now()
obj.save()


Expand Down
37 changes: 36 additions & 1 deletion useraudit_testapp/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import useraudit_testapp.urls
import useraudit.password_expiry
from useraudit.signals import login_failure_limit_reached, password_has_expired, account_has_expired, password_will_expire_warning
from useraudit.models import UserDeactivation
from useraudit.models import UserDeactivation, LoginAttempt

# Saving a reference to the USER_MODEL set in the settings.py file
# Our pre_save handler in password_expiry.py gets registered just for this sender
Expand Down Expand Up @@ -451,6 +451,41 @@ def handler(sender, user=None, **kwargs):

self.assertTrue(self.handler_called)

class LoginAttemtpsTimestampTestCase(TestCase):
username = "testuser"
password = "testuser"

def setUp(self):
self.user = User.objects.create(
username=self.username,
email="testuser@localhost",
)
self.user.set_password(self.password)
self.user.save()

def tearDown(self):
self.user.delete()

@override_settings(USE_TZ=False)
def test_timestamp_naive(self):
_ = authenticate(username=self.username, password="INCORRECT")
_ = authenticate(username=self.username, password="INCORRECT")
login_attempt = LoginAttempt.objects.get(username=self.username)

timestamp = login_attempt.timestamp
is_naive = timestamp.tzinfo is None or timestamp.tzinfo.utcoffset(timestamp) is None
self.assertTrue(is_naive)

@override_settings(USE_TZ=True)
def test_timestamp_aware(self):
_ = authenticate(username=self.username, password="INCORRECT")
_ = authenticate(username=self.username, password="INCORRECT")
login_attempt = LoginAttempt.objects.get(username=self.username)

timestamp = login_attempt.timestamp
is_aware = timestamp.tzinfo is not None and timestamp.tzinfo.utcoffset(timestamp) is not None
self.assertTrue(is_aware)


class MiddlewareTestCase(TestCase):

Expand Down