From 80f1f9699cc2b9e0f0cea898867104cb4bc67e79 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Wed, 24 Apr 2019 13:48:32 +0200 Subject: [PATCH] clubs: move Membership form validation outside of model and fix and add tests --- club/models.py | 12 ---------- club/tests.py | 65 +++++++++++++++++++++++++++++++++++++++++++------- club/views.py | 13 ++++++---- 3 files changed, 65 insertions(+), 25 deletions(-) diff --git a/club/models.py b/club/models.py index 00c9239d..250d584f 100644 --- a/club/models.py +++ b/club/models.py @@ -276,18 +276,6 @@ class Membership(models.Model): _("description"), max_length=128, null=False, blank=True ) - def clean(self): - sub = User.objects.filter(pk=self.user.pk).first() - if sub is None or not sub.is_subscribed: - raise ValidationError(_("User must be subscriber to take part to a club")) - if ( - Membership.objects.filter(user=self.user) - .filter(club=self.club) - .filter(end_date=None) - .exists() - ): - raise ValidationError(_("User is already member of that club")) - def __str__(self): return ( self.club.name diff --git a/club/tests.py b/club/tests.py index c0409a9f..b9130bb4 100644 --- a/club/tests.py +++ b/club/tests.py @@ -44,7 +44,7 @@ class ClubTest(TestCase): self.client.login(username="root", password="plop") self.client.post( reverse("club:club_members", kwargs={"club_id": self.bdf.id}), - {"user": self.skia.id, "start_date": "12/06/2016", "role": 3}, + {"users": self.skia.id, "start_date": "12/06/2016", "role": 3}, ) response = self.client.get( reverse("club:club_members", kwargs={"club_id": self.bdf.id}) @@ -55,14 +55,37 @@ class ClubTest(TestCase): in str(response.content) ) + def test_create_add_multiple_user_to_club_from_root_ok(self): + self.client.login(username="root", password="plop") + self.client.post( + reverse("club:club_members", kwargs={"club_id": self.bdf.id}), + { + "users": "|%d|%d|" % (self.skia.id, self.rbatsbak.id), + "start_date": "12/06/2016", + "role": 3, + }, + ) + response = self.client.get( + reverse("club:club_members", kwargs={"club_id": self.bdf.id}) + ) + self.assertTrue(response.status_code == 200) + content = str(response.content) + self.assertTrue( + "S' Kia\\n Responsable info" in content + ) + self.assertTrue( + "Richard Batsbak\\n Responsable info" + in content + ) + def test_create_add_user_to_club_from_root_fail_not_subscriber(self): self.client.login(username="root", password="plop") response = self.client.post( reverse("club:club_members", kwargs={"club_id": self.bdf.id}), - {"user": self.guy.id, "start_date": "12/06/2016", "role": 3}, + {"users": self.guy.id, "start_date": "12/06/2016", "role": 3}, ) self.assertTrue(response.status_code == 200) - self.assertTrue('