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_basic_order_flow(with_company): cache.clear() create_default_order_statuses() n_orders_pre = Order.objects.count() populate_if_required() c = SmartClient() product_ids = _populate_client_basket(c) addresses_path = reverse("wshop: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("wshop: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("wshop:checkout", kwargs={"phase": "confirm"}) confirm_soup = c.soup(confirm_path) Product.objects.get(pk=product_ids[0]).soft_delete() assert c.post(confirm_path, data=extract_form_fields( confirm_soup)).status_code == 200 # user needs to reconfirm data = extract_form_fields(confirm_soup) data['product_ids'] = ','.join(product_ids[1:]) assert c.post(confirm_path, data=data).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_registered(regular_user): cache.clear() create_default_order_statuses() n_orders_pre = Order.objects.count() populate_if_required() get_test_script("test script", "order_received") # paths addresses_path = reverse("wshop:checkout", kwargs={"phase": "addresses"}) methods_path = reverse("wshop:checkout", kwargs={"phase": "methods"}) confirm_path = reverse("wshop:checkout", kwargs={"phase": "confirm"}) template_data = STEP_DATA[0]["actions"][0]["template_data"] LANG_CODE = {"en": "US", "fi": "FI"} for lang in ["en", "fi"]: n_outbox_pre = len(mail.outbox) contact = get_person_contact(regular_user) contact.language = lang contact.save() c = SmartClient() c.login(username=REGULAR_USER_USERNAME, password=REGULAR_USER_PASSWORD) product_ids = _populate_client_basket(c) addresses_soup = c.soup(addresses_path) address = get_address(country=LANG_CODE[lang]) inputs = fill_address_inputs(addresses_soup, address) response = c.post(addresses_path, data=inputs) assert response.status_code == 302 # Should redirect forth methods_soup = c.soup(methods_path) assert c.post(methods_path, data=extract_form_fields( methods_soup)).status_code == 302 # Should redirect forth confirm_soup = c.soup(confirm_path) Product.objects.get(pk=product_ids[0]).soft_delete() assert c.post(confirm_path, data=extract_form_fields( confirm_soup)).status_code == 200 # user needs to reconfirm data = extract_form_fields(confirm_soup) data['product_ids'] = ','.join(product_ids[1:]) assert c.post(confirm_path, data=data).status_code == 302 # Should redirect forth n_orders_post = Order.objects.count() assert n_orders_post > n_orders_pre, "order was created" assert (len(mail.outbox) == n_outbox_pre + 1), "Sending email failed" latest_mail = mail.outbox[-1] # mail is always sent in fallback language since user is not registered assert latest_mail.subject == template_data[lang][ "subject"], "Subject doesn't match" assert latest_mail.body == template_data[lang][ "body"], "Body doesn't match"
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=2) assert variables["page"].number == 1 assert len(variables["objects"]) == 2 assert variables["page_range"][0] == 1 assert variables["page_range"][-1] == 5 context = get_jinja_context(path="/?page=5", **vars) variables = general.get_pagination_variables(context, context["products"], limit=2) assert variables["page"].number == 5 assert len(variables["objects"]) == 2 assert variables["page_range"][0] == 3 assert variables["page_range"][-1] == 7 variables = general.get_pagination_variables(context, context["products"], limit=20) assert not variables["is_paginated"] assert variables["page"].number == 1 assert variables["page_range"][0] == variables["page_range"][-1] == 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 assert variables["page_range"][0] == 1 assert variables["page_range"][-1] == 5 vars = {"products": []} context = get_jinja_context(path="/", **vars) variables = general.get_pagination_variables(context, context["products"], limit=4) assert not variables["is_paginated"] assert variables["page_range"][0] == variables["page_range"][-1] == 1
def test_checkout_empty_basket(rf): cache.clear() create_default_order_statuses() n_orders_pre = Order.objects.count() populate_if_required() c = SmartClient() product_ids = _populate_client_basket(c) addresses_path = reverse("wshop:checkout", kwargs={"phase": "addresses"}) addresses_soup = c.soup(addresses_path) inputs = fill_address_inputs(addresses_soup) for product_id in product_ids: Product.objects.get(pk=product_id).soft_delete() response, soup = c.response_and_soup(addresses_path, data=inputs, method="post") assert response.status_code == 200 # Should redirect forth assert b"Your shopping cart is empty." in soup.renderContents()
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_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_order_flow_with_phases(get_shipping_method, shipping_data, get_payment_method, payment_data): cache.clear() 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("wshop:checkout", kwargs={"phase": "addresses"}) methods_path = reverse("wshop:checkout", kwargs={"phase": "methods"}) shipping_path = reverse("wshop:checkout", kwargs={"phase": "shipping"}) payment_path = reverse("wshop:checkout", kwargs={"phase": "payment"}) confirm_path = reverse("wshop: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("wshop:order_process_payment", kwargs={ "pk": order.pk, "key": order.key }) process_payment_return_path = reverse( "wshop:order_process_payment_return", kwargs={ "pk": order.pk, "key": order.key }) order_complete_path = reverse("wshop: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