Example #1
0
    def test_is_entitlement_regainable(self):
        """
        Test that the entitlement is not expired when created now, and is expired when created20 days
        ago with a policy that sets the expiration period to 14 days
        """
        entitlement = CourseEntitlementFactory.create(
            enrollment_course_run=self.enrollment)
        assert entitlement.is_entitlement_regainable() is True

        # Create and associate a GeneratedCertificate for a user and course and make sure it isn't regainable
        GeneratedCertificateFactory(
            user=entitlement.user,
            course_id=entitlement.enrollment_course_run.course_id,
            mode=MODES.verified,
            status=CertificateStatuses.downloadable,
        )

        assert entitlement.is_entitlement_regainable() is False

        # Create a date 20 days in the past (greater than the policy expire period of 14 days)
        # and apply it to both the entitlement and the course
        past_datetime = now() - timedelta(days=20)
        entitlement = CourseEntitlementFactory.create(
            enrollment_course_run=self.enrollment, created=past_datetime)
        self.enrollment.created = past_datetime
        self.course.start = past_datetime

        self.course.save()
        self.enrollment.save()

        assert entitlement.is_entitlement_regainable() is False

        entitlement = CourseEntitlementFactory.create(expired_at=now())

        assert entitlement.is_entitlement_regainable
Example #2
0
    def test_unfulfilled_entitlement(self, mock_course_overview, mock_pseudo_session,
                                     mock_course_runs, mock_get_programs):
        """
        When a learner has an unfulfilled entitlement, their course dashboard should have:
            - a hidden 'View Course' button
            - the text 'In order to view the course you must select a session:'
            - an unhidden course-entitlement-selection-container
            - a related programs message
        """
        program = ProgramFactory()
        CourseEntitlementFactory.create(user=self.user, course_uuid=program['courses'][0]['uuid'])
        mock_get_programs.return_value = [program]
        course_key = CourseKey.from_string('course-v1:FAKE+FA1-MA1.X+3T2017')
        mock_course_overview.return_value = CourseOverviewFactory.create(start=self.TOMORROW, id=course_key)
        mock_course_runs.return_value = [
            {
                'key': six.text_type(course_key),
                'enrollment_end': str(self.TOMORROW),
                'pacing_type': 'instructor_paced',
                'type': 'verified',
                'status': 'published'
            }
        ]
        mock_pseudo_session.return_value = {
            'key': six.text_type(course_key),
            'type': 'verified'
        }
        response = self.client.get(self.path)
        self.assertContains(response, 'class="course-target-link enter-course hidden"')
        self.assertContains(response, 'You must select a session to access the course.')
        self.assertContains(response, '<div class="course-entitlement-selection-container ">')
        self.assertContains(response, 'Related Programs:')

        # If an entitlement has already been redeemed by the user for a course run, do not let the run be selectable
        enrollment = CourseEnrollmentFactory(
            user=self.user, course=mock_course_overview.return_value, mode=CourseMode.VERIFIED
        )
        CourseEntitlementFactory.create(
            user=self.user, course_uuid=program['courses'][0]['uuid'], enrollment_course_run=enrollment
        )

        mock_course_runs.return_value = [
            {
                'key': 'course-v1:edX+toy+2012_Fall',
                'enrollment_end': str(self.TOMORROW),
                'pacing_type': 'instructor_paced',
                'type': 'verified',
                'status': 'published'
            }
        ]
        response = self.client.get(self.path)
        # There should be two entitlements on the course page, one prompting for a mandatory session, but no
        # select option for the courses as there is only the single course run which has already been redeemed
        self.assertContains(response, '<li class="course-item">', count=2)
        self.assertContains(response, 'You must select a session to access the course.')
        self.assertNotContains(response, 'To access the course, select a session.')
    def test_no_tasks_if_no_work(self, mock_task):
        """
        Verify that we never try to spin off a task if there are no database rows.
        """
        call_command('expire_old_entitlements', commit=True)
        assert mock_task.call_count == 0

        # Now confirm that the above test wasn't a fluke and we will create a task if there is work
        CourseEntitlementFactory.create()
        call_command('expire_old_entitlements', commit=True)
        assert mock_task.call_count == 1
    def test_no_commit(self, mock_task):
        """
        Verify that relevant tasks are only enqueued when the commit option is passed.
        """
        CourseEntitlementFactory.create()

        call_command('expire_old_entitlements')
        assert mock_task.call_count == 0

        call_command('expire_old_entitlements', commit=True)
        assert mock_task.call_count == 1
    def test_pagination(self, mock_task):
        """
        Verify that we chunk up our requests to celery.
        """
        for _ in range(5):
            CourseEntitlementFactory.create()

        call_command('expire_old_entitlements', commit=True, batch_size=2)

        args_list = mock_task.call_args_list
        assert len(args_list) == 3
        assert args_list[0][0] == (1, 3)
        assert args_list[1][0] == (3, 5)
        assert args_list[2][0] == (5, 6)
Example #6
0
    def test_get_user_entitlements(self):
        user2 = UserFactory()
        CourseEntitlementFactory.create()
        entitlement_user2 = CourseEntitlementFactory.create(user=user2)
        url = reverse('entitlements_api:v1:entitlements-list')
        url += '?user={username}'.format(username=user2.username)
        response = self.client.get(
            url,
            content_type='application/json',
        )
        assert response.status_code == 200

        results = response.data.get('results', [])
        assert results == CourseEntitlementSerializer([entitlement_user2], many=True).data
    def test_can_receive_discount_entitlement(self, entitlement_mode):
        """
        Ensure that only users who have not already purchased courses receive the discount.
        """
        CourseEnrollmentFactory(is_active=True,
                                course_id=self.course.id,
                                user=self.user)

        if entitlement_mode is not None:
            CourseEntitlementFactory.create(mode=entitlement_mode,
                                            user=self.user)

        applicability = can_receive_discount(user=self.user,
                                             course=self.course)
        assert applicability == (entitlement_mode is None)
Example #8
0
    def test_reinstate_unrefundable_entitlement(self):
        """ Verify that a no longer refundable entitlement does not become refundable when support reinstates it. """
        enrollment = CourseEnrollmentFactory(user=self.user, is_active=True)
        expired_entitlement = CourseEntitlementFactory.create(
            user=self.user,
            enrollment_course_run=enrollment,
            expired_at=datetime.now())
        assert expired_entitlement.is_entitlement_refundable() is False
        url = reverse(self.ENTITLEMENTS_DETAILS_PATH,
                      args=[str(expired_entitlement.uuid)])

        update_data = {
            'expired_at':
            None,
            'enrollment_course_run':
            None,
            'support_details': [{
                'unenrolled_run': str(enrollment.course.id),
                'action': CourseEntitlementSupportDetail.REISSUE,
                'comments': 'Severe illness.'
            }]
        }

        response = self.client.patch(url,
                                     data=json.dumps(update_data),
                                     content_type='application/json')
        assert response.status_code == 200

        reinstated_entitlement = CourseEntitlement.objects.get(
            uuid=expired_entitlement.uuid)
        assert reinstated_entitlement.refund_locked is True
        assert reinstated_entitlement.is_entitlement_refundable() is False
Example #9
0
    def test_ecommerce_refund_not_verified_notification_for_entitlement(
            self, mock_send_notification):
        """
        Note that we are currently notifying Support whenever a refund require approval for entitlements as
        Entitlements are only available in paid modes. This test should be updated if this logic changes
        in the future.

        PROFESSIONAL mode is used here although we never auto approve PROFESSIONAL refunds right now
        """
        httpretty.register_uri(httpretty.POST,
                               settings.ECOMMERCE_API_URL + 'refunds/',
                               status=201,
                               body='[1]',
                               content_type='application/json')
        httpretty.register_uri(httpretty.PUT,
                               settings.ECOMMERCE_API_URL +
                               'refunds/1/process/',
                               status=400,
                               body='{}',
                               content_type='application/json')
        course_entitlement = CourseEntitlementFactory.create(
            mode=CourseMode.PROFESSIONAL)
        refund_success = refund_entitlement(course_entitlement)
        assert mock_send_notification.is_called
        call_args = list(mock_send_notification.call_args)
        assert call_args[0] == (course_entitlement.user, [1])
        assert refund_success
Example #10
0
 def test_ecommerce_successful_refund(self):
     httpretty.register_uri(httpretty.POST,
                            settings.ECOMMERCE_API_URL + 'refunds/',
                            status=201,
                            body='[1]',
                            content_type='application/json')
     httpretty.register_uri(httpretty.PUT,
                            settings.ECOMMERCE_API_URL +
                            'refunds/1/process/',
                            status=200,
                            body=json.dumps({
                                "id": 9,
                                "created": "2017-12-21T18:23:49.468298Z",
                                "modified": "2017-12-21T18:24:02.741426Z",
                                "total_credit_excl_tax": "100.00",
                                "currency": "USD",
                                "status": "Complete",
                                "order": 15,
                                "user": 5
                            }),
                            content_type='application/json')
     course_entitlement = CourseEntitlementFactory.create(
         mode=CourseMode.VERIFIED)
     refund_success = refund_entitlement(course_entitlement)
     assert refund_success
Example #11
0
    def test_user_can_switch(self, mock_get_course_runs):
        mock_get_course_runs.return_value = self.return_values
        course_entitlement = CourseEntitlementFactory.create(
            user=self.user, mode=CourseMode.VERIFIED)

        url = reverse(self.ENTITLEMENTS_ENROLLMENT_NAMESPACE,
                      args=[str(course_entitlement.uuid)])
        assert course_entitlement.enrollment_course_run is None

        data = {'course_run_id': str(self.course.id)}
        response = self.client.post(
            url,
            data=json.dumps(data),
            content_type='application/json',
        )
        course_entitlement.refresh_from_db()

        assert response.status_code == 201
        assert CourseEnrollment.is_enrolled(self.user, self.course.id)

        data = {'course_run_id': str(self.course2.id)}
        response = self.client.post(
            url,
            data=json.dumps(data),
            content_type='application/json',
        )
        assert response.status_code == 201

        course_entitlement.refresh_from_db()
        assert CourseEnrollment.is_enrolled(self.user, self.course2.id)
        assert course_entitlement.enrollment_course_run is not None
def make_entitlement(expired=False):  # lint-amnesty, pylint: disable=missing-function-docstring
    age = CourseEntitlementPolicy.DEFAULT_EXPIRATION_PERIOD_DAYS
    past_datetime = datetime.now(tz=pytz.UTC) - timedelta(days=age)
    expired_at = past_datetime if expired else None
    entitlement = CourseEntitlementFactory.create(created=past_datetime,
                                                  expired_at=expired_at)
    return entitlement
Example #13
0
    def test_course_run_missing_overview_not_fulfillable(self):
        entitlement = CourseEntitlementFactory.create(mode=CourseMode.VERIFIED)

        assert not is_course_run_entitlement_fulfillable(
            CourseKey.from_string('course-v1:edx+FakeCourse+3T2017'),
            entitlement
        )
Example #14
0
    def test_unenroll_entitlement_with_audit_course_enrollment(
            self, mock_refund, mock_get_course_uuid):
        """
        Test that entitlement is not refunded if un-enroll is called on audit course un-enroll.
        """
        self.enrollment.mode = CourseMode.AUDIT
        self.enrollment.user = self.user
        self.enrollment.save()
        entitlement = CourseEntitlementFactory.create(user=self.user)
        mock_get_course_uuid.return_value = entitlement.course_uuid
        CourseEnrollment.unenroll(self.user, self.course.id)

        assert not mock_refund.called
        entitlement.refresh_from_db()
        assert entitlement.expired_at is None

        self.enrollment.mode = CourseMode.VERIFIED
        self.enrollment.is_active = True
        self.enrollment.save()
        entitlement.enrollment_course_run = self.enrollment
        entitlement.save()
        CourseEnrollment.unenroll(self.user, self.course.id)

        assert mock_refund.called
        entitlement.refresh_from_db()
        assert entitlement.expired_at < now()
Example #15
0
    def test_check_for_existing_entitlement_and_enroll(self,
                                                       mock_get_course_uuid):
        course = CourseFactory()
        CourseModeFactory(
            course_id=course.id,
            mode_slug=CourseMode.VERIFIED,
            # This must be in the future to ensure it is returned by downstream code.
            expiration_datetime=now() + timedelta(days=1))
        entitlement = CourseEntitlementFactory.create(
            mode=CourseMode.VERIFIED,
            user=self.user,
        )
        mock_get_course_uuid.return_value = entitlement.course_uuid

        assert not CourseEnrollment.is_enrolled(user=self.user,
                                                course_key=course.id)

        CourseEntitlement.check_for_existing_entitlement_and_enroll(
            user=self.user,
            course_run_key=course.id,
        )

        assert CourseEnrollment.is_enrolled(user=self.user,
                                            course_key=course.id)

        entitlement.refresh_from_db()
        assert entitlement.enrollment_course_run
Example #16
0
    def test_reinstate_entitlement(self):
        enrollment = CourseEnrollmentFactory(user=self.user, is_active=True)
        expired_entitlement = CourseEntitlementFactory.create(
            user=self.user,
            enrollment_course_run=enrollment,
            expired_at=datetime.now())
        url = reverse(self.ENTITLEMENTS_DETAILS_PATH,
                      args=[str(expired_entitlement.uuid)])

        update_data = {
            'expired_at':
            None,
            'enrollment_course_run':
            None,
            'support_details': [{
                'unenrolled_run': str(enrollment.course.id),
                'action': CourseEntitlementSupportDetail.REISSUE,
                'comments': 'Severe illness.'
            }]
        }

        response = self.client.patch(url,
                                     data=json.dumps(update_data),
                                     content_type='application/json')
        assert response.status_code == 200

        results = response.data
        reinstated_entitlement = CourseEntitlement.objects.get(
            uuid=expired_entitlement.uuid)
        assert results == CourseEntitlementSerializer(
            reinstated_entitlement).data
Example #17
0
    def test_user_already_enrolled_in_unpaid_mode(self, mock_get_course_runs):
        course_entitlement = CourseEntitlementFactory.create(
            user=self.user, mode=CourseMode.VERIFIED)
        mock_get_course_runs.return_value = self.return_values

        url = reverse(self.ENTITLEMENTS_ENROLLMENT_NAMESPACE,
                      args=[str(course_entitlement.uuid)])

        CourseEnrollment.enroll(self.user,
                                self.course.id,
                                mode=CourseMode.AUDIT)
        data = {'course_run_id': str(self.course.id)}
        response = self.client.post(
            url,
            data=json.dumps(data),
            content_type='application/json',
        )
        course_entitlement.refresh_from_db()

        assert response.status_code == 201
        assert CourseEnrollment.is_enrolled(self.user, self.course.id)
        (enrolled_mode, is_active) = CourseEnrollment.enrollment_mode_for_user(
            self.user, self.course.id)
        assert is_active and (enrolled_mode == course_entitlement.mode)
        assert course_entitlement.enrollment_course_run is not None
    def test_change_enrollment_mode_fullfills_entitlement(self, search_string_type, mock_get_course_uuid):
        """
        Assert that changing student's enrollment fulfills it's respective entitlement if it exists.
        """
        assert ManualEnrollmentAudit.get_manual_enrollment_by_email(self.student.email) is None
        enrollment = CourseEnrollment.get_enrollment(self.student, self.course.id)
        entitlement = CourseEntitlementFactory.create(
            user=self.user,
            mode=CourseMode.VERIFIED,
            enrollment_course_run=enrollment
        )
        mock_get_course_uuid.return_value = entitlement.course_uuid

        url = reverse(
            'support:enrollment_list',
            kwargs={'username_or_email': getattr(self.student, search_string_type)}
        )
        response = self.client.post(url, data={
            'course_id': str(self.course.id),
            'old_mode': CourseMode.AUDIT,
            'new_mode': CourseMode.VERIFIED,
            'reason': 'Financial Assistance'
        })
        entitlement.refresh_from_db()
        assert response.status_code == 200
        assert ManualEnrollmentAudit.get_manual_enrollment_by_email(self.student.email) is not None
        assert entitlement.enrollment_course_run is not None
        assert entitlement.is_entitlement_redeemable() is False
        self.assert_enrollment(CourseMode.VERIFIED)
Example #19
0
 def test_get_days_until_expiration(self):
     """
     Test that the expiration period is always less than or equal to the policy expiration
     """
     entitlement = CourseEntitlementFactory.create(enrollment_course_run=self.enrollment)
     # This will always either be 1 less than the expiration_period_days because the get_days_until_expiration
     # method will have had at least some time pass between object creation in setUp and this method execution,
     # or the exact same as the original expiration_period_days if somehow no time has passed
     assert entitlement.get_days_until_expiration() <= entitlement.policy.expiration_period.days
Example #20
0
    def test_course_run_not_fulfillable_enrollment_start_in_future(self):
        course_overview = self.create_course(start_from_now=-3,
                                             end_from_now=2,
                                             enrollment_start_from_now=2,
                                             enrollment_end_from_now=4)

        entitlement = CourseEntitlementFactory.create(mode=CourseMode.VERIFIED)

        assert not is_course_run_entitlement_fulfillable(
            course_overview.id, entitlement)
Example #21
0
 def test_no_ecommerce_connection_and_failure(self):
     httpretty.register_uri(httpretty.POST,
                            settings.ECOMMERCE_API_URL + 'refunds/',
                            status=404,
                            body='{}',
                            content_type='application/json')
     course_entitlement = CourseEntitlementFactory.create(
         mode=CourseMode.VERIFIED)
     refund_success = refund_entitlement(course_entitlement)
     assert not refund_success
Example #22
0
    def test_is_entitlement_redeemable(self):
        """
        Test that the entitlement is not expired when created now, and is expired when created 2 years
        ago with a policy that sets the expiration period to 450 days
        """

        entitlement = CourseEntitlementFactory.create()

        assert entitlement.is_entitlement_redeemable() is True

        # Create a date 2 years in the past (greater than the policy expire period of 450 days)
        past_datetime = now() - timedelta(days=365 * 2)
        entitlement.created = past_datetime
        entitlement.save()

        assert entitlement.is_entitlement_redeemable() is False

        entitlement = CourseEntitlementFactory.create(expired_at=now())

        assert entitlement.is_entitlement_refundable() is False
Example #23
0
    def test_delete_and_revoke_entitlement(self):
        course_entitlement = CourseEntitlementFactory.create()
        url = reverse(self.ENTITLEMENTS_DETAILS_PATH, args=[str(course_entitlement.uuid)])

        response = self.client.delete(
            url,
            content_type='application/json',
        )
        assert response.status_code == 204
        course_entitlement.refresh_from_db()
        assert course_entitlement.expired_at is not None
Example #24
0
    def test_is_entitlement_refundable(self):
        """
        Test that the entitlement is refundable when created now, and is not refundable when created 70 days
        ago with a policy that sets the expiration period to 60 days. Also test that if the entitlement is spent
        and greater than 14 days it is no longer refundable.
        """
        entitlement = CourseEntitlementFactory.create()
        assert entitlement.is_entitlement_refundable() is True

        # If there is no order_number make sure the entitlement is not refundable
        entitlement.order_number = None
        assert entitlement.is_entitlement_refundable() is False

        # Create a date 70 days in the past (greater than the policy refund expire period of 60 days)
        past_datetime = now() - timedelta(days=70)
        entitlement = CourseEntitlementFactory.create(created=past_datetime)

        assert entitlement.is_entitlement_refundable() is False

        entitlement = CourseEntitlementFactory.create(
            enrollment_course_run=self.enrollment)
        # Create a date 20 days in the past (less than the policy refund expire period of 60 days)
        # but more than the policy regain period of 14 days and also the course start
        past_datetime = now() - timedelta(days=20)
        entitlement.created = past_datetime
        self.enrollment.created = past_datetime
        self.course.start = past_datetime
        entitlement.save()
        self.course.save()
        self.enrollment.save()

        assert entitlement.is_entitlement_refundable() is False

        # Removing the entitlement being redeemed, make sure that the entitlement is refundable
        entitlement.enrollment_course_run = None

        assert entitlement.is_entitlement_refundable() is True

        entitlement = CourseEntitlementFactory.create(expired_at=now())

        assert entitlement.is_entitlement_refundable() is False
Example #25
0
    def test_staff_user_required_for_delete(self):
        not_staff_user = UserFactory()
        self.client.login(username=not_staff_user.username, password=TEST_PASSWORD)

        course_entitlement = CourseEntitlementFactory.create()
        url = reverse(self.ENTITLEMENTS_DETAILS_PATH, args=[str(course_entitlement.uuid)])

        response = self.client.delete(
            url,
            content_type='application/json',
        )
        assert response.status_code == 403
Example #26
0
    def test_course_run_fulfillable_already_enrolled_course_ended(self):
        course_overview = self.create_course(
            start_from_now=-3,
            end_from_now=-1,
            enrollment_start_from_now=-2,
            enrollment_end_from_now=-1,
        )

        entitlement = CourseEntitlementFactory.create(mode=CourseMode.VERIFIED)
        CourseEnrollmentFactory.create(user=entitlement.user, course_id=course_overview.id)

        assert is_course_run_entitlement_fulfillable(course_overview.id, entitlement)
Example #27
0
    def test_course_run_not_fulfillable_no_start_date(self):
        course_overview = self.create_course(start_from_now=-2,
                                             end_from_now=2,
                                             enrollment_start_from_now=-1,
                                             enrollment_end_from_now=1)
        course_overview.start = None
        course_overview.save()

        entitlement = CourseEntitlementFactory.create(mode=CourseMode.VERIFIED)

        assert not is_course_run_entitlement_fulfillable(
            course_overview.id, entitlement)
Example #28
0
    def test_staff_get_only_staff_entitlements(self):
        CourseEntitlementFactory.create_batch(2)
        entitlement = CourseEntitlementFactory.create(user=self.user)

        response = self.client.get(
            self.entitlements_list_url,
            content_type='application/json',
        )
        assert response.status_code == 200

        results = response.data.get('results', [])
        assert results == CourseEntitlementSerializer([entitlement], many=True).data
Example #29
0
    def test_course_run_fulfillable_user_enrolled(self):
        course_overview = self.create_course(
            start_from_now=-3,
            end_from_now=2,
            enrollment_start_from_now=-2,
            enrollment_end_from_now=1
        )

        entitlement = CourseEntitlementFactory.create(mode=CourseMode.VERIFIED)
        # Enroll User in the Course, but do not update the entitlement
        CourseEnrollmentFactory.create(user=entitlement.user, course_id=course_overview.id)

        assert is_course_run_entitlement_fulfillable(course_overview.id, entitlement)
Example #30
0
    def test_non_staff_get_select_entitlements(self):
        not_staff_user = UserFactory()
        self.client.login(username=not_staff_user.username, password=TEST_PASSWORD)
        CourseEntitlementFactory.create_batch(2)
        entitlement = CourseEntitlementFactory.create(user=not_staff_user)
        response = self.client.get(
            self.entitlements_list_url,
            content_type='application/json',
        )
        assert response.status_code == 200

        results = response.data.get('results', [])
        assert results == CourseEntitlementSerializer([entitlement], many=True).data