From 36b8e6ca85aa966136db8e9a167f12940a1a71dc Mon Sep 17 00:00:00 2001 From: ssorin Date: Wed, 18 Dec 2024 18:19:34 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8(dashboard)=20remove=20unused=20Consen?= =?UTF-8?q?t=20creation=20and=20update=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since we now use signals to create consents, the consent is automatically created after the delivery point is created. Updated test cases to use DeliveryPoint instead of Consent directly. --- .../apps/consent/fixtures/consent.py | 4 +- src/dashboard/apps/consent/signals.py | 28 +++++------ .../apps/consent/tests/test_views.py | 47 ++++++++++++------- 3 files changed, 46 insertions(+), 33 deletions(-) diff --git a/src/dashboard/apps/consent/fixtures/consent.py b/src/dashboard/apps/consent/fixtures/consent.py index 742c2105..856188a1 100644 --- a/src/dashboard/apps/consent/fixtures/consent.py +++ b/src/dashboard/apps/consent/fixtures/consent.py @@ -36,5 +36,5 @@ def seed_consent(): DeliveryPointFactory(entity=entity4) # create awaiting consents - for delivery_point in DeliveryPoint.objects.all(): - ConsentFactory(delivery_point=delivery_point, created_by=user1) + # for delivery_point in DeliveryPoint.objects.all(): + # ConsentFactory(delivery_point=delivery_point, created_by=user1) diff --git a/src/dashboard/apps/consent/signals.py b/src/dashboard/apps/consent/signals.py index 8d3b7897..b3a09774 100644 --- a/src/dashboard/apps/consent/signals.py +++ b/src/dashboard/apps/consent/signals.py @@ -23,17 +23,17 @@ def handle_new_delivery_point(sender, instance, created, **kwargs): end=timezone.now() + timedelta(days=90), ) -@receiver(post_save, sender=DeliveryPoint) -def handle_update_delivery_point(sender, instance, update_fields, **kwargs): - """Signal triggered after a new DeliveryPoint is updated. - - si entity change - ou - si is_active change - """ - if update_fields: - Consent.objects.create( - delivery_point=instance, - start=timezone.now(), - end=timezone.now() + timedelta(days=90), - ) +# @receiver(post_save, sender=DeliveryPoint) +# def handle_update_delivery_point(sender, instance, update_fields, **kwargs): +# """Signal triggered after a new DeliveryPoint is updated. +# +# si entity change +# ou +# si is_active change +# """ +# if update_fields: +# Consent.objects.create( +# delivery_point=instance, +# start=timezone.now(), +# end=timezone.now() + timedelta(days=90), +# ) diff --git a/src/dashboard/apps/consent/tests/test_views.py b/src/dashboard/apps/consent/tests/test_views.py index 9743a9ec..248ba79b 100644 --- a/src/dashboard/apps/consent/tests/test_views.py +++ b/src/dashboard/apps/consent/tests/test_views.py @@ -1,5 +1,6 @@ """Dashboard consent views tests.""" +import uuid from http import HTTPStatus import pytest @@ -10,7 +11,7 @@ from apps.consent.factories import ConsentFactory from apps.consent.models import Consent from apps.consent.views import ConsentFormView -from apps.core.factories import EntityFactory +from apps.core.factories import DeliveryPointFactory, EntityFactory @pytest.mark.django_db @@ -23,7 +24,8 @@ def test_bulk_update_consent_status_without_ids(rf): view.setup(request) size = 4 - ConsentFactory.create_batch(size) + # ConsentFactory.create_batch(size) + DeliveryPointFactory.create_batch(size) # check data before update assert all(c == AWAITING for c in Consent.objects.values_list("status", flat=True)) @@ -49,8 +51,8 @@ def test_bulk_update_consent_status(rf): # create entity for the user and consents for the entity size = 3 entity = EntityFactory(users=(user,)) - consents = ConsentFactory.create_batch(size, delivery_point__entity=entity) - ids = [c.id for c in consents] + DeliveryPointFactory.create_batch(size, entity=entity) + ids = list(Consent.objects.values_list("id", flat=True)) # check data before update assert all(c == AWAITING for c in Consent.objects.values_list("status", flat=True)) @@ -76,8 +78,10 @@ def test_bulk_update_consent_status_with_fake_id(rf): # create entity for the user and consents for the entity size = 3 entity = EntityFactory(users=(user,)) - consents = ConsentFactory.create_batch(size, delivery_point__entity=entity) - ids = [c.id for c in consents] + # consents = ConsentFactory.create_batch(size, delivery_point__entity=entity) + # ids = [c.id for c in consents] + DeliveryPointFactory.create_batch(size, entity=entity) + ids = list(Consent.objects.values_list("id", flat=True)) # add a fake id to the ids to update ids.append("fa62cf1d-c510-498a-b428-fdf72fa35651") @@ -107,14 +111,17 @@ def test_bulk_update_consent_without_user_perms(rf): # create entity for the user and consents for the entity size = 3 entity = EntityFactory(users=(user,)) - consents = ConsentFactory.create_batch(size, delivery_point__entity=entity) - ids = [c.id for c in consents] + # consents = ConsentFactory.create_batch(size, delivery_point__entity=entity) + # ids = [c.id for c in consents] + DeliveryPointFactory.create_batch(size, entity=entity) + ids = list(Consent.objects.values_list("id", flat=True)) # create wrong consent wrong_user = UserFactory() wrong_entity = EntityFactory(users=(wrong_user,)) wrong_consent = ConsentFactory(delivery_point__entity=wrong_entity) + # add wrong_id to ids ids.append(wrong_consent.id) assert len(ids) == size + 1 @@ -154,11 +161,13 @@ def test_get_awaiting_ids_with_bad_parameters(rf): view.setup(request) # create entity for the user and consents for the entity - size = 3 - entity = EntityFactory(users=(user,)) - consents = ConsentFactory.create_batch(size, delivery_point__entity=entity) - # create a list of QuerySet instead of str - ids = [c.id for c in consents] + # size = 3 + # entity = EntityFactory(users=(user,)) + # consents = ConsentFactory.create_batch(size, delivery_point__entity=entity) + # # create a list of QuerySet instead of str + # ids = [c.id for c in consents] + ids = [uuid.uuid4(), uuid.uuid4(), uuid.uuid4()] + # check _get_awaiting_ids() raise exception # (ids must be a list of string not of QuerySet) @@ -180,8 +189,10 @@ def test_get_awaiting_ids(rf): # create entity for the user and consents for the entity size = 3 entity = EntityFactory(users=(user,)) - consents = ConsentFactory.create_batch(size, delivery_point__entity=entity) - ids = [str(c.id) for c in consents] + # consents = ConsentFactory.create_batch(size, delivery_point__entity=entity) + # ids = [str(c.id) for c in consents] + DeliveryPointFactory.create_batch(size, entity=entity) + ids = [str(c.id) for c in Consent.active_objects.filter(delivery_point__entity=entity)] # removes one `id` from the list `ids`, # this is the one we must find with _get_awaiting_ids() @@ -326,7 +337,9 @@ def test_templates_render_html_content_with_consents(rf): # create entity size = 3 entity = EntityFactory(users=(user,)) - consents = ConsentFactory.create_batch(size, delivery_point__entity=entity) + # consents = ConsentFactory.create_batch(size, delivery_point__entity=entity) + DeliveryPointFactory.create_batch(size, entity=entity) + consents = Consent.objects.filter(delivery_point__entity=entity) # Get response object and force template rendering response = view.dispatch(request) @@ -334,7 +347,7 @@ def test_templates_render_html_content_with_consents(rf): html = rendered.content.decode() assert (entity.name in html) is True - assert all(str(c.id) in html for c in consents) + assert all(str(dl.id) in html for dl in consents) @pytest.mark.django_db