-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
Fixed #18763 -- Added with_perm() to User manager. #7153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a5e5efa
Fixed #18763 -- Added with_perm() to User manager.
berkerpeksag 5d72120
Address most of the review comments
berkerpeksag 4c49c1e
Fix isort
berkerpeksag 132689e
Add support for Permission instances
berkerpeksag 108b447
Drop with_perm from test names
berkerpeksag File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
from django.contrib.auth.models import User | ||
from django.db import models | ||
|
||
|
||
class MinimalUser(models.Model): | ||
REQUIRED_FIELDS = () | ||
USERNAME_FIELD = 'id' | ||
|
||
|
||
class CustomModel(models.Model): | ||
# Used by with_perm() tests. | ||
user = models.ForeignKey(User, on_delete=models.CASCADE) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
from django.db.models.signals import post_save | ||
from django.test import SimpleTestCase, TestCase, override_settings | ||
|
||
from .models import IntegerUsernameUser | ||
from .models import CustomModel, IntegerUsernameUser | ||
from .models.with_custom_email_field import CustomEmailField | ||
|
||
|
||
|
@@ -261,6 +261,172 @@ def test_check_password_upgrade(self): | |
hasher.iterations = old_iterations | ||
|
||
|
||
class UserWithPermTestCase(TestCase): | ||
|
||
@classmethod | ||
def setUpTestData(cls): | ||
cls.content_type = ContentType.objects.get_for_model(Group) | ||
cls.permission = Permission.objects.create( | ||
name='test', content_type=cls.content_type, codename='test' | ||
) | ||
cls.user1 = User.objects.create_user('user1', '[email protected]') | ||
cls.user1.user_permissions.add(cls.permission) | ||
|
||
cls.group = Group.objects.create(name='test') | ||
cls.group.permissions.add(cls.permission) | ||
cls.group2 = Group.objects.create(name='test2') | ||
cls.group2.permissions.add(cls.permission) | ||
cls.user2 = User.objects.create_user('user2', '[email protected]') | ||
cls.user2.groups.add(cls.group, cls.group2) | ||
|
||
cls.user3 = User.objects.create_user('user3', '[email protected]') | ||
cls.superuser = User.objects.create_superuser( | ||
'superuser', '[email protected]', 'superpassword', | ||
) | ||
cls.inactive_user = User.objects.create_user( | ||
'inactive_user', '[email protected]', is_active=False, | ||
) | ||
cls.inactive_user.user_permissions.add(cls.permission) | ||
cls.charlie = User.objects.create_user('charlie', '[email protected]') | ||
cls.charlie_brown = User.objects.create_user('charliebrown', '[email protected]') | ||
|
||
def test_invalid_permission_name(self): | ||
msg = "Permission name should be in the form 'app_label.perm_name'." | ||
for perm in ('nodots', 'too.many.dots', '...', ''): | ||
with self.assertRaisesMessage(ValueError, msg): | ||
User.objects.with_perm(perm) | ||
|
||
def test_user_permissions(self): | ||
self.assertCountEqual( | ||
User.objects.with_perm('auth.test'), | ||
[self.user1, self.user2, self.superuser], | ||
) | ||
|
||
def test_group_permissions(self): | ||
self.assertCountEqual( | ||
User.objects.with_perm('auth.test'), | ||
[self.user1, self.user2, self.superuser], | ||
) | ||
|
||
def test_no_permissions(self): | ||
self.assertNotIn(self.user3, User.objects.with_perm('auth.test')) | ||
|
||
def test_superuser(self): | ||
self.assertIn(self.superuser, User.objects.with_perm('auth.test')) | ||
|
||
def test_exclude_superuser(self): | ||
users = User.objects.with_perm('auth.test', is_superuser=False) | ||
self.assertNotIn(self.superuser, users) | ||
self.assertIn(self.user1, users) | ||
|
||
def test_exclude_inactive(self): | ||
self.assertNotIn(self.inactive_user, User.objects.with_perm('auth.test')) | ||
|
||
def test_inactive(self): | ||
users = User.objects.with_perm('auth.test', is_active=False) | ||
self.assertIn(self.inactive_user, users) | ||
self.assertNotIn(self.user1, users) | ||
|
||
def test_non_duplicate_users(self): | ||
self.assertCountEqual( | ||
User.objects.with_perm('auth.test'), | ||
[self.user1, self.user2, self.superuser], | ||
) | ||
|
||
def test_with_permission_instance(self): | ||
self.assertCountEqual( | ||
User.objects.with_perm(self.permission), | ||
[self.user1, self.user2, self.superuser], | ||
) | ||
|
||
def test_default_backend_with_permission_instance_and_obj(self): | ||
obj = CustomModel.objects.create(user=self.charlie_brown) | ||
self.assertCountEqual( | ||
User.objects.with_perm(self.permission, obj=obj), | ||
[], | ||
) | ||
|
||
@override_settings(AUTHENTICATION_BACKENDS=['auth_tests.test_auth_backends.CustomModelBackend']) | ||
def test_custom_backend_with_permission_instance_and_obj(self): | ||
obj = CustomModel.objects.create(user=self.charlie_brown) | ||
self.assertCountEqual( | ||
User.objects.with_perm(self.permission, obj=obj), | ||
[self.charlie_brown], | ||
) | ||
|
||
def test_invalid_perm_type(self): | ||
msg = 'The `perm` argument must be a string or a permission instance.' | ||
|
||
class FakePermission: | ||
pass | ||
|
||
for perm in (b'auth.test', FakePermission()): | ||
with self.subTest(perm=perm): | ||
with self.assertRaisesMessage(TypeError, msg): | ||
User.objects.with_perm(perm) | ||
|
||
@override_settings(AUTHENTICATION_BACKENDS=[ | ||
'auth_tests.test_auth_backends.CustomModelBackend', | ||
'django.contrib.auth.backends.ModelBackend', | ||
]) | ||
def test_custom_backend(self): | ||
msg = ( | ||
'You have multiple authentication backends configured and ' | ||
'therefore must provide the `backend` argument.' | ||
) | ||
backend = 'auth_tests.test_auth_backends.CustomModelBackend' | ||
with self.assertRaisesMessage(ValueError, msg): | ||
User.objects.with_perm('auth.test') | ||
self.assertCountEqual( | ||
User.objects.with_perm('auth.test', backend=backend), | ||
[self.charlie, self.charlie_brown], | ||
) | ||
self.assertNotIn(self.user1, User.objects.with_perm('auth.test', backend=backend)) | ||
|
||
def test_default_backend_with_obj(self): | ||
obj = CustomModel.objects.create(user=self.charlie_brown) | ||
self.assertCountEqual( | ||
User.objects.with_perm('auth.test', obj=obj), | ||
[], | ||
) | ||
|
||
@override_settings(AUTHENTICATION_BACKENDS=['auth_tests.test_auth_backends.CustomModelBackend']) | ||
def test_custom_backend_with_obj(self): | ||
obj = CustomModel.objects.create(user=self.charlie_brown) | ||
self.assertCountEqual( | ||
User.objects.with_perm('auth.test', obj=obj), | ||
[self.charlie_brown], | ||
) | ||
|
||
def test_invalid_backend_type(self): | ||
msg = 'The `backend` argument must be a string.' | ||
with self.assertRaisesMessage(TypeError, msg): | ||
User.objects.with_perm( | ||
'auth.test', | ||
backend=b'auth_tests.test_auth_backends.CustomModelBackend', | ||
) | ||
|
||
def test_nonexistent_backend(self): | ||
with self.assertRaises(ImportError): | ||
User.objects.with_perm( | ||
'auth.test', | ||
backend='invalid.backend.CustomModelBackend', | ||
) | ||
|
||
def test_invalid_permission(self): | ||
self.assertCountEqual( | ||
User.objects.with_perm('invalid.perm'), | ||
[self.superuser], | ||
) | ||
|
||
@override_settings(AUTHENTICATION_BACKENDS=['auth_tests.test_auth_backends.BareModelBackend']) | ||
felixxm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def test_backend_without_with_perm(self): | ||
self.assertCountEqual( | ||
User.objects.with_perm('auth.test'), | ||
[], | ||
) | ||
|
||
|
||
class IsActiveTestCase(TestCase): | ||
""" | ||
Tests the behavior of the guaranteed is_active attribute | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would make it more DRY and readable, e.g.
What do you think?
I wonder if we can use
Exists()
to avoiddistinct()
call.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, your suggestion definitely looks better. I'll update the PR. Thanks!
Do you mean the
queryset.exists()
method or doesExists()
mean something else?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was talking about Exists() subquery.