Skip to content

Commit

Permalink
Update mandat object
Browse files Browse the repository at this point in the history
  • Loading branch information
Anna-Livia committed Oct 28, 2019
1 parent 84cc004 commit bb555df
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 7 deletions.
14 changes: 14 additions & 0 deletions 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")}
)
]
21 changes: 21 additions & 0 deletions aidants_connect_web/models.py
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions aidants_connect_web/signals.py
Expand Up @@ -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)
18 changes: 18 additions & 0 deletions aidants_connect_web/tests/test_models.py
Expand Up @@ -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):
Expand Down
75 changes: 73 additions & 2 deletions aidants_connect_web/tests/test_views/test_new_mandat.py
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 5 additions & 3 deletions aidants_connect_web/views/new_mandat.py
Expand Up @@ -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:
Expand Down

0 comments on commit bb555df

Please sign in to comment.