2013-04-03 17:55:30 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2013-12-09 23:56:01 +01:00
|
|
|
from django.test.client import Client, RequestFactory
|
|
|
|
from mock import MagicMock, patch
|
2013-04-03 17:55:30 +02:00
|
|
|
|
|
|
|
from openslides import get_version
|
2013-10-30 17:48:09 +01:00
|
|
|
from openslides.agenda.models import Item
|
2013-11-14 19:29:08 +01:00
|
|
|
from openslides.config.api import config
|
2013-12-09 23:56:01 +01:00
|
|
|
from openslides.core import views
|
2013-04-03 17:55:30 +02:00
|
|
|
from openslides.participant.models import User
|
2013-09-25 10:01:01 +02:00
|
|
|
from openslides.utils.test import TestCase
|
2013-04-03 17:55:30 +02:00
|
|
|
|
|
|
|
|
2013-12-09 23:56:01 +01:00
|
|
|
class SelectWidgetsViewTest(TestCase):
|
|
|
|
rf = RequestFactory()
|
|
|
|
|
|
|
|
@patch('openslides.core.views.SelectWidgetsForm')
|
|
|
|
@patch('openslides.core.views.TemplateView.get_context_data')
|
|
|
|
@patch('openslides.core.views.Widget')
|
|
|
|
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})
|
|
|
|
|
|
|
|
@patch('openslides.core.views.messages')
|
|
|
|
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.')
|
|
|
|
|
|
|
|
|
2013-04-03 17:55:30 +02:00
|
|
|
class VersionViewTest(TestCase):
|
2013-10-30 17:48:09 +01:00
|
|
|
def setUp(self):
|
2013-09-25 10:01:01 +02:00
|
|
|
User.objects.create_user('CoreMaximilian', 'xxx@xx.xx', 'default')
|
2013-10-30 17:48:09 +01:00
|
|
|
self.client = Client()
|
|
|
|
self.client.login(username='CoreMaximilian', password='default')
|
|
|
|
|
|
|
|
def test_get(self):
|
|
|
|
response = self.client.get('/version/')
|
2013-04-03 17:55:30 +02:00
|
|
|
self.assertContains(response, get_version(), status_code=200)
|
2013-10-30 17:48:09 +01:00
|
|
|
|
|
|
|
@patch('openslides.core.views.settings')
|
|
|
|
def test_with_missing_plugin(self, mock_settings):
|
|
|
|
"""
|
2013-11-29 16:47:31 +01:00
|
|
|
Tests that a not existing app does not appear on the version view.
|
2013-10-30 17:48:09 +01:00
|
|
|
"""
|
|
|
|
mock_settings.INSTALLED_PLUGINS = ('unexisting_app_nvhbkdfgmnsd',)
|
2013-11-29 16:47:31 +01:00
|
|
|
self.assertRaises(ImportError, self.client.get, '/version/')
|
2013-10-30 17:48:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
class SearchViewTest(TestCase):
|
|
|
|
def test_simple_search(self):
|
|
|
|
Item.objects.create(title='agenda_item_bnghfdjkgndkjdfg')
|
|
|
|
User.objects.create_user('CoreMaximilian', 'xxx@xx.xx', 'default')
|
|
|
|
self.client = Client()
|
|
|
|
self.client.login(username='CoreMaximilian', password='default')
|
|
|
|
response = self.client.get('/search/?q=agenda_item_bnghfd')
|
|
|
|
text = '<span class="highlighted">agenda_item_bnghfd</span>jkgndkjdfg'
|
|
|
|
self.assertContains(response, text)
|
2013-11-14 19:29:08 +01:00
|
|
|
|
|
|
|
def test_anonymous(self):
|
|
|
|
self.assertFalse(config['system_enable_anonymous'])
|
|
|
|
self.assertEqual(Client().get('/search/').status_code, 403)
|
|
|
|
config['system_enable_anonymous'] = True
|
|
|
|
self.assertEqual(Client().get('/search/').status_code, 200)
|