mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #30226 -- Added BaseBackend for authentication.
This commit is contained in:
committed by
Mariusz Felisiak
parent
4b6dfe1622
commit
75337a6050
@@ -4,7 +4,7 @@ from unittest import mock
|
||||
from django.contrib.auth import (
|
||||
BACKEND_SESSION_KEY, SESSION_KEY, authenticate, get_user, signals,
|
||||
)
|
||||
from django.contrib.auth.backends import ModelBackend
|
||||
from django.contrib.auth.backends import BaseBackend, ModelBackend
|
||||
from django.contrib.auth.hashers import MD5PasswordHasher
|
||||
from django.contrib.auth.models import AnonymousUser, Group, Permission, User
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
@@ -20,6 +20,28 @@ from .models import (
|
||||
)
|
||||
|
||||
|
||||
class SimpleBackend(BaseBackend):
|
||||
def get_group_permissions(self, user_obj, obj=None):
|
||||
return ['group_perm']
|
||||
|
||||
|
||||
@override_settings(AUTHENTICATION_BACKENDS=['auth_tests.test_auth_backends.SimpleBackend'])
|
||||
class BaseBackendTest(TestCase):
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
cls.user = User.objects.create_user('test', 'test@example.com', 'test')
|
||||
|
||||
def test_get_group_permissions(self):
|
||||
self.assertEqual(self.user.get_group_permissions(), {'group_perm'})
|
||||
|
||||
def test_get_all_permissions(self):
|
||||
self.assertEqual(self.user.get_all_permissions(), {'group_perm'})
|
||||
|
||||
def test_has_perm(self):
|
||||
self.assertIs(self.user.has_perm('group_perm'), True)
|
||||
self.assertIs(self.user.has_perm('other_perm', TestObj()), False)
|
||||
|
||||
|
||||
class CountingMD5PasswordHasher(MD5PasswordHasher):
|
||||
"""Hasher that counts how many times it computes a hash."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user