From 4231279d3b724d00cc9a8ac26d6924fd1f7468ba Mon Sep 17 00:00:00 2001 From: Rolf Erik Lekang Date: Wed, 1 Oct 2014 14:47:33 +0200 Subject: [PATCH] fix #28, add verification of users --- docs/usage.rst | 6 ++++++ nopassword/backends.py | 7 ++++--- tests/tests.py | 35 +++++++++++++++++++++++++++-------- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/docs/usage.rst b/docs/usage.rst index 7992437..90bb963 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -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 diff --git a/nopassword/backends.py b/nopassword/backends.py index 5aea511..3d2f399 100644 --- a/nopassword/backends.py +++ b/nopassword/backends.py @@ -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: @@ -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): diff --git a/tests/tests.py b/tests/tests.py index 4359d63..58dc944 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -2,8 +2,8 @@ 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 @@ -11,6 +11,7 @@ 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 @@ -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) @@ -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): @@ -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) @@ -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): @@ -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)