Skip to content
test_enrollment.py 50.4 KiB
Newer Older
                 'n_created': len(self.preapprove_data_emails),
                 'n_exist': 0,
                 'n_requested_approved': len(enroll_request_users)
             }]
        )
        self.assertEqual(
            len([m.to for m in mail.outbox]), len(enroll_request_users))

    def test_preapproval_inst_id_type_approve_pending_require_id_verified(self):
        assert self.course.preapproval_require_verified_inst_id is True
        enroll_request_users = [
            self.non_participation_user1, self.non_participation_user2]
        for u in enroll_request_users:
            self.c.force_login(u)
            self.c.post(self.enroll_request_url, follow=True)
            self.c.logout()
        self.flush_mailbox()
        n_expected_newly_enrolled_users = (
            len([u for u in enroll_request_users if u.institutional_id_verified]))
        expected_participation_count = (
            self.get_participation_count_by_status(participation_status.active)
            + n_expected_newly_enrolled_users
        )
        resp = self.post_preapprovel(
            self.instructor_participation.user, "institutional_id",
            self.preapprove_data_institutional_ids)
        self.assertEqual(
            self.get_participation_count_by_status(
                participation_status.active), expected_participation_count)

        self.assertResponseMessagesEqual(
            resp,
            [MESSAGE_BATCH_PREAPPROVED_RESULT_PATTERN
             % {
                 'n_created': len(self.preapprove_data_institutional_ids),
                 'n_exist': 0,
                 'n_requested_approved': n_expected_newly_enrolled_users
             }]
        )
        self.assertEqual(
            len([m.to for m in mail.outbox]), n_expected_newly_enrolled_users)


class EnrollmentPreapprovalInstIdNotRequireVerifiedTest(
                                        EnrollmentPreapprovalTestMixin, TestCase):

    # We'll have to mock course at two place if use mock, so I separate this
    # test out of EnrollmentPreapprovalTest
    course_attributes_extra = {
        "enrollment_approval_required": True,
        "preapproval_require_verified_inst_id": False}

    def test_preapproval_inst_id_type_approve_pending_not_require_id_verified(self):
        assert self.course.preapproval_require_verified_inst_id is False
        enroll_request_users = [
            self.non_participation_user1, self.non_participation_user2]
        for u in enroll_request_users:
            self.c.force_login(u)
            self.c.post(self.enroll_request_url, follow=True)
            self.c.logout()
        self.flush_mailbox()
        n_expected_newly_enrolled_users = len(enroll_request_users)
        expected_participation_count = (
            self.get_participation_count_by_status(participation_status.active)
            + n_expected_newly_enrolled_users
        )
        resp = self.post_preapprovel(
            self.instructor_participation.user, "institutional_id",
            self.preapprove_data_institutional_ids)
        self.assertEqual(
            self.get_participation_count_by_status(
                participation_status.active), expected_participation_count)

        self.assertResponseMessagesEqual(
            resp,
            [MESSAGE_BATCH_PREAPPROVED_RESULT_PATTERN
             % {
                 'n_created': len(self.preapprove_data_institutional_ids),
                 'n_exist': 0,
                 'n_requested_approved': n_expected_newly_enrolled_users
             }]
        )

        self.assertEqual(
            len([m.to for m in mail.outbox]), n_expected_newly_enrolled_users)


class EnrollmentEmailConnectionsTestMixin(LocmemBackendTestsMixin):
    #  Ensure request/decision mail will be sent with/without EmailConnection
    # settings. https://github.com/inducer/relate/pull/366
    course_attributes_extra = {"enrollment_approval_required": True}

    email_connections = {
        "enroll": {
            'host': 'smtp.gmail.com',
            'username': 'blah@blah.com',
            'password': 'password',
            'port': 587,
            'use_tls': True,
        },
    }

    email_connections_none = {}
    enrollment_email_from = "enroll@example.com"
    robot_email_from = "robot@example.com"


class EnrollmentRequestEmailConnectionsTest(
            EnrollmentEmailConnectionsTestMixin, EnrollmentTestBaseMixin, TestCase):

    def test_email_with_email_connections1(self):
        # with EMAIL_CONNECTIONS and ENROLLMENT_EMAIL_FROM configured
        with self.settings(
                EMAIL_CONNECTIONS=self.email_connections,
                ROBOT_EMAIL_FROM=self.robot_email_from,
                ENROLLMENT_EMAIL_FROM=self.enrollment_email_from):

            expected_from_email = settings.ENROLLMENT_EMAIL_FROM

            self.c.force_login(self.non_participation_user1)
            self.c.post(self.enroll_request_url, follow=True)
            msg = mail.outbox[0]
            self.assertEqual(msg.from_email, expected_from_email)

    def test_email_with_email_connections3(self):
        # with neither EMAIL_CONNECTIONS nor ENROLLMENT_EMAIL_FROM configured
        with self.settings(
                EMAIL_CONNECTIONS=self.email_connections,
                ROBOT_EMAIL_FROM=self.robot_email_from):
            if hasattr(settings, ENROLLMENT_EMAIL_FROM):
                del settings.ENROLLMENT_EMAIL_FROM

            expected_from_email = settings.ROBOT_EMAIL_FROM

            self.c.force_login(self.non_participation_user1)
            self.c.post(self.enroll_request_url, follow=True)
            msg = mail.outbox[0]
            self.assertEqual(msg.from_email, expected_from_email)


class EnrollmentDecisionEmailConnectionsTest(
        EnrollmentEmailConnectionsTestMixin, EnrollmentDecisionTestMixin, TestCase):

    # {{{ with EMAIL_CONNECTIONS and ENROLLMENT_EMAIL_FROM configured
    def test_email_with_email_connections1(self):
        with self.settings(
                RELATE_EMAIL_SMTP_ALLOW_NONAUTHORIZED_SENDER=False,
                EMAIL_CONNECTIONS=self.email_connections,
                ROBOT_EMAIL_FROM=self.robot_email_from,
                ENROLLMENT_EMAIL_FROM=self.enrollment_email_from):

            expected_from_email = settings.ENROLLMENT_EMAIL_FROM

            self.c.force_login(self.instructor_participation.user)
            self.c.post(self.my_participation_edit_url, self.approve_post_data)

            msg = mail.outbox[0]
            self.assertEqual(msg.from_email, expected_from_email)

    def test_email_with_email_connections2(self):
        with self.settings(
                RELATE_EMAIL_SMTP_ALLOW_NONAUTHORIZED_SENDER=True,
                EMAIL_CONNECTIONS=self.email_connections,
                ROBOT_EMAIL_FROM=self.robot_email_from,
                ENROLLMENT_EMAIL_FROM=self.enrollment_email_from):

            expected_from_email = self.course.from_email

            self.c.force_login(self.instructor_participation.user)
            self.c.post(self.my_participation_edit_url, self.approve_post_data)

            msg = mail.outbox[0]
            self.assertEqual(msg.from_email, expected_from_email)
    # }}}

    # {{{ with neither EMAIL_CONNECTIONS nor ENROLLMENT_EMAIL_FROM configured
    def test_email_with_email_connections3(self):
        with self.settings(
                RELATE_EMAIL_SMTP_ALLOW_NONAUTHORIZED_SENDER=False,
                ROBOT_EMAIL_FROM=self.robot_email_from):
            if hasattr(settings, EMAIL_CONNECTIONS):
                del settings.EMAIL_CONNECTIONS
            if hasattr(settings, ENROLLMENT_EMAIL_FROM):
                del settings.ENROLLMENT_EMAIL_FROM

            expected_from_email = settings.ROBOT_EMAIL_FROM

            self.c.force_login(self.instructor_participation.user)
            self.c.post(self.my_participation_edit_url, self.approve_post_data)
            msg = mail.outbox[0]
            self.assertEqual(msg.from_email, expected_from_email)

    def test_email_with_email_connections4(self):
        with self.settings(
                RELATE_EMAIL_SMTP_ALLOW_NONAUTHORIZED_SENDER=True,
                ROBOT_EMAIL_FROM=self.robot_email_from):
            if hasattr(settings, EMAIL_CONNECTIONS):
                del settings.EMAIL_CONNECTIONS
            if hasattr(settings, ENROLLMENT_EMAIL_FROM):
                del settings.ENROLLMENT_EMAIL_FROM

            expected_from_email = self.course.from_email

            self.c.force_login(self.instructor_participation.user)
            self.c.post(self.my_participation_edit_url, self.approve_post_data)
            msg = mail.outbox[0]
            self.assertEqual(msg.from_email, expected_from_email)
    # }}}


# vim: foldmethod=marker