add settings variable ENABLE_ELECTRONIC_VOTING

This commit is contained in:
jsangmeister 2019-11-05 09:30:55 +01:00 committed by FinnStutzenstein
parent 046a152ec5
commit 09ef3c5071
5 changed files with 97 additions and 7 deletions

View File

@ -1,3 +1,5 @@
from django.conf import settings
from ..utils.auth import get_group_model from ..utils.auth import get_group_model
from ..utils.rest_api import ( from ..utils.rest_api import (
CharField, CharField,
@ -5,7 +7,9 @@ from ..utils.rest_api import (
IdPrimaryKeyRelatedField, IdPrimaryKeyRelatedField,
ModelSerializer, ModelSerializer,
SerializerMethodField, SerializerMethodField,
ValidationError,
) )
from .models import BasePoll
BASE_VOTE_FIELDS = ("id", "weight", "value", "user", "option", "pollstate") BASE_VOTE_FIELDS = ("id", "weight", "value", "user", "option", "pollstate")
@ -77,10 +81,10 @@ class BasePollSerializer(ModelSerializer):
def update(self, instance, validated_data): def update(self, instance, validated_data):
""" """
Adjusts the 100%-base to the pollmethod. This might be needed, Adjusts the 100%-base to the pollmethod. This might be needed,
if at least one of them was changed. Wrong comobinations should be if at least one of them was changed. Wrong combinations should be
also handled by the client, but here we make it sure aswell! also handled by the client, but here we make it sure aswell!
E.g. the pollmethod is YN, but the 100%-base is YNA, this micht noght be E.g. the pollmethod is YN, but the 100%-base is YNA, this might not be
possible (see implementing serializers to see forbidden combinations) possible (see implementing serializers to see forbidden combinations)
""" """
old_100_percent_base = instance.onehundred_percent_base old_100_percent_base = instance.onehundred_percent_base
@ -95,6 +99,24 @@ class BasePollSerializer(ModelSerializer):
return instance return instance
def validate(self, data):
"""
Check that the given polltype is allowed.
"""
# has to be called in function instead of globally to enable tests to change the setting
ENABLE_ELECTRONIC_VOTING = getattr(settings, "ENABLE_ELECTRONIC_VOTING", False)
if (
"type" in data
and data["type"] != BasePoll.TYPE_ANALOG
and not ENABLE_ELECTRONIC_VOTING
):
raise ValidationError(
{
"detail": "Electronic voting is disabled. Only analog polls are allowed"
}
)
return data
def norm_100_percent_base_to_pollmethod( def norm_100_percent_base_to_pollmethod(
self, onehundred_percent_base, pollmethod, old_100_percent_base=None self, onehundred_percent_base, pollmethod, old_100_percent_base=None
): ):

View File

@ -127,6 +127,10 @@ if ENABLE_SAML:
INSTALLED_APPS += ['openslides.saml'] INSTALLED_APPS += ['openslides.saml']
# Controls if electronic voting (means non-analog polls) are enabled.
ENABLE_ELECTRONIC_VOTING = False
# Internationalization # Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/ # https://docs.djangoproject.com/en/1.10/topics/i18n/

View File

@ -3,6 +3,7 @@ from decimal import Decimal
from typing import Any from typing import Any
import pytest import pytest
from django.conf import settings
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.urls import reverse from django.urls import reverse
from rest_framework import status from rest_framework import status
@ -242,6 +243,23 @@ class CreateAssignmentPoll(TestCase):
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertFalse(AssignmentPoll.objects.exists()) self.assertFalse(AssignmentPoll.objects.exists())
def test_not_allowed_type(self):
setattr(settings, "ENABLE_ELECTRONIC_VOTING", False)
response = self.client.post(
reverse("assignmentpoll-list"),
{
"title": "test_title_yaiyeighoh0Iraet3Ahc",
"pollmethod": AssignmentPoll.POLLMETHOD_YNA,
"type": AssignmentPoll.TYPE_NAMED,
"assignment_id": self.assignment.id,
"onehundred_percent_base": AssignmentPoll.PERCENT_BASE_YN,
"majority_method": AssignmentPoll.MAJORITY_SIMPLE,
},
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertFalse(AssignmentPoll.objects.exists())
setattr(settings, "ENABLE_ELECTRONIC_VOTING", True)
def test_not_supported_pollmethod(self): def test_not_supported_pollmethod(self):
response = self.client.post( response = self.client.post(
reverse("assignmentpoll-list"), reverse("assignmentpoll-list"),
@ -415,6 +433,17 @@ class UpdateAssignmentPoll(TestCase):
poll = AssignmentPoll.objects.get() poll = AssignmentPoll.objects.get()
self.assertEqual(poll.type, "named") self.assertEqual(poll.type, "named")
def test_patch_not_allowed_type(self):
setattr(settings, "ENABLE_ELECTRONIC_VOTING", False)
response = self.client.patch(
reverse("assignmentpoll-detail", args=[self.poll.pk]),
{"type": BasePoll.TYPE_NAMED},
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
poll = AssignmentPoll.objects.get()
self.assertEqual(poll.type, BasePoll.TYPE_NAMED)
setattr(settings, "ENABLE_ELECTRONIC_VOTING", False)
def test_patch_groups_to_empty(self): def test_patch_groups_to_empty(self):
response = self.client.patch( response = self.client.patch(
reverse("assignmentpoll-detail", args=[self.poll.pk]), {"groups_id": []}, reverse("assignmentpoll-detail", args=[self.poll.pk]), {"groups_id": []},

View File

@ -1,6 +1,7 @@
from decimal import Decimal from decimal import Decimal
import pytest import pytest
from django.conf import settings
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.urls import reverse from django.urls import reverse
from rest_framework import status from rest_framework import status
@ -151,11 +152,11 @@ class CreateMotionPoll(TestCase):
reverse("motionpoll-list"), reverse("motionpoll-list"),
{ {
"title": "test_title_Thoo2eiphohhi1eeXoow", "title": "test_title_Thoo2eiphohhi1eeXoow",
"pollmethod": "YNA", "pollmethod": MotionPoll.POLLMETHOD_YNA,
"type": "named", "type": MotionPoll.TYPE_NAMED,
"motion_id": self.motion.id, "motion_id": self.motion.id,
"onehundred_percent_base": "YN", "onehundred_percent_base": MotionPoll.PERCENT_BASE_YN,
"majority_method": "simple", "majority_method": MotionPoll.MAJORITY_SIMPLE,
"groups_id": [], "groups_id": [],
}, },
) )
@ -168,14 +169,33 @@ class CreateMotionPoll(TestCase):
reverse("motionpoll-list"), reverse("motionpoll-list"),
{ {
"title": "test_title_yaiyeighoh0Iraet3Ahc", "title": "test_title_yaiyeighoh0Iraet3Ahc",
"pollmethod": "YNA", "pollmethod": MotionPoll.POLLMETHOD_YNA,
"type": "not_existing", "type": "not_existing",
"motion_id": self.motion.id, "motion_id": self.motion.id,
"onehundred_percent_base": MotionPoll.PERCENT_BASE_YN,
"majority_method": MotionPoll.MAJORITY_SIMPLE,
}, },
) )
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertFalse(MotionPoll.objects.exists()) self.assertFalse(MotionPoll.objects.exists())
def test_not_allowed_type(self):
setattr(settings, "ENABLE_ELECTRONIC_VOTING", False)
response = self.client.post(
reverse("motionpoll-list"),
{
"title": "test_title_3jdWIXbKBa7ZXutf3RYf",
"pollmethod": MotionPoll.POLLMETHOD_YN,
"type": MotionPoll.TYPE_NAMED,
"motion_id": self.motion.id,
"onehundred_percent_base": MotionPoll.PERCENT_BASE_YN,
"majority_method": MotionPoll.MAJORITY_SIMPLE,
},
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertFalse(MotionPoll.objects.exists())
setattr(settings, "ENABLE_ELECTRONIC_VOTING", True)
def test_not_supported_pollmethod(self): def test_not_supported_pollmethod(self):
response = self.client.post( response = self.client.post(
reverse("motionpoll-list"), reverse("motionpoll-list"),
@ -184,6 +204,8 @@ class CreateMotionPoll(TestCase):
"pollmethod": "not_existing", "pollmethod": "not_existing",
"type": "named", "type": "named",
"motion_id": self.motion.id, "motion_id": self.motion.id,
"onehundred_percent_base": MotionPoll.PERCENT_BASE_YN,
"majority_method": MotionPoll.MAJORITY_SIMPLE,
}, },
) )
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
@ -270,6 +292,17 @@ class UpdateMotionPoll(TestCase):
poll = MotionPoll.objects.get() poll = MotionPoll.objects.get()
self.assertEqual(poll.type, "named") self.assertEqual(poll.type, "named")
def test_patch_not_allowed_type(self):
setattr(settings, "ENABLE_ELECTRONIC_VOTING", False)
response = self.client.patch(
reverse("motionpoll-detail", args=[self.poll.pk]),
{"type": BasePoll.TYPE_NAMED},
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
poll = MotionPoll.objects.get()
self.assertEqual(poll.type, BasePoll.TYPE_NAMED)
setattr(settings, "ENABLE_ELECTRONIC_VOTING", True)
def test_patch_100_percent_base(self): def test_patch_100_percent_base(self):
response = self.client.patch( response = self.client.patch(
reverse("motionpoll-detail", args=[self.poll.pk]), reverse("motionpoll-detail", args=[self.poll.pk]),

View File

@ -75,3 +75,5 @@ PASSWORD_HASHERS = ["django.contrib.auth.hashers.MD5PasswordHasher"]
RESTRICTED_DATA_CACHE = False RESTRICTED_DATA_CACHE = False
REST_FRAMEWORK = {"TEST_REQUEST_DEFAULT_FORMAT": "json"} REST_FRAMEWORK = {"TEST_REQUEST_DEFAULT_FORMAT": "json"}
ENABLE_ELECTRONIC_VOTING = True