kpsul/gestioncof/tests/test_views.py

164 lines
5.1 KiB
Python
Raw Normal View History

2018-01-19 18:36:03 +01:00
from django.contrib import messages
from django.contrib.messages import get_messages
from django.contrib.messages.storage.base import Message
from django.test import TestCase
from gestioncof.models import Event
from gestioncof.tests.testcases import ViewTestCaseMixin
class EventViewTests(ViewTestCaseMixin, TestCase):
url_name = 'event.details'
http_methods = ['GET', 'POST']
auth_user = 'user'
auth_forbidden = [None]
post_expected_message = Message(messages.SUCCESS, (
"Votre inscription a bien été enregistrée ! Vous pouvez cependant la "
"modifier jusqu'à la fin des inscriptions."
))
@property
def url_kwargs(self):
return {'event_id': self.e.pk}
@property
def url_expected(self):
return '/event/{}'.format(self.e.pk)
def setUp(self):
super().setUp()
self.e = Event.objects.create()
self.ecf1 = self.e.commentfields.create(name='Comment Field 1')
self.ecf2 = self.e.commentfields.create(
name='Comment Field 2', fieldtype='char',
)
self.o1 = self.e.options.create(name='Option 1')
self.o2 = self.e.options.create(name='Option 2', multi_choices=True)
self.oc1 = self.o1.choices.create(value='O1 - Choice 1')
self.oc2 = self.o1.choices.create(value='O1 - Choice 2')
self.oc3 = self.o2.choices.create(value='O2 - Choice 1')
self.oc4 = self.o2.choices.create(value='O2 - Choice 2')
def test_get(self):
r = self.client.get(self.url)
self.assertEqual(r.status_code, 200)
def test_post_new(self):
r = self.client.post(self.url, {
'option_{}'.format(self.o1.pk): [str(self.oc1.pk)],
'option_{}'.format(self.o2.pk): [
str(self.oc3.pk), str(self.oc4.pk),
],
})
self.assertEqual(r.status_code, 200)
self.assertIn(self.post_expected_message, get_messages(r.wsgi_request))
er = self.e.eventregistration_set.get(user=self.users['user'])
self.assertQuerysetEqual(
er.options.all(), map(repr, [self.oc1, self.oc3, self.oc4]),
ordered=False,
)
# TODO: Make the view care about comments.
# self.assertQuerysetEqual(
# er.comments.all(), map(repr, []),
# ordered=False,
# )
def test_post_edit(self):
er = self.e.eventregistration_set.create(user=self.users['user'])
er.options.add(self.oc1, self.oc3, self.oc4)
er.comments.create(
commentfield=self.ecf1, content='Comment 1',
)
r = self.client.post(self.url, {
'option_{}'.format(self.o1.pk): [],
'option_{}'.format(self.o2.pk): [str(self.oc3.pk)],
})
self.assertEqual(r.status_code, 200)
self.assertIn(self.post_expected_message, get_messages(r.wsgi_request))
er.refresh_from_db()
self.assertQuerysetEqual(
er.options.all(), map(repr, [self.oc3]),
ordered=False,
)
# TODO: Make the view care about comments.
# self.assertQuerysetEqual(
# er.comments.all(), map(repr, []),
# ordered=False,
# )
class EventStatusViewTests(ViewTestCaseMixin, TestCase):
url_name = 'event.details.status'
http_methods = ['GET', 'POST']
auth_user = 'staff'
auth_forbidden = [None, 'user', 'member']
@property
def url_kwargs(self):
return {'event_id': self.e.pk}
@property
def url_expected(self):
return '/event/{}/status'.format(self.e.pk)
def setUp(self):
super().setUp()
self.e = Event.objects.create()
self.cf1 = self.e.commentfields.create(name='Comment Field 1')
self.cf2 = self.e.commentfields.create(
name='Comment Field 2', fieldtype='char',
)
self.o1 = self.e.options.create(name='Option 1')
self.o2 = self.e.options.create(name='Option 2', multi_choices=True)
self.oc1 = self.o1.choices.create(value='O1 - Choice 1')
self.oc2 = self.o1.choices.create(value='O1 - Choice 2')
self.oc3 = self.o2.choices.create(value='O2 - Choice 1')
self.oc4 = self.o2.choices.create(value='O2 - Choice 2')
self.er1 = self.e.eventregistration_set.create(user=self.users['user'])
self.er1.options.add(self.oc1)
self.er2 = self.e.eventregistration_set.create(
user=self.users['member'],
)
def _get_oc_filter_name(self, oc):
return 'option_{}_choice_{}'.format(oc.event_option.pk, oc.pk)
def _test_filters(self, filters, expected):
r = self.client.post(self.url, {
self._get_oc_filter_name(oc): v for oc, v in filters
})
self.assertEqual(r.status_code, 200)
self.assertQuerysetEqual(
r.context['user_choices'], map(repr, expected),
ordered=False,
)
def test_filter_none(self):
self._test_filters([(self.oc1, 'none')], [self.er1, self.er2])
def test_filter_yes(self):
self._test_filters([(self.oc1, 'yes')], [self.er1])
def test_filter_no(self):
self._test_filters([(self.oc1, 'no')], [self.er2])