From 636efeb4c3bbd583eda01fb05cfb52469aa00d87 Mon Sep 17 00:00:00 2001 From: Chlorophyllia Date: Thu, 7 Nov 2024 19:34:33 +0100 Subject: [PATCH] Tests de la commande 'crm_brevo_sync_companies' --- lemarche/crm/tests.py | 92 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 lemarche/crm/tests.py diff --git a/lemarche/crm/tests.py b/lemarche/crm/tests.py new file mode 100644 index 000000000..b5e3f7f4a --- /dev/null +++ b/lemarche/crm/tests.py @@ -0,0 +1,92 @@ +from unittest.mock import MagicMock, patch + +from django.core.management import call_command +from django.test import TestCase + +from lemarche.siaes.factories import SiaeFactory +from lemarche.utils.apis.api_brevo import create_or_update_company +from lemarche.utils.urls import get_object_admin_url, get_object_share_url + + +class CrmBrevoSyncCompaniesCommandTests(TestCase): + @classmethod + def setUpTestData(cls): + """Siae instances initialization""" + cls.siae1 = SiaeFactory(name="Test Company 1") + cls.siae2 = SiaeFactory() + + @patch("lemarche.utils.apis.api_brevo.sib_api_v3_sdk.Body") + def test_create_or_update_company_with_mocked_body(self, mock_body): + """Test create_or_update_company with mocked body""" + mock_body_instance = MagicMock() + mock_body.return_value = mock_body_instance + + create_or_update_company(self.siae1) + + expected_attributes = { + "domain": self.siae1.website, + "phone_number": self.siae1.contact_phone_display, + "app_id": self.siae1.id, + "siae": True, + "active": self.siae1.is_active, + "description": self.siae1.description, + "kind": self.siae1.kind, + "address_street": self.siae1.address, + "address_post_code": self.siae1.post_code, + "address_city": self.siae1.city, + "contact_email": self.siae1.contact_email, + "logo_url": self.siae1.logo_url, + "geo_range": self.siae1.geo_range, + "app_url": get_object_share_url(self.siae1), + "app_admin_url": get_object_admin_url(self.siae1), + **self.siae1.extra_data, # includes completion_rate, tender_email_send_count, etc. + } + + mock_body.assert_called_once_with(name="Test Company 1", attributes=expected_attributes) + + @patch("lemarche.utils.apis.api_brevo.create_or_update_company") + def test_new_siaes_are_synced_in_brevo(self, mock_create_or_update_company): + """Test new siaes are synced in brevo""" + call_command("crm_brevo_sync_companies") + + mock_create_or_update_company.assert_any_call(self.siae1) + mock_create_or_update_company.assert_any_call(self.siae2) + self.assertEqual(mock_create_or_update_company.call_count, 2) + + @patch("lemarche.utils.apis.api_brevo.create_or_update_company") + def test_siae_extra_data_is_updated(self, mock_create_or_update_company): + """Test siae is updated if extra_data is changed.""" + initial_extra_data = self.siae2.extra_data.copy() + + self.siae2.completion_rate = 70 + self.siae2.tender_email_send_count = 10 + self.siae2.save() + + call_command("crm_brevo_sync_companies", recently_updated=True) + + self.assertTrue(mock_create_or_update_company.called) + + self.siae2.refresh_from_db() + expected_extra_data = { + "completion_rate": self.siae2.completion_rate, + "tender_email_send_count": self.siae2.tender_email_send_count, + "recent_tender_detail_click_count": self.siae2.recent_tender_detail_click_count, + } + + self.assertNotEqual(initial_extra_data, expected_extra_data, "siae.extra_data aurait dût être mis à jour.") + + @patch("lemarche.utils.apis.api_brevo.create_or_update_company") + def test_siae_extra_data_is_not_updated_if_no_change(self, mock_create_or_update_company): + """Test siae is not updated if no change in extra_data.""" + initial_extra_data = self.siae2.extra_data.copy() + + call_command("crm_brevo_sync_companies", recently_updated=True) + + self.assertTrue(mock_create_or_update_company.called) + + self.siae2.refresh_from_db() + self.assertEqual( + initial_extra_data, + self.siae2.extra_data, + "siae.extra_data a été mis à jour alors qu'il n'y avait pas de changement.", + )