예제 #1
0
def test_serialize_line():
    """Test that LineSerializer produces the correct serialized data"""
    line = LineFactory.create()
    serialized_data = LineSerializer(instance=line).data
    assert serialized_data == {
        "id": line.id,
        "product": format_hubspot_id(line.product_version.product.id),
        "order": format_hubspot_id(line.order_id),
        "quantity": line.quantity,
        "status": line.order.status,
        "product_id": line.product_version.text_id,
    }
예제 #2
0
def test_serialize_b2b_product_version():
    """Test that B2BProductVersionToLineSerializer produces the correct serialized data"""
    order = B2BOrderFactory.create(status=Order.FULFILLED, num_seats=10)
    serialized_data = B2BProductVersionToLineSerializer(instance=order).data
    assert serialized_data == {
        "id": format_hubspot_id(order.product_version.id),
        "product": format_hubspot_id(order.product_version.product.id),
        "order": format_hubspot_id(order.integration_id),
        "quantity": order.num_seats,
        "status": order.status,
        "product_id": order.product_version.text_id,
    }
예제 #3
0
def test_serialize_order(status):
    """Test that OrderToDealSerializer produces the correct serialized data"""
    order = OrderFactory.create(status=status)
    line = LineFactory.create(order=order)
    serialized_data = OrderToDealSerializer(instance=order).data
    assert serialized_data == {
        "id": order.id,
        "name": f"XPRO-ORDER-{order.id}",
        "purchaser": format_hubspot_id(order.purchaser.id),
        "stage": ORDER_STATUS_MAPPING[status],
        "amount": line.product_version.price.to_eng_string(),
        "discount_amount": "0.0000",
        "close_date": (
            int(order.updated_on.timestamp() * 1000)
            if status == Order.FULFILLED
            else None
        ),
        "coupon_code": None,
        "company": None,
        "payment_type": None,
        "payment_transaction": None,
        "discount_percent": "0",
        "order_type": ORDER_TYPE_B2C,
        "lines": [LineSerializer(instance=line).data],
        "status": order.status,
    }
예제 #4
0
def test_serialize_order_with_coupon():
    """Test that OrderToDealSerializer produces the correct serialized data for an order with coupon"""
    line = LineFactory.create()
    order = line.order
    coupon_redemption = CouponRedemptionFactory.create(order=order)
    discount = round_half_up(
        coupon_redemption.coupon_version.payment_version.amount
        * line.product_version.price
    )
    serialized_data = OrderToDealSerializer(instance=order).data
    assert serialized_data == {
        "id": order.id,
        "name": f"XPRO-ORDER-{order.id}",
        "purchaser": format_hubspot_id(order.purchaser.id),
        "stage": ORDER_STATUS_MAPPING[order.status],
        "amount": line.product_version.price.to_eng_string(),
        "discount_amount": discount.to_eng_string(),
        "close_date": (
            int(order.updated_on.timestamp() * 1000)
            if order.status == Order.FULFILLED
            else None
        ),
        "coupon_code": coupon_redemption.coupon_version.coupon.coupon_code,
        "company": coupon_redemption.coupon_version.payment_version.company.name,
        "order_type": ORDER_TYPE_B2B,
        "payment_type": coupon_redemption.coupon_version.payment_version.payment_type,
        "payment_transaction": coupon_redemption.coupon_version.payment_version.payment_transaction,
        "discount_percent": (
            coupon_redemption.coupon_version.payment_version.amount * 100
        ).to_eng_string(),
        "lines": [LineSerializer(instance=line).data],
        "status": order.status,
    }
예제 #5
0
def test_serialize_b2b_order(status, existing_user, user):
    """Test that B2BOrderToDealSerializer produces the correct serialized data"""
    order = B2BOrderFactory.create(status=status, num_seats=10)
    purchaser_id = order.email
    if existing_user:
        order.email = user.email
        purchaser_id = user.id
    serialized_data = B2BOrderToDealSerializer(instance=order).data
    assert serialized_data == {
        "id": order.id,
        "name": f"XPRO-B2BORDER-{order.id}",
        "stage": ORDER_STATUS_MAPPING[status],
        "amount": order.total_price.to_eng_string(),
        "discount_amount": None,
        "close_date": (
            int(order.updated_on.timestamp() * 1000)
            if status == Order.FULFILLED
            else None
        ),
        "coupon_code": None,
        "company": None,
        "payment_type": None,
        "payment_transaction": None,
        "discount_percent": None,
        "num_seats": 10,
        "status": order.status,
        "purchaser": format_hubspot_id(purchaser_id),
    }
예제 #6
0
def test_serialize_b2b_order_with_coupon(client, mocker):
    """Test that B2BOrderToDealSerializer produces the correct serialized data for an order with coupon"""

    product_version = ProductVersionFactory.create(price=10)
    payload = {"a": "payload"}
    mocker.patch(
        "b2b_ecommerce.views.generate_b2b_cybersource_sa_payload",
        autospec=True,
        return_value=payload,
    )
    coupon = B2BCouponFactory.create(
        product=product_version.product, discount_percent=Decimal(0.8)
    )
    num_seats = 10
    resp = client.post(
        reverse("b2b-checkout"),
        {
            "num_seats": num_seats,
            "email": "*****@*****.**",
            "product_version_id": product_version.id,
            "discount_code": coupon.coupon_code,
            "contract_number": "",
        },
    )

    assert resp.status_code == status.HTTP_200_OK
    assert B2BOrder.objects.count() == 1
    order = B2BOrder.objects.first()
    discount = round(Decimal(coupon.discount_percent) * 100, 2)
    serialized_data = B2BOrderToDealSerializer(instance=order).data
    assert serialized_data == {
        "id": order.id,
        "name": f"XPRO-B2BORDER-{order.id}",
        "stage": ORDER_STATUS_MAPPING[order.status],
        "discount_amount": discount.to_eng_string(),
        "amount": order.total_price.to_eng_string(),
        "close_date": (
            int(order.updated_on.timestamp() * 1000)
            if order.status == Order.FULFILLED
            else None
        ),
        "coupon_code": coupon.coupon_code,
        "company": coupon.company.name,
        "payment_type": None,
        "payment_transaction": None,
        "num_seats": num_seats,
        "discount_percent": round(
            Decimal(coupon.discount_percent) * 100, 2
        ).to_eng_string(),
        "status": order.status,
        "purchaser": format_hubspot_id(order.email),
    }
예제 #7
0
 def get_order(self, instance):
     """ Get the order id and return the hubspot deal integratorObject id"""
     return format_hubspot_id(instance.integration_id)
예제 #8
0
 def get_product(self, instance):
     """ Get the product id and return the hubspot product integratorObject id"""
     return format_hubspot_id(instance.product_version.product.id)
예제 #9
0
 def get_id(self, instance):
     """ Get the product version id"""
     return format_hubspot_id(instance.product_version.id)
예제 #10
0
 def get_purchaser(self, instance):
     """ Get the Hubspot ID for the purchaser"""
     return format_hubspot_id(instance.purchaser.id)
예제 #11
0
 def get_purchaser(self, instance):
     """Get the purchaser id"""
     if instance.email:
         existing_user = User.objects.filter(email=instance.email).first()
         user_id = existing_user.id if existing_user else instance.email
         return format_hubspot_id(user_id)