import csv import uuid from datetime import timedelta from django.contrib import messages from django.contrib.messages.api import get_messages from django.contrib.messages.storage.base import Message from django.test import Client, TestCase from django.urls import reverse from bda.models import Salle, Tirage from gestioncof.models import ( CalendarSubscription, Club, Event, Survey, SurveyAnswer ) from gestioncof.tests.testcases import ViewTestCaseMixin from .utils import create_member, create_root, create_user class HomeViewTests(ViewTestCaseMixin, TestCase): url_name = 'home' url_expected = '/' auth_user = 'user' auth_forbidden = [None] def test(self): r = self.client.get(self.url) self.assertEqual(r.status_code, 200) class ProfileViewTests(ViewTestCaseMixin, TestCase): url_name = 'profile' url_expected = '/profile' http_methods = ['GET', 'POST'] auth_user = 'member' auth_forbidden = [None, 'user'] def test_get(self): r = self.client.get(self.url) self.assertEqual(r.status_code, 200) def test_post(self): u = self.users['member'] r = self.client.post(self.url, { 'u-first_name': 'First', 'u-last_name': 'Last', 'p-phone': '', # 'mailing_cof': '1', # 'mailing_bda': '1', # 'mailing_bda_revente': '1', }) self.assertEqual(r.status_code, 200) expected_message = Message(messages.SUCCESS, ( "Votre profil a été mis à jour avec succès !" )) self.assertIn(expected_message, get_messages(r.wsgi_request)) u.refresh_from_db() self.assertEqual(u.first_name, 'First') self.assertEqual(u.last_name, 'Last') self.assertFalse(u.profile.mailing_cof) self.assertFalse(u.profile.mailing_bda) self.assertFalse(u.profile.mailing_bda_revente) class UtilsViewTests(ViewTestCaseMixin, TestCase): url_name = 'utile_cof' url_expected = '/utile_cof' auth_user = 'staff' auth_forbidden = [None, 'user', 'member'] def test(self): r = self.client.get(self.url) self.assertEqual(r.status_code, 200) class MailingListDiffCof(ViewTestCaseMixin, TestCase): url_name = 'ml_diffcof' url_expected = '/utile_cof/diff_cof' auth_user = 'staff' auth_forbidden = [None, 'user', 'member'] def setUp(self): super().setUp() self.u1 = create_member('u1', attrs={'mailing_cof': True}) self.u2 = create_member('u2', attrs={'mailing_cof': False}) self.u3 = create_user('u3', attrs={'mailing_cof': True}) def test(self): r = self.client.get(self.url) self.assertEqual(r.status_code, 200) self.assertEqual(r.context['personnes'].get(), self.u1.profile) class ConfigUpdateViewTests(ViewTestCaseMixin, TestCase): url_name = 'config.edit' url_expected = '/config' http_methods = ['GET', 'POST'] auth_user = 'root' auth_forbidden = [None, 'user', 'member', 'staff'] def get_users_extra(self): return { 'root': create_root('root'), } def test_get(self): r = self.client.get(self.url) self.assertEqual(r.status_code, 200) def test_post(self): r = self.client.post(self.url, { 'gestion_banner': 'Announcement !', }) self.assertRedirects(r, reverse('home')) class UserAutocompleteViewTests(ViewTestCaseMixin, TestCase): url_name = 'cof-user-autocomplete' url_expected = '/user/autocomplete' auth_user = 'staff' auth_forbidden = [None, 'user', 'member'] def test(self): r = self.client.get(self.url, {'q': 'user'}) self.assertEqual(r.status_code, 200) class ExportMembersViewTests(ViewTestCaseMixin, TestCase): url_name = 'cof.membres_export' url_expected = '/export/members' auth_user = 'staff' auth_forbidden = [None, 'user', 'member'] def test(self): u1, u2 = self.users['member'], self.users['staff'] u1.first_name = 'first' u1.last_name = 'last' u1.email = 'user@mail.net' u1.save() u1.profile.phone = '0123456789' u1.profile.departement = 'Dept' u1.profile.save() r = self.client.get(self.url) self.assertEqual(r.status_code, 200) data = list(csv.reader(r.content.decode('utf-8').split('\n')[:-1])) expected = [ [ str(u1.pk), 'member', 'first', 'last', 'user@mail.net', '0123456789', '1A', 'Dept', 'normalien', ], [str(u2.pk), 'staff', '', '', '', '', '1A', '', 'normalien'], ] # Sort before checking equality, the order of the output of csv.reader # does not seem deterministic expected.sort(key=lambda row: int(row[0])) data.sort(key=lambda row: int(row[0])) self.assertListEqual(data, expected) class MegaHelpers: def setUp(self): super().setUp() u1 = create_user('u1') u1.first_name = 'first' u1.last_name = 'last' u1.email = 'user@mail.net' u1.save() u1.profile.phone = '0123456789' u1.profile.departement = 'Dept' u1.profile.comments = 'profile.comments' u1.profile.save() u2 = create_user('u2') u2.profile.save() m = Event.objects.create(title='MEGA 2017') cf1 = m.commentfields.create(name='Commentaire') cf2 = m.commentfields.create( name='Comment Field 2', fieldtype='char', ) option_type = m.options.create(name='Conscrit/Orga ?') choice_orga = option_type.choices.create(value='Orga') choice_conscrit = option_type.choices.create(value='Conscrit') mr1 = m.eventregistration_set.create(user=u1) mr1.options.add(choice_orga) mr1.comments.create(commentfield=cf1, content='Comment 1') mr1.comments.create(commentfield=cf2, content='Comment 2') mr2 = m.eventregistration_set.create(user=u2) mr2.options.add(choice_conscrit) self.u1 = u1 self.u2 = u2 self.m = m self.choice_orga = choice_orga self.choice_conscrit = choice_conscrit class ExportMegaViewTests(MegaHelpers, ViewTestCaseMixin, TestCase): url_name = 'cof.mega_export' url_expected = '/export/mega' auth_user = 'staff' auth_forbidden = [None, 'user', 'member'] def test(self): r = self.client.get(self.url) self.assertEqual(r.status_code, 200) self.assertListEqual(self.load_from_csv_response(r), [ [ 'u1', 'first', 'last', 'user@mail.net', '0123456789', str(self.u1.pk), 'profile.comments', 'Comment 1---Comment 2', ], ['u2', '', '', '', '', str(self.u2.pk), '', ''], ]) class ExportMegaOrgasViewTests(MegaHelpers, ViewTestCaseMixin, TestCase): url_name = 'cof.mega_export_orgas' url_expected = '/export/mega/orgas' auth_user = 'staff' auth_forbidden = [None, 'user', 'member'] def test(self): r = self.client.get(self.url) self.assertEqual(r.status_code, 200) self.assertListEqual(self.load_from_csv_response(r), [ [ 'u1', 'first', 'last', 'user@mail.net', '0123456789', str(self.u1.pk), 'profile.comments', 'Comment 1---Comment 2', ], ]) class ExportMegaParticipantsViewTests( MegaHelpers, ViewTestCaseMixin, TestCase): url_name = 'cof.mega_export_participants' url_expected = '/export/mega/participants' auth_user = 'staff' auth_forbidden = [None, 'user', 'member'] def test(self): r = self.client.get(self.url) self.assertEqual(r.status_code, 200) self.assertListEqual(self.load_from_csv_response(r), [ ['u2', '', '', '', '', str(self.u2.pk), '', ''], ]) class ExportMegaRemarksViewTests( MegaHelpers, ViewTestCaseMixin, TestCase): url_name = 'cof.mega_export_remarks' url_expected = '/export/mega/avecremarques' auth_user = 'staff' auth_forbidden = [None, 'user', 'member'] def test(self): r = self.client.get(self.url) self.assertEqual(r.status_code, 200) self.assertListEqual(self.load_from_csv_response(r), [ [ 'u1', 'first', 'last', 'user@mail.net', '0123456789', str(self.u1.pk), 'profile.comments', 'Comment 1', ], ]) class ClubListViewTests(ViewTestCaseMixin, TestCase): url_name = 'liste-clubs' url_expected = '/clubs/liste' auth_user = 'member' auth_forbidden = [None, 'user'] def setUp(self): super().setUp() self.c1 = Club.objects.create(name='Club1') self.c2 = Club.objects.create(name='Club2') m = self.users['member'] self.c1.membres.add(m) self.c1.respos.add(m) def test_as_member(self): r = self.client.get(self.url) self.assertEqual(r.status_code, 200) self.assertEqual(r.context['owned_clubs'].get(), self.c1) self.assertEqual(r.context['other_clubs'].get(), self.c2) def test_as_staff(self): u = self.users['staff'] c = Client() c.force_login(u) r = c.get(self.url) self.assertEqual(r.status_code, 200) self.assertQuerysetEqual( r.context['owned_clubs'], map(repr, [self.c1, self.c2]), ordered=False, ) class ClubMembersViewTests(ViewTestCaseMixin, TestCase): url_name = 'membres-club' auth_user = 'staff' auth_forbidden = [None, 'user', 'member'] @property def url_kwargs(self): return {'name': self.c.name} @property def url_expected(self): return '/clubs/membres/{}'.format(self.c.name) def setUp(self): super().setUp() self.u1 = create_user('u1') self.u2 = create_user('u2') self.c = Club.objects.create(name='Club') self.c.membres.add(self.u1, self.u2) self.c.respos.add(self.u1) def test_as_staff(self): r = self.client.get(self.url) self.assertEqual(r.status_code, 200) self.assertEqual(r.context['members_no_respo'].get(), self.u2) def test_as_respo(self): u = self.users['user'] self.c.respos.add(u) c = Client() c.force_login(u) r = c.get(self.url) self.assertEqual(r.status_code, 200) class ClubChangeRespoViewTests(ViewTestCaseMixin, TestCase): url_name = 'change-respo' auth_user = 'staff' auth_forbidden = [None, 'user', 'member'] @property def url_kwargs(self): return {'club_name': self.c.name, 'user_id': self.users['user'].pk} @property def url_expected(self): return '/clubs/change_respo/{}/{}'.format( self.c.name, self.users['user'].pk, ) def setUp(self): super().setUp() self.c = Club.objects.create(name='Club') def test(self): u = self.users['user'] expected_redirect = reverse('membres-club', kwargs={ 'name': self.c.name, }) self.c.membres.add(u) r = self.client.get(self.url) self.assertRedirects(r, expected_redirect) self.assertIn(u, self.c.respos.all()) self.client.get(self.url) self.assertNotIn(u, self.c.respos.all()) class CalendarViewTests(ViewTestCaseMixin, TestCase): url_name = 'calendar' url_expected = '/calendar/subscription' auth_user = 'member' auth_forbidden = [None, 'user'] post_expected_message = Message( messages.SUCCESS, "Calendrier mis à jour avec succès.") 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, { 'subscribe_to_events': True, 'subscribe_to_my_shows': True, 'other_shows': [], }) self.assertEqual(r.status_code, 200) self.assertIn(self.post_expected_message, get_messages(r.wsgi_request)) cs = self.users['member'].calendarsubscription self.assertTrue(cs.subscribe_to_events) self.assertTrue(cs.subscribe_to_my_shows) def test_post_edit(self): u = self.users['member'] token = uuid.uuid4() cs = CalendarSubscription.objects.create(token=token, user=u) r = self.client.post(self.url, { 'other_shows': [], }) self.assertEqual(r.status_code, 200) self.assertIn(self.post_expected_message, get_messages(r.wsgi_request)) cs.refresh_from_db() self.assertEqual(cs.token, token) self.assertFalse(cs.subscribe_to_events) self.assertFalse(cs.subscribe_to_my_shows) def test_post_other_shows(self): t = Tirage.objects.create( ouverture=self.now, fermeture=self.now, active=True, ) l = Salle.objects.create() s = t.spectacle_set.create( date=self.now, price=3.5, slots=20, location=l, listing=True) r = self.client.post(self.url, {'other_shows': [str(s.pk)]}) self.assertEqual(r.status_code, 200) class CalendarICSViewTests(ViewTestCaseMixin, TestCase): url_name = 'calendar.ics' auth_user = None auth_forbidden = [] @property def url_kwargs(self): return {'token': self.token} @property def url_expected(self): return '/calendar/{}/calendar.ics'.format(self.token) def setUp(self): super().setUp() self.token = uuid.uuid4() self.t = Tirage.objects.create( ouverture=self.now, fermeture=self.now, active=True, ) l = Salle.objects.create(name='Location') self.s1 = self.t.spectacle_set.create( price=1, slots=10, location=l, listing=True, title='Spectacle 1', date=self.now + timedelta(days=1), ) self.s2 = self.t.spectacle_set.create( price=2, slots=20, location=l, listing=True, title='Spectacle 2', date=self.now + timedelta(days=2), ) self.s3 = self.t.spectacle_set.create( price=3, slots=30, location=l, listing=True, title='Spectacle 3', date=self.now + timedelta(days=3), ) def test(self): u = self.users['user'] p = u.participant_set.create(tirage=self.t) p.attribution_set.create(spectacle=self.s1) self.cs = CalendarSubscription.objects.create( user=u, token=self.token, subscribe_to_my_shows=True, subscribe_to_events=True, ) self.cs.other_shows.add(self.s2) r = self.client.get(self.url) def get_dt_from_ical(v): return v.dt self.assertCalEqual(r.content.decode('utf-8'), [ { 'summary': 'Spectacle 1', 'dtstart': (get_dt_from_ical, ( (self.now + timedelta(days=1)).replace(microsecond=0) )), 'dtend': (get_dt_from_ical, ( (self.now + timedelta(days=1, hours=2)).replace( microsecond=0) )), 'location': 'Location', 'uid': 'show-{}-{}@example.com'.format(self.s1.pk, self.t.pk), }, { 'summary': 'Spectacle 2', 'dtstart': (get_dt_from_ical, ( (self.now + timedelta(days=2)).replace(microsecond=0) )), 'dtend': (get_dt_from_ical, ( (self.now + timedelta(days=2, hours=2)).replace( microsecond=0) )), 'location': 'Location', 'uid': 'show-{}-{}@example.com'.format(self.s2.pk, self.t.pk), }, ]) 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]) 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])