Esempio n. 1
0
    def test_edit_with_multiple_memberships_and_same_siret(
            self, mock_call_ban_geocoding_api):
        """
        Updating information of the prescriber organization must be possible
        when user is member of multiple orgs with the same SIRET (and different types)
        (was a regression)
        """
        organization = PrescriberOrganizationWithMembershipFactory(
            kind=PrescriberOrganization.Kind.ML)
        siret = organization.siret
        user = organization.members.first()

        org2 = PrescriberOrganizationWithMembershipFactory(
            kind=PrescriberOrganization.Kind.PLIE, siret=siret)
        org2.members.add(user)
        org2.save()

        self.client.login(username=user.email, password=DEFAULT_PASSWORD)

        url = reverse("prescribers_views:edit_organization")
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

        post_data = {
            "siret": siret,
            "name": "foo",
            "description":
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
            "address_line_1": "2 Rue de Soufflenheim",
            "address_line_2": "",
            "city": "Betschdorf",
            "post_code": "67660",
            "department": "67",
            "email": "",
            "phone": "0610203050",
            "website": "https://famous-siae.com",
        }
        response = self.client.post(url, data=post_data)
        self.assertEqual(response.status_code, 302)

        url = reverse("dashboard:index")
        self.assertEqual(url, response.url)
Esempio n. 2
0
class TestAcceptPrescriberWithOrgInvitation(TestCase):
    def setUp(self):
        self.organization = PrescriberOrganizationWithMembershipFactory()
        # Create a second member to make sure emails are also
        # sent to regular members
        self.organization.members.add(PrescriberFactory())
        self.organization.save()
        self.sender = self.organization.members.first()

    def assert_invitation_is_accepted(self, response, user, invitation):
        self.assertRedirects(response, DASHBOARD_URL)

        user.refresh_from_db()
        invitation.refresh_from_db()
        self.assertTrue(user.is_prescriber)

        self.assertTrue(invitation.accepted)
        self.assertTrue(invitation.accepted_at)
        self.assertEqual(self.organization.members.count(), 3)

        # Make sure there's a welcome message.
        self.assertContains(
            response, escape(f"Vous êtes désormais membre de l'organisation {self.organization.display_name}.")
        )

        # A confirmation e-mail is sent to the invitation sender.
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(len(mail.outbox[0].to), 1)
        self.assertEqual(invitation.sender.email, mail.outbox[0].to[0])

        # Assert the user sees his new organization dashboard.
        current_org = get_current_org_or_404(response.wsgi_request)
        # A user can be member of one or more organizations
        self.assertTrue(current_org in user.prescriberorganization_set.all())

    def test_accept_prescriber_org_invitation(self):
        invitation = PrescriberWithOrgSentInvitationFactory(sender=self.sender, organization=self.organization)
        post_data = {
            "first_name": invitation.first_name,
            "last_name": invitation.last_name,
            "password1": "Erls92#32",
            "password2": "Erls92#32",
        }

        response = self.client.post(invitation.acceptance_link, data=post_data, follow=True)
        user = User.objects.get(email=invitation.email)
        self.assert_invitation_is_accepted(response, user, invitation)

    def test_accept_existing_user_is_prescriber_without_org(self):
        user = PrescriberFactory()
        invitation = PrescriberWithOrgSentInvitationFactory(
            sender=self.sender,
            organization=self.organization,
            first_name=user.first_name,
            last_name=user.last_name,
            email=user.email,
        )
        self.client.login(email=user.email, password=DEFAULT_PASSWORD)
        response = self.client.get(invitation.acceptance_link, follow=True)
        self.assert_invitation_is_accepted(response, user, invitation)

    def test_accept_existing_user_belongs_to_another_organization(self):
        user = PrescriberOrganizationWithMembershipFactory().members.first()
        invitation = PrescriberWithOrgSentInvitationFactory(
            sender=self.sender,
            organization=self.organization,
            first_name=user.first_name,
            last_name=user.last_name,
            email=user.email,
        )
        self.client.login(email=user.email, password=DEFAULT_PASSWORD)
        response = self.client.get(invitation.acceptance_link, follow=True)
        self.assert_invitation_is_accepted(response, user, invitation)

    def test_accept_existing_user_not_logged_in(self):
        invitation = PrescriberWithOrgSentInvitationFactory(sender=self.sender, organization=self.organization)
        user = PrescriberFactory()
        # The user verified its email
        EmailAddress(user_id=user.pk, email=user.email, verified=True, primary=True).save()
        invitation = PrescriberWithOrgSentInvitationFactory(
            sender=self.sender,
            organization=self.organization,
            first_name=user.first_name,
            last_name=user.last_name,
            email=user.email,
        )
        response = self.client.get(invitation.acceptance_link, follow=True)
        self.assertIn(reverse("account_login"), response.wsgi_request.get_full_path())
        self.assertFalse(invitation.accepted)

        response = self.client.post(
            response.wsgi_request.get_full_path(),
            data={"login": user.email, "password": DEFAULT_PASSWORD},
            follow=True,
        )
        self.assertTrue(response.context["user"].is_authenticated)
        self.assert_invitation_is_accepted(response, user, invitation)