Exemplo n.º 1
0
def post_save_user_create_receiver(sender,
                                   instance=None,
                                   created=False,
                                   **kwargs):
    if created:
        WishlistToolkit.create_new_wishlist(user=instance)
        BillingProfileToolkit.create_new_billing_profile(user=instance)
Exemplo n.º 2
0
def test_add_courses_to_cart(courses_count, wishlist, course_factory):
    for i in range(courses_count):
        WishlistToolkit.add_course_to_wishlist(
            wishlist=wishlist, course_slug=course_factory().slug)

    wishlist.refresh_from_db()

    assert wishlist.courses.count() == courses_count
Exemplo n.º 3
0
def test_add_to_wishlist_course_with_wrong_slug(wishlist):
    with pytest.raises(Course.DoesNotExist) as exc:
        WishlistToolkit.add_course_to_wishlist(wishlist=wishlist,
                                               course_slug="somerandomslug")

    wishlist.refresh_from_db()

    assert wishlist.courses.count() == 0
Exemplo n.º 4
0
def test_add_unpublished_course_to_wishlist(wishlist, unpublished_course):
    with pytest.raises(Course.DoesNotExist) as exc:
        WishlistToolkit.add_course_to_wishlist(
            wishlist=wishlist, course_slug=unpublished_course.slug)

    wishlist.refresh_from_db()

    assert wishlist.courses.count() == 0
Exemplo n.º 5
0
def test_add_already_added_course_to_wishlist(wishlist, course_factory):
    course = course_factory()
    wishlist.courses.add(course)
    WishlistToolkit.add_course_to_wishlist(wishlist=wishlist,
                                           course_slug=course.slug)

    wishlist.refresh_from_db()

    assert wishlist.courses.count() == 1
Exemplo n.º 6
0
def test_add_course_to_wishlist(wishlist, course_factory):
    course = course_factory()
    WishlistToolkit.add_course_to_wishlist(wishlist=wishlist,
                                           course_slug=course.slug)

    wishlist.refresh_from_db()

    test_course = wishlist.courses.first()
    assert wishlist.courses.count() == 1
    assert test_course == course
Exemplo n.º 7
0
def test_remove_courses_from_cart(course_factory, wishlist, course_count):
    courses = []
    for i in range(course_count):
        course = course_factory()
        wishlist.courses.add(course)
        courses.append(course)

    for i in range(course_count):
        WishlistToolkit.remove_course_from_wishlist(
            wishlist=wishlist, course_slug=courses[-1].slug)

        assert courses[-1] not in wishlist.courses.all()
        assert wishlist.courses.count() == course_count - i - 1

        courses.pop(-1)
Exemplo n.º 8
0
def check_on_course_already_in_wishlist_api(request, *args, **kwargs):
    try:
        data = request.GET
        course_slug = data["course_slug"]
    except KeyError:
        logger.error("Request object doesn't have a slug field")
        return Response({"error": CartErrorMessages.REQUEST_FIELDS_ERROR.value}, status=400)

    user = request.user
    wishlist = WishlistToolkit.load_wishlist(user=user)
    try:
        course_already_in_wishlist = WishlistToolkit.check_on_course_in_wishlist(wishlist=wishlist, course_slug=course_slug)
    except Course.DoesNotExist:
        return Response({"error": CourseErrorMessages.COURSE_DOES_NOT_EXIST_ERROR.value}, status=400)
    return Response({"course_already_in_wishlist": course_already_in_wishlist}, status=200)
Exemplo n.º 9
0
def test_create_already_existed_wishlist(user):
    wishlist = Wishlist.objects.create(user=user)
    with pytest.raises(ValidationError) as exc:
        got = WishlistToolkit.create_new_wishlist(user=user)

    assert str(
        exc.value) == WishlistErrorMessages.WISHLIST_ALREADY_EXISTS_ERROR.value
Exemplo n.º 10
0
def remove_course_from_wishlist(request, *args, **kwargs):
    try:
        course_slug = request.POST["course_slug"]
    except KeyError:
        logger.error("Request object doesn't have a slug field")
        return Response({"error": CartErrorMessages.REQUEST_FIELDS_ERROR.value}, status=400)

    user = request.user
    ids = CartListsToolkit.get_cart_lists_ids_from_request(request)
    cart_lists = CartListsSelector.get_cart_lists_by_user_and_ids(user=request.user, ids=ids)

    try:
        WishlistToolkit.remove_course_from_wishlist(wishlist=cart_lists["wishlist"], course_slug=course_slug)
    except Course.DoesNotExist:
        return Response({"error": CourseErrorMessages.COURSE_DOES_NOT_EXIST_ERROR.value}, status=400)

    return Response({}, status=200)
Exemplo n.º 11
0
def test_load_non_existing_wishlist_call_create_method(mocker, user,
                                                       another_user):
    wishlist = Wishlist.objects.get_or_create(user=another_user)
    create_wishlist = mocker.patch(
        "carts.services.WishlistToolkit._create_wishlist",
        return_value=wishlist)
    got = WishlistToolkit.load_wishlist(user=user)

    create_wishlist.assert_called_once()
Exemplo n.º 12
0
def test_create_new_wishlist(mocker, user, another_user):
    wishlist = Wishlist.objects.get_or_create(user=another_user)
    create_wishlist = mocker.patch(
        "carts.services.WishlistToolkit._create_wishlist",
        return_value=wishlist)

    got = WishlistToolkit.create_new_wishlist(user=user)

    create_wishlist.assert_called_once()
Exemplo n.º 13
0
 def get_cart_lists_by_user_and_ids(cls, user: User, ids: dict, *args,
                                    **kwargs) -> dict:
     cart = CartToolkit.load_cart(user=user, cart_id=ids["cart_id"])
     saved_for_later = SavedForLaterToolkit.load_saved_for_later(
         user=user, saved_for_later_id=ids["saved_for_later_id"])
     if user.is_authenticated:
         wishlist = WishlistToolkit.load_wishlist(user=user)
     else:
         wishlist = None
     return {
         "cart": cart,
         "saved_for_later": saved_for_later,
         "wishlist": wishlist,
     }
Exemplo n.º 14
0
def add_course_to_wishlist_api(request, *args, **kwargs):
    try:
        course_slug = request.POST["course_slug"]
    except KeyError:
        logger.error("Request object doesn't have a slug field")
        return Response({"error": CartErrorMessages.REQUEST_FIELDS_ERROR.value}, status=400)

    user = request.user
    ids = CartListsToolkit.get_cart_lists_ids_from_request(request)
    cart_lists = CartListsSelector.get_cart_lists_by_user_and_ids(user=request.user, ids=ids)

    try:
        wishlist = WishlistToolkit.add_course_to_wishlist(wishlist=cart_lists["wishlist"], course_slug=course_slug)
    except Course.DoesNotExist:
        return Response({"error": CourseErrorMessages.COURSE_DOES_NOT_EXIST_ERROR.value}, status=400)
    CartListsToolkit.delete_duplicates_excluding_instance(course_slug=course_slug, instance=wishlist, **cart_lists)

    serializer = CartOnlyInfoSerializer(instance=cart_lists["cart"])
    return Response(serializer.data, status=200)
Exemplo n.º 15
0
def test_load_wishlist_with_anonymoun_user(anonymous_user):
    with pytest.raises(PermissionError) as exc:
        wishlist = WishlistToolkit.load_wishlist(user=anonymous_user)

    assert str(exc.value
               ) == AccountErrorMessages.USER_IS_NOT_AUTHENTICATED_ERROR.value
Exemplo n.º 16
0
def test_load_non_existing_wishlist(user):
    wishlist = WishlistToolkit.load_wishlist(user=user)

    assert wishlist.user == user
    assert wishlist.courses.count() == 0
Exemplo n.º 17
0
def test_load_existing_wishlist(wishlist):
    got = WishlistToolkit.load_wishlist(user=wishlist.user)

    assert got == wishlist
Exemplo n.º 18
0
def test_remove_not_added_course(course_factory, wishlist):
    WishlistToolkit.remove_course_from_wishlist(
        wishlist=wishlist, course_slug=course_factory().slug)

    assert wishlist.courses.count() == 0
Exemplo n.º 19
0
def test_remove_course_with_wrong_slug(wishlist):
    with pytest.raises(Course.DoesNotExist):
        WishlistToolkit.remove_course_from_wishlist(
            wishlist=wishlist, course_slug="somerandomslug")

    assert wishlist.courses.count() == 0