mirror of
https://github.com/jazzband/django-axes.git
synced 2026-03-16 22:30:23 +00:00
commit
95cd103f37
6 changed files with 126 additions and 49 deletions
10
.travis.yml
Normal file
10
.travis.yml
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
language: python
|
||||
python:
|
||||
- "2.7"
|
||||
env:
|
||||
- PYTHONPATH=$PYTHONPATH:$PWD DJANGO_VERSION=1.4.5
|
||||
- PYTHONPATH=$PYTHONPATH:$PWD DJANGO_VERSION=1.5
|
||||
install:
|
||||
- pip install --use-mirrors Django==$DJANGO_VERSION
|
||||
script:
|
||||
- django-admin.py test axes --settings=axes.test_settings
|
||||
42
README.rst
42
README.rst
|
|
@ -15,33 +15,29 @@ Requirements
|
|||
work around the Django admin and the regular ``django.contrib.auth``
|
||||
login-powered pages.
|
||||
|
||||
|
||||
Installation
|
||||
============
|
||||
|
||||
Download ``django-axes`` using **one** of the following methods:
|
||||
Download and install ``django-axes`` using **one** of the following methods:
|
||||
|
||||
easy_install
|
||||
------------
|
||||
PIP
|
||||
---
|
||||
|
||||
You can download the package from the `CheeseShop <http://pypi.python.org/pypi/django-axes/>`_ or use::
|
||||
You can install the latest stable package running this command::
|
||||
|
||||
easy_install django-axes
|
||||
$ pip install django-axes
|
||||
|
||||
to download and install ``django-axes``.
|
||||
Also you can install the development version running this command::
|
||||
|
||||
Package Download
|
||||
----------------
|
||||
$ pip install -e git+http://github.com/codekoala/django-axes.git#egg=django_axes-dev
|
||||
|
||||
Download the latest ``.tar.gz`` file from the downloads section and extract it
|
||||
somewhere you'll remember. Use ``python setup.py install`` to install it.
|
||||
Setuptools
|
||||
----------
|
||||
|
||||
Checkout from GitHub
|
||||
--------------------
|
||||
You can install the latest stable package running::
|
||||
|
||||
Execute the following command, and make sure you're checking ``django-axes``
|
||||
out somewhere on the ``PYTHONPATH``::
|
||||
|
||||
git clone git://github.com/codekoala/django-axes.git
|
||||
$ easy_install django-axes
|
||||
|
||||
Verifying Installation
|
||||
----------------------
|
||||
|
|
@ -55,6 +51,20 @@ If that command completes with some sort of version number, you're probably
|
|||
good to go. If you see error output, you need to check your installation (I'd
|
||||
start with your ``PYTHONPATH``).
|
||||
|
||||
|
||||
Development
|
||||
===========
|
||||
|
||||
You can contribute to this project forking it from github and sending pull requests.
|
||||
|
||||
Running tests
|
||||
-------------
|
||||
|
||||
Tests can be run, after you clone the repository and having django installed, like::
|
||||
|
||||
$ PYTHONPATH=$PYTHONPATH:$PWD django-admin.py test axes --settings=axes.test_settings
|
||||
|
||||
|
||||
Configuration
|
||||
=============
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import logging
|
||||
import os
|
||||
|
||||
VERSION = (1, 2, 9)
|
||||
VERSION = (1, 3, 0)
|
||||
|
||||
|
||||
def get_version():
|
||||
|
|
|
|||
52
axes/test_settings.py
Normal file
52
axes/test_settings.py
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
import os
|
||||
import django
|
||||
|
||||
if django.VERSION[:2] >= (1, 3):
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
'NAME': ':memory:',
|
||||
}
|
||||
}
|
||||
else:
|
||||
DATABASE_ENGINE = 'sqlite3'
|
||||
|
||||
SITE_ID = 1
|
||||
|
||||
MIDDLEWARE_CLASSES = (
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'axes.middleware.FailedLoginMiddleware'
|
||||
)
|
||||
|
||||
ROOT_URLCONF = 'axes.test_urls'
|
||||
|
||||
INSTALLED_APPS = [
|
||||
'django.contrib.auth',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.sites',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.admin',
|
||||
|
||||
'axes',
|
||||
]
|
||||
|
||||
SECRET_KEY = 'too-secret-for-test'
|
||||
|
||||
LOGGING = {
|
||||
'version': 1,
|
||||
'root': {
|
||||
'level': 'DEBUG',
|
||||
'handlers': ['console'],
|
||||
},
|
||||
'handlers': {
|
||||
'console': {
|
||||
'level': 'DEBUG',
|
||||
'class': 'logging.StreamHandler',
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
# AXES_LOGIN_FAILURE_LIMIT = 1
|
||||
6
axes/test_urls.py
Normal file
6
axes/test_urls.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
from django.conf.urls import patterns, include
|
||||
from django.contrib import admin
|
||||
|
||||
urlpatterns = patterns('',
|
||||
(r'^admin/', include(admin.site.urls)),
|
||||
)
|
||||
|
|
@ -10,32 +10,19 @@ from django.contrib.auth.models import User
|
|||
from django.core.urlresolvers import reverse
|
||||
from django.test.utils import override_settings
|
||||
|
||||
|
||||
FAILURE_LIMIT = 3
|
||||
from axes.decorators import FAILURE_LIMIT
|
||||
from axes.decorators import LOGIN_FORM_KEY
|
||||
|
||||
|
||||
@override_settings(
|
||||
AXES_LOGIN_FAILURE_LIMIT=FAILURE_LIMIT,
|
||||
AXES_LOCKOUT_URL=None,
|
||||
AXES_USE_USER_AGENT=False,
|
||||
AXES_COOLOFF_TIME=None,
|
||||
AXES_LOCKOUT_TEMPLATE=None,
|
||||
)
|
||||
class AccessAttemptTest(TestCase):
|
||||
"""Test case using custom settings for testing"""
|
||||
|
||||
def setUp(self):
|
||||
for i in range(0, random.randrange(10, 50)):
|
||||
username = "person%s" % i
|
||||
email = "%s@example.org" % username
|
||||
u = User.objects.create_user(email=email, username=username, password=username)
|
||||
u.is_staff = True
|
||||
u.save()
|
||||
"""Test case using custom settings for testing
|
||||
"""
|
||||
LOCKED_MESSAGE = 'Account locked: too many login attempts.'
|
||||
|
||||
def _generate_random_string(self):
|
||||
"""Generates a random string"""
|
||||
return ''.join(random.choice(string.ascii_uppercase + string.digits)
|
||||
for x in range(20))
|
||||
chars = string.ascii_uppercase + string.digits
|
||||
return ''.join(random.choice(chars) for x in range(20))
|
||||
|
||||
def _random_username(self, existing_username=False):
|
||||
"""Returns a username, existing or not depending on params"""
|
||||
|
|
@ -44,40 +31,52 @@ class AccessAttemptTest(TestCase):
|
|||
|
||||
return self._generate_random_string()
|
||||
|
||||
def _attempt_login(self, existing_username=False):
|
||||
def _login(self, existing_username=False):
|
||||
response = self.client.post(reverse('admin:index'), {
|
||||
'username': self._random_username(existing_username),
|
||||
'password': self._generate_random_string()
|
||||
'password': self._generate_random_string(),
|
||||
})
|
||||
|
||||
return response
|
||||
|
||||
def setUp(self):
|
||||
for i in range(0, random.randrange(10, 50)):
|
||||
username = "person%s" % i
|
||||
email = "%s@example.org" % username
|
||||
u = User.objects.create_user(
|
||||
username=username,
|
||||
password=username,
|
||||
email=email,
|
||||
)
|
||||
u.is_staff = True
|
||||
u.save()
|
||||
|
||||
def test_login_max(self, existing_username=False):
|
||||
for i in range(0, FAILURE_LIMIT - 1):
|
||||
response = self._attempt_login(existing_username=existing_username)
|
||||
response = self._login(existing_username=existing_username)
|
||||
# Check if we are in the same login page
|
||||
self.assertIn('this_is_the_login_form', response.content)
|
||||
self.assertContains(response, LOGIN_FORM_KEY)
|
||||
|
||||
# So, we shouldn't have gotten a lock-out yet.
|
||||
# But we should get one now
|
||||
response = self._attempt_login()
|
||||
self.assertContains(response, 'Account locked')
|
||||
response = self._login()
|
||||
self.assertContains(response, self.LOCKED_MESSAGE)
|
||||
|
||||
def test_with_real_username_max(self):
|
||||
self.test_login_max(existing_username=True)
|
||||
|
||||
def test_login_max_with_more_attempts(self, existing_username=False):
|
||||
for i in range(0, FAILURE_LIMIT - 1):
|
||||
response = self._attempt_login(existing_username=existing_username)
|
||||
for i in range(0, FAILURE_LIMIT - 2):
|
||||
response = self._login(existing_username=existing_username)
|
||||
# Check if we are in the same login page
|
||||
self.assertIn('this_is_the_login_form', response.content)
|
||||
self.assertContains(response, LOGIN_FORM_KEY)
|
||||
|
||||
# So, we shouldn't have gotten a lock-out yet.
|
||||
# But we should get one now
|
||||
for i in range(0, random.randrange(1, 100)):
|
||||
# try to log in a bunch of times
|
||||
response = self._attempt_login()
|
||||
self.assertContains(response, 'Account locked')
|
||||
response = self._login()
|
||||
self.assertContains(response, self.LOCKED_MESSAGE)
|
||||
|
||||
def test_with_real_username_max_with_more(self):
|
||||
self.test_login_max_with_more_attempts(existing_username=True)
|
||||
|
|
@ -88,4 +87,4 @@ class AccessAttemptTest(TestCase):
|
|||
'username': valid_username,
|
||||
'password': valid_username
|
||||
})
|
||||
self.assertNotIn('authentication_form', response.context)
|
||||
self.assertNotIn(LOGIN_FORM_KEY, response.context)
|
||||
|
|
|
|||
Loading…
Reference in a new issue