From bb1be908295b19079644a2e77964ee8a5fba37ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Reuiller?= Date: Wed, 30 Aug 2023 17:03:12 +0200 Subject: [PATCH] add tests for send first email function --- lemarche/www/conversations/tests.py | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 lemarche/www/conversations/tests.py diff --git a/lemarche/www/conversations/tests.py b/lemarche/www/conversations/tests.py new file mode 100644 index 000000000..e8a294f3e --- /dev/null +++ b/lemarche/www/conversations/tests.py @@ -0,0 +1,39 @@ +from unittest import mock + +from django.conf import settings +from django.test import TestCase + +from lemarche.conversations.factories import ConversationFactory +from lemarche.siaes.factories import SiaeFactory +from lemarche.users.factories import UserFactory +from lemarche.www.conversations.tasks import send_first_email_from_conversation + + +class TestConversationTasks(TestCase): + def test_send_first_email_from_conversation(self): + conversation = ConversationFactory() + + with mock.patch("lemarche.www.conversations.tasks.send_mail_async") as mock_send_mail_async: + send_first_email_from_conversation(conversation) + + mock_send_mail_async.assert_called_once() + email_body = mock_send_mail_async.call_args[1]["email_body"] + self.assertTrue(f"{conversation.sender_first_name} {conversation.sender_last_name}" in email_body) + self.assertTrue("Ce client vous a contacté via le Marché de l'inclusion." in email_body) + + # conversation with authenticated user + user = UserFactory() + siae_with_user = SiaeFactory() + siae_with_user.users.add(user) + + conversation_with_user = ConversationFactory(sender_user=user) + with mock.patch("lemarche.www.conversations.tasks.send_mail_async") as mock_send_mail_async: + send_first_email_from_conversation(conversation_with_user) + + mock_send_mail_async.assert_called_once() + email_body = mock_send_mail_async.call_args[1]["email_body"] + self.assertTrue( + f"{conversation_with_user.sender_first_name} {conversation_with_user.sender_last_name}" in email_body + ) + self.assertTrue(siae_with_user.name in email_body) + self.assertTrue("Ce client vous a contacté via le Marché de l'inclusion." in email_body)