|
@@ -3,6 +3,7 @@ import json
|
|
|
import warnings
|
|
|
|
|
|
from django.core import mail
|
|
|
+from django.core.management import call_command
|
|
|
from django.core.signing import BadSignature
|
|
|
from django.contrib.auth.models import User
|
|
|
from django.test import TestCase, Client, override_settings
|
|
@@ -89,6 +90,50 @@ class TestContribPrivacy(TestCase):
|
|
|
self.assertEqual(c.get_public_field('name'), None)
|
|
|
|
|
|
|
|
|
+class TestContribQuerySet(TestCase):
|
|
|
+ def test_expired(self):
|
|
|
+ with freeze_time('12-11-2100', tz_offset=0):
|
|
|
+ Contrib.objects.create(
|
|
|
+ name='foo', orientations=['S'],
|
|
|
+ contrib_type=Contrib.CONTRIB_CONNECT,
|
|
|
+ latitude=0.5, longitude=0.5)
|
|
|
+ # one year and one month later
|
|
|
+ with freeze_time('12-12-2101', tz_offset=0):
|
|
|
+ Contrib.objects.create(
|
|
|
+ name='bar', orientations=['S'],
|
|
|
+ contrib_type=Contrib.CONTRIB_CONNECT,
|
|
|
+ latitude=0.5, longitude=0.5)
|
|
|
+
|
|
|
+ expired = Contrib.objects.expired()
|
|
|
+ self.assertEqual(expired.count(), 1)
|
|
|
+ self.assertEqual(expired.first().name, 'foo')
|
|
|
+
|
|
|
+ def test_expired_in_days(self):
|
|
|
+ Contrib.objects.create(
|
|
|
+ name='foo', orientations=['S'],
|
|
|
+ contrib_type=Contrib.CONTRIB_CONNECT,
|
|
|
+ latitude=0.5, longitude=0.5)
|
|
|
+
|
|
|
+ self.assertEqual(Contrib.objects.expired_in_days(0).count(), 0)
|
|
|
+ self.assertEqual(Contrib.objects.expired_in_days(366).count(), 1)
|
|
|
+
|
|
|
+ def test_expires_in_days(self):
|
|
|
+ with freeze_time('12-11-2101 12:00', tz_offset=0):
|
|
|
+ Contrib.objects.create(
|
|
|
+ name='foo', orientations=['S'],
|
|
|
+ contrib_type=Contrib.CONTRIB_CONNECT,
|
|
|
+ latitude=0.5, longitude=0.5)
|
|
|
+ self.assertEqual(Contrib.objects.expires_in_days(364).count(), 0)
|
|
|
+ self.assertEqual(Contrib.objects.expires_in_days(365).count(), 1)
|
|
|
+ self.assertEqual(Contrib.objects.expires_in_days(366).count(), 0)
|
|
|
+
|
|
|
+ # One year, one hour and two minutes later
|
|
|
+ # (check that minutes/hours are ignored)
|
|
|
+ with freeze_time('12-11-2101 13:02', tz_offset=0):
|
|
|
+ self.assertEqual(Contrib.objects.expires_in_days(365).count(), 1)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
class TestViews(APITestCase):
|
|
|
def mk_contrib_post_data(self, *args, **kwargs):
|
|
|
post_data = {
|
|
@@ -354,3 +399,55 @@ class ContribTokenManagerTests(TestCase):
|
|
|
self.assertEqual(
|
|
|
manager.get_instance_if_allowed(token, contrib.pk),
|
|
|
contrib)
|
|
|
+
|
|
|
+
|
|
|
+class TestManagementCommands(TestCase):
|
|
|
+ def setUp(self):
|
|
|
+ contrib = Contrib.objects.create(
|
|
|
+ name='John',
|
|
|
+ email='foo@example.com',
|
|
|
+ contrib_type=Contrib.CONTRIB_CONNECT,
|
|
|
+ latitude=0.5,
|
|
|
+ longitude=0.5,
|
|
|
+ )
|
|
|
+ contrib.expiration_date = datetime.datetime(
|
|
|
+ 2010, 10, 10, 1, 0, tzinfo=pytz.utc)
|
|
|
+ contrib.save()
|
|
|
+
|
|
|
+ @override_settings(DATA_EXPIRATION_REMINDERS=[10])
|
|
|
+ def test_send_expiration_reminders(self):
|
|
|
+ # 11 days before (should not send)
|
|
|
+ with freeze_time('29-09-2010', tz_offset=0):
|
|
|
+ call_command('send_expiration_reminders')
|
|
|
+ self.assertEqual(Contrib.objects.count(), 1)
|
|
|
+
|
|
|
+ # 9 days before (should not send)
|
|
|
+ with freeze_time('01-10-2010', tz_offset=0):
|
|
|
+ call_command('send_expiration_reminders')
|
|
|
+ self.assertEqual(Contrib.objects.count(), 1)
|
|
|
+
|
|
|
+ # 10 days before (should send)
|
|
|
+ with freeze_time('30-09-2010', tz_offset=0):
|
|
|
+ call_command('send_expiration_reminders', '--dry-run')
|
|
|
+ self.assertEqual(Contrib.objects.count(), 1)
|
|
|
+ call_command('send_expiration_reminders')
|
|
|
+ self.assertEqual(len(mail.outbox), 1)
|
|
|
+
|
|
|
+ def test_delete_expired_contribs(self):
|
|
|
+ # 1 days before expiration
|
|
|
+ with freeze_time('09-09-2010', tz_offset=0):
|
|
|
+ call_command('delete_expired_contribs')
|
|
|
+ self.assertEqual(Contrib.objects.count(), 1)
|
|
|
+
|
|
|
+ # expiration day
|
|
|
+ with freeze_time('10-10-2010 23:59', tz_offset=0):
|
|
|
+ call_command('delete_expired_contribs', '--dry-run')
|
|
|
+ self.assertEqual(Contrib.objects.count(), 1)
|
|
|
+ call_command('delete_expired_contribs')
|
|
|
+ self.assertEqual(Contrib.objects.count(), 0)
|
|
|
+
|
|
|
+ self.setUp()
|
|
|
+ # 1 day after expiration
|
|
|
+ with freeze_time('11-10-2010', tz_offset=0):
|
|
|
+ call_command('delete_expired_contribs')
|
|
|
+ self.assertEqual(Contrib.objects.count(), 0)
|