Files
Notfellchen/src/tests/test_admin_tasks.py

118 lines
5.3 KiB
Python

from datetime import timedelta
from django.utils import timezone
from fellchensammlung.tools.admin import get_unchecked_adoption_notices, deactivate_unchecked_adoption_notices, \
deactivate_404_adoption_notices, mask_organization_contact_data
from fellchensammlung.tools.misc import is_404
from django.test import TestCase
from model_bakery import baker
from fellchensammlung.models import AdoptionNotice, RescueOrganization
from fellchensammlung.tools.model_helpers import AdoptionNoticeStatusChoices
class DeactivationTest(TestCase):
@classmethod
def setUpTestData(cls):
now = timezone.now()
more_than_three_weeks_ago = now - timedelta(weeks=3, days=2)
less_than_three_weeks_ago = now - timedelta(weeks=1, days=2)
cls.adoption1 = baker.make(AdoptionNotice,
name="TestAdoption1",
created_at=more_than_three_weeks_ago)
cls.adoption2 = baker.make(AdoptionNotice, name="TestAdoption2")
cls.adoption3 = baker.make(AdoptionNotice,
name="TestAdoption3",
created_at=less_than_three_weeks_ago)
cls.adoption1.adoption_notice_status = AdoptionNoticeStatusChoices.Active.SEARCHING
cls.adoption1.last_checked = more_than_three_weeks_ago # Reset updated_at to simulate test conditions
cls.adoption1.save()
cls.adoption3.adoption_notice_status = AdoptionNoticeStatusChoices.Active.SEARCHING
cls.adoption3.last_checked = less_than_three_weeks_ago # Reset updated_at to simulate test conditions
cls.adoption3.save()
def test_get_unchecked_adoption_notices(self):
result = get_unchecked_adoption_notices()
self.assertIn(self.adoption1, result)
self.assertNotIn(self.adoption2, result)
self.assertNotIn(self.adoption3, result)
def test_deactivate_unchecked_adoption_notices(self):
self.assertTrue(self.adoption1.is_active)
self.assertFalse(self.adoption2.is_active)
self.assertTrue(self.adoption3.is_active)
deactivate_unchecked_adoption_notices()
self.adoption1.refresh_from_db()
self.adoption2.refresh_from_db()
self.adoption3.refresh_from_db()
self.assertFalse(self.adoption1.is_active)
self.assertFalse(self.adoption2.is_active)
self.assertTrue(self.adoption3.is_active)
class PingTest(TestCase):
@classmethod
def setUpTestData(cls):
link_active = "https://hyteck.de/"
link_inactive = "https://hyteck.de/maxwell"
now = timezone.now()
less_than_three_weeks_ago = now - timedelta(weeks=1, days=2)
cls.adoption1 = baker.make(AdoptionNotice,
name="TestAdoption1",
created_at=less_than_three_weeks_ago,
last_checked=less_than_three_weeks_ago,
further_information=link_active,
adoption_notice_status=AdoptionNoticeStatusChoices.Active.SEARCHING)
cls.adoption2 = baker.make(AdoptionNotice,
name="TestAdoption2",
created_at=less_than_three_weeks_ago,
last_checked=less_than_three_weeks_ago,
further_information=link_inactive,
adoption_notice_status=AdoptionNoticeStatusChoices.Active.SEARCHING)
cls.adoption3 = baker.make(AdoptionNotice,
name="TestAdoption3",
created_at=less_than_three_weeks_ago,
last_checked=less_than_three_weeks_ago,
further_information=None,
adoption_notice_status=AdoptionNoticeStatusChoices.Active.SEARCHING)
def test_is_404(self):
urls = [("https://hyteck.de/maxwell", True),
("https://hyteck.de", False)]
for url, expected_result in urls:
self.assertEqual(is_404(url), expected_result)
def test_deactivate_404_adoption_notices(self):
self.assertTrue(self.adoption1.is_active)
self.assertTrue(self.adoption2.is_active)
deactivate_404_adoption_notices()
self.adoption1.refresh_from_db()
self.adoption2.refresh_from_db()
self.assertTrue(self.adoption1.is_active)
self.assertFalse(self.adoption2.is_active)
class MaskingTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.rescue1 = baker.make(RescueOrganization, email="test1@example.com", )
cls.rescue2 = baker.make(RescueOrganization, email="test-2@example.com", phone_number="0123456789", )
def test_masking(self):
mask_organization_contact_data()
self.assertEqual(RescueOrganization.objects.count(), 2)
# Ensure that the rescues are pulled from the database again, otherwise this test will fail
self.rescue1.refresh_from_db()
self.rescue2.refresh_from_db()
self.assertNotEqual(self.rescue1.phone_number, "0123456789")
self.assertEqual(self.rescue1.email, "test1-example.com@example.org")
self.assertEqual(self.rescue2.email, "test-2-example.com@example.org")