From 2bf4137123a9b2d23606a2052cfddc26b9bbe5be Mon Sep 17 00:00:00 2001 From: vincent porte Date: Thu, 19 Dec 2024 11:46:37 +0100 Subject: [PATCH] cleanup brevo routes management --- config/settings/base.py | 4 ++-- lacommunaute/notification/emails.py | 8 ++++++-- lacommunaute/notification/tests/tests_emails.py | 6 +++--- lacommunaute/notification/tests/tests_tasks.py | 5 +++-- lacommunaute/users/tests/tests_views.py | 3 ++- 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/config/settings/base.py b/config/settings/base.py index c78061c5..4c1c9725 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -360,8 +360,8 @@ # SENDINBLUE # --------------------------------------- SIB_URL = os.getenv("SIB_URL", "http://test.com") -SIB_SMTP_URL = os.path.join(SIB_URL, "smtp/email") -SIB_CONTACTS_URL = os.path.join(SIB_URL, "contacts/import") +SIB_SMTP_ROUTE = "smtp/email" +SIB_CONTACTS_ROUTE = "contacts/import" SIB_API_KEY = os.getenv("SIB_API_KEY", "set-sib-api-key") DEFAULT_FROM_EMAIL = os.getenv("DEFAULT_FROM_EMAIL", "noreply@inclusion.gouv.fr") diff --git a/lacommunaute/notification/emails.py b/lacommunaute/notification/emails.py index 415cde02..0485a107 100644 --- a/lacommunaute/notification/emails.py +++ b/lacommunaute/notification/emails.py @@ -1,4 +1,5 @@ import logging +from urllib.parse import urljoin import httpx from django.conf import settings @@ -9,6 +10,9 @@ logger = logging.getLogger(__name__) +SIB_SMTP_URL = urljoin(settings.SIB_URL, settings.SIB_SMTP_ROUTE) +SIB_CONTACTS_URL = urljoin(settings.SIB_URL, settings.SIB_CONTACTS_ROUTE) + def send_email(to, params, template_id, kind, bcc=None): headers = {"api-key": settings.SIB_API_KEY, "Content-Type": "application/json", "Accept": "application/json"} @@ -25,7 +29,7 @@ def send_email(to, params, template_id, kind, bcc=None): # We don't want to send emails in debug mode, payload is saved in the database response = httpx.Response(200, json={"message": "OK"}) else: - response = httpx.post(settings.SIB_SMTP_URL, headers=headers, json=payload) + response = httpx.post(SIB_SMTP_URL, headers=headers, json=payload) EmailSentTrack.objects.create( status_code=response.status_code, @@ -47,7 +51,7 @@ def bulk_send_user_to_list(users, list_id): "emptyContactsAttributes": True, } headers = {"accept": "application/json", "content-type": "application/json", "api-key": settings.SIB_API_KEY} - response = httpx.post(settings.SIB_CONTACTS_URL, headers=headers, json=payload) + response = httpx.post(SIB_CONTACTS_URL, headers=headers, json=payload) EmailSentTrack.objects.create( status_code=response.status_code, response=response.text, datas=payload, kind=EmailSentTrackKind.ONBOARDING diff --git a/lacommunaute/notification/tests/tests_emails.py b/lacommunaute/notification/tests/tests_emails.py index ea9c52cc..8d959047 100644 --- a/lacommunaute/notification/tests/tests_emails.py +++ b/lacommunaute/notification/tests/tests_emails.py @@ -6,7 +6,7 @@ from django.test import TestCase from faker import Faker -from lacommunaute.notification.emails import bulk_send_user_to_list, send_email +from lacommunaute.notification.emails import SIB_CONTACTS_URL, SIB_SMTP_URL, bulk_send_user_to_list, send_email from lacommunaute.notification.models import EmailSentTrack from lacommunaute.users.factories import UserFactory @@ -17,7 +17,7 @@ class SendEmailTestCase(TestCase): @classmethod def setUpTestData(cls): - respx.post(settings.SIB_SMTP_URL).mock(return_value=httpx.Response(200, json={"message": "OK"})) + respx.post(SIB_SMTP_URL).mock(return_value=httpx.Response(200, json={"message": "OK"})) cls.to = [{"email": faker.email()}] cls.params = faker.text() cls.template_id = faker.random_int() @@ -52,7 +52,7 @@ def test_send_email_with_bcc(self): class BulkSendUserToListTestCase(TestCase): @classmethod def setUpTestData(cls): - respx.post(settings.SIB_CONTACTS_URL).mock(return_value=httpx.Response(200, json={"message": "OK"})) + respx.post(SIB_CONTACTS_URL).mock(return_value=httpx.Response(200, json={"message": "OK"})) @respx.mock def test_bulk_send_user_to_list(self): diff --git a/lacommunaute/notification/tests/tests_tasks.py b/lacommunaute/notification/tests/tests_tasks.py index 24ed8429..b687300b 100644 --- a/lacommunaute/notification/tests/tests_tasks.py +++ b/lacommunaute/notification/tests/tests_tasks.py @@ -12,6 +12,7 @@ TopicFactory, ) from lacommunaute.forum_member.shortcuts import get_forum_member_display_name +from lacommunaute.notification.emails import SIB_CONTACTS_URL, SIB_SMTP_URL from lacommunaute.notification.enums import NotificationDelay from lacommunaute.notification.factories import NotificationFactory from lacommunaute.notification.models import EmailSentTrack @@ -30,7 +31,7 @@ @pytest.fixture(name="mock_respx_post_to_sib_smtp_url") def mock_respx_post_to_sib_smtp_url_fixture(): with respx.mock: - respx.post(settings.SIB_SMTP_URL).mock(return_value=httpx.Response(200, json={"message": "OK"})) + respx.post(SIB_SMTP_URL).mock(return_value=httpx.Response(200, json={"message": "OK"})) yield @@ -108,7 +109,7 @@ def test_num_queries(self, db, django_assert_num_queries, mock_respx_post_to_sib class AddUserToListWhenRegister(TestCase): def setUp(self): super().setUp() - respx.post(settings.SIB_CONTACTS_URL).mock(return_value=httpx.Response(200, json={"message": "OK"})) + respx.post(SIB_CONTACTS_URL).mock(return_value=httpx.Response(200, json={"message": "OK"})) @respx.mock def test_add_user_to_list_when_register(self): diff --git a/lacommunaute/users/tests/tests_views.py b/lacommunaute/users/tests/tests_views.py index beeb4c7f..71097384 100644 --- a/lacommunaute/users/tests/tests_views.py +++ b/lacommunaute/users/tests/tests_views.py @@ -18,6 +18,7 @@ from pytest_django.asserts import assertContains from lacommunaute.forum_member.models import ForumProfile +from lacommunaute.notification.emails import SIB_SMTP_URL from lacommunaute.users.enums import IdentityProvider from lacommunaute.users.factories import UserFactory from lacommunaute.users.models import User @@ -33,7 +34,7 @@ @pytest.fixture(name="mock_respx_post_to_sib_smtp_url") def mock_respx_post_to_sib_smtp_url_fixture(): with respx.mock: - respx.post(settings.SIB_SMTP_URL).mock(return_value=httpx.Response(200, json={"message": "OK"})) + respx.post(SIB_SMTP_URL).mock(return_value=httpx.Response(200, json={"message": "OK"})) yield