2015-05-26 18:21:30 +02:00
|
|
|
from unittest import TestCase
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
|
|
|
|
from openslides.agenda.views import ItemViewSet
|
|
|
|
|
|
|
|
|
|
|
|
class ItemViewSetManageSpeaker(TestCase):
|
|
|
|
"""
|
|
|
|
Tests views of ItemViewSet to manage speakers.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2015-05-26 18:21:30 +02:00
|
|
|
def setUp(self):
|
|
|
|
self.request = MagicMock()
|
|
|
|
self.view_instance = ItemViewSet()
|
|
|
|
self.view_instance.request = self.request
|
|
|
|
self.view_instance.get_object = get_object_mock = MagicMock()
|
|
|
|
get_object_mock.return_value = self.mock_item = MagicMock()
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@patch("openslides.agenda.views.inform_changed_data")
|
|
|
|
@patch("openslides.agenda.views.has_perm")
|
|
|
|
@patch("openslides.agenda.views.Speaker")
|
2017-04-28 22:10:18 +02:00
|
|
|
def test_add_oneself_as_speaker(self, mock_speaker, mock_has_perm, mock_icd):
|
2019-01-06 16:22:33 +01:00
|
|
|
self.request.method = "POST"
|
2017-01-26 15:34:24 +01:00
|
|
|
self.request.user = 1
|
|
|
|
mock_has_perm.return_value = True
|
2015-05-26 18:21:30 +02:00
|
|
|
self.request.data = {}
|
|
|
|
self.mock_item.speaker_list_closed = False
|
2017-01-26 15:34:24 +01:00
|
|
|
|
2015-05-26 18:21:30 +02:00
|
|
|
self.view_instance.manage_speaker(self.request)
|
2017-01-26 15:34:24 +01:00
|
|
|
|
2015-05-26 18:21:30 +02:00
|
|
|
mock_speaker.objects.add.assert_called_with(self.request.user, self.mock_item)
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@patch("openslides.agenda.views.inform_changed_data")
|
|
|
|
@patch("openslides.agenda.views.has_perm")
|
|
|
|
@patch("openslides.agenda.views.get_user_model")
|
|
|
|
@patch("openslides.agenda.views.Speaker")
|
|
|
|
def test_add_someone_else_as_speaker(
|
|
|
|
self, mock_speaker, mock_get_user_model, mock_has_perm, mock_icd
|
|
|
|
):
|
|
|
|
self.request.method = "POST"
|
2017-01-26 15:34:24 +01:00
|
|
|
self.request.user = 1
|
2019-01-06 16:22:33 +01:00
|
|
|
self.request.data = {
|
|
|
|
"user": "2"
|
|
|
|
} # It is assumed that the request user has pk!=2.
|
2015-05-26 18:21:30 +02:00
|
|
|
mock_get_user_model.return_value = MockUser = MagicMock()
|
|
|
|
MockUser.objects.get.return_value = mock_user = MagicMock()
|
2017-01-26 15:34:24 +01:00
|
|
|
mock_has_perm.return_value = True
|
|
|
|
|
2015-05-26 18:21:30 +02:00
|
|
|
self.view_instance.manage_speaker(self.request)
|
2017-01-26 15:34:24 +01:00
|
|
|
|
2015-05-26 18:21:30 +02:00
|
|
|
MockUser.objects.get.assert_called_with(pk=2)
|
|
|
|
mock_speaker.objects.add.assert_called_with(mock_user, self.mock_item)
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@patch("openslides.agenda.views.Speaker")
|
2015-05-26 18:21:30 +02:00
|
|
|
def test_remove_oneself(self, mock_speaker):
|
2019-01-06 16:22:33 +01:00
|
|
|
self.request.method = "DELETE"
|
2015-05-26 18:21:30 +02:00
|
|
|
self.request.data = {}
|
|
|
|
self.view_instance.manage_speaker(self.request)
|
|
|
|
mock_queryset = mock_speaker.objects.filter.return_value.exclude.return_value
|
|
|
|
mock_queryset.get.return_value.delete.assert_called_with()
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@patch("openslides.agenda.views.inform_changed_data")
|
|
|
|
@patch("openslides.agenda.views.has_perm")
|
|
|
|
@patch("openslides.agenda.views.Speaker")
|
|
|
|
def test_remove_someone_else(
|
|
|
|
self, mock_speaker, mock_has_perm, mock_inform_changed_data
|
|
|
|
):
|
|
|
|
self.request.method = "DELETE"
|
2017-01-26 15:34:24 +01:00
|
|
|
self.request.user = 1
|
2019-01-06 16:22:33 +01:00
|
|
|
self.request.data = {"speaker": "1"}
|
2017-01-26 15:34:24 +01:00
|
|
|
mock_has_perm.return_value = True
|
|
|
|
|
2015-05-26 18:21:30 +02:00
|
|
|
self.view_instance.manage_speaker(self.request)
|
2017-01-26 15:34:24 +01:00
|
|
|
|
2015-05-27 15:42:32 +02:00
|
|
|
mock_speaker.objects.get.assert_called_with(pk=1)
|
2019-01-06 16:22:33 +01:00
|
|
|
mock_speaker.objects.get.return_value.delete.assert_called_with(
|
|
|
|
skip_autoupdate=True
|
|
|
|
)
|
2017-03-24 08:14:08 +01:00
|
|
|
mock_inform_changed_data.assert_called_with(self.mock_item)
|
2015-05-27 15:42:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ItemViewSetSpeak(TestCase):
|
|
|
|
"""
|
2015-09-04 18:24:41 +02:00
|
|
|
Tests views of ItemViewSet to begin and end speech.
|
2015-05-27 15:42:32 +02:00
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2015-05-27 15:42:32 +02:00
|
|
|
def setUp(self):
|
|
|
|
self.request = MagicMock()
|
|
|
|
self.view_instance = ItemViewSet()
|
|
|
|
self.view_instance.request = self.request
|
|
|
|
self.view_instance.get_object = get_object_mock = MagicMock()
|
|
|
|
get_object_mock.return_value = self.mock_item = MagicMock()
|
|
|
|
|
2015-09-04 18:24:41 +02:00
|
|
|
def test_begin_speech(self):
|
2019-01-06 16:22:33 +01:00
|
|
|
self.request.method = "PUT"
|
2015-05-27 15:42:32 +02:00
|
|
|
self.request.user.has_perm.return_value = True
|
|
|
|
self.request.data = {}
|
|
|
|
self.mock_item.get_next_speaker.return_value = mock_next_speaker = MagicMock()
|
|
|
|
self.view_instance.speak(self.request)
|
2015-09-04 18:24:41 +02:00
|
|
|
mock_next_speaker.begin_speech.assert_called_with()
|
2015-05-27 15:42:32 +02:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@patch("openslides.agenda.views.Speaker")
|
2015-09-04 18:24:41 +02:00
|
|
|
def test_begin_speech_specific_speaker(self, mock_speaker):
|
2019-01-06 16:22:33 +01:00
|
|
|
self.request.method = "PUT"
|
2015-05-27 15:42:32 +02:00
|
|
|
self.request.user.has_perm.return_value = True
|
2019-01-06 16:22:33 +01:00
|
|
|
self.request.data = {"speaker": "1"}
|
2015-05-27 15:42:32 +02:00
|
|
|
mock_speaker.objects.get.return_value = mock_next_speaker = MagicMock()
|
|
|
|
self.view_instance.speak(self.request)
|
2015-09-04 18:24:41 +02:00
|
|
|
mock_next_speaker.begin_speech.assert_called_with()
|
2015-05-27 15:42:32 +02:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@patch("openslides.agenda.views.Speaker")
|
2015-09-04 18:24:41 +02:00
|
|
|
def test_end_speech(self, mock_speaker):
|
2019-01-06 16:22:33 +01:00
|
|
|
self.request.method = "DELETE"
|
2015-05-27 15:42:32 +02:00
|
|
|
self.request.user.has_perm.return_value = True
|
2019-01-06 16:22:33 +01:00
|
|
|
mock_speaker.objects.filter.return_value.exclude.return_value.get.return_value = (
|
|
|
|
mock_speaker
|
|
|
|
) = MagicMock()
|
2015-05-27 15:42:32 +02:00
|
|
|
self.view_instance.speak(self.request)
|
2015-09-04 18:24:41 +02:00
|
|
|
mock_speaker.end_speech.assert_called_with()
|