From 2eaf257ac185d8f0b7e0b5c708a024ad4bb89186 Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Wed, 27 Mar 2024 11:40:12 +0100 Subject: [PATCH] Add test --- lemarche/www/tenders/tests.py | 41 +++++++++++++++++++++++++++++++---- lemarche/www/tenders/views.py | 2 +- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/lemarche/www/tenders/tests.py b/lemarche/www/tenders/tests.py index 7b43bdab1..d55950733 100644 --- a/lemarche/www/tenders/tests.py +++ b/lemarche/www/tenders/tests.py @@ -1134,10 +1134,11 @@ def test_tender_contact_details_display(self): self.assertNotContains(response, "Voir l'appel d'offres") self.assertContains(response, "Lien partagé") - def test_update_tendersiae_stats_on_tender_view(self): - self.tender_1.siaes.add(self.siae_2) + def test_update_tendersiae_stats_on_tender_view_with_siae_id(self): + self.tender_1.siaes.add(self.siae_2) # create new tendersiae self.assertEqual(self.tender_1.tendersiae_set.count(), 3 + 1) self.assertEqual(self.tender_1.tendersiae_set.first().siae, self.siae_2) + self.assertIsNone(self.tender_1.tendersiae_set.first().user) self.assertIsNone(self.tender_1.tendersiae_set.first().email_link_click_date) self.assertIsNone(self.tender_1.tendersiae_set.first().detail_display_date) self.assertEqual(self.tender_1.tendersiae_set.last().siae, self.siae_1) @@ -1148,18 +1149,19 @@ def test_update_tendersiae_stats_on_tender_view(self): response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertContains(response, "Déjà 3 prestataires inclusifs") - # reload anonymous user with ?siae_id= (already in tendersiae) + # reload anonymous user with siae_id (already in tendersiae) url = reverse("tenders:detail", kwargs={"slug": self.tender_1.slug}) + f"?siae_id={self.siae_2.id}" response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(self.tender_1.tendersiae_set.count(), 4) # unchanged siae_2_email_link_click_date = self.tender_1.tendersiae_set.first().email_link_click_date self.assertIsNotNone(siae_2_email_link_click_date) # email_link_click_date updated + self.assertIsNone(self.tender_1.tendersiae_set.first().user) self.assertIsNone(self.tender_1.tendersiae_set.first().detail_display_date) self.assertIsNotNone(self.tender_1.tendersiae_set.last().detail_display_date) self.assertContains(response, "Déjà 4 prestataires inclusifs") self.assertNotContains(response, "contactez dès maintenant le client") - # reload logged in with ?siae_id= (updats detail_display_date, but not email_link_click_date) + # reload logged in user with siae_id (updates detail_display_date, but not email_link_click_date) self.client.force_login(self.siae_user_2) url = reverse("tenders:detail", kwargs={"slug": self.tender_1.slug}) + f"?siae_id={self.siae_2.id}" response = self.client.get(url) @@ -1180,6 +1182,37 @@ def test_update_tendersiae_stats_on_tender_view(self): self.assertContains(response, "Déjà 4 prestataires inclusifs") self.assertNotContains(response, "contactez dès maintenant le client") + def test_update_tendersiae_stats_on_tender_view_with_siae_id_and_user_id(self): + self.tender_1.siaes.add(self.siae_2) # create new tendersiae + self.assertEqual(self.tender_1.tendersiae_set.count(), 3 + 1) + self.assertEqual(self.tender_1.tendersiae_set.first().siae, self.siae_2) + self.assertIsNone(self.tender_1.tendersiae_set.first().user) + self.assertIsNone(self.tender_1.tendersiae_set.first().email_link_click_date) + self.assertIsNone(self.tender_1.tendersiae_set.first().detail_display_date) + self.assertEqual(self.tender_1.tendersiae_set.last().siae, self.siae_1) + self.assertIsNotNone(self.tender_1.tendersiae_set.last().email_link_click_date) + self.assertIsNotNone(self.tender_1.tendersiae_set.last().detail_display_date) + # first load anonymous user + url = reverse("tenders:detail", kwargs={"slug": self.tender_1.slug}) + response = self.client.get(url) + self.assertEqual(response.status_code, 200) + self.assertContains(response, "Déjà 3 prestataires inclusifs") + # reload anonymous user with siae_id & user_id (already in tendersiae) + url = ( + reverse("tenders:detail", kwargs={"slug": self.tender_1.slug}) + + f"?siae_id={self.siae_2.id}&user_id={self.siae_user_2.id}" + ) + response = self.client.get(url) + self.assertEqual(response.status_code, 200) + self.assertEqual(self.tender_1.tendersiae_set.count(), 4) # unchanged + siae_2_email_link_click_date = self.tender_1.tendersiae_set.first().email_link_click_date + self.assertIsNotNone(siae_2_email_link_click_date) # email_link_click_date updated + self.assertIsNotNone(self.tender_1.tendersiae_set.first().user) # user updated + self.assertIsNone(self.tender_1.tendersiae_set.first().detail_display_date) + self.assertIsNotNone(self.tender_1.tendersiae_set.last().detail_display_date) + self.assertContains(response, "Déjà 4 prestataires inclusifs") + self.assertNotContains(response, "contactez dès maintenant le client") + def test_create_tendersiae_stats_on_tender_view_by_existing_user(self): self.tender_1.siaes.add(self.siae_2) self.assertEqual(self.tender_1.tendersiae_set.count(), 3 + 1) diff --git a/lemarche/www/tenders/views.py b/lemarche/www/tenders/views.py index 1a7c06023..76ab580d1 100644 --- a/lemarche/www/tenders/views.py +++ b/lemarche/www/tenders/views.py @@ -347,7 +347,7 @@ def get(self, request, *args, **kwargs): self.user_id = request.GET.get("user_id", None) # update 'email_link_click_date' if self.siae_id: - if self.user_id: + if self.user_id: # TODO: check if user in siae ? TenderSiae.objects.filter(tender=self.object, siae_id=self.siae_id, email_link_click_date=None).update( user_id=self.user_id, email_link_click_date=timezone.now(), updated_at=timezone.now() )