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
|
|
|
|
|
2018-04-06 00:16:08 +02:00
|
|
|
from gestioncof.models import Event, Survey, SurveyAnswer
|
2018-01-19 18:36:03 +01:00
|
|
|
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])
|
2018-04-06 00:16:08 +02:00
|
|
|
|
|
|
|
|
2018-01-19 17:57:43 +01:00
|
|
|
class SurveyViewTests(ViewTestCaseMixin, TestCase):
|
|
|
|
url_name = 'survey.details'
|
|
|
|
http_methods = ['GET', 'POST']
|
|
|
|
|
|
|
|
auth_user = 'user'
|
|
|
|
auth_forbidden = [None]
|
|
|
|
|
|
|
|
post_expected_message = Message(messages.SUCCESS, (
|
|
|
|
"Votre réponse a bien été enregistrée ! Vous pouvez cependant la "
|
|
|
|
"modifier jusqu'à la fin du sondage."
|
|
|
|
))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def url_kwargs(self):
|
|
|
|
return {'survey_id': self.s.pk}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def url_expected(self):
|
|
|
|
return '/survey/{}'.format(self.s.pk)
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super().setUp()
|
|
|
|
|
|
|
|
self.s = Survey.objects.create(title='Title')
|
|
|
|
|
|
|
|
self.q1 = self.s.questions.create(question='Question 1 ?')
|
|
|
|
self.q2 = self.s.questions.create(
|
|
|
|
question='Question 2 ?',
|
|
|
|
multi_answers=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.qa1 = self.q1.answers.create(answer='Q1 - Answer 1')
|
|
|
|
self.qa2 = self.q1.answers.create(answer='Q1 - Answer 2')
|
|
|
|
self.qa3 = self.q2.answers.create(answer='Q2 - Answer 1')
|
|
|
|
self.qa4 = self.q2.answers.create(answer='Q2 - Answer 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, {
|
|
|
|
'question_{}'.format(self.q1.pk): [str(self.qa1.pk)],
|
|
|
|
'question_{}'.format(self.q2.pk): [
|
|
|
|
str(self.qa3.pk), str(self.qa4.pk),
|
|
|
|
],
|
|
|
|
})
|
|
|
|
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
self.assertIn(self.post_expected_message, get_messages(r.wsgi_request))
|
|
|
|
|
|
|
|
a = self.s.surveyanswer_set.get(user=self.users['user'])
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
a.answers.all(), map(repr, [self.qa1, self.qa3, self.qa4]),
|
|
|
|
ordered=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_post_edit(self):
|
|
|
|
a = self.s.surveyanswer_set.create(user=self.users['user'])
|
|
|
|
a.answers.add(self.qa1, self.qa1, self.qa4)
|
|
|
|
|
|
|
|
r = self.client.post(self.url, {
|
|
|
|
'question_{}'.format(self.q1.pk): [],
|
|
|
|
'question_{}'.format(self.q2.pk): [str(self.qa3.pk)],
|
|
|
|
})
|
|
|
|
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
self.assertIn(self.post_expected_message, get_messages(r.wsgi_request))
|
|
|
|
|
|
|
|
a.refresh_from_db()
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
a.answers.all(), map(repr, [self.qa3]),
|
|
|
|
ordered=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_post_delete(self):
|
|
|
|
a = self.s.surveyanswer_set.create(user=self.users['user'])
|
|
|
|
a.answers.add(self.qa1, self.qa4)
|
|
|
|
|
|
|
|
r = self.client.post(self.url, {'delete': '1'})
|
|
|
|
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
expected_message = Message(
|
|
|
|
messages.SUCCESS, "Votre réponse a bien été supprimée")
|
|
|
|
self.assertIn(expected_message, get_messages(r.wsgi_request))
|
|
|
|
|
|
|
|
with self.assertRaises(SurveyAnswer.DoesNotExist):
|
|
|
|
a.refresh_from_db()
|
|
|
|
|
|
|
|
def test_forbidden_closed(self):
|
|
|
|
self.s.survey_open = False
|
|
|
|
self.s.save()
|
|
|
|
|
|
|
|
r = self.client.get(self.url)
|
|
|
|
|
|
|
|
self.assertNotEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
def test_forbidden_old(self):
|
|
|
|
self.s.old = True
|
|
|
|
self.s.save()
|
|
|
|
|
|
|
|
r = self.client.get(self.url)
|
|
|
|
|
|
|
|
self.assertNotEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
|
|
|
|
class SurveyStatusViewTests(ViewTestCaseMixin, TestCase):
|
|
|
|
url_name = 'survey.details.status'
|
|
|
|
|
|
|
|
http_methods = ['GET', 'POST']
|
|
|
|
|
|
|
|
auth_user = 'staff'
|
|
|
|
auth_forbidden = [None, 'user', 'member']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def url_kwargs(self):
|
|
|
|
return {'survey_id': self.s.pk}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def url_expected(self):
|
|
|
|
return '/survey/{}/status'.format(self.s.pk)
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super().setUp()
|
|
|
|
|
|
|
|
self.s = Survey.objects.create(title='Title')
|
|
|
|
|
|
|
|
self.q1 = self.s.questions.create(question='Question 1 ?')
|
|
|
|
self.q2 = self.s.questions.create(
|
|
|
|
question='Question 2 ?',
|
|
|
|
multi_answers=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.qa1 = self.q1.answers.create(answer='Q1 - Answer 1')
|
|
|
|
self.qa2 = self.q1.answers.create(answer='Q1 - Answer 2')
|
|
|
|
self.qa3 = self.q2.answers.create(answer='Q2 - Answer 1')
|
|
|
|
self.qa4 = self.q2.answers.create(answer='Q2 - Answer 2')
|
|
|
|
|
|
|
|
self.a1 = self.s.surveyanswer_set.create(user=self.users['user'])
|
|
|
|
self.a1.answers.add(self.qa1)
|
|
|
|
self.a2 = self.s.surveyanswer_set.create(user=self.users['member'])
|
|
|
|
|
|
|
|
def test_get(self):
|
|
|
|
r = self.client.get(self.url)
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
def _get_qa_filter_name(self, qa):
|
|
|
|
return 'question_{}_answer_{}'.format(qa.survey_question.pk, qa.pk)
|
|
|
|
|
|
|
|
def _test_filters(self, filters, expected):
|
|
|
|
r = self.client.post(self.url, {
|
|
|
|
self._get_qa_filter_name(qa): v for qa, v in filters
|
|
|
|
})
|
|
|
|
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
r.context['user_answers'], map(repr, expected),
|
|
|
|
ordered=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_filter_none(self):
|
|
|
|
self._test_filters([(self.qa1, 'none')], [self.a1, self.a2])
|
|
|
|
|
|
|
|
def test_filter_yes(self):
|
|
|
|
self._test_filters([(self.qa1, 'yes')], [self.a1])
|
|
|
|
|
|
|
|
def test_filter_no(self):
|
|
|
|
self._test_filters([(self.qa1, 'no')], [self.a2])
|