clubs: tests for adding mailings

This commit is contained in:
2019-05-09 18:06:11 +02:00
parent d1fb9cc4c3
commit 654099067e
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:
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:
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.utils import timezone
from django.core.urlresolvers import reverse
from django.core.management import call_command
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.
@ -373,3 +376,73 @@ class ClubTest(TestCase):
"S&#39; Kia</a></td>\\n <td>Responsable info</td>"
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
"""
users_to_save = []
for user in cleaned_data["subscription_users"]:
sub = MailingSubscription(
mailing=cleaned_data["subscription_mailing"], user=user
)
sub.clean()
sub.save()
try:
sub.clean()
except ValidationError as validation_error:
return validation_error
users_to_save.append(sub.save())
if cleaned_data["subscription_email"]:
sub = MailingSubscription(
@ -582,6 +588,10 @@ class ClubMailingView(ClubTabsMixin, CanEditMixin, DetailFormView):
return validation_error
sub.save()
# Save users after we are sure there is no error
for user in users_to_save:
user.save()
return None
def remove_subscription(self, cleaned_data):