Skip to content

Commit

Permalink
✨(dashboard) remove unused Consent creation and update logic
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ssorin committed Dec 18, 2024
1 parent 91834ff commit 36b8e6c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/dashboard/apps/consent/fixtures/consent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
28 changes: 14 additions & 14 deletions src/dashboard/apps/consent/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
# )
47 changes: 30 additions & 17 deletions src/dashboard/apps/consent/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Dashboard consent views tests."""

import uuid
from http import HTTPStatus

import pytest
Expand All @@ -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
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -326,15 +337,17 @@ 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)
rendered = response.render()
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
Expand Down

0 comments on commit 36b8e6c

Please sign in to comment.