示例#1
0
    def test_get_context_data(self, mock_Widget, mock_get_context_data,
                              mock_SelectWidgetsForm):
        view = views.SelectWidgetsView()
        view.request = self.rf.get('/')
        view.request.session = MagicMock()
        widget = MagicMock()
        widget.name = 'some_widget_Bohsh1Pa0eeziRaihu8O'
        widget.is_active.return_value = True
        mock_Widget.get_all.return_value = [widget]
        mock_get_context_data.return_value = {}

        # Test get
        context = view.get_context_data()
        self.assertIn('widgets', context)
        self.assertIn(widget, context['widgets'])
        mock_SelectWidgetsForm.assert_called_with(
            prefix='some_widget_Bohsh1Pa0eeziRaihu8O',
            initial={'widget': True})

        # Test post
        view.request = self.rf.post('/')
        view.request.session = MagicMock()
        context = view.get_context_data()
        mock_SelectWidgetsForm.assert_called_with(
            view.request.POST,
            prefix='some_widget_Bohsh1Pa0eeziRaihu8O',
            initial={'widget': True})
示例#2
0
    def test_post(self, mock_messages):
        view = views.SelectWidgetsView()
        view.request = self.rf.post('/')
        view.request.session = {}
        widget = MagicMock()
        widget.name = 'some_widget_ahgaeree8JeReichue8u'
        context = {'widgets': [widget]}
        mock_context_data = MagicMock(return_value=context)

        with patch('openslides.core.views.SelectWidgetsView.get_context_data',
                   mock_context_data):
            widget.form.is_valid.return_value = True
            view.post(view.request)
            self.assertIn('some_widget_ahgaeree8JeReichue8u',
                          view.request.session['widgets'])

            # Test with errors in form
            widget.form.is_valid.return_value = False
            view.request.session = {}
            view.post(view.request)
            self.assertNotIn('widgets', view.request.session)
            mock_messages.error.assert_called_with(
                view.request, 'There are errors in the form.')