def test_contact_company_list_multishop(rf): with override_settings(WSHOP_MANAGE_CONTACTS_PER_SHOP=True, WSHOP_ENABLE_MULTIPLE_SHOPS=True): staff_user = create_random_user(is_staff=True) shop1 = get_shop(identifier="shop-1", enabled=True) shop2 = get_shop(identifier="shop-2", enabled=True) shop1.staff_members.add(staff_user) shop2.staff_members.add(staff_user) # only available in shop2 contact = create_random_person(locale="en_US", minimum_name_comp_len=5) contact.shops.add(shop2) # only available in shop1 company = create_random_company() company.shops.add(shop1) view = ContactListView() request = apply_request_middleware(rf.get("/"), user=staff_user, shop=shop1) view.request = request assert company in view.get_queryset() assert contact not in view.get_queryset() request = apply_request_middleware(rf.get("/"), user=staff_user, shop=shop2) view.request = request assert contact in view.get_queryset() assert company not in view.get_queryset()
def get_request_for_contact_tests(rf): activate("en") request = rf.get("/") request.shop = get_shop(prices_include_tax=True) get_payment_method(request.shop) apply_request_middleware(request) return request
def test_multishops_middleware(rf, host): with override_provides("xtheme", [ "wshop_tests.xtheme.utils:FauxTheme", "wshop_tests.xtheme.utils:FauxTheme2" ]): shop1 = Shop.objects.create(identifier="shop1", domain="shop-1") shop2 = Shop.objects.create(identifier="shop2", domain="shop-test-2") theme_settings_shop1 = ThemeSettings.objects.create( theme_identifier=FauxTheme.identifier, shop=shop1) theme_settings_shop2 = ThemeSettings.objects.create( theme_identifier=FauxTheme2.identifier, shop=shop2) request = rf.get("/") request.META["HTTP_HOST"] = host # should apply the correct shop and the template apply_request_middleware(request) if host == "shop-1.somedomain.com": assert request.shop.id == shop1.id assert get_middleware_current_theme( ).identifier == FauxTheme.identifier assert get_middleware_current_theme( ).settings_obj.id == theme_settings_shop1.id else: assert request.shop.id == shop2.id assert get_middleware_current_theme( ).identifier == FauxTheme2.identifier assert get_middleware_current_theme( ).settings_obj.id == theme_settings_shop2.id
def test_get_shop(rf, get_shop_fn): with override_settings(WSHOP_ENABLE_MULTIPLE_SHOPS=True): activate("en") shop1 = Shop.objects.create(identifier="shop1", status=ShopStatus.ENABLED) shop2 = Shop.objects.create(identifier="shop2", status=ShopStatus.ENABLED) normal_user = factories.create_random_user() staff_user = factories.create_random_user(is_staff=True) request = apply_request_middleware(rf.post("/"), user=normal_user) # user not staff assert get_shop_fn(request) is None # staff user now request = apply_request_middleware(rf.post("/"), user=staff_user) # no shop set assert get_shop_fn(request) is None # adds the user to a shop shop1.staff_members.add(staff_user) assert get_shop_fn(request) == shop1 # adds the user to another shop shop2.staff_members.add(staff_user) # still the first shop as we do not set any assert get_shop_fn(request) == shop1
def test_simple_search_word_finder(rf): cache.clear() view = SearchView.as_view() name = "Savage Garden" sku = UNLIKELY_STRING prod = create_product( sku=sku, name=name, keywords="truly, madly, deeply", description="Descriptive text", shop=get_default_shop() ) resp = view(apply_request_middleware(rf.get("/"))) assert prod not in resp.context_data["object_list"], "No query no results" partial_sku = sku[:int(len(sku)/2)] valid_searches = ["Savage", "savage", "truly", "madly", "truly madly", "truly garden", "text", sku, partial_sku] for query in valid_searches: resp = view(apply_request_middleware(rf.get("/", {"q": query}))) assert name in resp.rendered_content invalid_searches = ["saavage", "", sku[::-1]] for query in invalid_searches: resp = view(apply_request_middleware(rf.get("/", {"q": query}))) assert name not in resp.rendered_content
def test_category_create_with_parent(rf, admin_user): shop = get_default_shop() default_category = get_default_category() default_category.shops.clear() assert shop not in default_category.shops.all() with override_settings(LANGUAGES=[("en", "en")]): view = CategoryEditView.as_view() cat_name = "Random name" data = { "base-name__en": cat_name, "base-status": CategoryStatus.VISIBLE.value, "base-visibility": CategoryVisibility.VISIBLE_TO_ALL.value, "base-ordering": 1, "base-parent": default_category.pk } assert Category.objects.count() == 1 request = apply_request_middleware(rf.post("/", data=data), user=admin_user, shop=shop) response = view(request, pk=None) if hasattr(response, "render"): response.render() assert response.status_code in [200, 302] assert Category.objects.count() == 1 default_category.shops.add(shop) request = apply_request_middleware(rf.post("/", data=data), user=admin_user, shop=shop) response = view(request, pk=None) if hasattr(response, "render"): response.render() assert response.status_code in [200, 302] assert Category.objects.count() == 2
def test_edit_view_adding_messages_to_form_group(rf, admin_user): get_default_shop() # obvious prerequisite product = get_default_product() view = ProductEditView.as_view() request = apply_request_middleware(rf.get("/"), user=admin_user) response = view(request, pk=product.pk) response.render() assert 200 <= response.status_code < 300 assert ProductEditView.add_form_errors_as_messages content = force_text(response.content) post = extract_form_fields(BeautifulSoup(content)) post_data = { # Error in the base form part "base-name__en": "", } post.update(post_data) request = apply_request_middleware(rf.post("/", post), user=admin_user) response = view(request, pk=product.pk) errors = response.context_data["form"].errors assert "base" in errors assert "name__en" in errors["base"]
def test_category_links_plugin_with_customer(rf, show_all_categories): """ Test plugin for categories that is visible for certain group """ shop = get_default_shop() group = get_default_customer_group() customer = create_random_person() customer.groups.add(group) customer.save() request = rf.get("/") request.shop = get_default_shop() apply_request_middleware(request) request.customer = customer category = get_default_category() category.status = CategoryStatus.VISIBLE category.visibility = CategoryVisibility.VISIBLE_TO_GROUPS category.visibility_groups.add(group) category.shops.add(shop) category.save() vars = {"request": request} context = get_jinja_context(**vars) plugin = CategoryLinksPlugin({"categories": [category.pk], "show_all_categories": show_all_categories}) assert category.is_visible(customer) assert category in plugin.get_context_data(context)["categories"] customer_without_groups = create_random_person() customer_without_groups.groups.clear() assert not category.is_visible(customer_without_groups) request.customer = customer_without_groups context = get_jinja_context(**vars) assert category not in plugin.get_context_data(context)["categories"]
def test_parallel_baskets(rf): request = get_request_with_basket() shop = get_default_shop() customer = create_random_person() request = rf.get("/") request.shop = shop apply_request_middleware(request) request.customer = customer basket_one = get_basket(request, basket_name="basket_one") basket_two = get_basket(request, basket_name="basket_two") product_one = get_default_product() product_two = get_default_product() product_two.sku = "derpy-hooves" sales_unit = SalesUnit.objects.create(identifier="test-sales-partial", decimals=2, name="Partial unit") product_two.sales_unit = sales_unit # Set the sales unit for the product product_two.save() basket_commands.handle_add(request, basket_one, product_id=product_one.pk, quantity=1) basket_commands.handle_add(request, basket_two, product_id=product_two.pk, quantity=3.5) assert basket_one.product_count == 1 assert basket_two.product_count == 3.5
def test_contact_module_search_multishop(rf): with override_settings(WSHOP_MANAGE_CONTACTS_PER_SHOP=True, WSHOP_ENABLE_MULTIPLE_SHOPS=True): staff_user = create_random_user(is_staff=True) shop1 = get_shop(identifier="shop-1", enabled=True) shop2 = get_shop(identifier="shop-2", enabled=True) shop1.staff_members.add(staff_user) shop2.staff_members.add(staff_user) cm = ContactModule() contact = create_random_person(locale="en_US", minimum_name_comp_len=5) contact.shops.add(shop2) request = apply_request_middleware(rf.get("/"), user=staff_user, shop=shop2) # find the shop assert not empty_iterable( cm.get_search_results(request, query=contact.email)) assert not empty_iterable( cm.get_search_results(request, query=contact.first_name)) # no shop found request = apply_request_middleware(rf.get("/"), user=staff_user, shop=shop1) assert empty_iterable( cm.get_search_results(request, query=contact.email))
def test_manufacturer_admin_simple_shop(rf, staff_user, admin_user): with override_settings(WSHOP_ENABLE_MULTIPLE_SHOPS=False): shop1 = factories.get_default_shop() shop1.staff_members.add(staff_user) factories.get_shop(identifier="shop2") assert Manufacturer.objects.count() == 0 # staff user request = apply_request_middleware(rf.post("/", data=dict(name="Manuf 1")), user=staff_user) view_func = ManufacturerEditView.as_view() response = view_func(request) assert response.status_code == 302 assert Manufacturer.objects.first().shops.first() == shop1 # superuser request = apply_request_middleware(rf.post("/", data=dict(name="Manuf 2")), user=admin_user) view_func = ManufacturerEditView.as_view() response = view_func(request) assert response.status_code == 302 assert Manufacturer.objects.count() == 2 assert Manufacturer.objects.last().shops.first() == shop1
def test_login_as_user_errors(rf, admin_user, regular_user): get_default_shop() view_func = LoginAsUserView.as_view() request = apply_request_middleware(rf.post("/"), user=regular_user) # log in as self with pytest.raises(Problem): view_func(request, pk=regular_user.pk) user = UserFactory() get_person_contact(user) # non superuser trying to login as someone else with pytest.raises(PermissionDenied): view_func(request, pk=user.pk) request = apply_request_middleware(rf.post("/"), user=admin_user) user.is_superuser = True user.save() # user is trying to login as another superuser with pytest.raises(PermissionDenied): view_func(request, pk=user.pk) user.is_superuser = False user.is_staff = True user.save() # user is trying to login as a staff user with pytest.raises(PermissionDenied): view_func(request, pk=user.pk) user.is_staff = False user.is_active = False user.save() # user is trying to login as an inactive user with pytest.raises(Problem): view_func(request, pk=user.pk)
def test_basket_clearing(rf): StoredBasket.objects.all().delete() shop = get_default_shop() supplier = get_default_supplier() product = create_product(printable_gibberish(), shop=shop, supplier=supplier, default_price=50) request = rf.get("/") request.session = {} request.shop = shop apply_request_middleware(request) basket = get_basket(request) pm = get_default_payment_method() sm = get_default_shipping_method() basket.shipping_method = sm basket.payment_method = pm basket.save() assert basket.shipping_method assert basket.payment_method basket.clear_all() assert not basket.shipping_method assert not basket.payment_method
def test_basket_orderability_change_shop_product(rf): StoredBasket.objects.all().delete() shop = get_default_shop() supplier = get_default_supplier() product = create_product(printable_gibberish(), shop=shop, supplier=supplier, default_price=50) request = rf.get("/") request.session = {} request.shop = shop apply_request_middleware(request) basket = get_basket(request) line = basket.add_product(supplier=supplier, shop=shop, product=product, quantity=1, force_new_line=True, extra={"foo": "foo"}) assert len(basket.get_lines()) == 1 assert len(basket.get_unorderable_lines()) == 0 product.get_shop_instance(shop).delete() basket.uncache() assert basket.dirty assert len(basket.get_lines()) == 0 assert len(basket.get_unorderable_lines()) == 1
def initialize_test(rf): shop = get_default_shop() request = rf.get("/") request.shop = shop apply_request_middleware(request) product1 = create_product("test-product1", shop=shop, default_price=120) product2 = create_product("test-product2", shop=shop, default_price=180) return (request, [product1, product2], shop.create_price)
def test_modules_in_core_admin_work(rf, admin_user): get_default_shop() request = rf.get("/") apply_request_middleware(request, user=admin_user) request = apply_request_middleware(rf.get("/"), user=admin_user) with replace_modules(WshopAdminAppConfig.provides["admin_module"]): assert all(get_module_urls()) assert get_menu_entry_categories(request)
def get_context(rf, customer=None): request = rf.get("/") request.shop = get_default_shop() apply_request_middleware(request) if customer: request.customer = customer vars = {"request": request} return get_jinja_context(**vars)
def test_simple_search_no_results(rf): cache.clear() with translation.override("xx"): # use built-in translation get_default_shop() view = SearchView.as_view() resp = view(apply_request_middleware(rf.get("/", {"q": UNLIKELY_STRING}))) assert NO_RESULTS_FOUND_STRING in resp.rendered_content resp = view(apply_request_middleware(rf.get("/"))) assert NO_RESULTS_FOUND_STRING in resp.rendered_content, "No query string no results"
def test_normalize_spaces(rf): cache.clear() view = SearchView.as_view() create_product(sku=UNLIKELY_STRING, name="Savage Garden", shop=get_default_shop()) query = "\t Savage \t \t \n \r Garden \n" resp = view(apply_request_middleware(rf.get("/"))) assert query not in resp.rendered_content resp = view(apply_request_middleware(rf.get("/", {"q": query}))) assert query in resp.rendered_content
def init_test(request, shop, prices): apply_request_middleware(request) parent = create_product("parent_product", shop=shop) children = [ create_product("child-%d" % price, shop=shop, default_price=price) for price in prices ] for child in children: child.link_to_parent(parent) return parent
def test_can_complete_productless_order(rf, admin_user): shop = get_shop(prices_include_tax=False) supplier = get_default_supplier() request = rf.get('/') request.shop = shop apply_request_middleware(request) customer = get_person_contact(admin_user) order = create_simple_order(request, admin_user, customer) assert not order.has_products() assert order.can_set_complete() assert not order.is_fully_shipped()
def test_simple_search_view_works(rf): cache.clear() view = SearchView.as_view() prod = create_product(sku=UNLIKELY_STRING, shop=get_default_shop()) query = prod.name[:8] # This test is pretty cruddy. TODO: Un-cruddify this test. resp = view(apply_request_middleware(rf.get("/"))) assert query not in resp.rendered_content resp = view(apply_request_middleware(rf.get("/", {"q": query}))) assert query in resp.rendered_content
def test_basic_order(rf, admin_user, mode): prices_include_tax = (mode == "taxful") shop = get_shop(prices_include_tax=prices_include_tax) request = rf.get('/') request.shop = shop apply_request_middleware(request) product = get_default_product() customer = get_person_contact(admin_user) for x in range(10): create_order(request, creator=admin_user, customer=customer, product=product) assert Order.objects.filter(customer=customer).count() == 10
def test_basket(rf): StoredBasket.objects.all().delete() quantities = [3, 12, 44, 23, 65] shop = get_default_shop() get_default_payment_method( ) # Can't create baskets without payment methods supplier = get_default_supplier() products_and_quantities = [] for quantity in quantities: product = create_product(printable_gibberish(), shop=shop, supplier=supplier, default_price=50) products_and_quantities.append((product, quantity)) for product, q in products_and_quantities: request = rf.get("/") request.session = {} request.shop = shop apply_request_middleware(request) basket = get_basket(request) assert basket == request.basket assert basket.product_count == 0 line = basket.add_product(supplier=supplier, shop=shop, product=product, quantity=q) basket.shipping_method = get_shipping_method( shop=shop) # For shippable product assert line.quantity == q assert basket.get_lines() assert basket.get_product_ids_and_quantities().get(product.pk) == q assert basket.product_count == q basket.save() delattr(request, "basket") basket = get_basket(request) assert basket.get_product_ids_and_quantities().get(product.pk) == q product_ids = set(StoredBasket.objects.last().products.values_list( "id", flat=True)) assert product_ids == set([product.pk]) stats = StoredBasket.objects.all().aggregate( n=Sum("product_count"), tfs=Sum("taxful_total_price_value"), tls=Sum("taxless_total_price_value"), ) assert stats["n"] == sum(quantities) if shop.prices_include_tax: assert stats["tfs"] == sum(quantities) * 50 else: assert stats["tls"] == sum(quantities) * 50 basket.finalize()
def test_basket_package_product_orderability_change(rf): if "wshop.simple_supplier" not in settings.INSTALLED_APPS: pytest.skip("Need wshop.simple_supplier in INSTALLED_APPS") from wshop_tests.simple_supplier.utils import get_simple_supplier StoredBasket.objects.all().delete() shop = get_default_shop() supplier = get_simple_supplier() product, child = get_unstocked_package_product_and_stocked_child( shop, supplier, child_logical_quantity=2) request = rf.get("/") request.session = {} request.shop = shop apply_request_middleware(request) basket = get_basket(request) # Add the package parent basket.add_product(supplier=supplier, shop=shop, product=product, quantity=1, force_new_line=True, extra={"foo": "foo"}) # Also add the child product separately basket.add_product(supplier=supplier, shop=shop, product=child, quantity=1, force_new_line=True, extra={"foo": "foo"}) # Should be stock for both assert len(basket.get_lines()) == 2 assert len(basket.get_unorderable_lines()) == 0 supplier.adjust_stock(child.id, -1) # Orderability is already cached, we need to uncache to force recheck basket.uncache() # After reducing stock to 1, should only be stock for one assert len(basket.get_lines()) == 1 assert len(basket.get_unorderable_lines()) == 1 supplier.adjust_stock(child.id, -1) basket.uncache() # After reducing stock to 0, should be stock for neither assert len(basket.get_lines()) == 0 assert len(basket.get_unorderable_lines()) == 2
def initialize_test(rf, include_tax=False): shop = get_shop_with_tax(include_tax=include_tax) group = get_default_customer_group() customer = create_random_person() customer.groups.add(group) customer.save() request = rf.get("/") request.shop = shop apply_request_middleware(request) request.customer = customer return request, shop, group
def test_sample_data_wizard_pane(rf, admin_user, settings): settings.WSHOP_SETUP_WIZARD_PANE_SPEC = [ "wshop.admin.modules.sample_data.views.SampleObjectsWizardPane" ] shop = get_default_shop() get_default_tax_class() data = { 'pane_id': 'sample', 'sample-business_segment': 'default', 'sample-categories': True, 'sample-products': True, 'sample-carousel': True } request = apply_request_middleware(rf.post("/", data=data), user=admin_user) response = WizardView.as_view()(request) assert response.status_code == 200 assert Product.objects.count() == len( BUSINESS_SEGMENTS["default"]["products"]) anon_contact = AnonymousContact() supplier = get_default_supplier() # check for the injected plugin using the carousel assert Carousel.objects.count() == 1 carousel = Carousel.objects.first() assert Slide.objects.count() == len( BUSINESS_SEGMENTS["default"]["carousel"]["slides"]) svc = SavedViewConfig.objects.first() assert svc.view_name == "IndexView" layout = svc.get_layout_data("front_content") assert layout['rows'][0]['cells'][0]['config']['carousel'] == carousel.pk assert layout['rows'][0]['cells'][0]['plugin'] == CarouselPlugin.identifier for product in Product.objects.all(): # all products must be orderable and have images assert product.get_shop_instance(shop).is_orderable( supplier=supplier, customer=anon_contact, quantity=1) assert product.primary_image is not None assert Category.objects.count() == len( BUSINESS_SEGMENTS["default"]["categories"]) request = apply_request_middleware(rf.get("/"), user=admin_user) response = WizardView.as_view()(request) assert response.status_code == 302 assert response["Location"] == reverse("wshop_admin:dashboard")
def test_method_phase_basic(rf, admin_user, get_method, data, method_id): activate("en") shop = get_default_shop() method = get_method(shop, price=0, name=data["name"]) method.description = data["description"] method.save() assert method.enabled view = MethodsOnlyCheckoutView.as_view() # To make method visible, basket must be available person = get_person_contact(admin_user) request = apply_request_middleware(rf.get("/")) request.shop = shop request.user = admin_user request.person = person request.customer = person basket = get_basket(request) # add product to bakset supplier = get_default_supplier() product = create_product(printable_gibberish(), shop=shop, supplier=supplier, default_price=50) basket.add_product(supplier=supplier, shop=shop, product=product, quantity=2) assert basket.product_count, "basket has products" basket.save() request = apply_request_middleware(request, user=admin_user, person=person, customer=person, basket=basket) # request = apply_request_middleware(rf.get("/")) response = view(request=request, phase='methods') if hasattr(response, "render"): response.render() assert response.status_code in [200, 302] soup = BeautifulSoup(response.content) method_soup = soup.find("div", {"id": method_id}) assert data["name"] in method_soup.text assert data["description"] in method_soup.text assert soup.find("input", {"id": "%s_%s" % (method_id, method.pk)})
def test_dashboard_redirect(rf, admin_user, settings): settings.WSHOP_SETUP_WIZARD_PANE_SPEC = [] shop = get_default_shop() shop.maintenance_mode = True shop.save() request = apply_request_middleware(rf.get("/"), user=admin_user) response = DashboardView.as_view()(request) assert response.status_code == 302 assert response["Location"] == reverse("wshop_admin:home") shop.maintenance_mode = False shop.save() request = apply_request_middleware(rf.get("/"), user=admin_user) response = DashboardView.as_view()(request) assert response.status_code == 200
def test_admin_order_with_campaign(rf, admin_user): request, shop, group = initialize_test(rf, False) customer = request.customer cat = Category.objects.create(name="test") rule1, rule2 = create_condition_and_filter(cat, request) campaign = CatalogCampaign.objects.create(shop=shop, name="test", active=True) campaign.conditions.add(rule1) ProductDiscountAmount.objects.create(campaign=campaign, discount_amount="10") product = create_product("Just-A-Product-Too", shop, default_price=20) shop_product = product.get_shop_instance(shop) shop_product.categories.add(cat) request = apply_request_middleware(rf.get( "/", { "command": "product_data", "shop_id": shop.id, "customer_id": customer.id, "id": product.id, "quantity": 1 }), user=admin_user, HTTP_HOST=shop.domain, shop=shop) response = OrderEditView.as_view()(request) data = json.loads(response.content.decode("utf8")) assert decimal.Decimal( data['unitPrice']['value']) == shop.create_price(10).value