From a7867176c456869ccf1c73397a31f55bfb25d02d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Reuiller?= Date: Tue, 19 Sep 2023 15:49:16 +0200 Subject: [PATCH] cocontracting btn for anonymous user with siae_id --- .../_detail_cocontracting_click_error.html | 5 ++ .../tenders/_detail_cta_cocontracting.html | 8 +-- lemarche/templates/tenders/detail.html | 2 + lemarche/www/tenders/tasks.py | 3 +- lemarche/www/tenders/tests.py | 58 ++++++++++++++----- lemarche/www/tenders/views.py | 13 ++++- 6 files changed, 67 insertions(+), 22 deletions(-) create mode 100644 lemarche/templates/tenders/_detail_cocontracting_click_error.html diff --git a/lemarche/templates/tenders/_detail_cocontracting_click_error.html b/lemarche/templates/tenders/_detail_cocontracting_click_error.html new file mode 100644 index 000000000..9e6b60975 --- /dev/null +++ b/lemarche/templates/tenders/_detail_cocontracting_click_error.html @@ -0,0 +1,5 @@ + diff --git a/lemarche/templates/tenders/_detail_cta_cocontracting.html b/lemarche/templates/tenders/_detail_cta_cocontracting.html index ddaf78b9f..af7d92e7d 100644 --- a/lemarche/templates/tenders/_detail_cta_cocontracting.html +++ b/lemarche/templates/tenders/_detail_cta_cocontracting.html @@ -2,16 +2,14 @@

Répondre en co-traitance ?

-

Notre équipe vous met en relation avec d’autres structures désireuses de répondre en co-traitance.

- {% if user.is_authenticated %} - {% else %} - {% endif %}
diff --git a/lemarche/templates/tenders/detail.html b/lemarche/templates/tenders/detail.html index 927bdf45d..7187fbcb1 100644 --- a/lemarche/templates/tenders/detail.html +++ b/lemarche/templates/tenders/detail.html @@ -148,11 +148,13 @@
{% elif not user_siae_has_detail_contact_click_date %} {% include "tenders/_detail_cta.html" with tender=tender user_can_click=True %} + {% include "tenders/_detail_cta_cocontracting.html" with tender=tender %} {% endif %} {% endif %} {% elif siae_id %} {% if not siae_has_detail_contact_click_date %} {% include "tenders/_detail_cta.html" with tender=tender user_can_click=True siae_id=siae_id %} + {% include "tenders/_detail_cta_cocontracting.html" with tender=tender siae_id=siae_id %} {% else %} {% include "tenders/_detail_contact.html" with tender=tender %} {% endif %} diff --git a/lemarche/www/tenders/tasks.py b/lemarche/www/tenders/tasks.py index 06935e47c..473540845 100644 --- a/lemarche/www/tenders/tasks.py +++ b/lemarche/www/tenders/tasks.py @@ -555,4 +555,5 @@ def notify_admin_siae_wants_cocontracting(tender: Tender, siae: Siae): recipient_list=[settings.NOTIFY_EMAIL], ) - # api_slack.send_message_to_channel(text=email_body, service_id=settings.SLACK_WEBHOOK_C4_SUPPORT_CHANNEL) + if settings.BITOUBI_ENV == "prod": + api_slack.send_message_to_channel(text=email_body, service_id=settings.SLACK_WEBHOOK_C4_SUPPORT_CHANNEL) diff --git a/lemarche/www/tenders/tests.py b/lemarche/www/tenders/tests.py index b02c9646b..a9be95942 100644 --- a/lemarche/www/tenders/tests.py +++ b/lemarche/www/tenders/tests.py @@ -1,5 +1,6 @@ import json from datetime import timedelta +from unittest import mock from django.conf import settings from django.contrib.gis.geos import Point @@ -854,7 +855,10 @@ def test_tender_cocontracting_display(self): url = reverse("tenders:detail", kwargs={"slug": self.tender_1.slug}) response = self.client.get(url) self.assertNotContains(response, "Répondre en co-traitance ?") - + # anonymous but with siae_id in url + url = reverse("tenders:detail", kwargs={"slug": self.tender_1.slug}) + response = self.client.get(f"{url}?siae_id=15") + self.assertContains(response, "Répondre en co-traitance ?") # siae user interested but has a detail_contact_click_date self.client.force_login(self.siae_user_1) url = reverse("tenders:detail", kwargs={"slug": self.tender_1.slug}) @@ -866,7 +870,7 @@ def test_tender_cocontracting_display(self): response = self.client.get(url) self.assertContains(response, "Répondre en co-traitance ?") # siae user interested - cls.tendersiae_1_2 = TenderSiae.objects.create( + tendersiae_1_2 = TenderSiae.objects.create( tender=self.tender_1, siae=self.siae_2, email_send_date=timezone.now() ) self.client.force_login(self.siae_user_2) @@ -999,33 +1003,61 @@ def test_update_tendersiae_stats_on_tender_contact_click_with_siae_id_param(self class TenderDetailCocontractingClickView(TestCase): @classmethod - def setUpTestData(cls): - cls.siae = SiaeFactory(name="ZZ ESI") - cls.siae_user = UserFactory(kind=User.KIND_SIAE, siaes=[cls.siae]) - cls.user_buyer = UserFactory(kind=User.KIND_BUYER, company_name="Entreprise Buyer") - cls.tender = TenderFactory( + def setUpTestData(self): + self.siae = SiaeFactory(name="ZZ ESI") + self.siae_user = UserFactory(kind=User.KIND_SIAE, siaes=[self.siae]) + self.user_buyer = UserFactory(kind=User.KIND_BUYER, company_name="Entreprise Buyer") + self.tender = TenderFactory( kind=tender_constants.KIND_TENDER, - author=cls.user_buyer, + author=self.user_buyer, amount=tender_constants.AMOUNT_RANGE_100_150, accept_share_amount=True, response_kind=[Tender.RESPONSE_KIND_EMAIL], ) - cls.tendersiae = TenderSiae.objects.create( - tender=cls.tender, - siae=cls.siae, + self.tendersiae = TenderSiae.objects.create( + tender=self.tender, + siae=self.siae, source="EMAIL", email_send_date=timezone.now(), email_link_click_date=timezone.now(), detail_display_date=timezone.now(), detail_contact_click_date=timezone.now(), ) - TenderQuestionFactory(tender=cls.tender) + TenderQuestionFactory(tender=self.tender) def test_user_can_notify_cocontracting_wish(self): url = reverse("tenders:detail-cocontracting-click", kwargs={"slug": self.tender.slug}) + with mock.patch("lemarche.www.tenders.tasks.send_mail_async") as mock_send_mail_async: + response = self.client.post(url, data={}) + self.assertEqual(response.status_code, 403) + mock_send_mail_async.assert_not_called() + + with mock.patch("lemarche.www.tenders.tasks.send_mail_async") as mock_send_mail_async: + response = self.client.post(f"{url}?siae_id=999999", data={}) + self.assertContains(response, "nous n'avons pas pu prendre en compte votre demande de mise en relation") + mock_send_mail_async.assert_not_called() + + with mock.patch("lemarche.www.tenders.tasks.send_mail_async") as mock_send_mail_async: + response = self.client.post(f"{url}?siae_id={self.siae.id}", data={}) + self.assertContains(response, "Nous avons bien pris en compte votre demande de mise en relation") + mock_send_mail_async.assert_called_once() + email_body = mock_send_mail_async.call_args[1]["email_body"] + self.assertTrue(f"La structure {self.siae.name } souhaite répondre en co-traitance" in email_body) + self.client.force_login(self.siae_user) - response = self.client.post(url, data={}) + with mock.patch("lemarche.www.tenders.tasks.send_mail_async") as mock_send_mail_async: + response = self.client.post(url, data={}) self.assertContains(response, "Nous avons bien pris en compte votre demande de mise en relation") + mock_send_mail_async.assert_called_once() + email_body = mock_send_mail_async.call_args[1]["email_body"] + self.assertTrue(f"La structure {self.siae.name } souhaite répondre en co-traitance" in email_body) + + user_without_siae = UserFactory(kind=User.KIND_SIAE) + self.client.force_login(user_without_siae) + with mock.patch("lemarche.www.tenders.tasks.send_mail_async") as mock_send_mail_async: + response = self.client.post(url, data={}) + self.assertContains(response, "nous n'avons pas pu prendre en compte votre demande de mise en relation") + mock_send_mail_async.assert_not_called() # TODO: this test doesn't work anymore. find a way to test logging post-email in non-prod environments? diff --git a/lemarche/www/tenders/views.py b/lemarche/www/tenders/views.py index e46f5fb3e..4a01418b7 100644 --- a/lemarche/www/tenders/views.py +++ b/lemarche/www/tenders/views.py @@ -404,7 +404,7 @@ def get_success_message(self, detail_contact_click_confirm): return f"{self.object.cta_card_button_text}
Pour {self.object.cta_card_button_text.lower()}, vous devez accepter d'être mis en relation avec l'acheteur." # noqa -class TenderDetailCocontractingClickView(LoginRequiredMixin, DetailView): +class TenderDetailCocontractingClickView(SiaeUserRequiredOrSiaeIdParamMixin, DetailView): """ Endpoint to handle cocontracting button click """ @@ -419,8 +419,15 @@ def post(self, request, *args, **kwargs): self.object = self.get_object() user = self.request.user - if settings.BITOUBI_ENV == "prod": - notify_admin_siae_wants_cocontracting(self.object, user.siaes.first()) + if self.request.user.is_authenticated: + siae = user.siaes.first() + else: + siae = Siae.objects.filter(pk=self.request.GET.get("siae_id", None)).first() + + if siae: + notify_admin_siae_wants_cocontracting(self.object, siae) + else: + self.template_name = "tenders/_detail_cocontracting_click_error.html" return self.get(request)