feat: Add option to mask e-mails and phone numbers

This is a prerequisite to do tests on DEv and UAT systems
This commit is contained in:
2025-08-01 20:28:51 +02:00
parent 62491b84c1
commit 39a098af8e
3 changed files with 49 additions and 2 deletions

View File

@@ -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)

View File

@@ -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()

View File

@@ -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")