Пример #1
0
    def test_handle_user_post_save_with_pending_course_enrollment(
            self, mock_course_enrollment, mock_course_key):
        mock_course_key.from_string.return_value = None
        mock_course_enrollment.enroll.return_value = None
        email = "*****@*****.**"
        user = UserFactory(id=1, email=email)
        pending_link = PendingEnterpriseCustomerUserFactory(user_email=email)
        pending_enrollment = PendingEnrollmentFactory(user=pending_link)

        assert len(EnterpriseCustomerUser.objects.filter(
            user_id=user.id)) == 0, "Precondition check: no links exists"
        assert len(PendingEnterpriseCustomerUser.objects.filter(user_email=email)) == 1, \
            "Precondition check: pending link exists"
        assert len(PendingEnrollment.objects.filter(
            user=pending_link)) == 1, 'Check that only one enrollment exists.'

        parameters = {'instance': user, "created": False}
        handle_user_post_save(mock.Mock(), **parameters)
        assert len(PendingEnterpriseCustomerUser.objects.all()) == 0
        assert len(
            EnterpriseCustomerUser.objects.filter(
                enterprise_customer=pending_link.enterprise_customer,
                user_id=user.id)) == 1
        assert len(PendingEnrollment.objects.all()) == 0
        assert len(EnterpriseCourseEnrollment.objects.all()) == 1
        mock_course_enrollment.enroll.assert_called_once_with(
            user, None, mode='audit', check_access=True)
        mock_course_key.from_string.assert_called_once_with(
            pending_enrollment.course_id)
Пример #2
0
    def test_handle_user_post_save_modified_user_already_linked(self):
        email = "*****@*****.**"
        user = UserFactory(id=1, email=email)
        enterprise_customer1, enterprise_customer2 = EnterpriseCustomerFactory(
        ), EnterpriseCustomerFactory()
        existing_link = EnterpriseCustomerUserFactory(
            enterprise_customer=enterprise_customer1, user_id=user.id)
        PendingEnterpriseCustomerUserFactory(
            enterprise_customer=enterprise_customer2, user_email=email)

        assert len(EnterpriseCustomerUser.objects.filter(
            user_id=user.id)) == 1, "Precondition check: links exists"
        assert len(PendingEnterpriseCustomerUser.objects.filter(user_email=email)) == 1, \
            "Precondition check: pending link exists"

        parameters = {"instance": user, "created": False}
        handle_user_post_save(mock.Mock(), **parameters)

        link = EnterpriseCustomerUser.objects.get(user_id=user.id)
        # TODO: remove suppression when https://github.com/landscapeio/pylint-django/issues/78 is fixed
        assert link.id == existing_link.id, "Should keep existing link intact"  # pylint: disable=no-member
        assert link.enterprise_customer == enterprise_customer1, "Should keep existing link intact"

        assert len(PendingEnterpriseCustomerUser.objects.all()
                   ) == 0, "Should delete pending link"
Пример #3
0
    def test_handle_user_post_save_modified_user_already_linked(self):
        email = "*****@*****.**"
        user = UserFactory(id=1, email=email)
        enterprise_customer1, enterprise_customer2 = EnterpriseCustomerFactory(), EnterpriseCustomerFactory()
        EnterpriseCustomerUserFactory(enterprise_customer=enterprise_customer1, user_id=user.id)
        PendingEnterpriseCustomerUserFactory(enterprise_customer=enterprise_customer2, user_email=email)

        assert EnterpriseCustomerUser.objects.filter(user_id=user.id).count() == 1, "Precondition check: links exists"
        assert PendingEnterpriseCustomerUser.objects.filter(user_email=email).count() == 1, \
            "Precondition check: pending link exists"

        parameters = {"instance": user, "created": False}
        handle_user_post_save(mock.Mock(), **parameters)

        assert EnterpriseCustomerUser.objects.filter(user_id=user.id).count() == 2, "Should return 2 existing links"

        link_1 = EnterpriseCustomerUser.objects.get(
            user_id=user.id,
            enterprise_customer=enterprise_customer1,
        )
        link_2 = EnterpriseCustomerUser.objects.get(
            user_id=user.id,
            enterprise_customer=enterprise_customer2,
        )
        assert link_1.enterprise_customer == enterprise_customer1
        assert link_2.enterprise_customer == enterprise_customer2

        assert PendingEnterpriseCustomerUser.objects.count() == 0, "Should delete pending link"
Пример #4
0
    def test_handle_user_post_save_with_pending_enterprise_admin(self, mock_create_tableau_user):
        email = "*****@*****.**"
        user = UserFactory(id=1, email=email)
        enterprise_customer = EnterpriseCustomerFactory()
        PendingEnterpriseCustomerAdminUserFactory(enterprise_customer=enterprise_customer, user_email=email)

        assert PendingEnterpriseCustomerAdminUser.objects.filter(user_email=email).count() == 1, \
            "Precondition check: pending admin user exists"

        parameters = {"instance": user, "created": False}
        handle_user_post_save(mock.Mock(), **parameters)

        assert EnterpriseCustomerUser.objects.filter(user_id=user.id).count() == 1, \
            "Precondition check: enterprise customer user exists"

        enterprise_customer_user = EnterpriseCustomerUser.objects.get(user_id=user.id)

        enterprise_admin_role, __ = SystemWideEnterpriseRole.objects.get_or_create(name=ENTERPRISE_ADMIN_ROLE)
        admin_role_assignment = SystemWideEnterpriseUserRoleAssignment.objects.filter(
            user=user,
            role=enterprise_admin_role,
        )
        assert admin_role_assignment.exists()

        mock_create_tableau_user.assert_called_once_with(
            str(enterprise_customer.uuid).replace('-', ''),
            enterprise_customer_user,
        )

        assert PendingEnterpriseCustomerAdminUser.objects.filter(user_email=email).count() == 0, \
            "Final check: pending admin user no longer exists"
Пример #5
0
    def test_handle_user_post_save_with_pending_course_enrollment(self, mock_course_enrollment, mock_track_enrollment):
        mock_course_enrollment.enroll.return_value = None
        email = "*****@*****.**"
        user = UserFactory(id=1, email=email)
        pending_link = PendingEnterpriseCustomerUserFactory(user_email=email)
        course_id = 'course-v1:edX+DemoX+Demo_Course'
        PendingEnrollmentFactory(user=pending_link, course_id=course_id)

        assert EnterpriseCustomerUser.objects.filter(user_id=user.id).count() == 0, "Precondition check: no links exist"
        assert PendingEnterpriseCustomerUser.objects.filter(user_email=email).count() == 1, \
            "Precondition check: pending link exists"
        assert PendingEnrollment.objects.filter(user=pending_link).count() == 1, \
            'Precondition check: only one enrollment exists.'

        parameters = {'instance': user, "created": False}
        handle_user_post_save(mock.Mock(), **parameters)
        assert PendingEnterpriseCustomerUser.objects.count() == 0
        assert EnterpriseCustomerUser.objects.filter(
            enterprise_customer=pending_link.enterprise_customer, user_id=user.id
        ).count() == 1
        assert PendingEnrollment.objects.count() == 0
        assert EnterpriseCourseEnrollment.objects.count() == 1
        mock_course_enrollment.enroll.assert_called_once_with(
            user, CourseKey.from_string(course_id), mode='audit', check_access=True
        )
        mock_track_enrollment.assert_called_once_with('pending-admin-enrollment', user.id, course_id)
Пример #6
0
    def test_handle_user_post_save_no_user_instance_nothing_happens(self):
        # precondition checks
        assert len(PendingEnterpriseCustomerUser.objects.all()) == 0
        assert len(EnterpriseCustomerUser.objects.all()) == 0

        parameters = {"instance": None, "created": False}
        handle_user_post_save(mock.Mock(), **parameters)

        assert len(PendingEnterpriseCustomerUser.objects.all()) == 0
        assert len(EnterpriseCustomerUser.objects.all()) == 0
Пример #7
0
    def test_handle_user_post_save_no_user_instance_nothing_happens(self):
        # precondition checks
        assert PendingEnterpriseCustomerUser.objects.count() == 0
        assert EnterpriseCustomerUser.objects.count() == 0

        parameters = {"instance": None, "created": False}
        with transaction.atomic():
            handle_user_post_save(mock.Mock(), **parameters)

        assert PendingEnterpriseCustomerUser.objects.count() == 0
        assert EnterpriseCustomerUser.objects.count() == 0
Пример #8
0
    def test_handle_user_post_save_no_matching_pending_link(self):
        user = UserFactory(email="*****@*****.**")

        assert PendingEnterpriseCustomerUser.objects.count() == 0, "Precondition check: no pending links available"
        assert EnterpriseCustomerUser.objects.count() == 0, "Precondition check: no links exists"

        parameters = {"instance": user, "created": True}
        handle_user_post_save(mock.Mock(), **parameters)

        assert PendingEnterpriseCustomerUser.objects.count() == 0
        assert EnterpriseCustomerUser.objects.count() == 0
Пример #9
0
    def test_handle_user_post_save_raw(self):
        email = "*****@*****.**"
        user = UserFactory(id=1, email=email)
        PendingEnterpriseCustomerUserFactory(user_email=email)

        assert EnterpriseCustomerUser.objects.filter(user_id=user.id).count() == 0, "Precondition check: no links exist"
        assert PendingEnterpriseCustomerUser.objects.filter(user_email=email).count() == 1, \
            "Precondition check: pending link exists"

        parameters = {"instance": user, "created": False, "raw": True}
        handle_user_post_save(mock.Mock(), **parameters)

        assert EnterpriseCustomerUser.objects.filter(user_id=user.id).count() == 0, "Link have been created"
        assert PendingEnterpriseCustomerUser.objects.filter(user_email=email).count() == 1, \
            "Pending link should be kept"
Пример #10
0
    def test_handle_user_post_save_modified_user_not_linked(self):
        email = "*****@*****.**"
        user = UserFactory(id=1, email=email)
        pending_link = PendingEnterpriseCustomerUserFactory(user_email=email)

        assert EnterpriseCustomerUser.objects.filter(user_id=user.id).count() == 0, "Precondition check: no links exist"
        assert PendingEnterpriseCustomerUser.objects.filter(user_email=email).count() == 1, \
            "Precondition check: pending link exists"

        parameters = {"instance": user, "created": False}
        handle_user_post_save(mock.Mock(), **parameters)

        assert PendingEnterpriseCustomerUser.objects.count() == 0
        assert EnterpriseCustomerUser.objects.filter(
            enterprise_customer=pending_link.enterprise_customer, user_id=user.id
        ).count() == 1
Пример #11
0
    def test_handle_user_post_save_with_pending_course_enrollment(
            self,
            mock_course_enrollment,
            mock_track_enrollment,
            mock_track_event  # pylint: disable=unused-argument
    ):
        mock_course_enrollment.enroll.return_value = None
        email = "*****@*****.**"
        user = UserFactory(id=1, email=email)
        pending_link = PendingEnterpriseCustomerUserFactory(user_email=email)
        pending_link.enterprise_customer.enable_autocohorting = True
        pending_link.enterprise_customer.save()
        course_id = 'course-v1:edX+DemoX+Demo_Course'
        PendingEnrollmentFactory(user=pending_link,
                                 course_id=course_id,
                                 cohort_name=u'test_cohort')

        assert EnterpriseCustomerUser.objects.filter(
            user_id=user.id).count() == 0, "Precondition check: no links exist"
        assert PendingEnterpriseCustomerUser.objects.filter(user_email=email).count() == 1, \
            "Precondition check: pending link exists"
        assert PendingEnrollment.objects.filter(user=pending_link).count() == 1, \
            'Precondition check: only one enrollment exists.'

        mock_course_enrollment.return_value = mock.Mock(
            get_course_enrollment=mock.Mock(
                side_effect=[None, {
                    'is_active': False,
                    'mode': 'verified'
                }]),
            enroll_user_in_course=mock.Mock())
        parameters = {'instance': user, "created": False}
        with transaction.atomic():
            handle_user_post_save(mock.Mock(), **parameters)
        assert PendingEnterpriseCustomerUser.objects.count() == 0
        assert EnterpriseCustomerUser.objects.filter(
            enterprise_customer=pending_link.enterprise_customer,
            user_id=user.id).count() == 1
        assert PendingEnrollment.objects.count() == 0
        assert EnterpriseCourseEnrollment.objects.count() == 1
        mock_course_enrollment.return_value.enroll_user_in_course.assert_called_once_with(
            user.username, course_id, 'audit', cohort=u'test_cohort')
        mock_track_enrollment.assert_called_once_with(
            'pending-admin-enrollment', user.id, course_id)
Пример #12
0
    def test_handle_user_post_save_created_user_multiple_enterprises(self):
        email = "*****@*****.**"
        user = UserFactory(id=1, email=email)
        enterprise_customer = EnterpriseCustomerFactory()
        enterprise_customer_2 = EnterpriseCustomerFactory()
        PendingEnterpriseCustomerUserFactory(enterprise_customer=enterprise_customer, user_email=email)
        PendingEnterpriseCustomerUserFactory(enterprise_customer=enterprise_customer_2, user_email=email)

        assert EnterpriseCustomerUser.objects.filter(user_id=user.id).count() == 0, "Precondition check: no links exist"
        assert PendingEnterpriseCustomerUser.objects.filter(user_email=email).count() == 2, \
            "Precondition check: pending link exists"

        parameters = {"instance": user, "created": True}
        with transaction.atomic():
            handle_user_post_save(mock.Mock(), **parameters)

        assert PendingEnterpriseCustomerUser.objects.count() == 0
        assert EnterpriseCustomerUser.objects.filter(
            enterprise_customer=enterprise_customer, user_id=user.id
        ).count() == 1
        assert EnterpriseCustomerUser.objects.filter(
            enterprise_customer=enterprise_customer_2, user_id=user.id
        ).count() == 1