def test_deactivate_run_enrollment_api_fail(self, patches, keep_failed_enrollments): """ If a flag is provided, deactivate_run_enrollment should set local enrollment record to inactive even if the API call fails """ enrollment = CourseRunEnrollmentFactory.create(edx_enrolled=True) patches.edx_unenroll.side_effect = Exception deactivate_run_enrollment( enrollment, change_status=ENROLL_CHANGE_STATUS_REFUNDED, keep_failed_enrollments=keep_failed_enrollments, ) patches.edx_unenroll.assert_called_once_with(enrollment) patches.send_unenrollment_email.assert_not_called() patches.log_exception.assert_called_once() enrollment.refresh_from_db() assert enrollment.active is not keep_failed_enrollments
def handle(self, *args, **options): """Handle command execution""" user = fetch_user(options["user"]) keep_failed_enrollments = options["keep_failed_enrollments"] enrollment, _ = self.fetch_enrollment(user, options) if options["program"]: program_enrollment, run_enrollments = deactivate_program_enrollment( enrollment, change_status=ENROLL_CHANGE_STATUS_REFUNDED, keep_failed_enrollments=keep_failed_enrollments, ) else: program_enrollment = None run_enrollments = [] run_enrollment = deactivate_run_enrollment( enrollment, change_status=ENROLL_CHANGE_STATUS_REFUNDED, keep_failed_enrollments=keep_failed_enrollments, ) if run_enrollment: run_enrollments.append(run_enrollment) if program_enrollment or run_enrollments: success_msg = "Refunded enrollments for user: {} ({})\nEnrollments affected: {}".format( enrollment.user.username, enrollment.user.email, enrollment_summaries( filter(bool, [program_enrollment] + run_enrollments)), ) if enrollment.order: enrollment.order.status = Order.REFUNDED enrollment.order.save_and_log(None) success_msg += "\nOrder status set to '{}' (order id: {})".format( enrollment.order.status, enrollment.order.id) else: self.stdout.write( self.style.WARNING( "The given enrollment is not associated with an order, so no order status will be changed." )) self.stdout.write(self.style.SUCCESS(success_msg)) else: self.stdout.write( self.style.ERROR( "Failed to refund the enrollment – 'for' user: {} ({}) from course / program ({})\n" .format(user.username, user.email, options["run"] or options["program"])))
def test_deactivate_run_enrollment(self, patches): """ deactivate_run_enrollment should attempt to unenroll a user in a course run in edX and set the local enrollment record to inactive """ enrollment = CourseRunEnrollmentFactory.create(edx_enrolled=True) returned_enrollment = deactivate_run_enrollment( enrollment, change_status=ENROLL_CHANGE_STATUS_REFUNDED) patches.edx_unenroll.assert_called_once_with(enrollment) patches.send_unenrollment_email.assert_called_once_with(enrollment) enrollment.refresh_from_db() assert enrollment.change_status == ENROLL_CHANGE_STATUS_REFUNDED assert enrollment.active is False assert enrollment.edx_enrolled is False assert returned_enrollment == enrollment
def reverse_order_and_enrollments(order, enrollment): """ Sets the state of the given order and enrollment(s) to reflect that they have been refunded and are no longer active Args: order (Order): enrollment (ProgramEnrollment or CourseRunEnrollment): """ if isinstance(enrollment, ProgramEnrollment): deactivated_enrollment, _ = deactivate_program_enrollment( enrollment, change_status=ENROLL_CHANGE_STATUS_REFUNDED ) else: deactivated_enrollment = deactivate_run_enrollment( enrollment, change_status=ENROLL_CHANGE_STATUS_REFUNDED ) # When #1838 is completed, this logic can be removed if deactivated_enrollment is None: raise Exception("Enrollment change failed in edX") order.status = Order.REFUNDED order.save_and_log(acting_user=None)
def handle(self, *args, **options): from_user = fetch_user(options["from_user"]) to_user = fetch_user(options["to_user"]) keep_failed_enrollments = options["keep_failed_enrollments"] enrollment, enrolled_obj = self.fetch_enrollment(from_user, options) if options["program"]: to_user_existing_enrolled_run_ids = CourseRunEnrollment.get_program_run_enrollments( user=to_user, program=enrolled_obj).values_list("run__courseware_id", flat=True) if len(to_user_existing_enrolled_run_ids) > 0: raise CommandError( "'to' user is already enrolled in program runs ({})". format(list(to_user_existing_enrolled_run_ids))) new_program_enrollment, new_run_enrollments = self.create_program_enrollment( enrollment, to_user=to_user, keep_failed_enrollments=keep_failed_enrollments, ) if new_program_enrollment and new_run_enrollments: deactivate_program_enrollment( enrollment, change_status=ENROLL_CHANGE_STATUS_TRANSFERRED, keep_failed_enrollments=keep_failed_enrollments, ) else: new_program_enrollment = None new_run_enrollment = self.create_run_enrollment( enrollment, to_user=to_user, keep_failed_enrollments=keep_failed_enrollments, ) new_run_enrollments = [] if new_run_enrollment: new_run_enrollments.append(new_run_enrollment) deactivate_run_enrollment( enrollment, change_status=ENROLL_CHANGE_STATUS_TRANSFERRED, keep_failed_enrollments=keep_failed_enrollments, ) if new_program_enrollment or new_run_enrollments: self.stdout.write( self.style.SUCCESS( "Transferred enrollment – 'from' user: {} ({}), 'to' user: {} ({})\n" "Enrollments created/updated: {}".format( from_user.username, from_user.email, to_user.username, to_user.email, enrollment_summaries( filter(bool, [new_program_enrollment] + new_run_enrollments)), ))) else: self.stdout.write( self.style.ERROR( "Failed to transfer enrollment – 'from' user: {} ({}), 'to' user: {} ({})\n" .format( from_user.username, from_user.email, to_user.username, to_user.email, )))