clubs: tests for adding mailings

This commit is contained in:
Antoine Bartuccio 2019-05-09 18:06:11 +02:00
parent d1fb9cc4c3
commit 654099067e
Signed by: klmp200
GPG Key ID: E7245548C53F904B
5 changed files with 120 additions and 6 deletions

View File

@ -143,8 +143,6 @@ class MailingForm(forms.Form):
if cleaned_data["action"] == self.ACTION_NEW_MAILING: if cleaned_data["action"] == self.ACTION_NEW_MAILING:
self.check_required(cleaned_data, "mailing_email") self.check_required(cleaned_data, "mailing_email")
# self.check_required(cleaned_data, "mailing_club")
# self.check_required(cleaned_data, "mailing_moderator")
if cleaned_data["action"] == self.ACTION_NEW_SUBSCRIPTION: if cleaned_data["action"] == self.ACTION_NEW_SUBSCRIPTION:
self.check_required(cleaned_data, "subscription_mailing") self.check_required(cleaned_data, "subscription_mailing")

View File

@ -22,12 +22,15 @@
# #
# #
from django.conf import settings
from django.test import TestCase from django.test import TestCase
from django.utils import timezone
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.core.management import call_command from django.core.management import call_command
from core.models import User from core.models import User
from club.models import Club from club.models import Club, Membership
from club.forms import MailingForm
# Create your tests here. # Create your tests here.
@ -373,3 +376,73 @@ class ClubTest(TestCase):
"S&#39; Kia</a></td>\\n <td>Responsable info</td>" "S&#39; Kia</a></td>\\n <td>Responsable info</td>"
in content in content
) )
class MailingFormTest(TestCase):
"""Perform validation tests for MailingForm"""
def setUp(self):
call_command("populate")
self.skia = User.objects.filter(username="skia").first()
self.rbatsbak = User.objects.filter(username="rbatsbak").first()
self.guy = User.objects.filter(username="krophil").first()
self.comunity = User.objects.filter(username="comunity").first()
self.bdf = Club.objects.filter(unix_name="bdf").first()
Membership(
user=self.rbatsbak,
club=self.bdf,
start_date=timezone.now(),
role=settings.SITH_CLUB_ROLES_ID["Board member"],
).save()
def test_mailing_list_add_no_moderation(self):
# Test with Communication admin
self.client.login(username="comunity", password="plop")
self.client.post(
reverse("club:mailing", kwargs={"club_id": self.bdf.id}),
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "foyer"},
)
response = self.client.get(
reverse("club:mailing", kwargs={"club_id": self.bdf.id})
)
self.assertContains(response, text="Liste de diffusion foyer@utbm.fr")
# Test with Root
self.client.login(username="root", password="plop")
self.client.post(
reverse("club:mailing", kwargs={"club_id": self.bdf.id}),
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "mde"},
)
response = self.client.get(
reverse("club:mailing", kwargs={"club_id": self.bdf.id})
)
self.assertContains(response, text="Liste de diffusion mde@utbm.fr")
def test_mailing_list_add_moderation(self):
self.client.login(username="rbatsbak", password="plop")
self.client.post(
reverse("club:mailing", kwargs={"club_id": self.bdf.id}),
{"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "mde"},
)
response = self.client.get(
reverse("club:mailing", kwargs={"club_id": self.bdf.id})
)
self.assertNotContains(response, text="Liste de diffusion mde@utbm.fr")
self.assertContains(
response, text="<p>Listes de diffusions en attente de modération</p>"
)
self.assertContains(response, "<li>mde@utbm.fr")
def test_mailing_list_forbidden(self):
# With anonymous user
response = self.client.get(
reverse("club:mailing", kwargs={"club_id": self.bdf.id})
)
self.assertContains(response, "", status_code=403)
# With user not in club
self.client.login(username="krophil", password="plop")
response = self.client.get(
reverse("club:mailing", kwargs={"club_id": self.bdf.id})
)
self.assertContains(response, "", status_code=403)

View File

@ -563,12 +563,18 @@ class ClubMailingView(ClubTabsMixin, CanEditMixin, DetailFormView):
""" """
Add mailing subscriptions for each user given and/or for the specified email in form Add mailing subscriptions for each user given and/or for the specified email in form
""" """
users_to_save = []
for user in cleaned_data["subscription_users"]: for user in cleaned_data["subscription_users"]:
sub = MailingSubscription( sub = MailingSubscription(
mailing=cleaned_data["subscription_mailing"], user=user mailing=cleaned_data["subscription_mailing"], user=user
) )
sub.clean() try:
sub.save() sub.clean()
except ValidationError as validation_error:
return validation_error
users_to_save.append(sub.save())
if cleaned_data["subscription_email"]: if cleaned_data["subscription_email"]:
sub = MailingSubscription( sub = MailingSubscription(
@ -582,6 +588,10 @@ class ClubMailingView(ClubTabsMixin, CanEditMixin, DetailFormView):
return validation_error return validation_error
sub.save() sub.save()
# Save users after we are sure there is no error
for user in users_to_save:
user.save()
return None return None
def remove_subscription(self, cleaned_data): def remove_subscription(self, cleaned_data):

View File

@ -837,6 +837,27 @@ Welcome to the wiki page!
krophil_profile.save() krophil_profile.save()
krophil.profile_pict = krophil_profile krophil.profile_pict = krophil_profile
krophil.save() krophil.save()
# Adding user Com Unity
comunity = User(
username="comunity",
last_name="Unity",
first_name="Com",
email="comunity@git.an",
date_of_birth="1942-06-12",
)
comunity.set_password("plop")
comunity.save()
comunity.groups = [
Group.objects.filter(name="Communication admin").first().id
]
comunity.save()
Membership(
user=comunity,
club=bar_club,
start_date=timezone.now(),
role=settings.SITH_CLUB_ROLES_ID["Board member"],
).save()
# Adding subscription for sli # Adding subscription for sli
s = Subscription( s = Subscription(
member=User.objects.filter(pk=sli.pk).first(), member=User.objects.filter(pk=sli.pk).first(),
@ -861,6 +882,18 @@ Welcome to the wiki page!
start=s.subscription_start, start=s.subscription_start,
) )
s.save() s.save()
# Com Unity
s = Subscription(
member=comunity,
subscription_type=default_subscription,
payment_method=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0][0],
)
s.subscription_start = s.compute_start()
s.subscription_end = s.compute_end(
duration=settings.SITH_SUBSCRIPTIONS[s.subscription_type]["duration"],
start=s.subscription_start,
)
s.save()
Selling( Selling(
label=dcons.name, label=dcons.name,

View File

@ -948,7 +948,7 @@ msgstr "Ce champ est obligatoire."
#: club/forms.py:162 #: club/forms.py:162
msgid "You must specify at least an user or an email address" msgid "You must specify at least an user or an email address"
msgstr "" msgstr "vous devez spécifier au moins un utilisateur ou une adresse email"
#: club/forms.py:172 counter/views.py:1481 #: club/forms.py:172 counter/views.py:1481
msgid "Begin date" msgid "Begin date"