From 57078b077baf2c598ccece8bb9e6e2d5ec213cc7 Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Tue, 21 Nov 2023 11:03:28 +0100 Subject: [PATCH] =?UTF-8?q?[D=C3=A9p=C3=B4t=20de=20besoin]=20Renvoyer=20un?= =?UTF-8?q?e=20erreur=20404=20si=20le=20besoin=20n'est=20pas=20trouv=C3=A9?= =?UTF-8?q?=20(#977)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lemarche/utils/mixins.py | 3 ++- lemarche/www/tenders/tests.py | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lemarche/utils/mixins.py b/lemarche/utils/mixins.py index 3894a30ea..b4a103304 100644 --- a/lemarche/utils/mixins.py +++ b/lemarche/utils/mixins.py @@ -2,6 +2,7 @@ from django.contrib.auth.mixins import UserPassesTestMixin from django.contrib.auth.views import redirect_to_login from django.http import HttpResponseForbidden, HttpResponseRedirect +from django.shortcuts import get_object_or_404 from django.urls import reverse_lazy from sesame.utils import get_user as sesame_get_user @@ -161,7 +162,7 @@ class TenderAuthorOrAdminRequiredIfNotValidatedMixin(UserPassesTestMixin): def test_func(self): # user = self.request.user tender_slug = self.kwargs.get("slug") - tender = Tender.objects.get(slug=tender_slug) + tender = get_object_or_404(Tender.objects.all(), slug=tender_slug) if tender.status != tender_constants.STATUS_VALIDATED: return TenderAuthorOrAdminRequiredMixin.test_func(self) return True diff --git a/lemarche/www/tenders/tests.py b/lemarche/www/tenders/tests.py index f09436549..b6bf94ec8 100644 --- a/lemarche/www/tenders/tests.py +++ b/lemarche/www/tenders/tests.py @@ -540,6 +540,11 @@ def test_only_author_or_admin_can_view_non_validated_tender(self): self.assertEqual(response.status_code, 302) self.assertEqual(response.url, "/") + def test_tender_unknown_should_return_404(self): + url = reverse("tenders:detail", kwargs={"slug": f"{self.tender_1.slug}-bug"}) + response = self.client.get(url) + self.assertEqual(response.status_code, 404) + def test_tender_basic_fields_display(self): url = reverse("tenders:detail", kwargs={"slug": self.tender_1.slug}) response = self.client.get(url)