Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(Emails): ajout de tests de validation sur les TemplateTransactional #1405

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lemarche/conversations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.core.validators import ValidationError
from django.db import IntegrityError, models
from django.db.models import Count, Func, IntegerField, Q
from django.utils import timezone
Expand All @@ -13,6 +14,7 @@

from lemarche.conversations import constants as conversation_constants
from lemarche.utils.apis import api_brevo, api_mailjet
from lemarche.utils.data import add_validation_error


class ConversationQuerySet(models.QuerySet):
Expand Down Expand Up @@ -256,6 +258,35 @@ def __str__(self):
return f"{self.name} ({self.code})"
return self.name

def clean(self, *args, **kwargs):
# dict to store all ValidationErrors
validation_errors = dict()
# validation rules
if self.is_active:
if self.source == conversation_constants.SOURCE_MAILJET and not self.mailjet_id:
validation_errors = add_validation_error(
validation_errors,
"mailjet_id",
"Le mailjet_id est requis pour un template actif",
)
elif self.source == conversation_constants.SOURCE_BREVO and not self.brevo_id:
validation_errors = add_validation_error(
validation_errors,
"brevo_id",
"Le brevo_id est requis pour un template actif",
)
# return
if bool(validation_errors):
raise ValidationError(validation_errors)
super().clean(*args, **kwargs)

def save(self, *args, **kwargs):
"""
- run validations
"""
self.full_clean()
super().save(*args, **kwargs)

@property
def get_template_id(self):
if self.source and self.code:
Expand Down
39 changes: 37 additions & 2 deletions lemarche/conversations/tests.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from datetime import timedelta

from django.test import TestCase
from django.core.exceptions import ValidationError
from django.db import IntegrityError
from django.test import TestCase, TransactionTestCase
from django.utils import timezone

from lemarche.conversations import constants as conversation_constants
from lemarche.conversations.factories import ConversationFactory
from lemarche.conversations.factories import ConversationFactory, TemplateTransactionalFactory
from lemarche.conversations.models import Conversation, TemplateTransactional
from lemarche.siaes.factories import SiaeFactory

Expand Down Expand Up @@ -99,3 +101,36 @@ def test_get_template_id(self):
self.assertEqual(self.tt_inactive.get_template_id, self.tt_inactive.mailjet_id)
self.assertEqual(self.tt_active_mailjet.get_template_id, self.tt_active_mailjet.mailjet_id)
self.assertEqual(self.tt_active_brevo.get_template_id, self.tt_active_brevo.brevo_id)


class TemplateTransactionalModelSaveTest(TransactionTestCase):
@classmethod
def setUpTestData(cls):
pass

def test_template_transactional_field_rules(self):
self.assertRaises(IntegrityError, TemplateTransactionalFactory, source=None)

def test_template_transactional_validation_on_save(self):
TemplateTransactionalFactory(
mailjet_id=None, brevo_id=None, source=conversation_constants.SOURCE_BREVO, is_active=False
)
TemplateTransactionalFactory(
mailjet_id=None, brevo_id=123, source=conversation_constants.SOURCE_BREVO, is_active=True
)
self.assertRaises(
ValidationError,
TemplateTransactionalFactory,
mailjet_id=123,
brevo_id=None,
source=conversation_constants.SOURCE_BREVO,
is_active=True,
)
self.assertRaises(
ValidationError,
TemplateTransactionalFactory,
mailjet_id=None,
brevo_id=123,
source=conversation_constants.SOURCE_MAILJET,
is_active=True,
)
15 changes: 15 additions & 0 deletions lemarche/utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,18 @@ def phone_number_display(phone_number_model_field):
return phone_number_model_field.as_e164
except AttributeError:
return phone_number_model_field


def add_validation_error(dict, key, value):
"""
Build a dictionary of validation errors
{"field1": ["error1", "error2"], "field2": ["error1"]}
"""
if key not in dict:
dict[key] = value
else:
if type(dict[key]) is list:
dict[key] += [value]
if type(dict[key]) is str:
dict[key] = [dict[key], value]
return dict
4 changes: 2 additions & 2 deletions lemarche/www/dashboard_siaes/tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.test import TestCase
from django.urls import reverse

from lemarche.conversations.models import TemplateTransactional
from lemarche.conversations.factories import TemplateTransactionalFactory
from lemarche.siaes.factories import SiaeFactory
from lemarche.siaes.models import SiaeUser
from lemarche.users.factories import UserFactory
Expand All @@ -19,7 +19,7 @@ def setUpTestData(cls):
cls.siae_with_user = SiaeFactory()
cls.siae_with_user.users.add(cls.user_siae)
cls.siae_without_user = SiaeFactory()
TemplateTransactional.objects.create(code="SIAEUSERREQUEST_ASSIGNEE")
TemplateTransactionalFactory(code="SIAEUSERREQUEST_ASSIGNEE")

def test_anonymous_user_cannot_adopt_siae(self):
url = reverse("dashboard_siaes:siae_search_by_siret")
Expand Down
Loading