Skip to content
This repository has been archived by the owner on May 5, 2020. It is now read-only.

Commit

Permalink
fix #28, add verification of users
Browse files Browse the repository at this point in the history
  • Loading branch information
relekang committed Oct 1, 2014
1 parent d7e7cb0 commit 4231279
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
6 changes: 6 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ Add urls to your *urls.py*::
url(r'^accounts/', include('nopassword.urls')),
)

Verify users
~~~~~~~~~~~~
If it is necessary to verify that users still are active in another system. Override
*verify_user(user)* to implement your check. In *NoPasswordBackend* that method checks
whether the user is active in the django app.

Custom backends
~~~~~~~~~~~~~~~
In backends.py there is a *NoPasswordBackend*, from which it is possible
Expand Down
7 changes: 4 additions & 3 deletions nopassword/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@

class NoPasswordBackend:

supports_inactive_user = True

def authenticate(self, code=None, **credentials):
try:
user = get_user_model().objects.get(**credentials)
if not user.is_active:
if not self.verify_user(user):
return None

if code is None:
Expand All @@ -42,6 +40,9 @@ def get_user(self, user_id):
def send_login_code(self):
raise NotImplementedError

def verify_user(self, user):
return user.is_active


class EmailBackend(NoPasswordBackend):

Expand Down
35 changes: 27 additions & 8 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
import time

from django.contrib.auth import authenticate
from django.test import Client
from django.http import Http404
from django.test import Client
from django.test import RequestFactory
from django.test.utils import override_settings
from django.utils import unittest

from nopassword import views
from nopassword.models import LoginCode
from nopassword.utils import get_user_model
from nopassword.backends import NoPasswordBackend

from .models import NoUsernameUser

Expand All @@ -20,6 +21,10 @@ def setUp(self):
self.user = get_user_model().objects.create(username='test_user')
self.inactive_user = get_user_model().objects.create(username='inactive', is_active=False)

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

def test_login_backend(self):
self.code = LoginCode.create_code_for_user(self.user)
self.assertEqual(len(self.code.code), 20)
Expand All @@ -42,10 +47,6 @@ def test_code_timeout(self):
time.sleep(3)
self.assertIsNone(authenticate(username=self.user.username, code=self.timeout_code.code))

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


class AuthenticationBackendTests(unittest.TestCase):

Expand All @@ -65,6 +66,9 @@ def setUp(self):
self.c = Client()
self.user = get_user_model().objects.create(username='user')

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

def test_login(self):
response = self.c.get('/accounts/login/')
self.assertEqual(response.status_code, 200)
Expand Down Expand Up @@ -102,9 +106,6 @@ def test_hide_username(self):
logout = self.c.get('/accounts/logout/')
self.assertEqual(logout.status_code, 302)

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


class TestUsersJsonView(unittest.TestCase):

Expand All @@ -124,3 +125,21 @@ def test_200(self):
request = self.factory.get('/accounts/users.json')
response = views.users_json(request)
self.assertEqual(response.status_code, 200)


class TestBackendUtils(unittest.TestCase):
def setUp(self):
self.user = get_user_model().objects.create(username='test_user')
self.inactive_user = get_user_model().objects.create(username='inactive', is_active=False)
self.backend = NoPasswordBackend()

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

def test_verify_user(self):
self.assertTrue(self.backend.verify_user(self.user))
self.assertFalse(self.backend.verify_user(self.inactive_user))

def test_send_login_code(self):
self.assertRaises(NotImplementedError, self.backend.send_login_code)

0 comments on commit 4231279

Please sign in to comment.