diff --git a/aidants_connect_web/migrations/0008_auto_20191021_1404.py b/aidants_connect_web/migrations/0008_auto_20191021_1404.py new file mode 100644 index 000000000..e5cc78262 --- /dev/null +++ b/aidants_connect_web/migrations/0008_auto_20191021_1404.py @@ -0,0 +1,14 @@ +# Generated by Django 2.2.4 on 2019-10-21 12:04 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [("aidants_connect_web", "0007_mandat_modified_by_access_token")] + + operations = [ + migrations.AlterUniqueTogether( + name="mandat", unique_together={("aidant", "demarche", "usager")} + ) + ] diff --git a/aidants_connect_web/models.py b/aidants_connect_web/models.py index bbd337e82..f0b67c9d2 100644 --- a/aidants_connect_web/models.py +++ b/aidants_connect_web/models.py @@ -65,6 +65,9 @@ class Mandat(models.Model): blank=False, default="No token provided" ) + class Meta: + unique_together = ["aidant", "demarche", "usager"] + class Connection(models.Model): state = models.TextField() # FS @@ -112,6 +115,24 @@ def mandat_creation(self, mandat: Mandat): ) return journal_entry + def mandat_update(self, mandat: Mandat): + aidant = mandat.aidant + usager = mandat.usager + + initiator = f"{aidant.get_full_name()} - {aidant.organisme} - {aidant.email}" + usager_info = f"{usager.get_full_name()} - {usager.id} - {usager.email}" + + journal_entry = self.create( + initiator=initiator, + usager=usager_info, + action="update_mandat", + demarche=mandat.demarche, + duree=mandat.duree, + access_token=mandat.modified_by_access_token, + mandat=mandat.id, + ) + return journal_entry + def mandat_use( self, aidant: Aidant, diff --git a/aidants_connect_web/signals.py b/aidants_connect_web/signals.py index 79f9a69b8..439993597 100644 --- a/aidants_connect_web/signals.py +++ b/aidants_connect_web/signals.py @@ -10,5 +10,8 @@ def on_login(sender, user, request, **kwargs): @receiver(post_save, sender=Mandat) -def on_mandat_change(sender, instance, **kwargs): - Journal.objects.mandat_creation(instance) +def on_mandat_change(sender, instance, created, **kwargs): + if created: + Journal.objects.mandat_creation(instance) + else: + Journal.objects.mandat_update(instance) diff --git a/aidants_connect_web/tests/test_models.py b/aidants_connect_web/tests/test_models.py index 0c3c4ec56..8245d4873 100644 --- a/aidants_connect_web/tests/test_models.py +++ b/aidants_connect_web/tests/test_models.py @@ -139,6 +139,24 @@ def test_saving_and_retrieving_mandat(self): self.assertEqual(first_mandat.demarche, "Carte grise") self.assertEqual(second_mandat.usager.family_name, "Flanders") + def test_cannot_have_two_mandat_for_user_demarche_tuple(self): + Mandat.objects.create( + aidant=self.aidant_marge, + usager=self.usager_homer, + demarche="Logement", + duree=3, + ) + self.assertEqual(Mandat.objects.count(), 1) + + self.assertRaises( + IntegrityError, + Mandat.objects.create, + aidant=self.aidant_marge, + usager=self.usager_homer, + demarche="Logement", + duree=6, + ) + class AidantModelTest(TestCase): def test_what_happens_to_password_when_not_set(self): diff --git a/aidants_connect_web/tests/test_views/test_new_mandat.py b/aidants_connect_web/tests/test_views/test_new_mandat.py index 6c223258e..14da9b679 100644 --- a/aidants_connect_web/tests/test_views/test_new_mandat.py +++ b/aidants_connect_web/tests/test_views/test_new_mandat.py @@ -12,7 +12,7 @@ from aidants_connect_web.forms import MandatForm from aidants_connect_web.views import new_mandat -from aidants_connect_web.models import Aidant, Usager, Journal, Connection +from aidants_connect_web.models import Usager, Journal, Connection, Mandat from aidants_connect_web.tests import factories fc_callback_url = settings.FC_AS_FI_CALLBACK_URL @@ -55,6 +55,7 @@ class RecapTests(TestCase): def setUp(self): self.client = Client() self.aidant_thierry = factories.UserFactory() + self.aidant_monique = factories.UserFactory(username="monique@monique.com") self.test_usager = Usager.objects.create( given_name="Fabrice", @@ -75,6 +76,10 @@ def setUp(self): ) Connection.objects.create(id=3, demarches=["papiers", "logement"], duree=1) + Connection.objects.create( + id=4, demarches=["papiers"], duree=6, usager=self.test_usager + ) + def test_recap_url_triggers_the_recap_view(self): found = resolve("/recap/") self.assertEqual(found.func, new_mandat.recap) @@ -123,6 +128,73 @@ def test_post_to_recap_without_usager_creates_error(self): messages = list(get_messages(response.wsgi_request)) self.assertEqual(len(messages), 1) + def test_updating_mandat_for_for_same_aidant(self): + self.client.force_login(self.aidant_thierry) + + # first session : creating the mandat + session = self.client.session + session["connection"] = 2 + session.save() + self.client.post("/recap/", data={"personal_data": True, "brief": True}) + self.assertEqual(Mandat.objects.count(), 2) + journal_entries = Journal.objects.all() + self.assertEqual(journal_entries.count(), 3) + self.assertEqual(journal_entries[1].action, "create_mandat") + self.assertEqual(journal_entries[2].action, "create_mandat") + + # second session : updating the mandat + session = self.client.session + session["connection"] = 4 + session.save() + + self.client.post("/recap/", data={"personal_data": True, "brief": True}) + + self.assertEqual(Mandat.objects.count(), 2) + first_mandat = Mandat.objects.get(demarche="papiers") + self.assertEqual(first_mandat.usager.given_name, "Fabrice") + self.assertEqual(first_mandat.duree, 6) + + journal_entries = Journal.objects.all() + self.assertEqual(journal_entries.count(), 4) + self.assertEqual(journal_entries[3].action, "update_mandat") + + def test_not_updating_mandat_for_different_aidant(self): + self.client.force_login(self.aidant_thierry) + + # first session : creating the mandat + session = self.client.session + session["connection"] = 2 + session.save() + self.client.post("/recap/", data={"personal_data": True, "brief": True}) + self.client.logout() + + # second session : updating the mandat + + self.client.force_login(self.aidant_monique) + + session = self.client.session + session["connection"] = 4 + session.save() + + self.client.post("/recap/", data={"personal_data": True, "brief": True}) + + self.assertEqual(Mandat.objects.count(), 3) + first_mandat = Mandat.objects.all()[0] + self.assertEqual(first_mandat.usager.given_name, "Fabrice") + self.assertEqual(first_mandat.demarche, "papiers") + self.assertEqual(first_mandat.aidant, self.aidant_thierry) + self.assertEqual(first_mandat.duree, 1) + + third_mandat = Mandat.objects.all()[2] + self.assertEqual(third_mandat.usager.given_name, "Fabrice") + self.assertEqual(third_mandat.demarche, "papiers") + self.assertEqual(third_mandat.aidant, self.aidant_monique) + self.assertEqual(third_mandat.duree, 6) + + journal_entries = Journal.objects.all() + self.assertEqual(journal_entries.count(), 5) + self.assertEqual(journal_entries[4].action, "create_mandat") + @tag("new_mandat") class GenerateMandatPDF(TestCase): @@ -164,7 +236,6 @@ def test_response_is_a_pdf_download(self): session = self.client.session session["connection"] = 1 session.save() - response = self.client.get("/generate_mandat_pdf/") self.assertEqual(response.status_code, 200) self.assertEquals( diff --git a/aidants_connect_web/views/new_mandat.py b/aidants_connect_web/views/new_mandat.py index 38eed1f62..8cb17f0af 100644 --- a/aidants_connect_web/views/new_mandat.py +++ b/aidants_connect_web/views/new_mandat.py @@ -81,12 +81,14 @@ def recap(request): if form.get("personal_data") and form.get("brief"): for demarche in connection.demarches: try: - Mandat.objects.create( + Mandat.objects.update_or_create( aidant=aidant, usager=usager, demarche=demarche, - duree=connection.duree, - modified_by_access_token=connection.access_token, + defaults={ + "duree": connection.duree, + "modified_by_access_token": connection.access_token, + }, ) except IntegrityError as e: