Skip to content
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

Ks 2022 11 support phase tests #4655

Merged
merged 2 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions meinberlin/apps/budgeting/predicates.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import rules
from rules import predicates as rules_predicates

from adhocracy4.modules.predicates import is_allowed_moderate_project
from adhocracy4.modules.predicates import is_context_member
from adhocracy4.modules.predicates import is_live_context
from adhocracy4.modules.predicates import is_public_context
from adhocracy4.modules.predicates import module_is_between_phases
from adhocracy4.phases.predicates import has_feature_active
from adhocracy4.phases.predicates import phase_allows_delete_vote
Expand Down Expand Up @@ -35,22 +37,33 @@ def phase_allows_support(user, item):
@rules.predicate
def is_allowed_support_item(user, item):
if item:
return rules_predicates.is_superuser(user) | \
return is_allowed_moderate_project(user, item) | \
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I think this was missing, right? Now moderators, initiators, group members should have more permissions...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes!

(is_context_member(user, item) &
is_live_context(user, item) &
phase_allows_support(user, item))
return False


@rules.predicate
def phase_allows_view_support(module, item_class):
if module:
return has_feature_active(module, item_class, 'support') | \
module_is_between_phases('meinberlin_budgeting:support',
'meinberlin_budgeting:voting',
module)
return False


@rules.predicate
def is_allowed_view_support(item_class):
@rules.predicate
def _view_support(user, module):
if module:
return has_feature_active(module, item_class, 'support')\
| module_is_between_phases('meinberlin_budgeting:support',
'meinberlin_budgeting:voting',
module)
return is_allowed_moderate_project(user, module) | \
((is_public_context(user, module) |
is_context_member(user, module)) &
is_live_context(user, module) &
phase_allows_view_support(module, item_class))
return False

return _view_support
2 changes: 2 additions & 0 deletions meinberlin/apps/budgeting/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
module_predicates.is_allowed_add_item(models.Proposal)
)

# is_allowed_support_item is needed, because support also uses
# the rating api which checks rate_proposal permission
rules.add_perm(
'meinberlin_budgeting.rate_proposal',
module_predicates.is_allowed_rate_item | is_allowed_support_item
Expand Down
181 changes: 181 additions & 0 deletions tests/budgeting/rules/test_rules_support.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
from datetime import timedelta

import pytest
import rules
from freezegun import freeze_time

from adhocracy4.projects.enums import Access
from adhocracy4.test.helpers import freeze_phase
from adhocracy4.test.helpers import freeze_post_phase
from adhocracy4.test.helpers import freeze_pre_phase
from adhocracy4.test.helpers import setup_phase
from adhocracy4.test.helpers import setup_users
from meinberlin.apps.budgeting import phases

perm_name = 'meinberlin_budgeting.support_proposal'


def test_perm_exists():
assert rules.perm_exists(perm_name)


@pytest.mark.django_db
def test_pre_phase(phase_factory, proposal_factory, user, admin):
phase, module, project, item = setup_phase(phase_factory, proposal_factory,
phases.RequestPhase)
anonymous, moderator, initiator = setup_users(project)

assert project.access == Access.PUBLIC
with freeze_pre_phase(phase):
assert not rules.has_perm(perm_name, anonymous, item)
assert not rules.has_perm(perm_name, user, item)
assert rules.has_perm(perm_name, moderator, item)
assert rules.has_perm(perm_name, initiator, item)
assert rules.has_perm(perm_name, admin, item)


@pytest.mark.django_db
def test_support_phase_active(phase_factory, proposal_factory, user, admin):
phase, module, project, item = setup_phase(phase_factory, proposal_factory,
phases.SupportPhase)
anonymous, moderator, initiator = setup_users(project)

assert project.access == Access.PUBLIC
with freeze_phase(phase):
assert not rules.has_perm(perm_name, anonymous, item)
assert rules.has_perm(perm_name, user, item)
assert rules.has_perm(perm_name, moderator, item)
assert rules.has_perm(perm_name, initiator, item)
assert rules.has_perm(perm_name, admin, item)


@pytest.mark.django_db
def test_between_support_and_voting_phase(phase_factory, proposal_factory,
user, admin):
support_phase, module, project, item = setup_phase(phase_factory,
proposal_factory,
phases.SupportPhase)
phase_factory(
phase_content=phases.VotingPhase,
module=module,
start_date=support_phase.end_date + timedelta(hours=2),
end_date=support_phase.end_date + timedelta(hours=3),
type='meinberlin_budgeting:voting'
)
between_phases = support_phase.end_date + timedelta(hours=1)

anonymous, moderator, initiator = setup_users(project)

assert project.access == Access.PUBLIC
with freeze_time(between_phases):
assert not rules.has_perm(perm_name, anonymous, item)
assert not rules.has_perm(perm_name, user, item)
assert rules.has_perm(perm_name, moderator, item)
assert rules.has_perm(perm_name, initiator, item)
assert rules.has_perm(perm_name, admin, item)


@pytest.mark.django_db
def test_voting_phase_active(phase_factory, proposal_factory, user, admin):
phase, module, project, item = setup_phase(phase_factory, proposal_factory,
phases.VotingPhase)
anonymous, moderator, initiator = setup_users(project)

assert project.access == Access.PUBLIC
with freeze_phase(phase):
assert not rules.has_perm(perm_name, anonymous, item)
assert not rules.has_perm(perm_name, user, item)
assert rules.has_perm(perm_name, moderator, item)
assert rules.has_perm(perm_name, initiator, item)
assert rules.has_perm(perm_name, admin, item)


@pytest.mark.django_db
def test_rating_phase_active(phase_factory, proposal_factory, user, admin):
phase, module, project, item = setup_phase(phase_factory, proposal_factory,
phases.RatingPhase)
anonymous, moderator, initiator = setup_users(project)

assert project.access == Access.PUBLIC
with freeze_phase(phase):
assert not rules.has_perm(perm_name, anonymous, item)
assert not rules.has_perm(perm_name, user, item)
assert rules.has_perm(perm_name, moderator, item)
assert rules.has_perm(perm_name, initiator, item)
assert rules.has_perm(perm_name, admin, item)


@pytest.mark.django_db
def test_phase_active_project_private(phase_factory, proposal_factory,
user, user2, admin):
phase, _, project, item = setup_phase(
phase_factory, proposal_factory, phases.SupportPhase,
module__project__access=Access.PRIVATE)
anonymous, moderator, initiator = setup_users(project)

participant = user2
project.participants.add(participant)

assert project.access == Access.PRIVATE
with freeze_phase(phase):
assert not rules.has_perm(perm_name, anonymous, item)
assert not rules.has_perm(perm_name, user, item)
assert rules.has_perm(perm_name, participant, item)
assert rules.has_perm(perm_name, moderator, item)
assert rules.has_perm(perm_name, initiator, item)
assert rules.has_perm(perm_name, admin, item)


@pytest.mark.django_db
def test_phase_active_project_semipublic(phase_factory, proposal_factory,
user, user2, admin):
phase, _, project, item = setup_phase(
phase_factory, proposal_factory, phases.SupportPhase,
module__project__access=Access.SEMIPUBLIC)
anonymous, moderator, initiator = setup_users(project)

participant = user2
project.participants.add(participant)

assert project.access == Access.SEMIPUBLIC
with freeze_phase(phase):
assert not rules.has_perm(perm_name, anonymous, item)
assert not rules.has_perm(perm_name, user, item)
assert rules.has_perm(perm_name, participant, item)
assert rules.has_perm(perm_name, moderator, item)
assert rules.has_perm(perm_name, initiator, item)
assert rules.has_perm(perm_name, admin, item)


@pytest.mark.django_db
def test_phase_active_project_draft(phase_factory, proposal_factory,
user, admin):
phase, _, project, item = setup_phase(phase_factory, proposal_factory,
phases.SupportPhase,
module__project__is_draft=True)
anonymous, moderator, initiator = setup_users(project)

assert project.is_draft
with freeze_phase(phase):
assert not rules.has_perm(perm_name, anonymous, item)
assert not rules.has_perm(perm_name, user, item)
assert rules.has_perm(perm_name, moderator, item)
assert rules.has_perm(perm_name, initiator, item)
assert rules.has_perm(perm_name, admin, item)


@pytest.mark.django_db
def test_post_phase_project_archived(phase_factory, proposal_factory,
user, admin):
phase, _, project, item = setup_phase(phase_factory, proposal_factory,
phases.SupportPhase,
module__project__is_archived=True)
anonymous, moderator, initiator = setup_users(project)

assert project.is_archived
with freeze_post_phase(phase):
assert not rules.has_perm(perm_name, anonymous, item)
assert not rules.has_perm(perm_name, user, item)
assert rules.has_perm(perm_name, moderator, item)
assert rules.has_perm(perm_name, initiator, item)
assert rules.has_perm(perm_name, admin, item)
Loading