Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Nov 5, 2023
1 parent 7a62d64 commit 6147602
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 57 deletions.
32 changes: 17 additions & 15 deletions lemarche/templates/tenders/survey_transactioned_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,26 @@ <h1 class="h1 mb-3 mb-lg-5">
</div>
</div>

<div class="row mb-3 mb-lg-5">
<div class="col-12 col-lg-8">
<div class="bg-white d-block rounded-lg shadow-lg p-3 p-lg-5">
<fieldset>
<legend class="h5">{{ form.survey_transactioned_amount.label }}</legend>
{% bootstrap_field form.survey_transactioned_amount show_label=False %}
</fieldset>
{% if tender.survey_transactioned_answer == None or tender.survey_transactioned_answer == True %}
<div class="row mb-3 mb-lg-5">
<div class="col-12 col-lg-8">
<div class="bg-white d-block rounded-lg shadow-lg p-3 p-lg-5">
<fieldset>
<legend class="h5">{{ form.survey_transactioned_amount.label }}</legend>
{% bootstrap_field form.survey_transactioned_amount show_label=False %}
</fieldset>
</div>
</div>
</div>
<div class="col-12 col-lg-4">
<div class="alert alert-info mt-3 mt-lg-0" role="alert">
<p class="mb-0">
<i class="ri-information-line ri-lg"></i>
Cette information restera strictement confidentielle et ne sera exploitée qu'à des fins statistiques.
</p>
<div class="col-12 col-lg-4">
<div class="alert alert-info mt-3 mt-lg-0" role="alert">
<p class="mb-0">
<i class="ri-information-line ri-lg"></i>
Cette information restera strictement confidentielle et ne sera exploitée qu'à des fins statistiques.
</p>
</div>
</div>
</div>
</div>
{% endif %}

<div class="row mb-3 mb-lg-5">
<div class="col-12 col-lg-8">
Expand Down
6 changes: 5 additions & 1 deletion lemarche/www/tenders/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,15 @@ 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"
].label = "Avez-vous contractualisé avec un prestataire trouvé via le Marché de l'inclusion ?"
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()
80 changes: 43 additions & 37 deletions lemarche/www/tenders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
18 changes: 14 additions & 4 deletions lemarche/www/tenders/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,19 +562,29 @@ 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):
context = super().get_context_data(**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 !"

0 comments on commit 6147602

Please sign in to comment.