def test_responsable_cannot_change_an_unrelated_organisation_on_their_aidants(
            self):
        responsable = self.responsable_of_2
        respo_org_1, respo_org_2 = responsable.responsable_de.all()
        aidant = AidantFactory()
        aidant_initial_org = aidant.organisation
        aidant.organisations.add(respo_org_1)

        self.assertIn(aidant_initial_org, aidant.organisations.all())
        self.assertIn(respo_org_1, aidant.organisations.all())
        self.assertEqual(len(aidant.organisations.all()), 2)

        self.client.force_login(responsable)
        self.client.post(
            self.get_form_url(aidant),
            {"organisations": [respo_org_1.id, respo_org_2.id]},
        )

        aidant.refresh_from_db()
        self.assertIn(aidant_initial_org, aidant.organisations.all())
        self.assertIn(respo_org_1, aidant.organisations.all())
        self.assertIn(respo_org_2, aidant.organisations.all())
        self.assertEqual(
            len(aidant.organisations.all()),
            3,
            "Aidant’s organisations should contain 3 organisations.",
        )
        self.assertEqual(aidant.organisation, aidant_initial_org)
    def test_organisations_are_modified(self):
        responsable = self.responsable_of_2
        aidant = AidantFactory(organisation=responsable.organisation)
        self.client.force_login(responsable)

        self.assertEqual(len(aidant.organisations.all()), 1)
        response = self.client.post(
            self.get_form_url(aidant),
            {
                "organisations":
                [org.id for org in responsable.responsable_de.all()]
            },
        )
        self.assertRedirects(response, self.get_aidant_url(aidant))
        aidant.refresh_from_db()
        self.assertEqual(len(aidant.organisations.all()), 2)
        self.assertTrue(
            all(org in aidant.organisations.all()
                for org in responsable.responsable_de.all()))
    def test_you_can_update_username_to_match_email(self):
        aidant = AidantFactory(
            email="*****@*****.**",
            username="******",
        )
        changed_data = {
            "username": "******",
            "email": "*****@*****.**",
            "first_name": aidant.first_name,
            "last_name": aidant.last_name,
            "profession": "Mediateur",
            "organisation": str(aidant.organisation.id),
        }
        form = AidantChangeForm(
            data=changed_data,
            initial=model_to_dict(aidant),
            instance=aidant,
        )

        aidant.refresh_from_db()
        self.assertTrue(form.is_valid())
        self.assertEqual(aidant.email, "*****@*****.**")
        self.assertEqual(aidant.username, "*****@*****.**")
    def test_active_organisation_is_switched_if_necessary(self):
        responsable = self.responsable_of_2
        first_org, other_org = responsable.responsable_de.all()

        aidant = AidantFactory(organisation=first_org)
        self.assertEqual(len(aidant.organisations.all()), 1)
        self.assertIn(first_org, aidant.organisations.all())

        self.client.force_login(responsable)
        response = self.client.post(
            self.get_form_url(aidant),
            {"organisations": [other_org.id]},
        )
        self.assertRedirects(response, self.get_aidant_url(aidant))

        aidant.refresh_from_db()
        self.assertEqual(
            len(aidant.organisations.all()),
            1,
            "Aidant’s organisations should contain only one organisation.",
        )
        self.assertIn(other_org, aidant.organisations.all())
        self.assertEqual(aidant.organisation, other_org)
class DesignationOfAnotherResponsable(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.client = Client()
        cls.orga = OrganisationFactory()
        responsable = AidantFactory(organisation=cls.orga)
        responsable.responsable_de.add(cls.orga)
        cls.respo = responsable
        cls.url = f"/espace-responsable/organisation/{cls.orga.id}/responsables/"
        cls.orga_url = f"/espace-responsable/organisation/{cls.orga.id}/"

    def _create_two_aidantes(self):
        self.aidante_maxine = AidantFactory(organisation=self.orga)
        self.aidante_ariane = AidantFactory()
        self.aidante_ariane.organisations.add(self.orga)

    def test_url_triggers_the_right_view(self):
        found = resolve(self.url)
        self.assertEqual(found.func,
                         espace_responsable.organisation_responsables)

    def test_url_triggers_the_right_template(self):
        self.client.force_login(self.respo)
        response = self.client.get(self.url)
        self.assertTemplateUsed(
            response,
            "aidants_connect_web/espace_responsable/responsables.html")

    def test_link_is_hidden_if_there_is_no_aidant_to_become_responsable(self):
        self.client.force_login(self.respo)
        response = self.client.get(self.orga_url)
        self.assertNotContains(response, "Désigner un responsable")

    def test_link_is_visible_if_there_is_an_aidant_to_become_responsable(self):
        self._create_two_aidantes()
        self.client.force_login(self.respo)
        response = self.client.get(self.orga_url)
        self.assertContains(response, "Désigner un responsable")

    def test_current_aidant_can_become_responsable(self):
        self._create_two_aidantes()
        self.client.force_login(self.respo)
        self.assertFalse(self.aidante_maxine.is_responsable_structure())
        self.client.post(self.url, data={"candidate": self.aidante_maxine.id})
        self.aidante_maxine.refresh_from_db()
        self.assertTrue(self.aidante_maxine.is_responsable_structure())
        self.assertIn(self.orga, self.aidante_maxine.responsable_de.all())

    def test_aidant_can_become_responsable(self):
        self._create_two_aidantes()
        self.client.force_login(self.respo)
        self.assertFalse(self.aidante_ariane.is_responsable_structure())
        self.client.post(self.url, data={"candidate": self.aidante_ariane.id})
        self.aidante_ariane.refresh_from_db()
        self.assertTrue(self.aidante_ariane.is_responsable_structure())
        self.assertIn(self.orga, self.aidante_ariane.responsable_de.all())

    def test_unrelated_aidant_cannot_become_responsable(self):
        self.aidante_bidule = AidantFactory()
        self.client.force_login(self.respo)
        self.assertFalse(self.aidante_bidule.is_responsable_structure())
        self.client.post(self.url, data={"candidate": self.aidante_bidule.id})
        self.aidante_bidule.refresh_from_db()
        self.assertFalse(self.aidante_bidule.is_responsable_structure())
        self.assertNotIn(self.orga, self.aidante_bidule.responsable_de.all())