def render_product_view(shop_product, request): view_func = ProductEditView.as_view() response = view_func(request, pk=shop_product.pk) assert (shop_product.product.sku in response.rendered_content) # it's probable the SKU is there response = view_func(request, pk=None) # "new mode" assert response.rendered_content # yeah, something gets rendered
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_product_edit_view_works_at_all(rf, admin_user): shop = get_default_shop() product = create_product("test-product", shop, default_price=200) shop_product = product.get_shop_instance(shop) shop_product.visibility_limit = ProductVisibility.VISIBLE_TO_GROUPS shop_product.save() request = apply_request_middleware(rf.get("/"), user=admin_user) with replace_modules([ CategoryModule, ImportAdminModule, ProductModule, MediaModule, ProductTypeModule, ManufacturerModule, PaymentMethodModule, ShippingMethodModule, ]): with admin_only_urls(): view_func = ProductEditView.as_view() response = view_func(request, pk=shop_product.pk) response.render() assert product.sku in response.rendered_content # it's probable the SKU is there response = view_func(request, pk=None) # "new mode" assert response.rendered_content # yeah, something gets rendered
def test_edit_view_adding_messages_to_form_group(rf, admin_user): shop = get_default_shop() # obvious prerequisite product = get_default_product() shop_product = product.get_shop_instance(shop) view = ProductEditView.as_view() request = apply_request_middleware(rf.get("/"), user=admin_user) response = view(request, pk=shop_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, "lxml")) 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=shop_product.pk) errors = response.context_data["form"].errors assert "base" in errors assert "name__en" in errors["base"]
def test_product_edit_view_multipleshops(rf): """ Check whether a staff user from Shop A can see the product from Shop B when the staff user is only attached to Shop A """ with override_settings(SHUUP_ENABLE_MULTIPLE_SHOPS=True): shop1 = get_default_shop() shop2 = get_new_shop(identifier="shop2", domain="shop2", name="Shop 2") shop2_staff = create_random_user(is_staff=True) shop2.staff_members.add(shop2_staff) product = create_product("shop1-product", shop=shop1) shop_product = product.get_shop_instance(shop1) request = apply_request_middleware(rf.get("/", HTTP_HOST=shop2.domain), user=shop2_staff) view_func = ProductEditView.as_view() with pytest.raises(Http404): view_func(request, pk=shop_product.pk) view_func = ProductListView.as_view() payload = {"jq": json.dumps({"perPage": 100, "page": 1}), "shop": shop2.pk} request = apply_request_middleware(rf.get("/", payload, HTTP_HOST=shop2.domain), user=shop2_staff) response = view_func(request) assert response.status_code == 200 data = json.loads(response.content.decode("utf-8")) assert len(data["items"]) == 0
def test_product_edit_view_multishop(rf, admin_user): with override_settings(SHUUP_ENABLE_MULTIPLE_SHOPS=True): activate("en") product = create_product(sku="TEST-SKU-HAHA") shop_products = [] for i in range(5): shop_name = "test%d" % i shop = Shop.objects.create(name=shop_name, domain=shop_name, status=ShopStatus.ENABLED) shop_products.append( ShopProduct.objects.create( product=product, shop=shop, visibility=ShopProductVisibility.ALWAYS_VISIBLE)) assert Product.objects.count() == 1 view = ProductEditView.as_view() for shop_product in shop_products: request = apply_request_middleware(rf.get( "/", HTTP_HOST=shop_product.shop.domain), user=admin_user) response = view(request, pk=shop_product.pk) assert response.status_code == 200 response.render() content = force_text(response.content) assert product.sku in content
def test_product_edit_view_with_params(rf, admin_user): get_default_shop() sku = "test-sku" name = "test name" request = apply_request_middleware(rf.get("/", { "name": name, "sku": sku }), user=admin_user) with replace_modules([ CategoryModule, ImportAdminModule, ProductModule, MediaModule, ProductTypeModule, ManufacturerModule, PaymentMethodModule, ShippingMethodModule, ]): with admin_only_urls(): view_func = ProductEditView.as_view() response = view_func(request) assert sku in response.rendered_content # it's probable the SKU is there assert name in response.rendered_content # it's probable the name is there
def test_product_edit_view_with_params(rf, admin_user): get_default_shop() sku = "test-sku" name = "test name" request = apply_request_middleware(rf.get("/", {"name": name, "sku": sku}), user=admin_user) with replace_modules([CategoryModule, ImportAdminModule, ProductModule, ProductTypeModule, ManufacturerModule, PaymentMethodModule, ShippingMethodModule]): with admin_only_urls(): view_func = ProductEditView.as_view() response = view_func(request) assert (sku in response.rendered_content) # it's probable the SKU is there assert (name in response.rendered_content) # it's probable the name is there
def test_product_edit_view_multipleshops(rf): """ Check whether a staff user from Shop A can see the product from Shop B when the staff user is only attached to Shop A """ with override_settings(SHUUP_ENABLE_MULTIPLE_SHOPS=True): assert Product.objects.count() == 0 shop1 = get_default_shop() shop2 = get_new_shop(identifier="shop2", domain="shop2", name="Shop 2") shop2_staff = create_random_user(is_staff=True) shop2.staff_members.add(shop2_staff) assert Product.objects.count() == 1 product = create_product("shop1-product", shop=shop1) assert Product.objects.count() == 2 # Default product is set to default shop as well assert ShopProduct.objects.filter(shop=shop1).count() == 2 # Default product created in get_new_shop-function assert ShopProduct.objects.filter(shop=shop2).count() == 1 shop_product = product.get_shop_instance(shop1) request = apply_request_middleware(rf.get("/", HTTP_HOST=shop2.domain), user=shop2_staff) assert get_shop(request) == shop2 view_func = ProductEditView.as_view() with pytest.raises(Http404): view_func(request, pk=shop_product.pk) view_func = ProductListView.as_view() payload = { "jq": json.dumps({ "perPage": 100, "page": 1 }), "shop": shop2.pk } request = apply_request_middleware(rf.get("/", payload, HTTP_HOST=shop2.domain), user=shop2_staff) assert get_shop(request) == shop2 response = view_func(request) assert response.status_code == 200 data = json.loads(response.content.decode("utf-8")) assert len(data["items"] ) == 1 # There is one shop product create in "get new shop"
def test_copy_url_at_edit_view(rf, admin_user): shop = factories.get_default_shop() request = apply_request_middleware(rf.get("/", {}), user=admin_user) product = factories.create_product("product", shop=shop) shop_product = product.get_shop_instance(shop) view_func = ProductEditView.as_view() response = view_func(request, pk=shop_product.pk) if hasattr(response, "render"): response.render() soup = BeautifulSoup(response.content) expected_url = "/sa/products/%s/copy/" % shop_product.pk assert len(soup.findAll("a", {"href": expected_url})) == 1
def test_product_edit_view_works_at_all(rf, admin_user): shop = get_default_shop() product = create_product("test-product", shop, default_price=200) shop_product = product.get_shop_instance(shop) shop_product.visibility_limit = ProductVisibility.VISIBLE_TO_GROUPS shop_product.save() request = apply_request_middleware(rf.get("/"), user=admin_user) with replace_modules([CategoryModule, ImportAdminModule, ProductModule, ProductTypeModule, ManufacturerModule, PaymentMethodModule, ShippingMethodModule]): with admin_only_urls(): view_func = ProductEditView.as_view() response = view_func(request, pk=product.pk) response.render() assert (product.sku in response.rendered_content) # it's probable the SKU is there response = view_func(request, pk=None) # "new mode" assert response.rendered_content # yeah, something gets rendered
def test_product_edit_view_multishop(rf, admin_user, settings): activate("en") product = create_product(sku="TEST-SKU-HAHA") for i in range(5): shop = Shop.objects.create(name="test-%d" % i) sp = ShopProduct.objects.create( product=product, shop=shop, visibility=ShopProductVisibility.ALWAYS_VISIBLE ) assert Product.objects.count() == 1 view = ProductEditView.as_view() for shop_product in ShopProduct.objects.all(): request = apply_request_middleware(rf.get("/"), user=admin_user) response = view(request, pk=shop_product.pk) response.render() content = force_text(response.content) assert product.sku in content
def test_product_edit_view_multiplessuppliers(rf, admin_user): shop = get_default_shop() supplier = get_default_supplier() product = create_product("product", shop=shop) shop_product = product.get_shop_instance(shop) product_with_supplier = create_product(sku="product_with_supplier", shop=shop, supplier=supplier) shop_product_with_supplier = product_with_supplier.get_shop_instance(shop) with override_settings( SHUUP_ADMIN_SUPPLIER_PROVIDER_SPEC= "shuup.testing.supplier_provider.FirstSupplierProvider"): request = apply_request_middleware(rf.get("/", HTTP_HOST=shop.domain), user=admin_user) view_func = ProductEditView.as_view() with pytest.raises(Http404): view_func(request, pk=shop_product.pk) view_func(request, pk=shop_product_with_supplier.pk)
def test_product_edit_view_multishop(rf, admin_user): with override_settings(SHUUP_ENABLE_MULTIPLE_SHOPS=True): activate("en") product = create_product(sku="TEST-SKU-HAHA") shop_products = [] for i in range(5): shop_name = "test%d" % i shop = Shop.objects.create(name=shop_name, domain=shop_name, status=ShopStatus.ENABLED) shop_products.append( ShopProduct.objects.create(product=product, shop=shop, visibility=ShopProductVisibility.ALWAYS_VISIBLE) ) assert Product.objects.count() == 1 view = ProductEditView.as_view() for shop_product in shop_products: request = apply_request_middleware(rf.get("/", HTTP_HOST=shop_product.shop.domain), user=admin_user) response = view(request, pk=shop_product.pk) assert response.status_code == 200 response.render() content = force_text(response.content) assert product.sku in content
def test_product_edit_view(rf, admin_user, settings): shop = get_default_shop() # obvious prerequisite product = get_default_product() shop_product = product.get_shop_instance(shop) cat = CategoryFactory() assert not shop_product.categories.exists() assert not shop_product.primary_category view = ProductEditView.as_view() request = apply_request_middleware(rf.get("/"), user=admin_user) response = view(request, pk=product.pk) response.render() content = force_text(response.content) post = extract_form_fields(BeautifulSoup(content)) # Needed for Django 1.8 tests to pass post.update({ 'shop1-default_price_value': '42', 'images-TOTAL_FORMS': '0', 'media-TOTAL_FORMS': '0', 'base-name__fi': 'test', 'base-name__it': 'test', 'base-name__ja': 'test', 'base-name__pt-br': 'test', 'base-name__zh-hans': 'test', }) post_data = { 'shop1-primary_category': [], 'shop1-categories': [] } post.update(post_data) request = apply_request_middleware(rf.post("/", post), user=admin_user) response = view(request, pk=product.pk) shop_product.refresh_from_db() assert not shop_product.categories.exists() assert not shop_product.primary_category post_data = { 'shop1-default_price_value': 12, 'shop1-primary_category': [cat.pk], 'shop1-categories': [] } post.update(post_data) usable_post = {} for k, v in six.iteritems(post): if not k: continue if not post[k]: continue usable_post[k] = v request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=product.pk) shop_product = ShopProduct.objects.first() if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.categories.count() == 1 assert shop_product.categories.first() == cat else: assert not shop_product.categories.count() assert shop_product.primary_category == cat post_data = { 'shop1-primary_category': [], 'shop1-categories': [] } usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=product.pk) # empty again shop_product = ShopProduct.objects.first() assert not shop_product.categories.exists() assert not shop_product.primary_category post_data = { 'shop1-primary_category': [], 'shop1-categories': [cat.pk] } usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=product.pk) shop_product = ShopProduct.objects.first() assert shop_product.categories.count() == 1 assert shop_product.categories.first() == cat if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.primary_category == cat else: assert not shop_product.primary_category cat2 = CategoryFactory() post_data = { 'shop1-primary_category': [], 'shop1-categories': [cat.pk, cat2.pk] } usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=product.pk) shop_product = ShopProduct.objects.first() assert shop_product.categories.count() == 2 assert cat in shop_product.categories.all() assert cat2 in shop_product.categories.all() if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.primary_category == cat else: assert not shop_product.primary_category
def test_product_edit_view(rf, admin_user, settings): shop = get_default_shop() # obvious prerequisite shop.staff_members.add(admin_user) parent = create_product("ComplexVarParent", shop=shop, supplier=get_default_supplier()) sizes = [("%sL" % ("X" * x)) for x in range(4)] for size in sizes: child = create_product( "ComplexVarChild-%s" % size, shop=shop, supplier=get_default_supplier()) child.link_to_parent(parent, variables={"size": size}) shop_product = parent.get_shop_instance(shop) cat = CategoryFactory() assert not shop_product.categories.exists() assert not shop_product.primary_category view = ProductEditView.as_view() request = apply_request_middleware(rf.get("/"), user=admin_user) response = view(request, pk=shop_product.pk) response.render() content = force_text(response.content) post = extract_form_fields(BeautifulSoup(content, "lxml")) # Needed for Django 1.8 tests to pass post.update({ 'shop1-default_price_value': '42', 'images-TOTAL_FORMS': '0', 'media-TOTAL_FORMS': '0', 'base-name__fi': 'test', 'base-name__it': 'test', 'base-name__ja': 'test', 'base-name__pt-br': 'test', 'base-name__zh-hans': 'test', }) post_data = { 'shop1-primary_category': [], 'shop1-categories': [] } post.update(post_data) request = apply_request_middleware(rf.post("/", post), user=admin_user) response = view(request, pk=shop_product.pk) shop_product.refresh_from_db() assert not shop_product.categories.exists() assert not shop_product.primary_category post_data = { 'shop1-default_price_value': 12, 'shop1-primary_category': cat.pk, 'shop1-categories': [] } post.update(post_data) usable_post = {} for k, v in six.iteritems(post): if not k: continue if not post[k]: continue usable_post[k] = v request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=shop_product.pk) shop_product = ShopProduct.objects.first() assert shop_product.primary_category if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.categories.count() == 1 assert shop_product.categories.first() == cat else: assert not shop_product.categories.count() assert shop_product.primary_category == cat post_data = { 'shop1-primary_category': [], 'shop1-categories': [] } usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=shop_product.pk) # empty again shop_product = ShopProduct.objects.first() assert not shop_product.categories.exists() assert not shop_product.primary_category post_data = { 'shop1-primary_category': [], 'shop1-categories': [cat.pk] } usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=shop_product.pk) shop_product = ShopProduct.objects.first() assert shop_product.categories.count() == 1 assert shop_product.categories.first() == cat if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.primary_category == cat else: assert not shop_product.primary_category cat2 = CategoryFactory() post_data = { 'shop1-primary_category': [], 'shop1-categories': [cat.pk, cat2.pk] } usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=shop_product.pk) shop_product = ShopProduct.objects.first() assert shop_product.categories.count() == 2 assert cat in shop_product.categories.all() assert cat2 in shop_product.categories.all() if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.primary_category == cat else: assert not shop_product.primary_category
def test_product_edit_view(rf, admin_user, settings): shop = get_default_shop() # obvious prerequisite shop.staff_members.add(admin_user) parent = create_product("ComplexVarParent", shop=shop, supplier=get_default_supplier()) sizes = [("%sL" % ("X" * x)) for x in range(4)] for size in sizes: child = create_product("ComplexVarChild-%s" % size, shop=shop, supplier=get_default_supplier()) child.link_to_parent(parent, variables={"size": size}) shop_product = parent.get_shop_instance(shop) cat = CategoryFactory() assert not shop_product.categories.exists() assert not shop_product.primary_category view = ProductEditView.as_view() request = apply_request_middleware(rf.get("/"), user=admin_user) response = view(request, pk=shop_product.pk) response.render() content = force_text(response.content) post = extract_form_fields(BeautifulSoup(content, "lxml")) # Needed for Django 1.8 tests to pass post.update({ "shop1-default_price_value": "42", "images-TOTAL_FORMS": "0", "media-TOTAL_FORMS": "0", "base-name__fi": "test", "base-name__it": "test", "base-name__ja": "test", "base-name__pt-br": "test", "base-name__zh-hans": "test", "base-name__es": "test", }) post_data = {"shop1-primary_category": [], "shop1-categories": []} post.update(post_data) request = apply_request_middleware(rf.post("/", post), user=admin_user) response = view(request, pk=shop_product.pk) shop_product.refresh_from_db() assert not shop_product.categories.exists() assert not shop_product.primary_category post_data = { "shop1-default_price_value": 12, "shop1-primary_category": cat.pk, "shop1-categories": [] } post.update(post_data) usable_post = {} for k, v in six.iteritems(post): if not k: continue if not post[k]: continue usable_post[k] = v with patch("django.db.transaction.on_commit", new=atomic_commit_mock): request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=shop_product.pk) shop_product = ShopProduct.objects.first() assert shop_product.primary_category # the catalog price was indexed catalog_price = ProductCatalogPrice.objects.filter( shop=shop_product.shop, product=shop_product.product).first() assert catalog_price.price_value == shop_product.default_price_value if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.categories.count() == 1 assert shop_product.categories.first() == cat else: assert not shop_product.categories.count() assert shop_product.primary_category == cat post_data = {"shop1-primary_category": [], "shop1-categories": []} usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=shop_product.pk) # empty again shop_product = ShopProduct.objects.first() assert not shop_product.categories.exists() assert not shop_product.primary_category post_data = {"shop1-primary_category": [], "shop1-categories": [cat.pk]} usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=shop_product.pk) shop_product = ShopProduct.objects.first() assert shop_product.categories.count() == 1 assert shop_product.categories.first() == cat if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.primary_category == cat else: assert not shop_product.primary_category cat2 = CategoryFactory() post_data = { "shop1-primary_category": [], "shop1-categories": [cat.pk, cat2.pk] } usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=shop_product.pk) shop_product = ShopProduct.objects.first() assert shop_product.categories.count() == 2 assert cat in shop_product.categories.all() assert cat2 in shop_product.categories.all() if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.primary_category == cat else: assert not shop_product.primary_category # Test for showing alert of validation issues view = ProductEditView.as_view() request = apply_request_middleware(rf.get("/"), user=admin_user) response = view(request, pk=shop_product.pk) response.render() content = force_text(response.content) soup = BeautifulSoup(content, "lxml") alert = soup.find_all("div", {"class": "validation-issues-alert"}) assert not alert with override_provides( "admin_product_validator", ["shuup.testing.admin_product_validator:TestAdminProductValidator"], ): view = ProductEditView.as_view() request = apply_request_middleware(rf.get("/"), user=admin_user) response = view(request, pk=shop_product.pk) response.render() content = force_text(response.content) soup = BeautifulSoup(content, "lxml") alert = soup.find_all( "div", {"class": "validation-issues-alert alert alert-danger"}) assert alert alert_danger = soup.find_all("div", {"class": "alert-danger"}) assert alert_danger alert = soup.find_all( "div", {"class": "validation-issues-alert alert alert-warning"}) alert_div = alert[0] strong = alert_div.find_all("strong") assert strong script = alert_div.find_all("script") assert not script
def test_product_edit_view(rf, admin_user, settings): shop = get_default_shop() # obvious prerequisite shop.staff_members.add(admin_user) parent = create_product("ComplexVarParent", shop=shop, supplier=get_default_supplier()) sizes = [("%sL" % ("X" * x)) for x in range(4)] for size in sizes: child = create_product("ComplexVarChild-%s" % size, shop=shop, supplier=get_default_supplier()) child.link_to_parent(parent, variables={"size": size}) shop_product = parent.get_shop_instance(shop) cat = CategoryFactory() assert not shop_product.categories.exists() assert not shop_product.primary_category view = ProductEditView.as_view() request = apply_request_middleware(rf.get("/"), user=admin_user) response = view(request, pk=shop_product.pk) response.render() content = force_text(response.content) post = extract_form_fields(BeautifulSoup(content, "lxml")) # Needed for Django 1.8 tests to pass post.update({ 'shop1-default_price_value': '42', 'images-TOTAL_FORMS': '0', 'media-TOTAL_FORMS': '0', 'base-name__fi': 'test', 'base-name__it': 'test', 'base-name__ja': 'test', 'base-name__pt-br': 'test', 'base-name__zh-hans': 'test', }) post_data = {'shop1-primary_category': [], 'shop1-categories': []} post.update(post_data) request = apply_request_middleware(rf.post("/", post), user=admin_user) response = view(request, pk=shop_product.pk) shop_product.refresh_from_db() assert not shop_product.categories.exists() assert not shop_product.primary_category post_data = { 'shop1-default_price_value': 12, 'shop1-primary_category': cat.pk, 'shop1-categories': [] } post.update(post_data) usable_post = {} for k, v in six.iteritems(post): if not k: continue if not post[k]: continue usable_post[k] = v request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=shop_product.pk) shop_product = ShopProduct.objects.first() assert shop_product.primary_category if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.categories.count() == 1 assert shop_product.categories.first() == cat else: assert not shop_product.categories.count() assert shop_product.primary_category == cat post_data = {'shop1-primary_category': [], 'shop1-categories': []} usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=shop_product.pk) # empty again shop_product = ShopProduct.objects.first() assert not shop_product.categories.exists() assert not shop_product.primary_category post_data = {'shop1-primary_category': [], 'shop1-categories': [cat.pk]} usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=shop_product.pk) shop_product = ShopProduct.objects.first() assert shop_product.categories.count() == 1 assert shop_product.categories.first() == cat if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.primary_category == cat else: assert not shop_product.primary_category cat2 = CategoryFactory() post_data = { 'shop1-primary_category': [], 'shop1-categories': [cat.pk, cat2.pk] } usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=shop_product.pk) shop_product = ShopProduct.objects.first() assert shop_product.categories.count() == 2 assert cat in shop_product.categories.all() assert cat2 in shop_product.categories.all() if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.primary_category == cat else: assert not shop_product.primary_category
def render_product_view(product, request): view_func = ProductEditView.as_view() response = view_func(request, pk=product.pk) assert (product.sku in response.rendered_content) # it's probable the SKU is there response = view_func(request, pk=None) # "new mode" assert response.rendered_content # yeah, something gets rendered
def test_product_edit_view(rf, admin_user, settings): shop = get_default_shop() # obvious prerequisite product = get_default_product() shop_product = product.get_shop_instance(shop) cat = CategoryFactory() assert not shop_product.categories.exists() assert not shop_product.primary_category view = ProductEditView.as_view() request = apply_request_middleware(rf.get("/"), user=admin_user) response = view(request, pk=product.pk) response.render() content = force_text(response.content) post = extract_form_fields(BeautifulSoup(content)) # Needed for Django 1.8 tests to pass post.update({ 'shop1-default_price_value': '42', 'images-TOTAL_FORMS': '0', 'media-TOTAL_FORMS': '0', 'base-name__fi': 'test', 'base-name__it': 'test', 'base-name__ja': 'test', 'base-name__pt-br': 'test', 'base-name__zh-hans': 'test', }) post_data = {'shop1-primary_category': [], 'shop1-categories': []} post.update(post_data) request = apply_request_middleware(rf.post("/", post), user=admin_user) response = view(request, pk=product.pk) shop_product.refresh_from_db() assert not shop_product.categories.exists() assert not shop_product.primary_category post_data = { 'shop1-default_price_value': 12, 'shop1-primary_category': [cat.pk], 'shop1-categories': [] } post.update(post_data) usable_post = {} for k, v in six.iteritems(post): if not k: continue if not post[k]: continue usable_post[k] = v request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=product.pk) shop_product = ShopProduct.objects.first() if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.categories.count() == 1 assert shop_product.categories.first() == cat else: assert not shop_product.categories.count() assert shop_product.primary_category == cat post_data = {'shop1-primary_category': [], 'shop1-categories': []} usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=product.pk) # empty again shop_product = ShopProduct.objects.first() assert not shop_product.categories.exists() assert not shop_product.primary_category post_data = {'shop1-primary_category': [], 'shop1-categories': [cat.pk]} usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=product.pk) shop_product = ShopProduct.objects.first() assert shop_product.categories.count() == 1 assert shop_product.categories.first() == cat if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.primary_category == cat else: assert not shop_product.primary_category cat2 = CategoryFactory() post_data = { 'shop1-primary_category': [], 'shop1-categories': [cat.pk, cat2.pk] } usable_post.update(post_data) request = apply_request_middleware(rf.post("/", usable_post), user=admin_user) response = view(request, pk=product.pk) shop_product = ShopProduct.objects.first() assert shop_product.categories.count() == 2 assert cat in shop_product.categories.all() assert cat2 in shop_product.categories.all() if settings.SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES: assert shop_product.primary_category == cat else: assert not shop_product.primary_category