diff --git a/gestioncof/templates/gestioncof/utile_cof.html b/gestioncof/templates/gestioncof/utile_cof.html index ae024949..8cea33df 100644 --- a/gestioncof/templates/gestioncof/utile_cof.html +++ b/gestioncof/templates/gestioncof/utile_cof.html @@ -7,15 +7,15 @@

Liens utiles du COF

COF

Mega

Note : pour ouvrir les fichiers .csv avec Excel, il faut diff --git a/gestioncof/tests/test_views.py b/gestioncof/tests/test_views.py index 2501ed2b..3b4a832b 100644 --- a/gestioncof/tests/test_views.py +++ b/gestioncof/tests/test_views.py @@ -1,3 +1,4 @@ +import csv import uuid from datetime import timedelta @@ -16,6 +17,161 @@ from gestioncof.tests.testcases import ViewTestCaseMixin from .utils import create_user +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' diff --git a/gestioncof/urls.py b/gestioncof/urls.py index dde543a5..7dc72092 100644 --- a/gestioncof/urls.py +++ b/gestioncof/urls.py @@ -6,12 +6,17 @@ from gestioncof import views, petits_cours_views from gestioncof.decorators import buro_required export_patterns = [ - url(r'^members$', views.export_members), - url(r'^mega/avecremarques$', views.export_mega_remarksonly), - url(r'^mega/participants$', views.export_mega_participants), - url(r'^mega/orgas$', views.export_mega_orgas), + url(r'^members$', views.export_members, + name='cof.membres_export'), + url(r'^mega/avecremarques$', views.export_mega_remarksonly, + name='cof.mega_export_remarks'), + url(r'^mega/participants$', views.export_mega_participants, + name='cof.mega_export_participants'), + url(r'^mega/orgas$', views.export_mega_orgas, + name='cof.mega_export_orgas'), # url(r'^mega/(?P.+)$', views.export_mega_bytype), - url(r'^mega$', views.export_mega), + url(r'^mega$', views.export_mega, + name='cof.mega_export'), ] petitcours_patterns = [ diff --git a/shared/tests/testcases.py b/shared/tests/testcases.py index 14a5fbfe..6967d9b7 100644 --- a/shared/tests/testcases.py +++ b/shared/tests/testcases.py @@ -1,3 +1,4 @@ +import csv from unittest import mock from urllib.parse import parse_qs, urlparse @@ -94,6 +95,10 @@ class TestCaseMixin: else: self.assertEqual(actual, expected) + def load_from_csv_response(self, r): + decoded = r.content.decode('utf-8') + return list(csv.reader(decoded.split('\n')[:-1])) + def _test_event_equal(self, event, exp): for k, v_desc in exp.items(): if isinstance(v_desc, tuple):