Tests for the motion views
This commit is contained in:
parent
c46ec570bf
commit
3832aa4178
@ -338,13 +338,16 @@ class Motion(SlideMixin, models.Model):
|
||||
|
||||
def is_submitter(self, person):
|
||||
"""Return True, if person is a submitter of this motion. Else: False."""
|
||||
self.submitter.filter(person=person).exists()
|
||||
return self.submitter.filter(person=person).exists()
|
||||
|
||||
@property
|
||||
def supporters(self):
|
||||
return sorted([object.person for object in self.supporter.all()],
|
||||
key=lambda person: person.sort_name)
|
||||
|
||||
def add_submitter(self, person):
|
||||
MotionSubmitter.objects.create(motion=self, person=person)
|
||||
|
||||
def is_supporter(self, person):
|
||||
"""Return True, if person is a supporter of this motion. Else: False."""
|
||||
return self.supporter.filter(person=person).exists()
|
||||
|
@ -165,6 +165,11 @@ class MotionCreateView(MotionMixin, CreateView):
|
||||
self.object.write_log(ugettext_noop('Motion created'), self.request.user)
|
||||
return value
|
||||
|
||||
def post_save(self, form):
|
||||
super(MotionCreateView, self).post_save(form)
|
||||
if not 'submitter' in form.cleaned_data:
|
||||
self.object.add_submitter(self.request.user)
|
||||
|
||||
motion_create = MotionCreateView.as_view()
|
||||
|
||||
|
||||
|
@ -16,11 +16,6 @@ from openslides.config.models import config
|
||||
register = template.Library()
|
||||
|
||||
|
||||
@register.simple_tag
|
||||
def get_min_supporters():
|
||||
return config['motion_min_supporters']
|
||||
|
||||
|
||||
@register.simple_tag
|
||||
def get_config(key):
|
||||
return config[key]
|
||||
|
138
tests/motion/test_views.py
Normal file
138
tests/motion/test_views.py
Normal file
@ -0,0 +1,138 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Tests for openslides.motion.models
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
:copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
|
||||
:license: GNU GPL, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
from django.test.client import Client
|
||||
|
||||
from openslides.utils.test import TestCase
|
||||
from openslides.participant.models import User, Group
|
||||
from openslides.motion.models import Motion
|
||||
|
||||
|
||||
class MotionViewTestCase(TestCase):
|
||||
def setUp(self):
|
||||
# Admin
|
||||
self.admin = User.objects.create_superuser('admin', 'admin@admin.admin', 'admin')
|
||||
self.admin_client = Client()
|
||||
self.admin_client.login(username='admin', password='admin')
|
||||
|
||||
# Delegate
|
||||
self.delegate = User.objects.create_user('delegate', 'delegate@user.user', 'delegate')
|
||||
delegate_group = Group.objects.get(name='Delegates')
|
||||
self.delegate.groups.add(delegate_group)
|
||||
self.delegate.save()
|
||||
self.delegate_client = Client()
|
||||
self.delegate_client.login(username='delegate', password='delegate')
|
||||
|
||||
# Registered
|
||||
self.registered = User.objects.create_user('registered', 'registered@user.user', 'registered')
|
||||
self.registered_client = Client()
|
||||
self.registered_client.login(username='registered', password='registered')
|
||||
|
||||
self.motion1 = Motion.objects.create(title='motion1')
|
||||
self.motion2 = Motion.objects.create(title='motion2')
|
||||
|
||||
def check_url(self, url, test_client, response_cose):
|
||||
response = test_client.get(url)
|
||||
self.assertEqual(response.status_code, response_cose)
|
||||
return response
|
||||
|
||||
|
||||
class TestMotionListView(MotionViewTestCase):
|
||||
def test_get(self):
|
||||
self.check_url('/motion/', self.admin_client, 200)
|
||||
|
||||
|
||||
class TestMotionDetailView(MotionViewTestCase):
|
||||
def test_get(self):
|
||||
self.check_url('/motion/1/', self.admin_client, 200)
|
||||
self.check_url('/motion/2/', self.admin_client, 200)
|
||||
self.check_url('/motion/500/', self.admin_client, 404)
|
||||
|
||||
|
||||
class TestMotionCreateView(MotionViewTestCase):
|
||||
url = '/motion/new/'
|
||||
|
||||
def test_get(self):
|
||||
self.check_url(self.url, self.admin_client, 200)
|
||||
|
||||
def test_admin(self):
|
||||
self.assertFalse(Motion.objects.filter(versions__title='new motion').exists())
|
||||
response = self.admin_client.post(self.url, {'title': 'new motion',
|
||||
'text': 'motion text',
|
||||
'reason': 'motion reason',
|
||||
'submitter': self.admin})
|
||||
self.assertEqual(response.status_code, 302)
|
||||
self.assertTrue(Motion.objects.filter(versions__title='new motion').exists())
|
||||
|
||||
def test_delegate(self):
|
||||
self.assertFalse(Motion.objects.filter(versions__title='delegate motion').exists())
|
||||
response = self.delegate_client.post(self.url, {'title': 'delegate motion',
|
||||
'text': 'motion text',
|
||||
'reason': 'motion reason',
|
||||
'submitter': self.admin})
|
||||
self.assertEqual(response.status_code, 302)
|
||||
motion = Motion.objects.get(versions__title='delegate motion')
|
||||
self.assertTrue(motion.is_submitter(self.delegate))
|
||||
|
||||
def test_registered(self):
|
||||
self.assertFalse(Motion.objects.filter(versions__title='registered motion').exists())
|
||||
response = self.registered_client.post(self.url, {'title': 'registered motion',
|
||||
'text': 'motion text',
|
||||
'reason': 'motion reason',
|
||||
'submitter': self.admin})
|
||||
self.assertEqual(response.status_code, 403)
|
||||
self.assertFalse(Motion.objects.filter(versions__title='registered motion').exists())
|
||||
|
||||
|
||||
class TestMotionUpdateView(MotionViewTestCase):
|
||||
url = '/motion/1/edit/'
|
||||
|
||||
def test_get(self):
|
||||
self.check_url(self.url, self.admin_client, 200)
|
||||
|
||||
def test_admin(self):
|
||||
response = self.admin_client.post(self.url, {'title': 'new motion_title',
|
||||
'text': 'motion text',
|
||||
'reason': 'motion reason',
|
||||
'submitter': self.admin})
|
||||
self.assertRedirects(response, '/motion/1/')
|
||||
motion = Motion.objects.get(pk=1)
|
||||
self.assertEqual(motion.title, 'new motion_title')
|
||||
|
||||
def test_delegate(self):
|
||||
response = self.delegate_client.post(self.url, {'title': 'my title',
|
||||
'text': 'motion text',
|
||||
'reason': 'motion reason'})
|
||||
self.assertEqual(response.status_code, 403)
|
||||
motion = Motion.objects.get(pk=1)
|
||||
motion.add_submitter(self.delegate)
|
||||
response = self.delegate_client.post(self.url, {'title': 'my title',
|
||||
'text': 'motion text',
|
||||
'reason': 'motion reason'})
|
||||
self.assertRedirects(response, '/motion/1/')
|
||||
motion = Motion.objects.get(pk=1)
|
||||
self.assertEqual(motion.title, 'my title')
|
||||
|
||||
|
||||
class TestMotionDeleteView(MotionViewTestCase):
|
||||
def test_get(self):
|
||||
response = self.check_url('/motion/2/del/', self.admin_client, 302)
|
||||
self.assertRedirects(response, '/motion/2/')
|
||||
|
||||
def test_admin(self):
|
||||
response = self.admin_client.post('/motion/2/del/', {})
|
||||
self.assertRedirects(response, '/motion/')
|
||||
|
||||
def test_delegate(self):
|
||||
response = self.delegate_client.post('/motion/2/del/', {})
|
||||
self.assertEqual(response.status_code, 403)
|
||||
motion = Motion.objects.get(pk=2).add_submitter(self.delegate)
|
||||
response = self.delegate_client.post('/motion/2/del/', {})
|
||||
self.assertRedirects(response, '/motion/')
|
Loading…
Reference in New Issue
Block a user