Skip to content

Commit

Permalink
cleanup Brevo settings declaration and usage
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Dec 11, 2024
1 parent 4f974c5 commit b66be49
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
11 changes: 6 additions & 5 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,13 +357,14 @@
MATOMO_SITE_ID = int(os.getenv("MATOMO_SITE_ID", "1"))
MATOMO_AUTH_TOKEN = os.getenv("MATOMO_AUTH_TOKEN", None)

# SENDINBLUE
# SENDINBLUE / BREVO
# ---------------------------------------
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_URL = os.getenv("SIB_URL")
if SIB_URL:
SIB_SMTP_URL = os.path.join(SIB_URL, "smtp/email")
SIB_CONTACTS_URL = os.path.join(SIB_URL, "contacts/import")

SIB_API_KEY = os.getenv("SIB_API_KEY", "set-sib-api-key")
SIB_API_KEY = os.getenv("SIB_API_KEY")
DEFAULT_FROM_EMAIL = os.getenv("DEFAULT_FROM_EMAIL", "[email protected]")

SIB_MAGIC_LINK_TEMPLATE = 31
Expand Down
7 changes: 7 additions & 0 deletions config/settings/dev.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Enable django-debug-toolbar with Docker.
import os
import socket

from lacommunaute.utils.enums import Environment
Expand All @@ -15,6 +16,12 @@
ENVIRONMENT = Environment.DEV

ALLOWED_HOSTS = ["localhost", "0.0.0.0", "127.0.0.1", "192.168.0.1"]
# SENDINBLUE / BREVO
# ---------------------------------------
SIB_URL = os.getenv("SIB_URL", "https://test.com")
SIB_SMTP_URL = os.path.join(SIB_URL, "smtp/email")
SIB_CONTACTS_URL = os.path.join(SIB_URL, "contacts/import")
SIB_API_KEY = os.getenv("SIB_API_KEY", "key_to_be_set")

# Security.
# ------------------------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions lacommunaute/notification/tests/tests_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import httpx
import respx
from django.conf import settings
from django.test import TestCase
from faker import Faker

from config.settings.base import DEFAULT_FROM_EMAIL, SIB_CONTACTS_URL, SIB_SMTP_URL
from lacommunaute.notification.emails import bulk_send_user_to_list, send_email
from lacommunaute.notification.models import EmailSentTrack
from lacommunaute.users.factories import UserFactory
Expand All @@ -17,13 +17,13 @@
class SendEmailTestCase(TestCase):
@classmethod
def setUpTestData(cls):
respx.post(SIB_SMTP_URL).mock(return_value=httpx.Response(200, json={"message": "OK"}))
respx.post(settings.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()
cls.kind = "first_reply"
cls.payload = {
"sender": {"name": "La Communauté", "email": DEFAULT_FROM_EMAIL},
"sender": {"name": "La Communauté", "email": settings.DEFAULT_FROM_EMAIL},
"to": cls.to,
"params": cls.params,
"templateId": cls.template_id,
Expand Down Expand Up @@ -52,7 +52,7 @@ def test_send_email_with_bcc(self):
class BulkSendUserToListTestCase(TestCase):
@classmethod
def setUpTestData(cls):
respx.post(SIB_CONTACTS_URL).mock(return_value=httpx.Response(200, json={"message": "OK"}))
respx.post(settings.SIB_CONTACTS_URL).mock(return_value=httpx.Response(200, json={"message": "OK"}))

@respx.mock
def test_bulk_send_user_to_list(self):
Expand Down
17 changes: 5 additions & 12 deletions lacommunaute/notification/tests/tests_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@
from django.urls import reverse
from faker import Faker

from config.settings.base import (
DEFAULT_FROM_EMAIL,
SIB_CONTACTS_URL,
SIB_ONBOARDING_LIST,
SIB_SMTP_URL,
SIB_UNANSWERED_QUESTION_TEMPLATE,
)
from lacommunaute.forum_conversation.factories import (
TopicFactory,
)
Expand All @@ -37,7 +30,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(SIB_SMTP_URL).mock(return_value=httpx.Response(200, json={"message": "OK"}))
respx.post(settings.SIB_SMTP_URL).mock(return_value=httpx.Response(200, json={"message": "OK"}))
yield


Expand Down Expand Up @@ -115,7 +108,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(SIB_CONTACTS_URL).mock(return_value=httpx.Response(200, json={"message": "OK"}))
respx.post(settings.SIB_CONTACTS_URL).mock(return_value=httpx.Response(200, json={"message": "OK"}))

@respx.mock
def test_add_user_to_list_when_register(self):
Expand All @@ -128,7 +121,7 @@ def test_add_user_to_list_when_register(self):
],
"emailBlacklist": False,
"smsBlacklist": False,
"listIds": [SIB_ONBOARDING_LIST],
"listIds": [settings.SIB_ONBOARDING_LIST],
"updateExistingContacts": True,
"emptyContactsAttributes": True,
}
Expand Down Expand Up @@ -163,10 +156,10 @@ def payload_for_staff_user_to_notify_on_unanswered_topics_fixture():
)
params = {"count": 1, "link": "".join(url)}
payload = {
"sender": {"name": "La Communauté", "email": DEFAULT_FROM_EMAIL},
"sender": {"name": "La Communauté", "email": settings.DEFAULT_FROM_EMAIL},
"to": to,
"params": params,
"templateId": SIB_UNANSWERED_QUESTION_TEMPLATE,
"templateId": settings.SIB_UNANSWERED_QUESTION_TEMPLATE,
}
yield payload

Expand Down

0 comments on commit b66be49

Please sign in to comment.