kmmbvnr / django-guardian-ng

Per object permissions for Django
https://django-guardian.readthedocs.io/
Other
1 stars 0 forks source link

get_objects_for_user not working #130

Open kmmbvnr opened 5 months ago

kmmbvnr commented 5 months ago

Original issue: https://github.com/django-guardian/django-guardian/issues/415

kmmbvnr commented 5 months ago
from django.test import TestCase
from django.contrib.auth.models import User
from app.models import Cashbox
from guardian.shortcuts import assign_perm, get_objects_for_user

class CashboxPermissionsTestCase(TestCase):
    def setUp(self):
        # Create a user
        self.user = User.objects.create_user('testuser', 'test@example.com', 'password123')

        # Create a cashbox
        self.cashbox = Cashbox.objects.create(name="Test Cashbox")

        # Assign permission to the user on the cashbox
        assign_perm('app.operation_in', self.user, self.cashbox)

    def test_user_can_access_cashbox(self):
        # Test that the cashbox is in the list of objects the user has permission to access
        accessible_cashboxes = get_objects_for_user(self.user, perms=['app.operation_in'], klass=Cashbox)
        self.assertIn(self.cashbox, accessible_cashboxes)

    def test_user_has_operation_in_permission(self):
        # Test that the user has 'operation_in' permission on the cashbox
        has_permission = self.user.has_perm('app.operation_in', self.cashbox)
        self.assertTrue(has_permission)

    def test_spelling_error_in_false_assertion(self):
        # Corrected the typo from 'Fasle' to 'False'
        has_permission = self.user.has_perm('app.operation_in', self.cashbox)
        self.assertFalse(not has_permission)  # double negative to assert the truth of the permission