def test_command_middleware(rf): """ Add product to basket """ with override_settings(**CORE_BASKET_SETTINGS): with override_provides("basket_command_middleware", [ "shuup.testing.basket_middleware.TestBasketCommandMiddleware" ]): shop = factories.get_default_shop() user = factories.create_random_user() product = factories.create_product("product", shop, factories.get_default_supplier(), 10) request = apply_request_middleware(rf.get("/"), user=user) basket = get_basket(request, "basket") basket.customer = get_person_contact(user) cmd_dispatcher = get_basket_command_dispatcher(request) cmd_dispatcher.ajax = True cmd_kwargs = {"product_id": product.pk} response = cmd_dispatcher.handle("add", kwargs=cmd_kwargs) data = json.loads(response.getvalue()) assert data["ok"] assert data["it_works"] assert data["line_id"] basket = get_basket(request) line = basket.get_basket_line(data["line_id"]) assert line.data["extra"]["line_options"] == "works"
def test_add_product_with_extra_parent_line(rf): """ Add product to basket with extra info and parent line """ with override_settings(**CORE_BASKET_SETTINGS): shop = factories.get_default_shop() user = factories.create_random_user() product = factories.create_product("product", shop, factories.get_default_supplier(), 10) request = apply_request_middleware(rf.get("/"), user=user) basket = get_basket(request, "basket") basket.customer = get_person_contact(user) cmd_response = basket_commands.handle_add(request, basket, product.id, 1, extra={"more": "stuff"}) line_id1 = cmd_response["line_id"] assert cmd_response["ok"] line1 = basket.get_basket_line(line_id1) assert line1._data["more"] == "stuff" cmd_response = basket_commands.handle_add( request, basket, product.id, 1, parent_line=line1, force_new_line=True) line_id2 = cmd_response["line_id"] assert cmd_response["ok"] line2 = basket.get_basket_line(line_id2) assert not line2._data assert line_id1 != line_id2 assert line2.parent_line.line_id == line_id1
def test_add_product_new_line(rf): """ Add product to basket and force always new line """ with override_settings(**CORE_BASKET_SETTINGS): shop = factories.get_default_shop() user = factories.create_random_user() product = factories.create_product("product", shop, factories.get_default_supplier(), 10) request = apply_request_middleware(rf.get("/"), user=user) basket = get_basket(request, "basket") basket.customer = get_person_contact(user) for _ in range(3): cmd_response = basket_commands.handle_add(request, basket, product.id, 1, force_new_line=True) assert cmd_response["ok"] assert cmd_response["line_id"] assert cmd_response["added"] == 1 assert len(basket.get_lines()) == 3
def _init_test_for_product_with_basket(rf, default_price): request, product = _init_test_for_product_without_basket(rf, default_price) shop = factories.get_default_shop() supplier = factories.get_default_supplier() basket = get_basket(request) basket.status = factories.get_initial_order_status() basket.add_product(supplier=supplier, shop=shop, product=product, quantity=1) basket.shipping_method = factories.get_shipping_method(shop=shop) basket.payment_method = factories.get_payment_method(shop=shop) assert basket.shop == request.shop assert basket.customer == request.customer return request, product, basket
def _create_order(request, customer, coupon, product, expected_product_price): creator = OrderCreator(request) shop = request.shop request.basket = None request.customer = customer basket = get_basket(request) basket.status = factories.get_initial_order_status() basket.add_product(supplier=factories.get_default_supplier(), shop=shop, product=product, quantity=1) basket.shipping_method = factories.get_shipping_method(shop=shop) basket.payment_method = factories.get_payment_method(shop=shop) basket.add_code(coupon) assert basket.shop == request.shop assert basket.customer == request.customer assert product.get_price_info(request).price == expected_product_price creator.create_order(basket)
def test_set_company_customer(rf): """ Set a company as the basket customer """ with override_settings(**CORE_BASKET_SETTINGS): user = factories.create_random_user() request = apply_request_middleware(rf.get("/"), user=user) basket = get_basket(request, "basket") basket.customer = get_person_contact(user) person = factories.create_random_person() company = factories.create_random_company() # no orderer provided with pytest.raises(ValidationError) as exc: basket_commands.handle_set_customer(request, basket, company) assert exc.value.code == "invalid_orderer" assert basket.customer == get_person_contact(user) # orderer provided but not member of the company with pytest.raises(ValidationError) as exc: basket_commands.handle_set_customer(request, basket, company, person) assert exc.value.code == "orderer_not_company_member" assert basket.customer == get_person_contact(user) # orderer provided but user not member of the company company.members.add(person) with pytest.raises(ValidationError) as exc: basket_commands.handle_set_customer(request, basket, company, person) assert exc.value.code == "not_company_member" assert basket.customer == get_person_contact(user) # staff and admin can add any the company and orderer without being member of the company superuser = factories.create_random_user(is_superuser=True) staff = factories.create_random_user(is_staff=True) basket.shop.staff_members.add(staff) for user in [superuser, staff]: basket.customer = None basket.orderer = None request = apply_request_middleware(rf.get("/"), user=user) assert basket_commands.handle_set_customer( request, basket, company, person)["ok"] is True assert basket.customer == company assert basket.orderer == person
def test_set_different_customer(rf): """ Set some customer to the basket that is not the request one """ with override_settings(**CORE_BASKET_SETTINGS): user = factories.create_random_user() request = apply_request_middleware(rf.get("/"), user=user) basket = get_basket(request, "basket") basket.customer = get_person_contact(user) person1 = factories.create_random_person() with pytest.raises(ValidationError) as exc: basket_commands.handle_set_customer(request, basket, person1) assert exc.value.code == "no_permission" assert basket.customer == get_person_contact(user) # with superuser person2 = factories.create_random_person() superuser = factories.create_random_user(is_superuser=True) request = apply_request_middleware(rf.get("/"), user=superuser) assert basket_commands.handle_set_customer(request, basket, person2)["ok"] is True assert basket.customer == person2 assert basket.orderer == person2 # with staff user not member of the shop person3 = factories.create_random_person() staff = factories.create_random_user(is_staff=True) request = apply_request_middleware(rf.get("/"), user=staff) with pytest.raises(ValidationError) as exc: basket_commands.handle_set_customer(request, basket, person3) assert exc.value.code == "no_permission" assert basket.customer == person2 assert basket.orderer == person2 # with staff user member of the shop person4 = factories.create_random_person() staff_member = factories.create_random_user(is_staff=True) basket.shop.staff_members.add(staff_member) request = apply_request_middleware(rf.get("/"), user=staff_member) assert basket_commands.handle_set_customer(request, basket, person4)["ok"] is True assert basket.customer == person4 assert basket.orderer == person4
def test_set_customer_with_custom_basket_lines(rf): """ Set anonymous to the basket customer """ with override_settings(**CORE_BASKET_SETTINGS): factories.get_default_shop() user = factories.create_random_user() request = apply_request_middleware(rf.get("/"), user=user) basket = get_basket(request, "basket") base_unit_price = basket.shop.create_price("10.99") basket.add_line(text="Custom Line", type=OrderLineType.OTHER, line_id="random-you-know", shop=basket.shop, quantity=1, base_unit_price=base_unit_price) basket.customer = get_person_contact(user) basket.refresh_lines() basket.save() assert basket.customer == get_person_contact(user)
def test_set_customer_with_custom_basket_lines(rf): """ Set anonymous to the basket customer """ with override_settings(**CORE_BASKET_SETTINGS): factories.get_default_shop() user = factories.create_random_user() request = apply_request_middleware(rf.get("/"), user=user) basket = get_basket(request, "basket") shipping_method = factories.get_default_shipping_method() payment_method = factories.get_default_payment_method() customer = get_person_contact(user) customer_comment = "Some comment" base_unit_price = basket.shop.create_price("10.99") basket.add_line(text="Custom Line", type=OrderLineType.OTHER, line_id="random-you-know", shop=basket.shop, quantity=1, base_unit_price=base_unit_price) basket.customer = customer assert basket.customer_comment is None basket.customer_comment = customer_comment assert basket.payment_method is None assert basket.shipping_method is None basket.payment_method = payment_method basket.shipping_method = shipping_method basket.refresh_lines() basket.save() assert basket.customer == get_person_contact(user) assert basket.customer_comment == "Some comment" assert basket.shipping_method == shipping_method assert basket.payment_method == payment_method