Ejemplo n.º 1
0
def test_create_program_enrollments(user):
    """
    create_program_enrollments should create or reactivate local enrollment records
    """
    num_programs = 2
    order = OrderFactory.create()
    company = CompanyFactory.create()
    programs = ProgramFactory.create_batch(num_programs)
    # Create an existing deactivate enrollment to test that it gets reactivated
    ProgramEnrollmentFactory.create(
        user=user,
        program=programs[0],
        order=order,
        change_status=ENROLL_CHANGE_STATUS_REFUNDED,
        active=False,
    )

    successful_enrollments = create_program_enrollments(user,
                                                        programs,
                                                        order=order,
                                                        company=company)
    assert len(successful_enrollments) == num_programs
    enrollments = ProgramEnrollment.objects.order_by("program__id").all()
    assert len(enrollments) == len(programs)
    for (program, enrollment) in zip(programs, enrollments):
        assert enrollment.change_status is None
        assert enrollment.active is True
        assert enrollment.program == program
Ejemplo n.º 2
0
def test_create_run_enrollments(mocker, user):
    """
    create_run_enrollments should call the edX API to create enrollments, create or reactivate local
    enrollment records, and notify enrolled users via email
    """
    num_runs = 3
    order = OrderFactory.create()
    company = CompanyFactory.create()
    runs = CourseRunFactory.create_batch(num_runs)
    # Create an existing deactivate enrollment to test that it gets reactivated
    CourseRunEnrollmentFactory.create(
        user=user,
        run=runs[0],
        order=order,
        change_status=ENROLL_CHANGE_STATUS_REFUNDED,
        active=False,
    )
    patched_edx_enroll = mocker.patch("courses.api.enroll_in_edx_course_runs")
    patched_send_enrollment_email = mocker.patch(
        "courses.api.mail_api.send_course_run_enrollment_email")

    successful_enrollments, edx_request_success = create_run_enrollments(
        user, runs, order=order, company=company)
    patched_edx_enroll.assert_called_once_with(user, runs)
    assert patched_send_enrollment_email.call_count == num_runs
    assert edx_request_success is True
    assert len(successful_enrollments) == num_runs
    enrollments = CourseRunEnrollment.objects.order_by("run__id").all()
    for (run, enrollment) in zip(runs, enrollments):
        assert enrollment.change_status is None
        assert enrollment.active is True
        assert enrollment.edx_enrolled is True
        assert enrollment.run == run
        patched_send_enrollment_email.assert_any_call(enrollment)
Ejemplo n.º 3
0
def voucher_and_partial_matches(voucher_and_user_client):
    """
    Returns a voucher with partial matching CourseRuns
    """
    voucher = voucher_and_user_client.voucher
    company = CompanyFactory()
    course_run_1 = CourseRunFactory(
        start_date=datetime.combine(voucher.course_start_date_input,
                                    datetime.min.time(),
                                    tzinfo=pytz.UTC),
        live=True,
    )
    course_run_2 = CourseRunFactory(
        course__readable_id=voucher.course_id_input, live=True)
    course_run_3 = CourseRunFactory(course__title=voucher.course_title_input,
                                    live=True)
    course_run_4 = CourseRunFactory(
        course__readable_id=f"{voucher.course_id_input}-noise", live=True)
    course_run_5 = CourseRunFactory(
        course__title=f"{voucher.course_title_input}-noise", live=True)
    return SimpleNamespace(
        **vars(voucher_and_user_client),
        company=company,
        partial_matches=[
            course_run_1,
            course_run_2,
            course_run_3,
            course_run_4,
            course_run_5,
        ],
    )
Ejemplo n.º 4
0
def test_defer_enrollment(mocker, keep_failed_enrollments):
    """
    defer_enrollment should deactivate a user's existing enrollment and create an enrollment in another
    course run
    """
    course = CourseFactory.create()
    course_runs = CourseRunFactory.create_batch(3, course=course)
    order = OrderFactory.create()
    company = CompanyFactory.create()
    existing_enrollment = CourseRunEnrollmentFactory.create(run=course_runs[0],
                                                            order=order,
                                                            company=company)
    target_run = course_runs[1]
    mock_new_enrollment = mocker.Mock()
    patched_create_enrollments = mocker.patch(
        "courses.api.create_run_enrollments",
        autospec=True,
        return_value=([
            mock_new_enrollment if keep_failed_enrollments else None
        ], True),
    )
    patched_deactivate_enrollments = mocker.patch(
        "courses.api.deactivate_run_enrollment",
        autospec=True,
        return_value=existing_enrollment if keep_failed_enrollments else None,
    )

    returned_from_enrollment, returned_to_enrollment = defer_enrollment(
        existing_enrollment.user,
        existing_enrollment.run.courseware_id,
        course_runs[1].courseware_id,
        keep_failed_enrollments=keep_failed_enrollments,
    )
    assert returned_from_enrollment == patched_deactivate_enrollments.return_value
    assert returned_to_enrollment == patched_create_enrollments.return_value[
        0][0]
    patched_create_enrollments.assert_called_once_with(
        existing_enrollment.user,
        [target_run],
        order=order,
        company=company,
        keep_failed_enrollments=keep_failed_enrollments,
    )
    patched_deactivate_enrollments.assert_called_once_with(
        existing_enrollment,
        ENROLL_CHANGE_STATUS_DEFERRED,
        keep_failed_enrollments=keep_failed_enrollments,
    )
Ejemplo n.º 5
0
def single_use_coupon_json(coupon_product_ids):
    """JSON for creating a batch of single-use coupons"""
    return {
        "tag": "TEST TAG 1",
        "name": "TEST NAME 1",
        "automatic": True,
        "activation_date": "2018-01-01T00:00:00Z",
        "expiration_date": "2019-12-31T00:00:00Z",
        "amount": 0.75,
        "num_coupon_codes": 5,
        "coupon_type": "single-use",
        "company": CompanyFactory.create().id,
        "payment_type": "credit_card",
        "payment_transaction": "fake_transaction_num",
        "product_ids": coupon_product_ids,
        "include_future_runs": False,
    }
Ejemplo n.º 6
0
def promo_coupon_json(coupon_product_ids):
    """ JSON for creating a promo coupon """
    return {
        "tag": None,
        "name": "TEST NAME 2",
        "automatic": True,
        "activation_date": "2018-01-01T00:00:00Z",
        "expiration_date": "2019-12-31T00:00:00Z",
        "amount": 0.75,
        "coupon_code": "TESTPROMOCODE",
        "coupon_type": "promo",
        "company": CompanyFactory.create().id,
        "payment_type": "purchase_order",
        "payment_transaction": "fake_transaction_num",
        "product_ids": coupon_product_ids,
        "include_future_runs": False,
    }
Ejemplo n.º 7
0
def voucher_and_exact_match(voucher_and_user_client):
    """
    Returns a voucher with and an exact matching and partial matching CourseRuns
    """
    voucher = voucher_and_user_client.voucher
    exact_match = CourseRunFactory(
        start_date=datetime.combine(voucher.course_start_date_input,
                                    datetime.min.time(),
                                    tzinfo=pytz.UTC),
        course__readable_id=voucher.course_id_input,
        course__title=voucher.course_title_input,
        live=True,
    )
    return SimpleNamespace(
        **vars(voucher_and_user_client),
        company=CompanyFactory(),
        exact_match=exact_match,
    )
Ejemplo n.º 8
0
def test_send_bulk_enroll_emails(mocker, settings):
    """
    send_bulk_enroll_emails should build messages for each recipient and send them
    """
    patched_send_messages = mocker.patch(
        "ecommerce.mail_api.api.send_messages")
    patched_build_user_messages = mocker.patch(
        "ecommerce.mail_api.api.build_user_specific_messages")
    settings.SITE_BASE_URL = "http://test.com/"

    num_assignments = 2
    bulk_assignment = BulkCouponAssignmentFactory.create()
    assignments = ProductCouponAssignmentFactory.create_batch(
        num_assignments, bulk_assignment=bulk_assignment)
    new_company = CompanyFactory.create()
    new_coupon_payment_versions = CouponPaymentVersionFactory.create_batch(
        num_assignments,
        payment=factory.Iterator([
            assignment.product_coupon.coupon.payment
            for assignment in assignments
        ]),
        company=factory.Iterator([new_company, None]),
    )

    send_bulk_enroll_emails(bulk_assignment.id, assignments)

    patched_send_messages.assert_called_once()
    patched_build_user_messages.assert_called_once()
    assert patched_build_user_messages.call_args[0][0] == EMAIL_BULK_ENROLL
    recipients_and_contexts_arg = list(
        patched_build_user_messages.call_args[0][1])
    for i, assignment in enumerate(assignments):
        product_type_str = assignment.product_coupon.product.type_string
        user_message_props = recipients_and_contexts_arg[i]
        assert isinstance(user_message_props, UserMessageProps) is True
        assert user_message_props.recipient == assignment.email
        assert user_message_props.context == {
            "enrollable_title":
            assignment.product_coupon.product.content_object.title,
            "enrollment_url":
            "http://test.com/checkout/?product={}&code={}".format(
                quote_plus(
                    assignment.product_coupon.product.content_object.text_id),
                assignment.product_coupon.coupon.coupon_code,
            ),
            "company_name": (None if not new_coupon_payment_versions[i].company
                             else new_coupon_payment_versions[i].company.name),
            "expiration_date":
            new_coupon_payment_versions[i].expiration_date.strftime(
                EMAIL_DATE_FORMAT),
        }
        assert user_message_props.metadata == EmailMetadata(
            tags=[BULK_ENROLLMENT_EMAIL_TAG],
            user_variables={
                "bulk_assignment":
                bulk_assignment.id,
                "enrollment_code":
                assignment.product_coupon.coupon.coupon_code,
                product_type_str:
                assignment.product_coupon.product.content_object.text_id,
            },
        )
Ejemplo n.º 9
0
def company():
    """Company object fixture"""
    return CompanyFactory.create(name="MIT")
Ejemplo n.º 10
0
def test_serialize_company():
    """ Test that CompanySerializer has correct data """
    company = CompanyFactory.create()
    serialized_data = CompanySerializer(instance=company).data
    assert serialized_data == {"id": company.id, "name": company.name}