def setUpTestData(cls):
     cls.client = Client()
     # Create one responsable
     cls.responsable_tom = AidantFactory(username="******")
     cls.responsable_tom.responsable_de.add(
         cls.responsable_tom.organisation)
     # Create one aidant
     cls.aidant_tim = AidantFactory(
         username="******",
         organisation=cls.responsable_tom.organisation,
         first_name="Tim",
         last_name="Onier",
     )
     # Create one carte TOTP
     cls.carte = CarteTOTPFactory(serial_number="A123",
                                  seed="FA169F10A9",
                                  aidant=cls.aidant_tim)
     cls.org_id = cls.responsable_tom.organisation.id
     # Create one TOTP Device
     cls.device = TOTPDevice(tolerance=30,
                             key=cls.carte.seed,
                             user=cls.aidant_tim,
                             step=60)
     cls.device.save()
     cls.organisation_url = f"/espace-responsable/organisation/{cls.org_id}"
     cls.aidant_url = f"/espace-responsable/aidant/{cls.aidant_tim.id}/"
     cls.validation_url = (
         f"/espace-responsable/aidant/{cls.aidant_tim.id}/valider-carte")
Exemplo n.º 2
0
 def test_dissociate_on_card_without_aidant(self):
     card = CarteTOTPFactory()
     card_dissociate_url = reverse(
         "otpadmin:aidants_connect_web_carte_totp_dissociate",
         args=(card.id, ),
     )
     response = self.bizdev_client.post(card_dissociate_url)
     self.assertNotEqual(response.status_code, 500)
Exemplo n.º 3
0
 def test_associate_aidant_whith_already_another_card(self):
     aidant = AidantFactory()
     CarteTOTPFactory(aidant=aidant)
     card = CarteTOTPFactory()
     card_associate_url = reverse(
         "otpadmin:aidants_connect_web_carte_totp_associate",
         args=(card.id, ),
     )
     response = self.bizdev_client.post(card_associate_url,
                                        {"aidant": aidant.id})
     self.assertRedirects(response,
                          card_associate_url,
                          fetch_redirect_response=False)
     response = self.bizdev_client.get(card_associate_url)
     self.assertContains(response,
                         f"L’aidant {aidant} a déjà une carte TOTP.")
     card_db = CarteTOTP.objects.get(id=card.id)
     self.assertIsNone(card_db.aidant)
Exemplo n.º 4
0
    def test_associate_button_is_displayed(self):
        card = CarteTOTPFactory()
        card_change_url = reverse(
            "otpadmin:aidants_connect_web_cartetotp_change",
            args=(card.id, ),
        )
        response = self.bizdev_client.get(card_change_url)

        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Lier la carte à un aidant")
Exemplo n.º 5
0
 def test_dissociate_aidant_whithout_totp_device(self):
     aidant = AidantFactory()
     card = CarteTOTPFactory(aidant=aidant)
     card_dissociate_url = reverse(
         "otpadmin:aidants_connect_web_carte_totp_dissociate",
         args=(card.id, ),
     )
     response = self.bizdev_client.post(card_dissociate_url)
     self.assertNotEqual(response.status_code, 500)
     card_db = CarteTOTP.objects.get(id=card.id)
     self.assertIsNone(card_db.aidant)
     self.assertEqual(0, TOTPDevice.objects.filter(user=aidant).count())
Exemplo n.º 6
0
    def test_associate_aidant_get(self):
        card = CarteTOTPFactory()
        card_change_url = reverse(
            "otpadmin:aidants_connect_web_carte_totp_associate",
            args=(card.id, ),
        )
        response = self.bizdev_client.get(card_change_url)

        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(
            response, "aidants_connect_web/admin/carte_totp/associate.html")
        self.assertContains(response, "Saisissez l'aidant ci-dessous.")
Exemplo n.º 7
0
    def test_dissociate_aidant_get(self):
        aidant = AidantFactory(last_name="Delacour", first_name="Joël")
        card = CarteTOTPFactory(aidant=aidant)
        card_change_url = reverse(
            "otpadmin:aidants_connect_web_carte_totp_dissociate",
            args=(card.id, ),
        )
        response = self.bizdev_client.get(card_change_url)

        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(
            response, "aidants_connect_web/admin/carte_totp/dissociate.html")
        self.assertContains(response, f"Séparer la carte {card} de {aidant}")
Exemplo n.º 8
0
 def test_associate_aidant_whith_already_totp_device(self):
     aidant = AidantFactory()
     card = CarteTOTPFactory()
     TOTPDeviceFactory(key=card.seed, user=aidant)
     card_associate_url = reverse(
         "otpadmin:aidants_connect_web_carte_totp_associate",
         args=(card.id, ),
     )
     response = self.bizdev_client.post(card_associate_url,
                                        {"aidant": aidant.id})
     self.assertNotEqual(response.status_code, 500)
     card_db = CarteTOTP.objects.get(id=card.id)
     self.assertEqual(card_db.aidant, aidant)
Exemplo n.º 9
0
    def test_aidant_with_carte_totp_does_not_appear_in_email(self):
        responsable = AidantFactory(first_name="Arès", last_name="Ponsable")
        responsable.responsable_de.add(responsable.organisation)
        CarteTOTPFactory(aidant=responsable)
        aidant_with_carte = AidantFactory(
            first_name="Gaetan",
            last_name="Carté",
            organisation=responsable.organisation,
        )
        CarteTOTPFactory(aidant=aidant_with_carte)
        aidant_without_carte = AidantFactory(
            first_name="Roberto",
            last_name="Bogan",
            organisation=responsable.organisation,
        )

        call_command("notify_no_totp_workers")
        self.assertEqual(len(mail.outbox), 1)
        message = mail.outbox[0]
        self.assertIn(f"{aidant_without_carte}", message.body)
        self.assertNotIn(f"{aidant_with_carte}", message.body)
        self.assertNotIn("vous-même", message.body)
Exemplo n.º 10
0
    def test_associate_aidant_post(self):
        card = CarteTOTPFactory()
        card_associate_url = reverse(
            "otpadmin:aidants_connect_web_carte_totp_associate",
            args=(card.id, ),
        )
        aidant = AidantFactory()
        self.bizdev_client.post(card_associate_url, {"aidant": aidant.id})

        card_db = CarteTOTP.objects.get(id=card.id)
        totp_device = TOTPDevice.objects.get(user=aidant)
        self.assertEqual(card_db.aidant, aidant)
        self.assertEqual(card_db.seed, totp_device.key)
        self.assertTrue(totp_device.confirmed)
Exemplo n.º 11
0
 def test_do_not_destroy_unrelated_totp_device(self):
     aidant_tim = AidantFactory()
     aidant_tom = AidantFactory()
     totp_device = TOTPDeviceFactory(user=aidant_tim)
     card = CarteTOTPFactory(aidant=aidant_tom, seed=totp_device.key)
     card_dissociate_url = reverse(
         "otpadmin:aidants_connect_web_carte_totp_dissociate",
         args=(card.id, ),
     )
     response = self.bizdev_client.post(card_dissociate_url)
     self.assertNotEqual(response.status_code, 500)
     card_db = CarteTOTP.objects.get(id=card.id)
     self.assertIsNone(card_db.aidant)
     # device should be deleted only if it is associated with the same aidant
     self.assertEqual(1, TOTPDevice.objects.filter(user=aidant_tim).count())
Exemplo n.º 12
0
    def test_import_carte_totp(self):
        excel_file = path_join(
            dirname(aidants_connect_web.__file__),
            "fixtures",
            "import-aidants",
            "edge-cases",
            "carte-totp-import.xlsx",
        )
        CarteTOTPFactory(serial_number="TEST01")

        self.import_file(excel_file)

        self.assertEqual(3, Aidant.objects.count(), "Unexpected count of Aidants in DB")

        aidant_a = Aidant.objects.get(username="******")
        self.assertTrue(aidant_a.has_a_carte_totp)
        self.assertTrue(aidant_a.has_a_totp_device)
Exemplo n.º 13
0
 def setUpTestData(cls):
     cls.client = Client()
     # Create one responsable
     cls.responsable_tom = AidantFactory(username="******")
     cls.responsable_tom.responsable_de.add(cls.responsable_tom.organisation)
     # Create one aidant
     cls.aidant_tim = AidantFactory(
         username="******",
         organisation=cls.responsable_tom.organisation,
         first_name="Tim",
         last_name="Onier",
     )
     # Create one carte TOTP
     cls.carte = CarteTOTPFactory(serial_number="A123", seed="zzzz")
     cls.org_id = cls.responsable_tom.organisation.id
     cls.association_url = (
         f"/espace-responsable/aidant/{cls.aidant_tim.id}/lier-carte"
     )
     cls.validation_url = (
         f"/espace-responsable/aidant/{cls.aidant_tim.id}/valider-carte"
     )
Exemplo n.º 14
0
    def test_diagnostic_totp(self):
        tested = self.tested

        # Cases in which everything is fine
        # Not linked to any aidant or totp device
        card_ok1 = CarteTOTPFactory()
        # Linked to exactly one device and one aidant
        card_ok2 = CarteTOTPFactory(aidant=AidantFactory())
        TOTPDeviceFactory(key=card_ok2.seed, user=card_ok2.aidant)
        self.assertIn("Tout va bien", tested.totp_devices_diagnostic(card_ok1))
        self.assertIn("Tout va bien", tested.totp_devices_diagnostic(card_ok2))

        # Not linked to an aidant, but has a totp device
        card_ko1 = CarteTOTPFactory()
        TOTPDeviceFactory(key=card_ko1.seed, user=AidantFactory())
        self.assertIn(
            "Cette carte devrait être associée à l’aidant",
            tested.totp_devices_diagnostic(card_ko1),
        )

        # Card is linked to aidant but no device exists
        card_ko2 = CarteTOTPFactory(aidant=AidantFactory())
        self.assertIn(
            "Aucun device ne correspond à cette carte",
            tested.totp_devices_diagnostic(card_ko2),
        )

        # Card and device linked to different aidants
        card_ko3 = CarteTOTPFactory(aidant=AidantFactory())
        device_ko3 = TOTPDeviceFactory(key=card_ko3.seed, user=AidantFactory())
        self.assertIn(
            f"mais le device est assigné à {device_ko3.user}.",
            tested.totp_devices_diagnostic(card_ko3),
        )

        # Several devices exist
        card_ko4 = CarteTOTPFactory(aidant=AidantFactory())
        for _ in range(2):
            TOTPDeviceFactory(key=card_ko4.seed, user=card_ko4.aidant)

        self.assertIn(
            "Il faudrait garder un seul TOTP Device",
            tested.totp_devices_diagnostic(card_ko4),
        )
Exemplo n.º 15
0
    def setUpTestData(cls):
        mairie_de_houlbec = OrganisationFactory()
        aidant_thierry = AidantFactory()
        CarteTOTPFactory(aidant=aidant_thierry)
        usager_homer = UsagerFactory()
        mandat_houlbec_homer = MandatFactory(organisation=mairie_de_houlbec,
                                             usager=usager_homer)
        autorisation_justice_houlbec_homer = AutorisationFactory(
            mandat=mandat_houlbec_homer)

        Journal.objects.create(
            aidant=aidant_thierry,
            organisation=aidant_thierry.organisation,
            usager=usager_homer,
            action="use_autorisation",
            demarche="justice",
            autorisation=autorisation_justice_houlbec_homer.id,
        )
        Journal.objects.create(
            aidant=aidant_thierry,
            organisation=aidant_thierry.organisation,
            usager=usager_homer,
            action="use_autorisation",
            demarche="justice",
            autorisation=autorisation_justice_houlbec_homer.id,
        )

        # An Aidant from Stafforg is among us !
        staff_organisation = Organisation.objects.create(
            name=settings.STAFF_ORGANISATION_NAME)
        aidant_staff_organisation = AidantFactory(
            username="******", organisation=staff_organisation)
        CarteTOTPFactory(aidant=aidant_staff_organisation)

        # an aidant staff_organisation has an attestation
        # with an usager also helped by another aidant

        mandat_stafforg_homer = MandatFactory(organisation=staff_organisation,
                                              usager=usager_homer)
        autorisation_justice_stafforg_homer = AutorisationFactory(
            mandat=mandat_stafforg_homer)

        Journal.objects.create(
            aidant=aidant_staff_organisation,
            organisation=aidant_staff_organisation.organisation,
            usager=usager_homer,
            action="use_autorisation",
            demarche="justice",
            autorisation=autorisation_justice_stafforg_homer.id,
        )
        # An aidant staff_organisation has an exclusive autorisation with a user
        usager_laurent = UsagerFactory(given_name="Laurent",
                                       sub="sub for laurent")

        mandat_stafforg_laurent = MandatFactory(
            organisation=staff_organisation, usager=usager_homer)
        autorisation_justice_stafforg_laurent = AutorisationFactory(
            mandat=mandat_stafforg_laurent, )

        Journal.objects.create(
            action="use_autorisation",
            aidant=aidant_staff_organisation,
            organisation=aidant_staff_organisation.organisation,
            usager=usager_laurent,
            demarche="justice",
            autorisation=autorisation_justice_stafforg_laurent.id,
        )

        # jacqueline has an expired autorisation and no active autorisations
        usager_jacqueline = UsagerFactory(
            given_name="Jacqueline",
            creation_date=datetime(year=2000,
                                   month=1,
                                   day=1,
                                   tzinfo=timezone.utc),
            sub="new_sub_for_jacqueline",
        )
        mandat_houlbec_jacqueline = MandatFactory(
            organisation=mairie_de_houlbec,
            usager=usager_jacqueline,
            expiration_date=datetime(year=2000,
                                     month=1,
                                     day=1,
                                     tzinfo=timezone.utc),
        )

        autorisation_justice_houlbec_jacqueline = AutorisationFactory(
            mandat=mandat_houlbec_jacqueline, )

        Journal.objects.create(
            action="use_autorisation",
            aidant=aidant_thierry,
            organisation=aidant_thierry.organisation,
            usager=usager_jacqueline,
            demarche=autorisation_justice_houlbec_jacqueline.demarche,
            duree=1,
            autorisation=autorisation_justice_houlbec_jacqueline.id,
        )
        Journal.objects.filter(usager=usager_jacqueline).update(
            creation_date=datetime(
                year=2000, month=1, day=1, tzinfo=timezone.utc))
 def create_carte_for_tim(self):
     self.carte = CarteTOTPFactory(serial_number="A123",
                                   seed="FA169F10A9",
                                   aidant=self.aidant_tim)
Exemplo n.º 17
0
 def test_sn_generation_and_seed_generation(self):
     card_1 = CarteTOTPFactory()
     card_2 = CarteTOTPFactory()
     self.assertNotEqual(card_1.serial_number, card_2.serial_number)
     self.assertNotEqual(card_1.seed, card_2.seed)