From 80ca35a4c0ff1f69916037ffba7713c4a57e5532 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Mon, 22 Jan 2018 14:49:02 +0100 Subject: [PATCH 1/4] Add helper to check HttpResponse containing csv --- shared/tests/testcases.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/shared/tests/testcases.py b/shared/tests/testcases.py index 15792383..cac036af 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 @@ -92,6 +93,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])) + class ViewTestCaseMixin(TestCaseMixin): """ From f371606cdbd98298383a038879b6f51d5242b447 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Mon, 22 Jan 2018 14:58:38 +0100 Subject: [PATCH 2/4] cof -- Add tests for export views --- gestioncof/tests/test_views.py | 159 +++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 gestioncof/tests/test_views.py diff --git a/gestioncof/tests/test_views.py b/gestioncof/tests/test_views.py new file mode 100644 index 00000000..8cd323cc --- /dev/null +++ b/gestioncof/tests/test_views.py @@ -0,0 +1,159 @@ +import csv + +from django.test import TestCase + +from gestioncof.models import Event +from gestioncof.tests.testcases import ViewTestCaseMixin + +from .utils import create_user + + +class ExportMembersViewTests(ViewTestCaseMixin, TestCase): + url_name = 'gestioncof.views.export_members' + 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])) + self.assertListEqual(data, [ + [ + str(u1.pk), 'member', 'first', 'last', 'user@mail.net', + '0123456789', '1A', 'Dept', 'normalien', + ], + [str(u2.pk), 'staff', '', '', '', '', '1A', '', 'normalien'], + ]) + + +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 = 'gestioncof.views.export_mega' + 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 = 'gestioncof.views.export_mega_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 = 'gestioncof.views.export_mega_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 = 'gestioncof.views.export_mega_remarksonly' + 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', + ], + ]) From a813507ddd6218e51769f8a0b8e17ddc7f04115f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Mon, 22 Jan 2018 14:59:57 +0100 Subject: [PATCH 3/4] Name urls of export views (cof members, mega) --- gestioncof/templates/gestioncof/utile_cof.html | 8 ++++---- gestioncof/tests/test_views.py | 10 +++++----- gestioncof/urls.py | 15 ++++++++++----- 3 files changed, 19 insertions(+), 14 deletions(-) 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 8cd323cc..02288e2b 100644 --- a/gestioncof/tests/test_views.py +++ b/gestioncof/tests/test_views.py @@ -9,7 +9,7 @@ from .utils import create_user class ExportMembersViewTests(ViewTestCaseMixin, TestCase): - url_name = 'gestioncof.views.export_members' + url_name = 'cof.membres_export' url_expected = '/export/members' auth_user = 'staff' @@ -83,7 +83,7 @@ class MegaHelpers: class ExportMegaViewTests(MegaHelpers, ViewTestCaseMixin, TestCase): - url_name = 'gestioncof.views.export_mega' + url_name = 'cof.mega_export' url_expected = '/export/mega' auth_user = 'staff' @@ -103,7 +103,7 @@ class ExportMegaViewTests(MegaHelpers, ViewTestCaseMixin, TestCase): class ExportMegaOrgasViewTests(MegaHelpers, ViewTestCaseMixin, TestCase): - url_name = 'gestioncof.views.export_mega_orgas' + url_name = 'cof.mega_export_orgas' url_expected = '/export/mega/orgas' auth_user = 'staff' @@ -124,7 +124,7 @@ class ExportMegaOrgasViewTests(MegaHelpers, ViewTestCaseMixin, TestCase): class ExportMegaParticipantsViewTests( MegaHelpers, ViewTestCaseMixin, TestCase): - url_name = 'gestioncof.views.export_mega_participants' + url_name = 'cof.mega_export_participants' url_expected = '/export/mega/participants' auth_user = 'staff' @@ -141,7 +141,7 @@ class ExportMegaParticipantsViewTests( class ExportMegaRemarksViewTests( MegaHelpers, ViewTestCaseMixin, TestCase): - url_name = 'gestioncof.views.export_mega_remarksonly' + url_name = 'cof.mega_export_remarks' url_expected = '/export/mega/avecremarques' auth_user = 'staff' diff --git a/gestioncof/urls.py b/gestioncof/urls.py index 2be609b3..ef3b4190 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 = [ From 6328cdaa1965a6ebf0e5381b157a30f5354dfbfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Sat, 7 Apr 2018 12:05:16 +0200 Subject: [PATCH 4/4] Tests: the order of our csv files is not relevant --- gestioncof/tests/test_views.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/gestioncof/tests/test_views.py b/gestioncof/tests/test_views.py index 1425353e..3b4a832b 100644 --- a/gestioncof/tests/test_views.py +++ b/gestioncof/tests/test_views.py @@ -38,13 +38,18 @@ class ExportMembersViewTests(ViewTestCaseMixin, TestCase): self.assertEqual(r.status_code, 200) data = list(csv.reader(r.content.decode('utf-8').split('\n')[:-1])) - self.assertListEqual(data, [ + 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: