52 lines
1.9 KiB
Python
52 lines
1.9 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
"""Gruppenhierarchie ohne Datenbank: Vererbung nach unten und oben, Kreise."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
pytest.importorskip("sqlalchemy")
|
|
|
|
from surveybox.accounts import scope
|
|
|
|
# 1 Gesamtwehr
|
|
# 2 Abteilung Nord
|
|
# 4 Jugendfeuerwehr
|
|
# 3 Abteilung Süd
|
|
# 5 Förderverein
|
|
TREE = {1: None, 2: 1, 3: 1, 4: 2, 5: None}
|
|
NAMES = {1: "Gesamtwehr", 2: "Abteilung Nord", 3: "Abteilung Süd", 4: "Jugendfeuerwehr",
|
|
5: "Förderverein"}
|
|
|
|
|
|
def test_descendants_and_ancestors() -> None:
|
|
assert scope.descendants(TREE, [2]) == {2, 4}
|
|
assert scope.descendants(TREE, [1]) == {1, 2, 3, 4}
|
|
assert scope.ancestors(TREE, [4]) == {4, 2, 1}
|
|
assert scope.ancestors(TREE, [5, 3]) == {5, 3, 1}
|
|
assert scope.descendants(TREE, [99]) == set()
|
|
|
|
|
|
def test_cycles_are_detected() -> None:
|
|
assert scope.would_cycle(TREE, 1, 4)
|
|
assert scope.would_cycle(TREE, 2, 2)
|
|
assert not scope.would_cycle(TREE, 4, 3)
|
|
assert not scope.would_cycle(TREE, 4, None)
|
|
|
|
|
|
def test_tree_order_with_and_without_selection() -> None:
|
|
assert scope.depth_order(TREE, NAMES) == [(5, 0), (1, 0), (2, 1), (4, 2), (3, 1)]
|
|
# Nur ein Teilbaum sichtbar: er beginnt bei Tiefe 0
|
|
assert scope.depth_order(TREE, NAMES, {2, 4}) == [(2, 0), (4, 1)]
|
|
|
|
|
|
def test_scope_flags() -> None:
|
|
group_admin = scope.Scope(user_id=1, instance_admin=False, global_creator=False,
|
|
managed=frozenset({2, 4}), creating=frozenset({2, 4}))
|
|
assert group_admin.can_manage_groups and group_admin.can_create_forms
|
|
assert group_admin.manages(4) and not group_admin.manages(3)
|
|
assert group_admin.may_create_in(4) and not group_admin.may_create_in(None)
|
|
creator = scope.Scope(user_id=2, instance_admin=False, global_creator=True)
|
|
assert creator.may_create_in(None) and creator.may_create_in(3)
|
|
assert not creator.can_manage_groups
|