Skip to content

Commit

Permalink
cleanup brevo routes management
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Dec 19, 2024
1 parent e914620 commit 2bf4137
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
4 changes: 2 additions & 2 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", "[email protected]")
Expand Down
8 changes: 6 additions & 2 deletions lacommunaute/notification/emails.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from urllib.parse import urljoin

import httpx
from django.conf import settings
Expand All @@ -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"}
Expand All @@ -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,
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lacommunaute/notification/tests/tests_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
Expand Down Expand Up @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions lacommunaute/notification/tests/tests_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand Down Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion lacommunaute/users/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand Down

0 comments on commit 2bf4137

Please sign in to comment.