Skip to content

Commit

Permalink
Anonymize inbound data
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilouf committed Dec 31, 2024
1 parent 882b240 commit db1cb1b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions lemarche/conversations/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ATTRIBUTES_TO_SAVE_FOR_INBOUND = ["From", "To", "CC", "ReplyTo", "SentAtDate", "Attachments"]
ATTRIBUTES_TO_KEEP_FOR_INBOUND = ["SentAtDate", "Attachments"]

SOURCE_MAILJET = "MAILJET"
SOURCE_BREVO = "BREVO"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
from django.conf import settings
from django.utils import timezone

from lemarche.conversations.constants import ATTRIBUTES_TO_KEEP_FOR_INBOUND
from lemarche.conversations.models import Conversation
from lemarche.utils.commands import BaseCommand


def clean_inbound_data(inbound_data: dict) -> dict:
"""Keep only allowed data once anonymized"""
return {key: inbound_data[key] for key in ATTRIBUTES_TO_KEEP_FOR_INBOUND}


class Command(BaseCommand):
"""
Command to anonymize outdated conversations
Expand All @@ -24,7 +30,7 @@ def handle(self, *args, **options):
conversation.sender_first_name = ""
conversation.sender_last_name = ""
conversation.initial_body_message = str(len(conversation.initial_body_message))
conversation.data = [str(len(conversation.data))]
conversation.data = [clean_inbound_data(data) for data in conversation.data]
conversation.is_anonymized = True

Conversation.objects.bulk_update(
Expand Down
8 changes: 6 additions & 2 deletions lemarche/conversations/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.utils import timezone

from lemarche.conversations import constants as conversation_constants
from lemarche.conversations.constants import ATTRIBUTES_TO_KEEP_FOR_INBOUND, ATTRIBUTES_TO_SAVE_FOR_INBOUND
from lemarche.conversations.factories import ConversationFactory, TemplateTransactionalFactory
from lemarche.conversations.models import Conversation, TemplateTransactional
from lemarche.siaes.factories import SiaeFactory
Expand Down Expand Up @@ -84,11 +85,14 @@ class ConversationAnonymizationTestCase(TestCase):
"""

def setUp(self):
inbound_data = {key: "something" for key in ATTRIBUTES_TO_SAVE_FOR_INBOUND}
self.anonymized_inbound_data = {key: "something" for key in ATTRIBUTES_TO_KEEP_FOR_INBOUND}

ConversationFactory(
title="anonymized",
created_at=datetime(year=2023, month=6, day=1, tzinfo=timezone.utc),
initial_body_message="blabla",
data=["blabla", "blabla"],
data=[inbound_data, inbound_data],
)
ConversationFactory(created_at=datetime(year=2023, month=8, day=1, tzinfo=timezone.utc))

Expand All @@ -101,7 +105,7 @@ def test_anonymize_command(self):
self.assertEqual(conv_anonymized.sender_first_name, "")
self.assertEqual(conv_anonymized.sender_last_name, "")
self.assertEqual(conv_anonymized.initial_body_message, "6")
self.assertEqual(conv_anonymized.data, ["2"])
self.assertEqual(conv_anonymized.data, [self.anonymized_inbound_data, self.anonymized_inbound_data])

self.assertTrue(Conversation.objects.get(is_anonymized=False), msg="active conversation wrongly anonymised !!")

Expand Down

0 comments on commit db1cb1b

Please sign in to comment.