def test_picotable_correctly_sorts_translated_fields(rf, admin_user, regular_user): """ Make sure that translated fields, such as product names, are correctly sorted """ populate_if_required() columns = [ Column("id", "Id", filter_config=Filter(), display=instance_id), Column("name", "Name", sort_field="translations__name", filter_config=TextFilter(filter_field="translations__name")), ] pico = get_pico(rf, model=Product, columns=columns) # Verify ascending sort sorted_products = pico.get_data({ "perPage": 100, "page": 1, "sort": "+name" }) sorted_names = [p["name"] for p in sorted_products["items"]] assert sorted_names == sorted(sorted_names) # Verify descending sort sorted_products = pico.get_data({ "perPage": 100, "page": 1, "sort": "-name" }) sorted_names = [p["name"] for p in sorted_products["items"]] assert sorted_names == sorted(sorted_names, reverse=True)
def test_get_pagination_variables(): populate_if_required() # Makes sure there is at least 30 products in db products = Product.objects.all()[:19] assert len(products) == 19 vars = {"products": products} context = get_jinja_context(**vars) variables = general.get_pagination_variables(context, context["products"], limit=4) assert variables["page"].number == 1 assert len(variables["objects"]) == 4 context = get_jinja_context(path="/?page=5", **vars) variables = general.get_pagination_variables(context, context["products"], limit=4) assert variables["page"].number == 5 assert len(variables["objects"]) == 3 variables = general.get_pagination_variables(context, context["products"], limit=20) assert not variables["is_paginated"] assert variables["page"].number == 1 context = get_jinja_context(path="/?page=42", **vars) variables = general.get_pagination_variables(context, context["products"], limit=4) assert variables["page"].number == 5 assert len(variables["objects"]) == 3 vars = {"products": []} context = get_jinja_context(path="/", **vars) variables = general.get_pagination_variables(context, context["products"], limit=4) assert not variables["is_paginated"]
def test_basic_order_flow(with_company): create_default_order_statuses() n_orders_pre = Order.objects.count() populate_if_required() c = SmartClient() _populate_client_basket(c) addresses_path = reverse("shoop:checkout", kwargs={"phase": "addresses"}) addresses_soup = c.soup(addresses_path) inputs = fill_address_inputs(addresses_soup, with_company=with_company) response = c.post(addresses_path, data=inputs) assert response.status_code == 302 # Should redirect forth methods_path = reverse("shoop:checkout", kwargs={"phase": "methods"}) methods_soup = c.soup(methods_path) assert c.post(methods_path, data=extract_form_fields( methods_soup)).status_code == 302 # Should redirect forth confirm_path = reverse("shoop:checkout", kwargs={"phase": "confirm"}) confirm_soup = c.soup(confirm_path) assert c.post(confirm_path, data=extract_form_fields( confirm_soup)).status_code == 302 # Should redirect forth n_orders_post = Order.objects.count() assert n_orders_post > n_orders_pre, "order was created"
def test_basic_order_flow(with_company): create_default_order_statuses() n_orders_pre = Order.objects.count() populate_if_required() c = SmartClient() _populate_client_basket(c) addresses_path = reverse("shoop:checkout", kwargs={"phase": "addresses"}) addresses_soup = c.soup(addresses_path) inputs = fill_address_inputs(addresses_soup, with_company=with_company) response = c.post(addresses_path, data=inputs) assert response.status_code == 302 # Should redirect forth methods_path = reverse("shoop:checkout", kwargs={"phase": "methods"}) methods_soup = c.soup(methods_path) assert c.post(methods_path, data=extract_form_fields(methods_soup)).status_code == 302 # Should redirect forth confirm_path = reverse("shoop:checkout", kwargs={"phase": "confirm"}) confirm_soup = c.soup(confirm_path) assert c.post(confirm_path, data=extract_form_fields(confirm_soup)).status_code == 302 # Should redirect forth n_orders_post = Order.objects.count() assert n_orders_post > n_orders_pre, "order was created"
def test_picotable_correctly_sorts_translated_fields(rf, admin_user, regular_user): """ Make sure that translated fields, such as product names, are correctly sorted """ populate_if_required() columns = [ Column("id", "Id", filter_config=Filter(), display=instance_id), Column( "name", "Name", sort_field="translations__name", filter_config=TextFilter(filter_field="translations__name")), ] pico = get_pico(rf, model=Product, columns=columns) # Verify ascending sort sorted_products = pico.get_data({"perPage": 100, "page": 1, "sort": "+name"}) sorted_names = [p["name"] for p in sorted_products["items"]] assert sorted_names == sorted(sorted_names) # Verify descending sort sorted_products = pico.get_data({"perPage": 100, "page": 1, "sort": "-name"}) sorted_names = [p["name"] for p in sorted_products["items"]] assert sorted_names == sorted(sorted_names, reverse=True)
def test_order_flow_with_payment_phase(): create_default_order_statuses() n_orders_pre = Order.objects.count() populate_if_required() c = SmartClient() _populate_client_basket(c) # Create methods shipping_method = get_default_shipping_method() processor = PaymentWithCheckoutPhase.objects.create( identifier="processor_with_phase", enabled=True) assert isinstance(processor, PaymentWithCheckoutPhase) payment_method = processor.create_service( None, identifier="payment_with_phase", shop=get_default_shop(), name="Test method with phase", enabled=True, tax_class=get_default_tax_class()) # Resolve paths addresses_path = reverse("shoop:checkout", kwargs={"phase": "addresses"}) methods_path = reverse("shoop:checkout", kwargs={"phase": "methods"}) payment_path = reverse("shoop:checkout", kwargs={"phase": "payment"}) confirm_path = reverse("shoop:checkout", kwargs={"phase": "confirm"}) # Phase: Addresses addresses_soup = c.soup(addresses_path) inputs = fill_address_inputs(addresses_soup, with_company=False) response = c.post(addresses_path, data=inputs) assert response.status_code == 302, "Address phase should redirect forth" assert response.url.endswith(methods_path) # Phase: Methods assert Order.objects.filter(payment_method=payment_method).count() == 0 response = c.post(methods_path, data={ "payment_method": payment_method.pk, "shipping_method": shipping_method.pk }) assert response.status_code == 302, "Methods phase should redirect forth" assert response.url.endswith(confirm_path) response = c.get(confirm_path) assert response.status_code == 302, "Confirm should first redirect forth" assert response.url.endswith(payment_path) # Phase: Payment c.soup(payment_path) response = c.post(payment_path, data={"will_pay": False}) assert response.status_code == 200, "Invalid payment form should return error" response = c.post(payment_path, data={"will_pay": True}) assert response.status_code == 302, "Valid payment form should redirect forth" assert response.url.endswith(confirm_path) # Phase: Confirm confirm_soup = c.soup(confirm_path) response = c.post(confirm_path, data=extract_form_fields(confirm_soup)) assert response.status_code == 302, "Confirm should redirect forth" # response.url should point to payment, checked below # Check resulting order and its contents n_orders_post = Order.objects.count() assert n_orders_post > n_orders_pre, "order was created" order = Order.objects.filter(payment_method=payment_method).first() assert order.payment_data.get("promised_to_pay") assert order.payment_status == PaymentStatus.NOT_PAID # Resolve order specific paths (payment and complete) process_payment_path = reverse("shoop:order_process_payment", kwargs={ "pk": order.pk, "key": order.key }) process_payment_return_path = reverse("shoop:order_process_payment_return", kwargs={ "pk": order.pk, "key": order.key }) order_complete_path = reverse("shoop:order_complete", kwargs={ "pk": order.pk, "key": order.key }) # Check confirm redirection to payment page assert response.url.endswith(process_payment_path), ( "Confirm should have redirected to payment page") # Visit payment page response = c.get(process_payment_path) assert response.status_code == 302, "Payment page should redirect forth" assert response.url.endswith(process_payment_return_path) # Check payment return response = c.get(process_payment_return_path) assert response.status_code == 302, "Payment return should redirect forth" assert response.url.endswith(order_complete_path) # Check payment status has changed to DEFERRED order = Order.objects.get(pk=order.pk) # reload assert order.payment_status == PaymentStatus.DEFERRED
def test_get_root_categories(): populate_if_required() context = get_jinja_context() for root in general.get_root_categories(context=context): assert not root.parent_id
def test_get_all_manufacturers(): populate_if_required() context = get_jinja_context() # TODO: This is not a good test assert len(general.get_all_manufacturers(context)) == 0
def test_get_random_products(): populate_if_required() context = get_jinja_context() assert len(list(general.get_random_products(context, n_products=4))) == 4
def test_order_flow_with_phases(get_shipping_method, shipping_data, get_payment_method, payment_data): create_default_order_statuses() populate_if_required() c = SmartClient() _populate_client_basket(c) # Create methods shipping_method = get_shipping_method() payment_method = get_payment_method() # Resolve paths addresses_path = reverse("shoop:checkout", kwargs={"phase": "addresses"}) methods_path = reverse("shoop:checkout", kwargs={"phase": "methods"}) shipping_path = reverse("shoop:checkout", kwargs={"phase": "shipping"}) payment_path = reverse("shoop:checkout", kwargs={"phase": "payment"}) confirm_path = reverse("shoop:checkout", kwargs={"phase": "confirm"}) # Phase: Addresses addresses_soup = c.soup(addresses_path) inputs = fill_address_inputs(addresses_soup, with_company=False) response = c.post(addresses_path, data=inputs) assert response.status_code == 302, "Address phase should redirect forth to methods" # Phase: Methods response = c.get(methods_path) assert response.status_code == 200 response = c.post(methods_path, data={ "shipping_method": shipping_method.pk, "payment_method": payment_method.pk }) assert response.status_code == 302, "Methods phase should redirect forth" if isinstance(shipping_method.carrier, CarrierWithCheckoutPhase): # Phase: Shipping response = c.get(shipping_path) assert response.status_code == 200 response = c.post(shipping_path, data=shipping_data) assert response.status_code == 302, "Payments phase should redirect forth" if isinstance(payment_method.payment_processor, PaymentWithCheckoutPhase): # Phase: payment response = c.get(payment_path) assert response.status_code == 200 response = c.post(payment_path, data=payment_data) assert response.status_code == 302, "Payments phase should redirect forth" # Phase: Confirm assert Order.objects.count() == 0 confirm_soup = c.soup(confirm_path) response = c.post(confirm_path, data=extract_form_fields(confirm_soup)) assert response.status_code == 302, "Confirm should redirect forth" order = Order.objects.first() if isinstance(shipping_method.carrier, CarrierWithCheckoutPhase): assert order.shipping_data.get("input_value") == "20540" if isinstance(payment_method.payment_processor, PaymentWithCheckoutPhase): assert order.payment_data.get("input_value") assert order.payment_status == PaymentStatus.NOT_PAID # Resolve order specific paths (payment and complete) process_payment_path = reverse("shoop:order_process_payment", kwargs={ "pk": order.pk, "key": order.key }) process_payment_return_path = reverse( "shoop:order_process_payment_return", kwargs={ "pk": order.pk, "key": order.key }) order_complete_path = reverse("shoop:order_complete", kwargs={ "pk": order.pk, "key": order.key }) # Check confirm redirection to payment page assert response.url.endswith(process_payment_path), ( "Confirm should have redirected to payment page") # Visit payment page response = c.get(process_payment_path) assert response.status_code == 302, "Payment page should redirect forth" assert response.url.endswith(process_payment_return_path) # Check payment return response = c.get(process_payment_return_path) assert response.status_code == 302, "Payment return should redirect forth" assert response.url.endswith(order_complete_path) # Check payment status has changed to DEFERRED order = Order.objects.get(pk=order.pk) # reload assert order.payment_status == PaymentStatus.DEFERRED
def test_order_flow_with_phases(get_shipping_method, shipping_data, get_payment_method, payment_data): create_default_order_statuses() populate_if_required() c = SmartClient() _populate_client_basket(c) # Create methods shipping_method = get_shipping_method() payment_method = get_payment_method() # Resolve paths addresses_path = reverse("shoop:checkout", kwargs={"phase": "addresses"}) methods_path = reverse("shoop:checkout", kwargs={"phase": "methods"}) shipping_path = reverse("shoop:checkout", kwargs={"phase": "shipping"}) payment_path = reverse("shoop:checkout", kwargs={"phase": "payment"}) confirm_path = reverse("shoop:checkout", kwargs={"phase": "confirm"}) # Phase: Addresses addresses_soup = c.soup(addresses_path) inputs = fill_address_inputs(addresses_soup, with_company=False) response = c.post(addresses_path, data=inputs) assert response.status_code == 302, "Address phase should redirect forth to methods" # Phase: Methods response = c.get(methods_path) assert response.status_code == 200 response = c.post( methods_path, data={ "shipping_method": shipping_method.pk, "payment_method": payment_method.pk } ) assert response.status_code == 302, "Methods phase should redirect forth" if isinstance(shipping_method.carrier, CarrierWithCheckoutPhase): # Phase: Shipping response = c.get(shipping_path) assert response.status_code == 200 response = c.post(shipping_path, data=shipping_data) assert response.status_code == 302, "Payments phase should redirect forth" if isinstance(payment_method.payment_processor, PaymentWithCheckoutPhase): # Phase: payment response = c.get(payment_path) assert response.status_code == 200 response = c.post(payment_path, data=payment_data) assert response.status_code == 302, "Payments phase should redirect forth" # Phase: Confirm assert Order.objects.count() == 0 confirm_soup = c.soup(confirm_path) response = c.post(confirm_path, data=extract_form_fields(confirm_soup)) assert response.status_code == 302, "Confirm should redirect forth" order = Order.objects.first() if isinstance(shipping_method.carrier, CarrierWithCheckoutPhase): assert order.shipping_data.get("input_value") == "20540" if isinstance(payment_method.payment_processor, PaymentWithCheckoutPhase): assert order.payment_data.get("input_value") assert order.payment_status == PaymentStatus.NOT_PAID # Resolve order specific paths (payment and complete) process_payment_path = reverse( "shoop:order_process_payment", kwargs={"pk": order.pk, "key": order.key}) process_payment_return_path = reverse( "shoop:order_process_payment_return", kwargs={"pk": order.pk, "key": order.key}) order_complete_path = reverse( "shoop:order_complete", kwargs={"pk": order.pk, "key": order.key}) # Check confirm redirection to payment page assert response.url.endswith(process_payment_path), ( "Confirm should have redirected to payment page") # Visit payment page response = c.get(process_payment_path) assert response.status_code == 302, "Payment page should redirect forth" assert response.url.endswith(process_payment_return_path) # Check payment return response = c.get(process_payment_return_path) assert response.status_code == 302, "Payment return should redirect forth" assert response.url.endswith(order_complete_path) # Check payment status has changed to DEFERRED order = Order.objects.get(pk=order.pk) # reload assert order.payment_status == PaymentStatus.DEFERRED
def test_get_root_categories(): populate_if_required() context = get_jinja_context() general.get_root_categories(context)
def test_order_flow_with_payment_phase(): create_default_order_statuses() n_orders_pre = Order.objects.count() populate_if_required() c = SmartClient() _populate_client_basket(c) # Create methods shipping_method = get_default_shipping_method() processor = PaymentWithCheckoutPhase.objects.create(identifier="processor_with_phase", enabled=True) assert isinstance(processor, PaymentWithCheckoutPhase) payment_method = processor.create_service( None, identifier="payment_with_phase", shop=get_default_shop(), name="Test method with phase", enabled=True, tax_class=get_default_tax_class(), ) # Resolve paths addresses_path = reverse("shoop:checkout", kwargs={"phase": "addresses"}) methods_path = reverse("shoop:checkout", kwargs={"phase": "methods"}) payment_path = reverse("shoop:checkout", kwargs={"phase": "payment"}) confirm_path = reverse("shoop:checkout", kwargs={"phase": "confirm"}) # Phase: Addresses addresses_soup = c.soup(addresses_path) inputs = fill_address_inputs(addresses_soup, with_company=False) response = c.post(addresses_path, data=inputs) assert response.status_code == 302, "Address phase should redirect forth" assert response.url.endswith(methods_path) # Phase: Methods assert Order.objects.filter(payment_method=payment_method).count() == 0 response = c.post(methods_path, data={"payment_method": payment_method.pk, "shipping_method": shipping_method.pk}) assert response.status_code == 302, "Methods phase should redirect forth" assert response.url.endswith(confirm_path) response = c.get(confirm_path) assert response.status_code == 302, "Confirm should first redirect forth" assert response.url.endswith(payment_path) # Phase: Payment c.soup(payment_path) response = c.post(payment_path, data={"will_pay": False}) assert response.status_code == 200, "Invalid payment form should return error" response = c.post(payment_path, data={"will_pay": True}) assert response.status_code == 302, "Valid payment form should redirect forth" assert response.url.endswith(confirm_path) # Phase: Confirm confirm_soup = c.soup(confirm_path) response = c.post(confirm_path, data=extract_form_fields(confirm_soup)) assert response.status_code == 302, "Confirm should redirect forth" # response.url should point to payment, checked below # Check resulting order and its contents n_orders_post = Order.objects.count() assert n_orders_post > n_orders_pre, "order was created" order = Order.objects.filter(payment_method=payment_method).first() assert order.payment_data.get("promised_to_pay") assert order.payment_status == PaymentStatus.NOT_PAID # Resolve order specific paths (payment and complete) process_payment_path = reverse("shoop:order_process_payment", kwargs={"pk": order.pk, "key": order.key}) process_payment_return_path = reverse( "shoop:order_process_payment_return", kwargs={"pk": order.pk, "key": order.key} ) order_complete_path = reverse("shoop:order_complete", kwargs={"pk": order.pk, "key": order.key}) # Check confirm redirection to payment page assert response.url.endswith(process_payment_path), "Confirm should have redirected to payment page" # Visit payment page response = c.get(process_payment_path) assert response.status_code == 302, "Payment page should redirect forth" assert response.url.endswith(process_payment_return_path) # Check payment return response = c.get(process_payment_return_path) assert response.status_code == 302, "Payment return should redirect forth" assert response.url.endswith(order_complete_path) # Check payment status has changed to DEFERRED order = Order.objects.get(pk=order.pk) # reload assert order.payment_status == PaymentStatus.DEFERRED