From 7bfb5dd28a8802f7b65b66abd03a56e5939722ed Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Sun, 5 Nov 2023 11:47:48 +0100 Subject: [PATCH] Add tests. Fix stuff --- .../tenders/survey_transactioned_detail.html | 46 ++++++----- lemarche/www/tenders/forms.py | 6 +- lemarche/www/tenders/tests.py | 80 ++++++++++--------- lemarche/www/tenders/views.py | 18 ++++- 4 files changed, 86 insertions(+), 64 deletions(-) diff --git a/lemarche/templates/tenders/survey_transactioned_detail.html b/lemarche/templates/tenders/survey_transactioned_detail.html index b6d62e132..7bb7ebcf0 100644 --- a/lemarche/templates/tenders/survey_transactioned_detail.html +++ b/lemarche/templates/tenders/survey_transactioned_detail.html @@ -30,7 +30,7 @@

Avez-vous contractualisé ?

-
+
{% csrf_token %} @@ -38,46 +38,48 @@

- {{ form.survey_transactioned_amount.label }} - {% bootstrap_field form.survey_transactioned_feedback show_label=False %} + {{ form.survey_transactioned_answer.label }} + {% bootstrap_field form.survey_transactioned_answer show_label=False %}

-
-
-
-
- {{ form.survey_transactioned_amount.label }} - {% bootstrap_field form.survey_transactioned_amount show_label=False %} -
+ {% if tender.survey_transactioned_answer == None or tender.survey_transactioned_answer == True %} +
+
+
+
+ {{ form.survey_transactioned_amount.label }} + {% bootstrap_field form.survey_transactioned_amount show_label=False %} +
+
-
-
- -
+ {% endif %}
- {{ form.survey_transactioned_amount.label }} + {{ form.survey_transactioned_feedback.label }} {% bootstrap_field form.survey_transactioned_feedback show_label=False %}
-
-
-
diff --git a/lemarche/www/tenders/forms.py b/lemarche/www/tenders/forms.py index 223652b32..a52298ae8 100644 --- a/lemarche/www/tenders/forms.py +++ b/lemarche/www/tenders/forms.py @@ -288,7 +288,7 @@ class Meta: "survey_transactioned_feedback", ] - def __init__(self, *args, **kwargs): + def __init__(self, tender_survey_transactioned_answer=None, *args, **kwargs): super().__init__(*args, **kwargs) self.fields[ "survey_transactioned_answer" @@ -296,3 +296,7 @@ def __init__(self, *args, **kwargs): self.fields["survey_transactioned_amount"].label = "Quel est le montant de la transaction ?" self.fields["survey_transactioned_feedback"].label = "Partagez-nous votre retour d'expérience" self.fields["survey_transactioned_feedback"].widget.attrs.update({"placeholder": "Champ libre"}) + if tender_survey_transactioned_answer is not None: + self.fields["survey_transactioned_answer"].disabled = True + if tender_survey_transactioned_answer is False: + self.fields["survey_transactioned_amount"].widget = forms.HiddenInput() diff --git a/lemarche/www/tenders/tests.py b/lemarche/www/tenders/tests.py index 00f36261e..689900cd7 100644 --- a/lemarche/www/tenders/tests.py +++ b/lemarche/www/tenders/tests.py @@ -1272,6 +1272,7 @@ def setUpTestData(cls): cls.user_partner = UserFactory(kind=User.KIND_PARTNER) cls.user_admin = UserFactory(kind=User.KIND_ADMIN) cls.tender = TenderFactory(kind=tender_constants.KIND_TENDER, author=cls.user_buyer_1, siaes=[cls.siae]) + cls.user_buyer_1_sesame_query_string = sesame_get_query_string(cls.user_buyer_1) def test_anonymous_user_cannot_call_tender_survey_transactioned(self): url = reverse("tenders:detail-survey-transactioned", kwargs={"slug": self.tender.slug}) @@ -1309,36 +1310,49 @@ def test_only_tender_author_with_sesame_token_can_call_tender_survey_transaction # self.assertEqual(response.status_code, 302) # self.assertEqual(response.url, "/accounts/login/?next=/profil/") + # def test_update_tender_stats_on_tender_survey_transactioned_answer_empty(self): + # self.assertEqual(self.tender.survey_transactioned_answer, None) + # self.assertEqual(self.tender.siae_transactioned, None) + # # load without answer: full form + # url = ( + # reverse("tenders:detail-survey-transactioned", kwargs={"slug": self.tender.slug}) + # + self.user_buyer_1_sesame_query_string + # ) + # response = self.client.get(url, follow=True) + # self.assertEqual(response.status_code, 200) + # self.assertEqual(Tender.objects.get(id=self.tender.id).survey_transactioned_answer, None) + # self.assertEqual(Tender.objects.get(id=self.tender.id).siae_transactioned, None) + # response = self.client.post(url, data={"survey_transactioned_answer": "true", "survey_transactioned_amount": 1000, "survey_transactioned_feedback": "Feedback"}, follow=True) # noqa + # self.assertEqual(response.status_code, 200) # redirect + # self.assertRedirects(response, reverse("tenders:detail", kwargs={"slug": self.tender.slug})) + # self.assertEqual(Tender.objects.get(id=self.tender.id).survey_transactioned_answer, True) + # self.assertEqual(Tender.objects.get(id=self.tender.id).survey_transactioned_amount, 1000) + # self.assertEqual(Tender.objects.get(id=self.tender.id).siae_transactioned, True) + def test_update_tender_stats_on_tender_survey_transactioned_answer_true(self): - user_sesame_query_string = sesame_get_query_string(self.user_buyer_1) - self.assertEqual(self.tender.survey_transactioned_answer, None) - self.assertEqual(self.tender.siae_transactioned, None) - # load without answer - url = ( - reverse("tenders:detail-survey-transactioned", kwargs={"slug": self.tender.slug}) - + user_sesame_query_string - ) - response = self.client.get(url, follow=True) - self.assertEqual(response.status_code, 200) - # self.assertRedirects(response, reverse("tenders:detail", kwargs={"slug": self.tender.slug})) - self.assertEqual(Tender.objects.get(id=self.tender.id).survey_transactioned_answer, None) - self.assertEqual(Tender.objects.get(id=self.tender.id).siae_transactioned, None) - # load with answer + # load with answer 'True': partial form url = ( reverse("tenders:detail-survey-transactioned", kwargs={"slug": self.tender.slug}) - + user_sesame_query_string + + self.user_buyer_1_sesame_query_string + "&answer=True" ) response = self.client.get(url, follow=True) self.assertEqual(response.status_code, 200) - # self.assertRedirects(response, reverse("tenders:detail", kwargs={"slug": self.tender.slug})) - # self.assertContains(response, "Merci pour vote réponse") self.assertEqual(Tender.objects.get(id=self.tender.id).survey_transactioned_answer, True) self.assertEqual(Tender.objects.get(id=self.tender.id).siae_transactioned, True) - # reload with answer, ignore changes + # fill in form + response = self.client.post( + url, data={"survey_transactioned_amount": 1000, "survey_transactioned_feedback": "Feedback"}, follow=True + ) + self.assertEqual(response.status_code, 200) # redirect + self.assertRedirects(response, reverse("tenders:detail", kwargs={"slug": self.tender.slug})) + self.assertContains(response, "Merci pour votre réponse") + self.assertEqual(Tender.objects.get(id=self.tender.id).survey_transactioned_answer, True) + self.assertEqual(Tender.objects.get(id=self.tender.id).survey_transactioned_amount, 1000) + # reload with answer, ignore changes and redirect url = ( reverse("tenders:detail-survey-transactioned", kwargs={"slug": self.tender.slug}) - + user_sesame_query_string + + self.user_buyer_1_sesame_query_string + "&answer=False" ) response = self.client.get(url, follow=True) @@ -1349,35 +1363,27 @@ def test_update_tender_stats_on_tender_survey_transactioned_answer_true(self): self.assertEqual(Tender.objects.get(id=self.tender.id).siae_transactioned, True) def test_update_tender_stats_on_tender_survey_transactioned_answer_false(self): - user_sesame_query_string = sesame_get_query_string(self.user_buyer_1) - self.assertEqual(self.tender.survey_transactioned_answer, None) - self.assertEqual(self.tender.siae_transactioned, None) - # load without answer + # load with answer 'False': partial form url = ( reverse("tenders:detail-survey-transactioned", kwargs={"slug": self.tender.slug}) - + user_sesame_query_string - ) - response = self.client.get(url, follow=True) - self.assertEqual(response.status_code, 200) - # self.assertRedirects(response, reverse("tenders:detail", kwargs={"slug": self.tender.slug})) - self.assertEqual(Tender.objects.get(id=self.tender.id).survey_transactioned_answer, None) - self.assertEqual(Tender.objects.get(id=self.tender.id).siae_transactioned, None) - # load with answer - url = ( - reverse("tenders:detail-survey-transactioned", kwargs={"slug": self.tender.slug}) - + user_sesame_query_string + + self.user_buyer_1_sesame_query_string + "&answer=False" ) response = self.client.get(url, follow=True) self.assertEqual(response.status_code, 200) - # self.assertRedirects(response, reverse("tenders:detail", kwargs={"slug": self.tender.slug})) - # self.assertContains(response, "Merci pour vote réponse") self.assertEqual(Tender.objects.get(id=self.tender.id).survey_transactioned_answer, False) self.assertEqual(Tender.objects.get(id=self.tender.id).siae_transactioned, False) + # fill in form + response = self.client.post(url, data={"survey_transactioned_feedback": "Feedback"}, follow=True) + self.assertEqual(response.status_code, 200) # redirect + self.assertRedirects(response, reverse("tenders:detail", kwargs={"slug": self.tender.slug})) + self.assertContains(response, "Merci pour votre réponse") + self.assertEqual(Tender.objects.get(id=self.tender.id).survey_transactioned_answer, False) + self.assertEqual(Tender.objects.get(id=self.tender.id).survey_transactioned_amount, None) # reload with answer, ignore changes url = ( reverse("tenders:detail-survey-transactioned", kwargs={"slug": self.tender.slug}) - + user_sesame_query_string + + self.user_buyer_1_sesame_query_string + "&answer=True" ) response = self.client.get(url, follow=True) diff --git a/lemarche/www/tenders/views.py b/lemarche/www/tenders/views.py index e3f4440ac..d339dd43f 100644 --- a/lemarche/www/tenders/views.py +++ b/lemarche/www/tenders/views.py @@ -562,7 +562,7 @@ def get(self, request, *args, **kwargs): return super().get(request, *args, **kwargs) # already answered else: - messages.add_message(self.request, messages.WARNING, self.get_success_message()) + messages.add_message(self.request, messages.WARNING, self.get_success_message(already_answered=True)) return HttpResponseRedirect(self.get_success_url()) def get_context_data(self, **kwargs): @@ -570,11 +570,21 @@ def get_context_data(self, **kwargs): context["tender"] = self.object return context + def get_form_kwargs(self): + kwargs = super().get_form_kwargs() + kwargs["tender_survey_transactioned_answer"] = self.object.survey_transactioned_answer + return kwargs + + def form_valid(self, form): + super().form_valid(form) + messages.add_message(self.request, messages.SUCCESS, self.get_success_message()) + return HttpResponseRedirect(self.get_success_url()) + def get_success_url(self): success_url = reverse_lazy("tenders:detail", args=[self.kwargs.get("slug")]) return success_url - def get_success_message(self, survey_transactioned_answer=None): - if survey_transactioned_answer is None: + def get_success_message(self, already_answered=False): + if already_answered: return "Votre réponse a déjà été prise en compte." - return "Merci pour vote réponse !" + return "Merci pour votre réponse !"