def test_traverse_fk_user_func(): user = UserFactory() shrubbery_match = ShrubberyFactory(branch__store=user.profile.branch.store) shrubbery_nomatch = ShrubberyFactory() store_check_r = R(branch__store=lambda user: user.profile.branch.store) assert store_check_r.check(user, shrubbery_match) assert not store_check_r.check(user, shrubbery_nomatch) qs = store_check_r.filter(user, Shrubbery.objects.all()) assert shrubbery_match in qs assert shrubbery_nomatch not in qs
def test_in_user_function_returning_qs(): u1 = UserFactory() u2 = UserFactory() s_in_1 = ShrubberyFactory(branch=u1.profile.branch) s_in_2 = ShrubberyFactory(branch=u1.profile.branch) s_out_1 = ShrubberyFactory(branch=u2.profile.branch) s_out_2 = ShrubberyFactory(branch=u2.profile.branch) is_in_branch = In(lambda u: u.profile.branch.shrubbery_set.all()) assert is_in_branch.check(u1, s_in_1) assert is_in_branch.check(u1, s_in_2) assert not is_in_branch.check(u1, s_out_1) assert not is_in_branch.check(u1, s_out_2) qs1 = is_in_branch.filter(u1, Shrubbery.objects.all()) qs2 = is_in_branch.filter(u2, Shrubbery.objects.all()) assert set(qs1) == {s_in_1, s_in_2} assert set(qs2) == {s_out_1, s_out_2}
def test_relation_to_user(): u1 = UserFactory() u2 = UserFactory() s1 = ShrubberyFactory(branch=u1.profile.branch) s2 = ShrubberyFactory(branch=u2.profile.branch) belongs_to_branch = Relation('branch', Is(lambda u: u.profile.branch)) assert belongs_to_branch.check(u1, s1) assert belongs_to_branch.check(u2, s2) assert not belongs_to_branch.check(u1, s2) assert not belongs_to_branch.check(u2, s1) qs1 = belongs_to_branch.filter(u1, Shrubbery.objects.all()) qs2 = belongs_to_branch.filter(u2, Shrubbery.objects.all()) assert qs1.count() == 1 assert s1 in qs1 assert s2 not in qs1 assert qs2.count() == 1 assert s2 in qs2 assert s1 not in qs2
def test_sentinels_with_single_R_kwarg(): class TestBranchRule(Rule): """A rule that allows superusers to see all branches, staff to see nothing, and other users to see their own. """ def query(self, user): if user.is_superuser: return UNIVERSAL elif user.is_staff: return EMPTY else: return Q(profile__user=user) def check(self, user, instance=None): if instance is None: return False if user.is_superuser: return True elif user.is_staff: return False else: return user.profile.branch == instance # Create 2 users with their own branches u1 = UserFactory() u2 = UserFactory() s1 = ShrubberyFactory(branch=u1.profile.branch) s2 = ShrubberyFactory(branch=u2.profile.branch) # Create superuser/staff superuser = UserFactory(is_superuser=True) staff = UserFactory(is_staff=True) # Create R rule belongs_to_branch_r = R(branch=TestBranchRule()) # Test users can only see their own assert belongs_to_branch_r.check(u1, s1) assert belongs_to_branch_r.check(u2, s2) assert not belongs_to_branch_r.check(u1, s2) assert not belongs_to_branch_r.check(u2, s1) # Test superuser can see all and staff see none assert belongs_to_branch_r.check(superuser, s1) assert belongs_to_branch_r.check(superuser, s2) assert not belongs_to_branch_r.check(staff, s2) assert not belongs_to_branch_r.check(staff, s1) # Check queryset filtering results in the same behaviour for users qs1_r = belongs_to_branch_r.filter(u1, Shrubbery.objects.all()) qs2_r = belongs_to_branch_r.filter(u2, Shrubbery.objects.all()) assert qs1_r.count() == 1 assert s1 in qs1_r assert s2 not in qs1_r assert qs2_r.count() == 1 assert s2 in qs2_r assert s1 not in qs2_r # Check queryset filtering results in the same behaviour for superusers # and staff qs_super_r = belongs_to_branch_r.filter(superuser, Shrubbery.objects.all()) qs_staff_r = belongs_to_branch_r.filter(staff, Shrubbery.objects.all()) assert qs_super_r.count() == 2 assert s1 in qs_super_r assert s2 in qs_super_r assert qs_staff_r.count() == 0 assert s2 not in qs_staff_r assert s1 not in qs_staff_r