Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Dec 1, 2023
1 parent a4bc772 commit 009ca22
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 33 deletions.
4 changes: 2 additions & 2 deletions lemarche/templates/tenders/_card_list_item.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
{% if tender.deadline_date_is_outdated_annotated %}
<span class="badge badge-xs badge-base badge-pill badge-pilotage float-right">Clôturé</span>
{% endif %}
{% if tender.status == tender.STATUS_DRAFT %}
{% if tender.is_draft %}
<span class="badge badge-xs badge-base badge-pill badge-outline-warning float-right">
<i class="ri-draft-fill"></i>Brouillon
</span>
{% elif tender.status == tender.STATUS_PUBLISHED %}
{% elif tender.is_pending_validation_or_validated %}
<span class="badge badge-xs badge-base badge-pill badge-outline-info float-right">
<i class="ri-loader-fill"></i>En cours de validation
</span>
Expand Down
4 changes: 2 additions & 2 deletions lemarche/templates/tenders/_detail_admin_extra_info.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ <h2 class="h3">Informations Admin</h2>
Publié le {{ tender.published_at|date }}
</li>
<li class="mb-2">
{% if tender.is_validated %}
Validé le {{ tender.validated_at|date }}
{% if tender.is_sent %}
Validé le {{ tender.sent_at|date }}
{% else %}
Statut : {{ tender.get_status_display }}
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion lemarche/templates/tenders/_detail_side_infos_author.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</div>
</div>
{% endif %}
{% if tender.is_validated %}
{% if tender.is_sent %}
<div class="alert alert-success fade show" role="status">
<div class="row">
<div class="col-auto pr-0">
Expand Down
10 changes: 5 additions & 5 deletions lemarche/templates/tenders/_list_item_buyer.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
{% if tender.deadline_date_is_outdated_annotated %}
<span class="badge badge-sm badge-base badge-pill badge-pilotage">Clôturé</span>
{% endif %}
{% if tender.status == tender.STATUS_DRAFT %}
{% if tender.is_draft %}
<span class="badge badge-sm badge-base badge-pill badge-outline-warning float-right">
<i class="ri-draft-fill"></i>Brouillon
</span>
{% elif tender.status == tender.STATUS_PUBLISHED %}
{% elif tender.is_pending_validation_or_validated %}
<span class="badge badge-sm badge-base badge-pill badge-outline-info float-right">
<i class="ri-loader-fill"></i>En cours de validation
</span>
{% elif tender.status == tender.STATUS_VALIDATED %}
{% elif tender.is_sent %}
<span class="badge badge-sm badge-base badge-pill badge-outline-success float-right">
<i class="ri-mail-send-line"></i>Envoyé
</span>
Expand All @@ -33,7 +33,7 @@ <h2 class="py-2">{{ tender.title }}</h2>
<i class="ri-time-line"></i>
Créé le {{ tender.created_at|date }}
</div>
{% if tender.status == tender.STATUS_VALIDATED %}
{% if tender.is_sent %}
<div class="col-md-4">
<i class="ri-time-line"></i>
Publié le {{ tender.validated_at|date }}
Expand All @@ -43,7 +43,7 @@ <h2 class="py-2">{{ tender.title }}</h2>
</div>
<div class="col-md-4 text-center my-auto">
<hr class="d-md-none" />
{% if tender.status == tender.STATUS_VALIDATED %}
{% if tender.is_sent %}
{% if tender.siae_detail_contact_click_since_last_seen_date_count_annotated %}
<span class="badge badge-base badge-pill badge-pilotage">
<i class="ri-thumb-up-line ri-xl"></i>&nbsp;{{ tender.siae_detail_contact_click_since_last_seen_date_count_annotated }} nouveau{{ tender.siae_detail_contact_click_since_last_seen_date_count_annotated|pluralize:"x" }} prestataire{{ tender.siae_detail_contact_click_since_last_seen_date_count_annotated|pluralize }} intéressé{{ tender.siae_detail_contact_click_since_last_seen_date_count_annotated|pluralize }}
Expand Down
8 changes: 4 additions & 4 deletions lemarche/templates/tenders/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ <h1>{{ page_title }}</h1>
{% url 'tenders:list' as TENDERS_LIST_URL %}
{% url 'tenders:list' status=tender_constants.STATUS_DRAFT as TENDERS_DRAFT_LIST_URL %}
{% url 'tenders:list' status=tender_constants.STATUS_PUBLISHED as TENDERS_PUBLISHED_LIST_URL %}
{% url 'tenders:list' status=tender_constants.STATUS_VALIDATED as TENDERS_VALIDATED_LIST_URL %}
{% url 'tenders:list' status=tender_constants.STATUS_SENT as TENDERS_SENT_LIST_URL %}

<li class="nav-item">
<a role="button" hx-push-url="true" hx-get="{{ TENDERS_LIST_URL }}"
Expand All @@ -71,8 +71,8 @@ <h1>{{ page_title }}</h1>
</a>
</li>
<li class="nav-item">
<a role="button" hx-push-url="true" hx-get="{{ TENDERS_VALIDATED_LIST_URL }}"
class="nav-link{% if request.get_full_path == TENDERS_VALIDATED_LIST_URL %} active{% endif %}"
<a role="button" hx-push-url="true" hx-get="{{ TENDERS_SENT_LIST_URL }}"
class="nav-link{% if request.get_full_path == TENDERS_SENT_LIST_URL %} active{% endif %}"
hx-target="#tendersList" hx-swap="outerHTML">
Envoyé
</a>
Expand All @@ -93,7 +93,7 @@ <h1>{{ page_title }}</h1>
{% if request.get_full_path == TENDERS_PUBLISHED_LIST_URL %}
Vous n'avez aucun besoin en cours de validation pour le moment.
{% endif %}
{% if request.get_full_path == TENDERS_VALIDATED_LIST_URL %}
{% if request.get_full_path == TENDERS_SENT_LIST_URL %}
Vous n'avez aucun besoin d'envoyé pour le moment.
<br />
Contacter notre équipe en cas de problème avec un de vos dépôts de besoins en cours de validation.
Expand Down
2 changes: 1 addition & 1 deletion lemarche/tenders/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def save_related(self, request, form, formsets, change):
super().save_related(request=request, form=form, formsets=formsets, change=change)
tender: Tender = form.instance
# we can add `and obj.status != obj.STATUS_DRAFT` to disable matching when is draft
if not tender.is_validated:
if not tender.is_validated_or_sent:
tender.set_siae_found_list()

def save_formset(self, request, form, formset, change):
Expand Down
5 changes: 3 additions & 2 deletions lemarche/tenders/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class Meta:
deadline_date = date.today() + timedelta(days=10)
start_working_date = date.today() + timedelta(days=random.randint(12, 90))
author = factory.SubFactory(UserFactory)
validated_at = timezone.now()
external_link = factory.Sequence("https://{0}example.com".format)
# Contact fields
contact_first_name = factory.Sequence("first_name{0}".format)
Expand All @@ -43,7 +42,9 @@ class Meta:
contact_phone = factory.fuzzy.FuzzyText(length=10, chars=string.digits)
# amount = tender_constants.AMOUNT_RANGE_100_150
# marche_benefits = factory.fuzzy.FuzzyChoice([key for (key, _) in constants.MARCHE_BENEFIT_CHOICES])
status = tender_constants.STATUS_VALIDATED
status = tender_constants.STATUS_SENT
validated_at = timezone.now()
sent_at = timezone.now()

@factory.post_generation
def perimeters(self, create, extracted, **kwargs):
Expand Down
6 changes: 5 additions & 1 deletion lemarche/tenders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,13 +637,17 @@ def is_pending_validation(self) -> bool:
def is_validated(self) -> bool:
return self.validated_at and self.status == tender_constants.STATUS_VALIDATED

@property
def is_pending_validation_or_validated(self) -> bool:
return self.is_pending_validation or self.is_validated

@property
def is_sent(self) -> bool:
return self.sent_at and self.status == tender_constants.STATUS_SENT

@property
def is_validated_or_sent(self) -> bool:
return self.validated_at and self.status in [tender_constants.STATUS_VALIDATED, tender_constants.STATUS_SENT]
return self.is_validated or self.is_sent

def set_validated_and_sent(self, with_save=True):
self.validated_at = timezone.now()
Expand Down
6 changes: 4 additions & 2 deletions lemarche/tenders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,12 @@ def test_status(self):
tender_pending_validation = TenderFactory(status=tender_constants.STATUS_PUBLISHED)
tender_validated_half = TenderFactory(status=tender_constants.STATUS_VALIDATED)
tender_validated_full = TenderFactory(status=tender_constants.STATUS_VALIDATED, validated_at=timezone.now())
tender_sent = TenderFactory(status=tender_constants.STATUS_SENT, sent_at=timezone.now())
self.assertTrue(tender_draft.is_draft, True)
self.assertTrue(tender_pending_validation.is_pending_validation, True)
self.assertTrue(tender_validated_half.is_validated, False)
self.assertTrue(tender_validated_full.is_validated, True)
self.assertTrue(tender_sent.is_sent, True)

def test_amount_display(self):
tender_with_amount = TenderFactory(amount=tender_constants.AMOUNT_RANGE_0_1, accept_share_amount=True)
Expand Down Expand Up @@ -329,9 +331,9 @@ def setUpTestData(cls):
TenderQuestionFactory(tender=cls.tender_with_siae_1)

def test_filter_with_siaes(self):
self.tender_with_siae_2.validated_at = None
self.tender_with_siae_2.sent_at = None
self.tender_with_siae_2.save()
# tender_with_siae_2 is not validated
# tender_with_siae_2 is not sent
self.assertEqual(Tender.objects.filter_with_siaes(self.user_siae.siaes.all()).count(), 1)

def test_with_siae_stats(self):
Expand Down
6 changes: 3 additions & 3 deletions lemarche/utils/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,16 @@ def handle_no_permission(self):
return HttpResponseRedirect(reverse_lazy("tenders:list"))


class TenderAuthorOrAdminRequiredIfNotValidatedMixin(UserPassesTestMixin):
class TenderAuthorOrAdminRequiredIfNotSentMixin(UserPassesTestMixin):
"""
If the Tender's status is not "validated", restrict access to the Tender's author (or Admin)
If the Tender's status is not "sent", restrict access to the Tender's author (or Admin)
"""

def test_func(self):
# user = self.request.user
tender_slug = self.kwargs.get("slug")
tender = get_object_or_404(Tender.objects.all(), slug=tender_slug)
if tender.status != tender_constants.STATUS_VALIDATED:
if tender.status != tender_constants.STATUS_SENT:
return TenderAuthorOrAdminRequiredMixin.test_func(self)
return True

Expand Down
2 changes: 1 addition & 1 deletion lemarche/www/dashboard_networks/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def setUpTestData(cls):
author=cls.user_buyer,
amount=tender_constants.AMOUNT_RANGE_100_150,
accept_share_amount=False,
status=tender_constants.STATUS_VALIDATED,
status=tender_constants.STATUS_SENT,
validated_at=timezone.now(),
deadline_date=timezone.now() - timedelta(days=5),
)
Expand Down
2 changes: 1 addition & 1 deletion lemarche/www/pages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def csrf_failure(request, reason=""): # noqa C901
if settings.BITOUBI_ENV == "prod":
notify_admin_tender_created(tender)

if tender.status == tender_constants.STATUS_DRAFT:
if tender.is_draft:
messages.add_message(
request=request,
level=messages.INFO,
Expand Down
24 changes: 18 additions & 6 deletions lemarche/www/tenders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,15 +387,14 @@ def setUpTestData(cls):
cls.user_buyer_1 = UserFactory(kind=User.KIND_BUYER, company_name="Entreprise Buyer")
cls.user_buyer_2 = UserFactory(kind=User.KIND_BUYER)
cls.user_partner = UserFactory(kind=User.KIND_PARTNER)
cls.tender = TenderFactory(author=cls.user_buyer_1, validated_at=timezone.now(), perimeters=[perimeter])
cls.tender = TenderFactory(author=cls.user_buyer_1, perimeters=[perimeter])
cls.tender_2 = TenderFactory(
author=cls.user_buyer_1, deadline_date=timezone.now() - timedelta(days=5), perimeters=[perimeter]
)
cls.tender_3 = TenderFactory(
author=cls.user_buyer_1,
amount=tender_constants.AMOUNT_RANGE_100_150,
accept_share_amount=False,
validated_at=timezone.now(),
deadline_date=timezone.now() - timedelta(days=5),
perimeters=[perimeter],
)
Expand Down Expand Up @@ -515,6 +514,8 @@ def setUpTestData(cls):
response_kind=[tender_constants.RESPONSE_KIND_EMAIL],
sectors=[sector_1],
location=grenoble_perimeter,
status=tender_constants.STATUS_SENT,
sent_at=timezone.now(),
)
cls.tendersiae_1_1 = TenderSiae.objects.create(
tender=cls.tender_1,
Expand All @@ -526,9 +527,14 @@ def setUpTestData(cls):
detail_contact_click_date=timezone.now(),
)
TenderQuestionFactory(tender=cls.tender_1)
cls.tender_2 = TenderFactory(author=cls.user_buyer_1, contact_company_name="Another company")
cls.tender_2 = TenderFactory(
author=cls.user_buyer_1,
contact_company_name="Another company",
status=tender_constants.STATUS_SENT,
sent_at=timezone.now(),
)

def test_anyone_can_view_validated_tenders(self):
def test_anyone_can_view_sent_tenders(self):
# anonymous
url = reverse("tenders:detail", kwargs={"slug": self.tender_1.slug})
response = self.client.get(url)
Expand All @@ -541,12 +547,18 @@ def test_anyone_can_view_validated_tenders(self):
response = self.client.get(url)
self.assertEqual(response.status_code, 200)

def test_only_author_or_admin_can_view_non_validated_tender(self):
def test_only_author_or_admin_can_view_non_sent_tender(self):
tender_draft = TenderFactory(author=self.user_buyer_1, status=tender_constants.STATUS_DRAFT)
tender_published = TenderFactory(
author=self.user_buyer_1, status=tender_constants.STATUS_PUBLISHED, published_at=timezone.now()
)
for tender in [tender_draft, tender_published]:
tender_validated_but_not_sent = TenderFactory(
author=self.user_buyer_1,
status=tender_constants.STATUS_VALIDATED,
published_at=timezone.now(),
validated_at=timezone.now(),
)
for tender in [tender_draft, tender_published, tender_validated_but_not_sent]:
# anonymous
url = reverse("tenders:detail", kwargs={"slug": tender.slug})
response = self.client.get(url)
Expand Down
4 changes: 2 additions & 2 deletions lemarche/www/tenders/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from lemarche.utils.mixins import (
SesameTenderAuthorRequiredMixin,
SiaeUserRequiredOrSiaeIdParamMixin,
TenderAuthorOrAdminRequiredIfNotValidatedMixin,
TenderAuthorOrAdminRequiredIfNotSentMixin,
TenderAuthorOrAdminRequiredMixin,
)
from lemarche.www.siaes.forms import SiaeFilterForm
Expand Down Expand Up @@ -324,7 +324,7 @@ def get_context_data(self, **kwargs):
return context


class TenderDetailView(TenderAuthorOrAdminRequiredIfNotValidatedMixin, DetailView):
class TenderDetailView(TenderAuthorOrAdminRequiredIfNotSentMixin, DetailView):
model = Tender
template_name = "tenders/detail.html"
context_object_name = "tender"
Expand Down

0 comments on commit 009ca22

Please sign in to comment.