Esempio n. 1
0
def test_type_string():
    """
    type_string should return a string representation of the Product type
    """
    program = ProgramFactory.create()
    run = CourseRunFactory.create()
    program_product = ProductFactory.create(content_object=program)
    assert program_product.type_string == "program"
    run_product = ProductFactory.create(content_object=run)
    assert run_product.type_string == "courserun"
Esempio n. 2
0
def test_start_date():
    """
    start_date should return a start next_run_date of the Product
    """
    program = ProgramFactory.create()
    course = CourseFactory.create()
    run = CourseRunFactory.create(course=course)

    program_product = ProductFactory.create(content_object=program)
    assert program_product.start_date == program.next_run_date
    run_product = ProductFactory.create(content_object=run)
    assert run_product.start_date == course.next_run_date
Esempio n. 3
0
def test_title():
    """
    title should return a string representation of the Product's title
    """
    program = ProgramFactory.create(title="test title of the program")
    course = CourseFactory.create(title="test title of the course")
    run = CourseRunFactory.create(course=course)

    program_product = ProductFactory.create(content_object=program)
    assert program_product.title == "test title of the program"
    run_product = ProductFactory.create(content_object=run)
    assert run_product.title == "test title of the course"
Esempio n. 4
0
def test_thumbnail_url():
    """
    thumbnail_url should return a url of the Product's thumbnail
    """
    program = ProgramFactory.create()
    program_product = ProductFactory.create(content_object=program)
    run = CourseRunFactory.create()
    run_product = ProductFactory.create(content_object=run)

    assert (program_product.thumbnail_url == program.page.thumbnail_image.
            get_rendition(CATALOG_COURSE_IMG_WAGTAIL_FILL).url)
    assert (run_product.thumbnail_url == run.course.page.thumbnail_image.
            get_rendition(CATALOG_COURSE_IMG_WAGTAIL_FILL).url)
Esempio n. 5
0
def test_product_version_save_text_id_badproduct(mocker):
    """ProductVersion.text_id should None if ProductVersion.product is invalid"""
    mock_log = mocker.patch("ecommerce.models.log")
    product_version = ProductVersionFactory.create(
        product=ProductFactory.create(content_object=LineFactory()))
    assert product_version.text_id is None
    assert mock_log.called_once_with(
        f"The content object for this ProductVersion ({product_version.id}) does not have a `text_id` property"
    )
Esempio n. 6
0
def test_sync_product_with_hubspot(mock_hubspot_request):
    """Test that send_hubspot_request is called properly for a PRODUCT sync"""
    product = ProductFactory.create()
    sync_product_with_hubspot(product.id)
    body = make_product_sync_message(product.id)
    body[0]["changeOccurredTimestamp"] = ANY
    mock_hubspot_request.assert_called_once_with("PRODUCT",
                                                 HUBSPOT_SYNC_URL,
                                                 "PUT",
                                                 body=body)
Esempio n. 7
0
def test_latest_version():
    """
    The latest_version property should return the latest product version
    """
    versions_to_create = 4
    product = ProductFactory.create()
    versions = ProductVersionFactory.create_batch(versions_to_create,
                                                  product=product)
    assert str(product) == "Product for {}".format(str(product.content_object))
    assert str(versions[0]) == "ProductVersion for {}, ${}".format(
        versions[0].description, versions[0].price)
    # Latest version should be the most recently created
    assert product.latest_version == versions[versions_to_create - 1]
Esempio n. 8
0
def test_apply_coupon_on_all_runs(include_future_runs):
    """
    Test that coupons added to all future course runs of a course,
    only if `include_future_runs = True`
    """

    course = CourseFactory.create()
    run = CourseRunFactory.create(course=course)
    coupon = CouponFactory.create(include_future_runs=include_future_runs)
    product = ProductFactory.create(content_object=run)
    CouponEligibilityFactory.create(coupon=coupon, product=product)

    # create another run with same course
    new_run = CourseRunFactory.create(course=course)
    new_product = ProductFactory.create(content_object=new_run)

    if include_future_runs:
        assert CouponEligibility.objects.filter(
            coupon=coupon, product=new_product
        ).exists()
    else:
        assert not CouponEligibility.objects.filter(
            coupon=coupon, product=new_product
        ).exists()
Esempio n. 9
0
def test_serialize_product(text_id, expected):
    """ Test that ProductSerializer has correct data """
    product_version = ProductVersionFactory.create(
        product=ProductFactory.create(
            content_object=CourseRunFactory.create(courseware_id=text_id)
        )
    )
    product = Product.objects.get(id=product_version.product.id)
    run = product.content_object
    serialized_data = ProductSerializer(instance=product).data
    assert serialized_data.get("title") == f"{run.title}: {expected}"
    assert serialized_data.get("product_type") == "courserun"
    assert serialized_data.get("id") == product.id
    assert serialized_data.get("price") == product.latest_version.price.to_eng_string()
    assert serialized_data.get("description") == product.latest_version.description
Esempio n. 10
0
def test_run_queryset(is_program):
    """
    run_queryset should return all runs related to the product
    """
    program = ProgramFactory.create()
    runs = [CourseRunFactory.create(course__program=program) for _ in range(4)]
    run = runs[2]
    obj = program if is_program else run
    product = ProductFactory.create(content_object=obj)

    def key_func(_run):
        return _run.id

    assert sorted(product.run_queryset,
                  key=key_func) == sorted(runs if is_program else [run],
                                          key=key_func)
Esempio n. 11
0
def test_send_enrollment_failure_message(mocker, is_program):
    """Test that send_enrollment_failure_message sends a message with proper formatting"""
    patched_django_mail = mocker.patch("ecommerce.mail_api.mail")
    product_object = (ProgramFactory.create()
                      if is_program else CourseRunFactory.create())
    product_version = ProductVersionFactory.create(
        product=ProductFactory.create(content_object=product_object))
    order = LineFactory.create(product_version=product_version).order
    details = "TestException on line 21"
    expected_message = "{name}({email}): Order #{order_id}, {error_obj} #{obj_id} ({obj_title})\n\n{details}".format(
        name=order.purchaser.username,
        email=order.purchaser.email,
        order_id=order.id,
        error_obj=("Program" if is_program else "Run"),
        obj_id=product_object.id,
        obj_title=product_object.title,
        details=details,
    )

    send_enrollment_failure_message(order, product_object, details)
    patched_django_mail.send_mail.assert_called_once()
    send_mail_args = patched_django_mail.send_mail.call_args[0]
    assert send_mail_args[0] == ENROLL_ERROR_EMAIL_SUBJECT
    assert send_mail_args[1] == expected_message
Esempio n. 12
0
def test_product_version_save_text_id_program():
    """ProductVersion.text_id should be set to Program.readable_id on save"""
    program = ProgramFactory.create()
    product_version = ProductVersionFactory.create(
        product=ProductFactory.create(content_object=program))
    assert product_version.text_id == program.readable_id
Esempio n. 13
0
def test_product_version_save_text_id_courserun():
    """ProductVersion.text_id should be set to CourseRun.courseware_id on save"""
    run = CourseRunFactory.create()
    product_version = ProductVersionFactory.create(
        product=ProductFactory.create(content_object=run))
    assert product_version.text_id == run.courseware_id