def test_inversion_of_attribute():
    u1 = UserFactory(username="******")
    u2 = UserFactory(username="******")
    s1 = StoreFactory(name="a")
    s2 = StoreFactory(name="b")
    p = ~Attribute("name", lambda u: u.username)

    assert p.check(u1, s2)
    assert p.check(u2, s1)
예제 #2
0
def test_in_current_groups():
    user = UserFactory()
    g_in_1 = Group.objects.create(name="g_in_1")
    g_in_2 = Group.objects.create(name="g_in_2")
    g_out_1 = Group.objects.create(name="g_out_1")
    Group.objects.create(name="g_out_2")
    user.groups.set((g_in_1, g_in_2))
    user.save()

    assert in_current_groups.check(user, g_in_1)
    assert not in_current_groups.check(user, g_out_1)

    assert set(in_current_groups.filter(user, Group.objects.all())) == {g_in_1, g_in_2}
예제 #3
0
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}
예제 #4
0
def test_is_user_function():
    u1 = UserFactory()
    u2 = UserFactory()
    is_own_profile = Is(lambda u: u.profile)

    assert is_own_profile.check(u1, u1.profile)
    assert is_own_profile.check(u2, u2.profile)
    assert not is_own_profile.check(u1, u2.profile)
    assert not is_own_profile.check(u2, u1.profile)

    qs1 = is_own_profile.filter(u1, Profile.objects.all())
    qs2 = is_own_profile.filter(u2, Profile.objects.all())
    assert qs1.count() == 1
    assert u1.profile in qs1
    assert u2.profile not in qs1
    assert qs2.count() == 1
    assert u2.profile in qs2
    assert u1.profile not in qs2
def test_user_func_attribute():
    u1 = UserFactory(username="******")
    u2 = UserFactory(username="******")
    s1 = StoreFactory(name="a")
    s2 = StoreFactory(name="b")
    p = Attribute("name", lambda u: u.username)

    assert p.check(u1, s1)
    assert p.check(u2, s2)
    assert not p.check(u1, s2)
    assert not p.check(u2, s1)

    qs1 = p.filter(u1, Store.objects.all())
    qs2 = p.filter(u2, Store.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_user_func_attribute():
    u1 = UserFactory(username="******")
    u2 = UserFactory(username="******")
    s1 = StoreFactory(name="a")
    s2 = StoreFactory(name="b")
    pr = R(name=lambda u: u.username)

    assert pr.check(u1, s1)
    assert pr.check(u2, s2)
    assert not pr.check(u1, s2)
    assert not pr.check(u2, s1)

    qs1_r = pr.filter(u1, Store.objects.all())
    qs2_r = pr.filter(u2, Store.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
def test_many_relation_to_user():
    s1 = StoreFactory()
    s2 = StoreFactory()
    u1 = UserFactory(profile__branch__store=s1)
    u2 = UserFactory(profile__branch__store=s2)
    user_branch_in_store = R(branch=lambda u: u.profile.branch)

    assert user_branch_in_store.check(u1, s1)
    assert user_branch_in_store.check(u2, s2)
    assert not user_branch_in_store.check(u1, s2)
    assert not user_branch_in_store.check(u2, s1)

    qs1 = user_branch_in_store.filter(u1, Store.objects.all())
    qs2 = user_branch_in_store.filter(u2, Store.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_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_traverse_nonexistent_fk():
    """Comparing with a reverse FK traversal that does not exist for the model."""
    user = UserFactory(profile=None)
    profile = ProfileFactory(user=UserFactory(profile=None))
    user_has_profile = R(profile=profile)
    user_has_no_profile = R(profile=None)

    # filter() tests
    users_with_profile = user_has_profile.filter(user, User.objects.all())
    assert user not in users_with_profile
    assert profile.user in users_with_profile
    users_with_no_profile = user_has_no_profile.filter(user,
                                                       User.objects.all())
    assert user in users_with_no_profile
    assert profile.user not in users_with_no_profile

    # check() tests
    assert not user_has_profile.check(user, user)
    assert user_has_profile.check(user, profile.user)
    assert user_has_no_profile.check(user, user)
    assert not user_has_no_profile.check(user, profile.user)
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_constant_attribute():
    user = UserFactory()
    s1 = StoreFactory(name="a")
    s2 = StoreFactory(name="b")
    pr = R(name="a")

    assert pr.check(user, s1)
    assert not pr.check(user, s2)

    filtered_qs_r = pr.filter(user, Store.objects.all())
    assert filtered_qs_r.count() == 1
    assert s1 in filtered_qs_r
    assert s2 not in filtered_qs_r
def test_constant_attribute():
    user = UserFactory()
    s1 = StoreFactory(name="a")
    s2 = StoreFactory(name="b")
    p = Attribute("name", "a")

    assert p.check(user, s1)
    assert not p.check(user, s2)

    filtered_qs = p.filter(user, Store.objects.all())
    assert filtered_qs.count() == 1
    assert s1 in filtered_qs
    assert s2 not in filtered_qs
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
예제 #14
0
def test_is_never_global():
    user = UserFactory()
    is_own_profile = Is(lambda u: u.profile)
    assert not is_own_profile.check(user)
def test_when_called_without_object():
    user = UserFactory(username="******")
    p = Attribute("name", lambda u: u.username)
    assert not p.check(user)
def test_when_called_without_object():
    user = UserFactory(username="******")
    pr = R(name=lambda u: u.username)
    assert not pr.check(user)
def test_relation_never_global():
    user = UserFactory()
    belongs_to_branch_r = R(branch=lambda u: u.profile.branch)
    assert not belongs_to_branch_r.check(user)
def test_relation_never_global():
    user = UserFactory()
    belongs_to_branch = Relation('branch', Is(lambda u: u.profile.branch))
    assert not belongs_to_branch.check(user)
예제 #19
0
def test_is_never_global():
    user = UserFactory()
    is_in_branch = In(lambda u: u.profile.branch.shrubbery_set.all())
    assert not is_in_branch.check(user)
예제 #20
0
def test_current_user():
    u1 = UserFactory()
    u2 = UserFactory()
    assert current_user.check(u1, u1)
    assert not current_user.check(u1, u2)
    assert set(current_user.filter(u1, User.objects.all())) == {u1}
def test_many_relation_never_global():
    user = UserFactory()
    user_branch_in_store = ManyRelation('branch_set', 'branch', Branch,
                                        Is(lambda u: u.profile.branch))
    assert not user_branch_in_store.check(user)