Skip to content

Commit

Permalink
refactor tests case
Browse files Browse the repository at this point in the history
  • Loading branch information
madjid-asa committed Jan 2, 2025
1 parent 6839228 commit 82b1a9d
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions lemarche/tenders/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Expand All @@ -53,25 +59,15 @@ 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 :)")

@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_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)
Expand All @@ -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 :)")

0 comments on commit 82b1a9d

Please sign in to comment.