From cdb1866cbb530d6aa215a7ffade9a2713fa552fe Mon Sep 17 00:00:00 2001 From: Andrew Crosio Date: Fri, 25 Apr 2014 14:55:46 +0400 Subject: [PATCH 1/2] fix for django 1.7 exception not existing --- axes/decorators.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/axes/decorators.py b/axes/decorators.py index ad1353c..343918e 100644 --- a/axes/decorators.py +++ b/axes/decorators.py @@ -5,7 +5,6 @@ from datetime import timedelta from django.conf import settings from django.contrib.auth import logout from django.core.exceptions import ObjectDoesNotExist -from django.contrib.auth.models import SiteProfileNotAvailable from django.http import HttpResponse from django.http import HttpResponseRedirect from django.shortcuts import render_to_response @@ -20,6 +19,11 @@ except ImportError: # django < 1.5 else: User = get_user_model() +try: + from django.contrib.auth.models import SiteProfileNotAvailable +except ImportError: # django >= 1.7 + SiteProfileNotAvailable = type('SiteProfileNotAvailable', (Exception,), {}) + from axes.models import AccessLog from axes.models import AccessAttempt from axes.signals import user_locked_out From 298ba366b8263ff18e5e92a08563dca3d8f15477 Mon Sep 17 00:00:00 2001 From: Andrew Crosio Date: Fri, 25 Apr 2014 14:55:58 +0400 Subject: [PATCH 2/2] fixing tests for django 1.7 --- axes/decorators.py | 1 - axes/tests.py | 19 ++++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/axes/decorators.py b/axes/decorators.py index 343918e..110e579 100644 --- a/axes/decorators.py +++ b/axes/decorators.py @@ -62,7 +62,6 @@ IP_BLACKLIST = getattr(settings, 'AXES_IP_BLACKLIST', None) ERROR_MESSAGE = ugettext_lazy("Please enter a correct username and password. " "Note that both fields are case-sensitive.") -LOGIN_FORM_KEY = 'this_is_the_login_form' def get_ip(request): diff --git a/axes/tests.py b/axes/tests.py index 11706e6..0727e2e 100644 --- a/axes/tests.py +++ b/axes/tests.py @@ -5,15 +5,24 @@ import time from django.test import TestCase from django.test.client import Client from django.contrib.auth.models import User +from django.core.urlresolvers import NoReverseMatch from django.core.urlresolvers import reverse from axes.decorators import COOLOFF_TIME from axes.decorators import FAILURE_LIMIT -from axes.decorators import LOGIN_FORM_KEY from axes.models import AccessLog from axes.utils import reset +# Django >= 1.7 compatibility +try: + ADMIN_LOGIN_URL = reverse('admin:login') + LOGIN_FORM_KEY = '
' +except NoReverseMatch: + ADMIN_LOGIN_URL = reverse('admin:index') + LOGIN_FORM_KEY = 'this_is_the_login_form' + + class AccessAttemptTest(TestCase): """Test case using custom settings for testing """ @@ -35,7 +44,7 @@ class AccessAttemptTest(TestCase): return self._generate_random_string() def _login(self, existing_username=False, user_agent='test-browser'): - response = self.client.post(reverse('admin:index'), { + response = self.client.post(ADMIN_LOGIN_URL, { 'username': self._random_username(existing_username), 'password': self._generate_random_string(), 'this_is_the_login_form': 1, @@ -102,7 +111,7 @@ class AccessAttemptTest(TestCase): """Tests a valid login for a real username """ valid_username = self._random_username(existing_username=True) - response = self.client.post(reverse('admin:index'), { + response = self.client.post(ADMIN_LOGIN_URL, { 'username': valid_username, 'password': valid_username, 'this_is_the_login_form': 1, @@ -122,7 +131,7 @@ class AccessAttemptTest(TestCase): def _unsuccessful_login(self, username): c = Client() - response = c.post('/admin/', { + response = c.post(ADMIN_LOGIN_URL, { 'username': username, 'password': 'wrong', 'this_is_the_login_form': 1, @@ -167,7 +176,7 @@ class AccessAttemptTest(TestCase): """Tests a valid logout and make sure the logout_time is updated """ valid_username = self._random_username(existing_username=True) - response = self.client.post(reverse('admin:index'), { + self.client.post(ADMIN_LOGIN_URL, { 'username': valid_username, 'password': valid_username, 'this_is_the_login_form': 1,