|
@@ -2,15 +2,18 @@
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
import os
|
|
|
-import datetime
|
|
|
+import logging
|
|
|
+import ldapdb
|
|
|
+from datetime import date
|
|
|
+from cStringIO import StringIO
|
|
|
+from dateutil.relativedelta import relativedelta
|
|
|
+
|
|
|
from django import db
|
|
|
from django.test import TestCase, Client, override_settings
|
|
|
from django.contrib.auth.models import User
|
|
|
-# from django.contrib.auth.tests.custom_user import ExtensionUser
|
|
|
+from django.core import mail, management
|
|
|
+
|
|
|
from coin.members.models import Member, MembershipFee, LdapUser
|
|
|
-import logging
|
|
|
-import ldapdb
|
|
|
-from pprint import pprint
|
|
|
|
|
|
|
|
|
class MemberTests(TestCase):
|
|
@@ -300,8 +303,8 @@ class MemberTests(TestCase):
|
|
|
last_name=last_name, username=username)
|
|
|
member.save()
|
|
|
|
|
|
- start_date = datetime.date.today()
|
|
|
- end_date = start_date + datetime.timedelta(365)
|
|
|
+ start_date = date.today()
|
|
|
+ end_date = start_date + relativedelta(years=+1)
|
|
|
|
|
|
# Créé une cotisation
|
|
|
membershipfee = MembershipFee(member=member, amount=20,
|
|
@@ -323,17 +326,17 @@ class MemberTests(TestCase):
|
|
|
last_name=last_name, username=username)
|
|
|
member.save()
|
|
|
|
|
|
- start_date = datetime.date.today()
|
|
|
- end_date = start_date + datetime.timedelta(365)
|
|
|
+ start_date = date.today()
|
|
|
+ end_date = start_date + relativedelta(years=+1)
|
|
|
|
|
|
# Test qu'un membre sans cotisation n'est pas à jour
|
|
|
self.assertEqual(member.is_paid_up(), False)
|
|
|
|
|
|
# Créé une cotisation passée
|
|
|
membershipfee = MembershipFee(member=member, amount=20,
|
|
|
- start_date=datetime.date.today() -
|
|
|
- datetime.timedelta(365),
|
|
|
- end_date=datetime.date.today() - datetime.timedelta(10))
|
|
|
+ start_date=date.today() +
|
|
|
+ relativedelta(years=-1),
|
|
|
+ end_date=date.today() + relativedelta(days=-10))
|
|
|
membershipfee.save()
|
|
|
# La cotisation s'étant terminée il y a 10 jours, il ne devrait pas
|
|
|
# être à jour de cotistion
|
|
@@ -341,9 +344,9 @@ class MemberTests(TestCase):
|
|
|
|
|
|
# Créé une cotisation actuelle
|
|
|
membershipfee = MembershipFee(member=member, amount=20,
|
|
|
- start_date=datetime.date.today() -
|
|
|
- datetime.timedelta(10),
|
|
|
- end_date=datetime.date.today() + datetime.timedelta(10))
|
|
|
+ start_date=date.today() +
|
|
|
+ relativedelta(days=-10),
|
|
|
+ end_date=date.today() + relativedelta(days=+10))
|
|
|
membershipfee.save()
|
|
|
# La cotisation se terminant dans 10 jour, il devrait être à jour
|
|
|
# de cotisation
|
|
@@ -403,6 +406,83 @@ class MemberAdminTests(TestCase):
|
|
|
member.delete()
|
|
|
|
|
|
|
|
|
+class MemberTestCallForMembershipCommand(TestCase):
|
|
|
+
|
|
|
+ def setUp(self):
|
|
|
+ # Créé un membre
|
|
|
+ self.username = MemberTestsUtils.get_random_username()
|
|
|
+ self.member = Member(first_name='Richard', last_name='Stallman',
|
|
|
+ username=self.username)
|
|
|
+ self.member.save()
|
|
|
+
|
|
|
+
|
|
|
+ def tearDown(self):
|
|
|
+ # Supprime le membre
|
|
|
+ self.member.delete()
|
|
|
+ MembershipFee.objects.all().delete()
|
|
|
+
|
|
|
+ def create_membership_fee(self, end_date):
|
|
|
+ # Créé une cotisation passée se terminant dans un mois
|
|
|
+ membershipfee = MembershipFee(member=self.member, amount=20,
|
|
|
+ start_date=end_date + relativedelta(years=-1),
|
|
|
+ end_date=end_date)
|
|
|
+ membershipfee.save()
|
|
|
+
|
|
|
+ def create_membership_fee(self, end_date):
|
|
|
+ # Créé une cotisation se terminant à la date indiquée
|
|
|
+ membershipfee = MembershipFee(member=self.member, amount=20,
|
|
|
+ start_date=end_date + relativedelta(years=-1),
|
|
|
+ end_date=end_date)
|
|
|
+ membershipfee.save()
|
|
|
+ return membershipfee
|
|
|
+
|
|
|
+ def do_test_email_sent(self, expected_emails = 1, reset_date_last_call = True):
|
|
|
+ # Vide la outbox
|
|
|
+ mail.outbox = []
|
|
|
+ # Call command
|
|
|
+ management.call_command('call_for_membership_fees', stdout=StringIO())
|
|
|
+ # Test
|
|
|
+ self.assertEqual(len(mail.outbox), expected_emails)
|
|
|
+ # Comme on utilise le même membre, on reset la date de dernier envoi
|
|
|
+ if reset_date_last_call:
|
|
|
+ self.member.date_last_call_for_membership_fees_email = None
|
|
|
+ self.member.save()
|
|
|
+
|
|
|
+ def do_test_for_a_end_date(self, end_date, expected_emails=1, reset_date_last_call = True):
|
|
|
+ # Supprimer toutes les cotisations (au cas ou)
|
|
|
+ MembershipFee.objects.all().delete()
|
|
|
+ # Créé la cotisation
|
|
|
+ membershipfee = self.create_membership_fee(end_date)
|
|
|
+ self.do_test_email_sent(expected_emails, reset_date_last_call)
|
|
|
+ membershipfee.delete()
|
|
|
+
|
|
|
+ def test_call_email_sent_at_expected_dates(self):
|
|
|
+ # 1 mois avant la fin, à la fin et chaque mois après la fin pendant 3 mois
|
|
|
+ self.do_test_for_a_end_date(date.today() + relativedelta(months=+1))
|
|
|
+ self.do_test_for_a_end_date(date.today())
|
|
|
+ self.do_test_for_a_end_date(date.today() + relativedelta(months=-1))
|
|
|
+ self.do_test_for_a_end_date(date.today() + relativedelta(months=-2))
|
|
|
+ self.do_test_for_a_end_date(date.today() + relativedelta(months=-3))
|
|
|
+
|
|
|
+ def test_call_email_not_sent_if_active_membership_fee(self):
|
|
|
+ # Créé une cotisation se terminant dans un mois
|
|
|
+ membershipfee = self.create_membership_fee(date.today() + relativedelta(months=+1))
|
|
|
+ # Un mail devrait être envoyé (ne pas vider date_last_call_for_membership_fees_email)
|
|
|
+ self.do_test_email_sent(1, False)
|
|
|
+ # Créé une cotisation enchainant et se terminant dans un an
|
|
|
+ membershipfee = self.create_membership_fee(date.today() + relativedelta(months=+1, years=+1))
|
|
|
+ # Pas de mail envoyé
|
|
|
+ self.do_test_email_sent(0)
|
|
|
+
|
|
|
+ def test_date_last_call_for_membership_fees_email(self):
|
|
|
+ # Créé une cotisation se terminant dans un mois
|
|
|
+ membershipfee = self.create_membership_fee(date.today() + relativedelta(months=+1))
|
|
|
+ # Un mail envoyé (ne pas vider date_last_call_for_membership_fees_email)
|
|
|
+ self.do_test_email_sent(1, False)
|
|
|
+ # Tente un deuxième envoi, qui devrait être à 0
|
|
|
+ self.do_test_email_sent(0)
|
|
|
+
|
|
|
+
|
|
|
class MemberTestsUtils(object):
|
|
|
|
|
|
@staticmethod
|