diff --git a/src/fellchensammlung/management/commands/mask_contact_data.py b/src/fellchensammlung/management/commands/mask_contact_data.py new file mode 100644 index 0000000..f7accb1 --- /dev/null +++ b/src/fellchensammlung/management/commands/mask_contact_data.py @@ -0,0 +1,13 @@ +from django.core.management import BaseCommand +from fellchensammlung.tools.admin import mask_organization_contact_data + + +class Command(BaseCommand): + help = 'Mask e-mail addresses and phone numbers of organizations for testing purposes.' + + def add_arguments(self, parser): + parser.add_argument("domain", type=str) + + def handle(self, *args, **options): + domain = options["domain"] + mask_organization_contact_data(domain) diff --git a/src/fellchensammlung/tools/admin.py b/src/fellchensammlung/tools/admin.py index 11b890a..0c8073c 100644 --- a/src/fellchensammlung/tools/admin.py +++ b/src/fellchensammlung/tools/admin.py @@ -1,5 +1,6 @@ import logging +from random import randint from notfellchen import settings from django.utils import timezone from datetime import timedelta @@ -139,3 +140,18 @@ def send_test_email(email): to = email mail.send_mail(subject, plain_message, from_email, [to], html_message=html_message) + + +def mask_organization_contact_data(catchall_domain="example.org"): + """ + Masks e-mails, so they are all sent to one domain, preferably a catchall domain. + """ + rescue_orgs_with_phone_number = RescueOrganization.objects.filter(phone_number__isnull=False) + for rescue_org_with_phone_number in rescue_orgs_with_phone_number: + rescue_org_with_phone_number.phone_number = randint(100000000000, 1000000000000) + rescue_org_with_phone_number.save() + rescue_orgs_with_email = RescueOrganization.objects.filter(email__isnull=False) + for rescue_org_with_email in rescue_orgs_with_email: + rescue_org_with_email.email = f"{rescue_org_with_email.email.replace('@', '-')}@{catchall_domain}" + rescue_org_with_email.save() + diff --git a/src/tests/test_admin_tasks.py b/src/tests/test_admin_tasks.py index cd66f28..780c9f8 100644 --- a/src/tests/test_admin_tasks.py +++ b/src/tests/test_admin_tasks.py @@ -2,12 +2,12 @@ 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 + 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 +from fellchensammlung.models import AdoptionNotice, RescueOrganization class DeactivationTest(TestCase): @@ -96,3 +96,21 @@ class PingTest(TestCase): 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")