-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(PC-34345)[API] feat: use json serialization for celery tasks
- Loading branch information
1 parent
359de2d
commit 5e32e0d
Showing
6 changed files
with
50 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,44 @@ | ||
import logging | ||
|
||
from brevo_python.rest import ApiException as SendinblueApiException | ||
from celery import shared_task | ||
from pydantic import ValidationError | ||
|
||
from pcapi.core.external import sendinblue | ||
from pcapi.core.mails.transactional.send_transactional_email import send_transactional_email | ||
from pcapi.tasks.serialization.sendinblue_tasks import SendTransactionalEmailRequest | ||
from pcapi.tasks.serialization.sendinblue_tasks import UpdateSendinblueContactRequest | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@shared_task(name="mails.tasks.update_contact_attributes", autoretry_for=(SendinblueApiException,), retry_backoff=True) | ||
def update_contact_attributes_task_celery(payload: UpdateSendinblueContactRequest) -> None: | ||
sendinblue.make_update_request(payload) | ||
try: | ||
request = UpdateSendinblueContactRequest.parse_obj(payload) | ||
sendinblue.make_update_request(request) | ||
except ValidationError as exp: | ||
logger.error("could not deserialize object", extra={"exception": exp}) | ||
|
||
|
||
@shared_task( | ||
name="mails.tasks.send_transactional_email_primary", autoretry_for=(SendinblueApiException,), retry_backoff=True | ||
) | ||
def send_transactional_email_primary_task_celery(payload: SendTransactionalEmailRequest) -> None: | ||
send_transactional_email(payload) | ||
def send_transactional_email_primary_task_celery(payload: dict) -> None: | ||
try: | ||
request = SendTransactionalEmailRequest.parse_obj(payload) | ||
send_transactional_email(request) | ||
except ValidationError as exp: | ||
logger.error("could not deserialize object", extra={"exception": exp}) | ||
|
||
|
||
@shared_task( | ||
name="mails.tasks.send_transactional_email_secondary", autoretry_for=(SendinblueApiException,), retry_backoff=True | ||
) | ||
def send_transactional_email_secondary_task_celery(payload: SendTransactionalEmailRequest) -> None: | ||
send_transactional_email(payload) | ||
try: | ||
request = SendTransactionalEmailRequest.parse_obj(payload) | ||
send_transactional_email(request) | ||
except ValidationError as exp: | ||
logger.error("could not deserialize object", extra={"exception": exp}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ def _get_backend_for_test(self): | |
"[email protected]", | ||
] | ||
) | ||
@patch("pcapi.core.mails.backends.sendinblue.send_transactional_email_secondary_task_celery.delay") | ||
@patch("pcapi.celery_tasks.sendinblue.send_transactional_email") | ||
def test_send_mail(self, mock_send_transactional_email_secondary_task_celery): | ||
backend = self._get_backend_for_test() | ||
backend(use_pro_subaccount=False).send_mail( | ||
|
@@ -64,7 +64,7 @@ def test_send_mail(self, mock_send_transactional_email_secondary_task_celery): | |
assert task_param.reply_to == self.expected_sent_data.reply_to | ||
assert task_param.enable_unsubscribe == self.expected_sent_data.enable_unsubscribe | ||
|
||
@patch("pcapi.core.mails.backends.sendinblue.send_transactional_email_secondary_task_celery.delay") | ||
@patch("pcapi.celery_tasks.sendinblue.send_transactional_email") | ||
def test_send_mail_with_no_sender(self, mock_send_transactional_email_secondary_task_celery): | ||
self.mock_template = models.TemplatePro( | ||
id_prod=1, | ||
|
@@ -162,7 +162,7 @@ def _get_backend_for_test(self): | |
return import_string("pcapi.core.mails.backends.sendinblue.ToDevSendinblueBackend") | ||
|
||
@pytest.mark.settings(WHITELISTED_EMAIL_RECIPIENTS=["[email protected]"]) | ||
@patch("pcapi.core.mails.backends.sendinblue.send_transactional_email_secondary_task_celery.delay") | ||
@patch("pcapi.celery_tasks.sendinblue.send_transactional_email") | ||
def test_send_mail_to_dev(self, mock_send_transactional_email_secondary_task_celery): | ||
backend = self._get_backend_for_test() | ||
backend(use_pro_subaccount=False).send_mail( | ||
|
@@ -179,7 +179,7 @@ def test_send_mail_to_dev(self, mock_send_transactional_email_secondary_task_cel | |
assert task_param.sender == self.expected_sent_data_to_dev.sender | ||
assert task_param.reply_to == self.expected_sent_data_to_dev.reply_to | ||
|
||
@patch("pcapi.core.mails.backends.sendinblue.send_transactional_email_secondary_task_celery.delay") | ||
@patch("pcapi.celery_tasks.sendinblue.send_transactional_email") | ||
def test_send_mail_test_user(self, mock_send_transactional_email_secondary_task_celery): | ||
users_factories.UserFactory(email=self.recipients[0], roles=[users_models.UserRole.TEST]) | ||
|
||
|
@@ -195,7 +195,7 @@ def test_send_mail_test_user(self, mock_send_transactional_email_secondary_task_ | |
["[email protected]", "[email protected]"], | ||
) | ||
@pytest.mark.settings(WHITELISTED_EMAIL_RECIPIENTS=["[email protected]", "*@passculture-test.app"]) | ||
@patch("pcapi.core.mails.backends.sendinblue.send_transactional_email_secondary_task_celery.delay") | ||
@patch("pcapi.celery_tasks.sendinblue.send_transactional_email") | ||
def test_send_mail_whitelisted(self, mock_send_transactional_email_secondary_task_celery, recipient): | ||
backend = self._get_backend_for_test() | ||
backend(use_pro_subaccount=False).send_mail( | ||
|
@@ -207,7 +207,7 @@ def test_send_mail_whitelisted(self, mock_send_transactional_email_secondary_tas | |
assert list(task_param.recipients) == [recipient] | ||
|
||
@pytest.mark.settings(IS_STAGING=True, IS_E2E_TESTS=True, END_TO_END_TESTS_EMAIL_ADDRESS="[email protected]") | ||
@patch("pcapi.core.mails.backends.sendinblue.send_transactional_email_secondary_task_celery.delay") | ||
@patch("pcapi.celery_tasks.sendinblue.send_transactional_email") | ||
def test_send_mail_whitelisted_qa_staging(self, mock_send_transactional_email_secondary_task_celery): | ||
recipient = "[email protected]" | ||
users_factories.UserFactory(email=recipient) | ||
|
@@ -220,7 +220,7 @@ def test_send_mail_whitelisted_qa_staging(self, mock_send_transactional_email_se | |
assert list(task_param.recipients) == [recipient] | ||
|
||
@pytest.mark.settings(IS_TESTING=True, IS_E2E_TESTS=True, END_TO_END_TESTS_EMAIL_ADDRESS="[email protected]") | ||
@patch("pcapi.core.mails.backends.sendinblue.send_transactional_email_secondary_task_celery.delay") | ||
@patch("pcapi.celery_tasks.sendinblue.send_transactional_email") | ||
def test_send_mail_whitelisted_qa_testing( | ||
self, mock_send_transactional_email_secondary_task_celery, recipient="[email protected]" | ||
): | ||
|
@@ -237,7 +237,7 @@ def test_send_mail_whitelisted_qa_testing( | |
@pytest.mark.features(WIP_ASYNCHRONOUS_CELERY_TASKS=True) | ||
class SendTest: | ||
@pytest.mark.settings(IS_TESTING=True, EMAIL_BACKEND="pcapi.core.mails.backends.sendinblue.ToDevSendinblueBackend") | ||
@patch("pcapi.core.mails.backends.sendinblue.send_transactional_email_secondary_task_celery.delay") | ||
@patch("pcapi.celery_tasks.sendinblue.send_transactional_email") | ||
def test_send_to_ehp_false_in_testing(self, mock_send_transactional_email_secondary_task_celery, caplog): | ||
mock_template_send_ehp_false = models.Template( | ||
id_prod=11, id_not_prod=12, tags=["some", "stuff"], send_to_ehp=False | ||
|
@@ -259,7 +259,7 @@ def test_send_to_ehp_false_in_testing(self, mock_send_transactional_email_second | |
) | ||
|
||
@pytest.mark.settings(IS_TESTING=True, EMAIL_BACKEND="pcapi.core.mails.backends.sendinblue.ToDevSendinblueBackend") | ||
@patch("pcapi.core.mails.backends.sendinblue.send_transactional_email_secondary_task_celery.delay") | ||
@patch("pcapi.celery_tasks.sendinblue.send_transactional_email") | ||
def test_send_to_ehp_true_in_testing(self, mock_send_transactional_email_secondary_task_celery, caplog): | ||
mock_template_send_ehp_true = models.Template( | ||
id_prod=11, id_not_prod=12, tags=["some", "stuff"], send_to_ehp=True | ||
|
@@ -282,7 +282,7 @@ def test_send_to_ehp_true_in_testing(self, mock_send_transactional_email_seconda | |
], | ||
) | ||
@pytest.mark.settings(EMAIL_BACKEND="pcapi.core.mails.backends.sendinblue.SendinblueBackend") | ||
@patch("pcapi.core.mails.backends.sendinblue.send_transactional_email_secondary_task_celery.delay") | ||
@patch("pcapi.celery_tasks.sendinblue.send_transactional_email") | ||
def test_send_mail_to_pro_with_FF( | ||
self, | ||
mock_send_transactional_email_secondary_task_celery, | ||
|
@@ -313,7 +313,7 @@ def test_send_mail_to_pro_with_FF( | |
], | ||
) | ||
@pytest.mark.settings(IS_TESTING=True, EMAIL_BACKEND="pcapi.core.mails.backends.sendinblue.SendinblueBackend") | ||
@patch("pcapi.core.mails.backends.sendinblue.send_transactional_email_secondary_task_celery.delay") | ||
@patch("pcapi.celery_tasks.sendinblue.send_transactional_email") | ||
def test_send_mail_to_pro_with_FF_in_ehp( | ||
self, | ||
mock_send_transactional_email_secondary_task_celery, | ||
|