diff --git a/lemarche/tenders/tests/test_admin.py b/lemarche/tenders/tests/test_admin.py index d31719fd9..a1e356e11 100644 --- a/lemarche/tenders/tests/test_admin.py +++ b/lemarche/tenders/tests/test_admin.py @@ -25,11 +25,9 @@ def setUp(self): self.user_admin = UserFactory(is_superuser=True) # Admin user self.admin = TenderAdmin(Tender, self.admin_site) - @patch("lemarche.tenders.admin.api_brevo.create_deal") # Mock the create_deal API call - @patch("lemarche.tenders.admin.api_brevo.link_deal_with_contact_list") # Mock the link_deal API call - def test_validate_send_to_siaes_not_synch_brevo(self, mock_link_deal, mock_create_deal): - tender = TenderFactory(is_followed_by_us=False) # Tender object - # Simulate a POST request without triggering the validation action + def setUpRequest(self, tender, is_followed_by_us): + tender.is_followed_by_us = is_followed_by_us + tender.save() request = self.factory.post("/", data={"_validate_send_to_siaes": "1"}) request.user = self.user_admin @@ -41,6 +39,14 @@ def test_validate_send_to_siaes_not_synch_brevo(self, mock_link_deal, mock_creat # Attach a message storage system to the request setattr(request, "_messages", FallbackStorage(request)) + return request + + @patch("lemarche.tenders.admin.api_brevo.create_deal") # Mock the create_deal API call + @patch("lemarche.tenders.admin.api_brevo.link_deal_with_contact_list") # Mock the link_deal API call + def test_validate_send_to_siaes_not_synch_brevo(self, mock_link_deal, mock_create_deal): + tender = TenderFactory(is_followed_by_us=False) # Tender object + request = self.setUpRequest(tender, is_followed_by_us=False) + # Call the response_change method without the validation action enabled response = self.admin.response_change(request, tender) @@ -53,7 +59,7 @@ def test_validate_send_to_siaes_not_synch_brevo(self, mock_link_deal, mock_creat self.assertIn(".", response.url) # Verify the flash messages - messages = list(request._messages._queued_messages) + messages = request._messages._queued_messages self.assertEqual(len(messages), 1) # Expecting only one message self.assertEqual(str(messages[0]), "Ce dépôt de besoin a été validé. Il sera envoyé en temps voulu :)") @@ -61,17 +67,7 @@ def test_validate_send_to_siaes_not_synch_brevo(self, mock_link_deal, mock_creat @patch("lemarche.tenders.admin.api_brevo.link_deal_with_contact_list") # Mock the link_deal API call def test_validate_send_to_siaes_with_sync_brevo(self, mock_link_deal, mock_create_deal): tender = TenderFactory(is_followed_by_us=True) # Tender object - # Simulate a POST request to trigger "_validate_send_to_siaes" - request = self.factory.post("/", data={"_validate_send_to_siaes": "1"}) - request.user = self.user_admin - - # Initialize and apply SessionMiddleware - middleware = SessionMiddleware(get_response) - middleware.process_request(request) - request.session.save() # Save the session to avoid any issues - - # Attach a message storage to the request - setattr(request, "_messages", FallbackStorage(request)) + request = self.setUpRequest(tender, is_followed_by_us=True) # Call the response_change method response = self.admin.response_change(request, tender) @@ -89,7 +85,7 @@ def test_validate_send_to_siaes_with_sync_brevo(self, mock_link_deal, mock_creat self.assertIn(".", response.url) # Verify flash messages - messages = list(request._messages._queued_messages) + messages = request._messages._queued_messages self.assertEqual(len(messages), 2) # Expecting two messages self.assertEqual(str(messages[0]), "Ce dépôt de besoin a été synchronisé avec Brevo") self.assertEqual(str(messages[1]), "Ce dépôt de besoin a été validé. Il sera envoyé en temps voulu :)")