def test_simple_children_formset():
    FormSet = formset_factory(SimpleVariationChildForm,
                              SimpleVariationChildFormSet,
                              extra=5,
                              can_delete=True)
    parent = create_product(printable_gibberish())
    child = create_product(printable_gibberish())

    # No links yet
    formset = FormSet(parent_product=parent)
    assert formset.initial_form_count() == 0  # No children yet

    # Save a link
    data = dict(get_form_data(formset, True), **{"form-0-child": child.pk})
    formset = FormSet(parent_product=parent, data=data)
    formset.save()
    assert parent.variation_children.filter(
        pk=child.pk).exists()  # Got link'd!

    # Remove the link
    formset = FormSet(parent_product=parent)
    assert formset.initial_form_count() == 1  # Got the child here
    data = dict(get_form_data(formset, True), **{"form-0-DELETE": "1"})
    formset = FormSet(parent_product=parent, data=data)
    formset.save()
    assert not parent.variation_children.exists()  # Got unlinked
def test_contact_group_members_formset(rf):
    FormSet = formset_factory(ContactGroupMembersForm,
                              ContactGroupMembersFormSet,
                              extra=1,
                              can_delete=True)
    contact_group = get_default_customer_group()
    person = create_random_person()

    # No members
    formset = FormSet(contact_group=contact_group)
    assert formset.initial_form_count() == 0

    # Add a member
    data = dict(get_form_data(formset, True), **{"form-0-member": person.pk})
    formset = FormSet(contact_group=contact_group, data=data)
    formset.save()
    assert contact_group.members.filter(pk=person.pk).exists()

    # Remove a member
    formset = FormSet(contact_group=contact_group)
    assert formset.initial_form_count() == 1
    data = dict(get_form_data(formset, True), **{"form-0-DELETE": "1"})
    formset = FormSet(contact_group=contact_group, data=data)
    formset.save()
    assert not contact_group.members.exists()
Exemple #3
0
def test_protected_fields():
    activate("en")
    shop = Shop.objects.create(
        name="testshop",
        identifier="testshop",
        status=ShopStatus.ENABLED,
        public_name="test shop",
        domain="derp",
        currency="EUR"
    )
    assert shop.name == "testshop"
    assert shop.currency == "EUR"
    shop_form = ShopBaseForm(instance=shop, languages=settings.LANGUAGES)
    assert not shop_form._get_protected_fields()  # No protected fields just yet, right?
    data = get_form_data(shop_form, prepared=True)
    shop_form = ShopBaseForm(data=data, instance=shop, languages=settings.LANGUAGES)
    _test_cleanliness(shop_form)
    shop_form.save()

    # Now let's make it protected!
    create_product(printable_gibberish(), shop=shop, supplier=get_default_supplier())
    order = create_random_order(customer=create_random_person(), shop=shop)
    assert order.shop == shop

    # And try again...
    data["currency"] = "XBT"  # Bitcoins!
    shop_form = ShopBaseForm(data=data, instance=shop, languages=settings.LANGUAGES)
    assert shop_form._get_protected_fields()  # So protected!
    _test_cleanliness(shop_form)
    shop = shop_form.save()
    assert shop.currency == "EUR"  # But the shop form ignored the change . . .
Exemple #4
0
def test_change_shop_price():
    product = _get_test_product()
    shop = get_default_shop()
    group = get_default_customer_group()

    form_field = "s_%d_g_%d" % (shop.id, group.id)

    frm = SimplePricingForm(product=product, empty_permitted=True)
    form_data = get_form_data(frm, prepared=True)
    # Price hike time!
    form_data[form_field] = "4000"
    frm = SimplePricingForm(product=product,
                            data=form_data,
                            empty_permitted=True)
    frm.full_clean()
    frm.save()
    assert SimpleProductPrice.objects.get(product=product,
                                          shop=shop,
                                          group=group).price == 4000

    # Never mind actually, same price for all shops
    form_data[form_field] = ""
    frm = SimplePricingForm(product=product,
                            data=form_data,
                            empty_permitted=True)
    frm.full_clean()
    frm.save()

    assert not SimpleProductPrice.objects.filter(
        product=product, shop=shop, group=group).exists()
Exemple #5
0
def test_change_shop_price():
    product = _get_test_product()
    shop = get_default_shop()

    form_field = "s_%d" % shop.id

    frm = DiscountPricingForm(product=product, empty_permitted=True)
    form_data = get_form_data(frm, prepared=True)
    # Price hike time!
    form_data[form_field] = "120"
    frm = DiscountPricingForm(product=product,
                              data=form_data,
                              empty_permitted=True)
    frm.full_clean()
    frm.save()
    assert DiscountedProductPrice.objects.get(product=product,
                                              shop=shop).price.value == 120

    # Never mind actually, same price for all shops
    form_data[form_field] = ""
    frm = DiscountPricingForm(product=product,
                              data=form_data,
                              empty_permitted=True)
    frm.full_clean()
    frm.save()

    assert not DiscountedProductPrice.objects.filter(product=product,
                                                     shop=shop).exists()
Exemple #6
0
def test_admin_catalog_campaign_edit_view(rf, admin_user):
    shop = get_default_shop()
    view = CatalogCampaignEditView(request=apply_request_middleware(rf.get("/"), user=admin_user))
    form_class = view.get_form_class()
    form_kwargs = view.get_form_kwargs()
    form = form_class(**form_kwargs)

    assert not form.is_bound

    data = get_form_data(form)
    data.update({
        "shop": shop.pk,
        "name": "test",
    })
    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()
    assert "You must define discount percentage or amount" in form.errors["__all__"][0]

    data.update({"discount_amount_value": "20"})
    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()

    # atleast 1 rule is required
    assert "You must set at least one rule for this campaign" in form.errors["__all__"][0]

    supplier = get_default_supplier()
    product = create_product(printable_gibberish(), shop=shop, supplier=supplier, default_price="20")
    data.update({"product_filter": [1]})

    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()
    assert not form.errors

    campaign = form.save()
    assert campaign.filters.count() == 1
Exemple #7
0
def test_editor_save(rf):
    layout, svc = get_test_layout_and_svc()

    with initialize_editor_view(svc.view_name,
                                layout.placeholder_name) as view_obj:
        view_obj.request.GET.update({"x": 0, "y": 0})
        view_obj.dispatch(view_obj.request)
        assert view_obj.current_cell
        assert view_obj.form
        assert "general" in view_obj.form.forms
        assert "plugin" in view_obj.form.forms
        form_data = get_form_data(view_obj.form, prepared=True)

    new_text = printable_gibberish()
    form_data["plugin-text_%s" % FALLBACK_LANGUAGE_CODE] = new_text
    form_data["save"] = "1"
    request = rf.post("/pepe/", data=form_data)  # sort of rare pepe
    request.GET = dict(request.GET, x=0, y=0)
    with initialize_editor_view(svc.view_name, layout.placeholder_name,
                                request) as view_obj:
        view_obj.dispatch(request)
        assert view_obj.form
        assert not view_obj.form.errors
        assert view_obj.current_cell.config["text"] == {
            FALLBACK_LANGUAGE_CODE: new_text
        }
def test_product_attributes_form():
    # Come up with a decent product...
    product = create_product("TestProduct", shop=get_default_shop())
    assert product.type.attributes.count()
    product.set_attribute_value("bogomips", 6400)
    product.set_attribute_value("genre", "Kauhu", "fi")
    product.set_attribute_value("genre", "Horror", "en")
    product.set_attribute_value("time_to_finish", 12.05)
    product.set_attribute_value("release_date", VOGONY_DATE)
    assert product.get_attribute_value("bogomips") == 6400

    # English is missing on purpose from the languages list; it'll still be available
    # for `genre` as it has an extant value.
    paf = ProductAttributesForm(product=product, languages=("fi", "sv"))
    assert compare_partial_dicts(
        paf.initial,
        {  # Check that things get loaded.
            "bogomips": 6400,
            "genre__fi": "Kauhu",
            "genre__en": "Horror",
            "release_date": VOGONY_DATE,
            "time_to_finish": Decimal("12.05")
        })
    form_data = get_form_data(paf)
    form_data.update({  # Change, clear and add fields
        "genre__sv":
        "Skräck",
        "genre__en":
        "Terror",
        "bogomips":
        "",
        "release_date":
        "",
        "awesome":
        "True",
        "important_datetime":
        make_aware(datetime.datetime(2000, 1, 1, 1, 2, 3), utc)
    })
    paf = ProductAttributesForm(product=product,
                                languages=("fi", "sv"),
                                data=form_data)
    paf.full_clean()
    assert not paf.errors
    paf.save()

    for identifier in ("bogomips", "release_date"):
        # Value should be gone
        assert not product.get_attribute_value(identifier)
        # ... and so should the actual product attribute object
        assert not ProductAttribute.objects.filter(
            attribute__identifier=identifier, product=product).exists()

    # Those other values got updated, right?
    assert product.get_attribute_value("genre", "en") == form_data["genre__en"]
    assert product.get_attribute_value("genre", "sv") == form_data["genre__sv"]
    assert product.get_attribute_value("awesome") is True
    assert str(product.get_attribute_value(
        "important_datetime")) == "2000-01-01 01:02:03+00:00"
Exemple #9
0
def test_no_changes_into_form():
    product = _get_test_product()
    shop = get_default_shop()
    frm = DiscountPricingForm(product=product, empty_permitted=True)
    form_data = get_form_data(frm, prepared=True)
    frm = DiscountPricingForm(product=product, data=form_data, empty_permitted=True)
    frm.full_clean()
    frm.save()
    assert DiscountedProductPrice.objects.get(product=product, shop=shop).price.value == 108
Exemple #10
0
def test_admin_catalog_campaign_edit_view(rf, admin_user):
    shop = get_default_shop()
    view = CatalogCampaignEditView(
        request=apply_request_middleware(rf.get("/"), user=admin_user))
    form_class = view.get_form_class()
    form_kwargs = view.get_form_kwargs()
    form = form_class(**form_kwargs)

    assert not form.is_bound

    data = get_form_data(form)
    data.update({
        "shop": shop.pk,
        "name": "test",
    })
    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()
    assert "At least one effect must be defined." in form.errors["__all__"][0]

    data.update({"discount_amount_effect": "20"})
    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()

    # at least 1 rule is required
    assert "You must set at least one rule for this campaign" in form.errors[
        "__all__"][0]

    supplier = get_default_supplier()
    product = create_product(printable_gibberish(),
                             shop=shop,
                             supplier=supplier,
                             default_price="20")
    data.update({"product_filter": [product.id]})

    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()
    assert not form.errors

    data.update({
        "start_datetime":
        datetime.datetime.now() + datetime.timedelta(days=1),
        "end_datetime":
        datetime.datetime.now()
    })
    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()

    assert "Campaign end date can't be before start date" in form.errors[
        "__all__"][0]

    data.update({"start_datetime": None})
    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()
    assert not form.errors

    campaign = form.save()
    assert campaign.filters.count() == 1
Exemple #11
0
def test_no_changes_into_form():
    product = _get_test_product()
    shop = get_default_shop()
    frm = SimplePricingForm(product=product, empty_permitted=True)
    # No changes made, right?
    form_data = get_form_data(frm, prepared=True)
    frm = SimplePricingForm(product=product, data=form_data, empty_permitted=True)
    frm.full_clean()
    frm.save()
    assert SimpleProductPrice.objects.get(product=product, shop=shop).price.value == 250
Exemple #12
0
def test_no_changes_into_form():
    product = _get_test_product()
    shop = get_default_shop()
    frm = CustomerGroupPricingForm(product=product, empty_permitted=True)
    # No changes made, right?
    form_data = get_form_data(frm, prepared=True)
    frm = CustomerGroupPricingForm(product=product, data=form_data, empty_permitted=True)
    frm.full_clean()
    frm.save()
    assert CgpPrice.objects.get(product=product, shop=shop).price.value == 250
Exemple #13
0
def test_no_changes_into_form():
    product = _get_test_product()
    shop = get_default_shop()
    frm = DiscountPricingForm(product=product, empty_permitted=True)
    form_data = get_form_data(frm, prepared=True)
    frm = DiscountPricingForm(product=product,
                              data=form_data,
                              empty_permitted=True)
    frm.full_clean()
    frm.save()
    assert DiscountedProductPrice.objects.get(product=product,
                                              shop=shop).price.value == 108
def test_simple_children_formset():
    FormSet = formset_factory(SimpleVariationChildForm, SimpleVariationChildFormSet, extra=5, can_delete=True)
    parent = create_product(printable_gibberish())
    child = create_product(printable_gibberish())

    # No links yet
    formset = FormSet(parent_product=parent)
    assert formset.initial_form_count() == 0  # No children yet

    # Save a link
    data = dict(get_form_data(formset, True), **{"form-0-child": child.pk})
    formset = FormSet(parent_product=parent, data=data)
    formset.save()
    assert parent.variation_children.filter(pk=child.pk).exists()  # Got link'd!

    # Remove the link
    formset = FormSet(parent_product=parent)
    assert formset.initial_form_count() == 1  # Got the child here
    data = dict(get_form_data(formset, True), **{"form-0-DELETE": "1"})
    formset = FormSet(parent_product=parent, data=data)
    formset.save()
    assert not parent.variation_children.exists()  # Got unlinked
def test_contact_group_members_formset(rf):
    FormSet = formset_factory(ContactGroupMembersForm, ContactGroupMembersFormSet, extra=1, can_delete=True)
    contact_group = get_default_customer_group()
    person = create_random_person()

    # No members
    formset = FormSet(contact_group=contact_group)
    assert formset.initial_form_count() == 0

    # Add a member
    data = dict(get_form_data(formset, True), **{"form-0-member": person.pk})
    formset = FormSet(contact_group=contact_group, data=data)
    formset.save()
    assert contact_group.members.filter(pk=person.pk).exists()

    # Remove a member
    formset = FormSet(contact_group=contact_group)
    assert formset.initial_form_count() == 1
    data = dict(get_form_data(formset, True), **{"form-0-DELETE": "1"})
    formset = FormSet(contact_group=contact_group, data=data)
    formset.save()
    assert not contact_group.members.exists()
Exemple #16
0
def test_admin_catalog_campaign_edit_view(rf, admin_user):
    shop = get_default_shop()
    view = CatalogCampaignEditView(request=apply_request_middleware(rf.get("/"), user=admin_user))
    form_class = view.get_form_class()
    form_kwargs = view.get_form_kwargs()
    form = form_class(**form_kwargs)

    assert not form.is_bound

    data = get_form_data(form)
    data.update({
        "shop": shop.pk,
        "name": "test",
    })
    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()
    assert "At least one effect must be defined." in form.errors["__all__"][0]

    data.update({"discount_amount_effect": "20"})
    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()

    # at least 1 rule is required
    assert "You must set at least one rule for this campaign" in form.errors["__all__"][0]

    supplier = get_default_supplier()
    product = create_product(printable_gibberish(), shop=shop, supplier=supplier, default_price="20")
    data.update({"product_filter": [product.id]})

    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()
    assert not form.errors

    data.update({
        "start_datetime": datetime.datetime.now() + datetime.timedelta(days=1),
        "end_datetime": datetime.datetime.now()
    })
    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()

    assert "Campaign end date can't be before start date" in form.errors["__all__"][0]

    data.update({
        "start_datetime": None
    })
    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()
    assert not form.errors

    campaign = form.save()
    assert campaign.filters.count() == 1
Exemple #17
0
def test_basic_form_sanity():
    shop = get_default_shop()
    group = get_default_customer_group()
    product = _get_test_product()
    frm = SimplePricingForm(product=product, empty_permitted=True)
    assert len(frm.groups) == 1 + ContactGroup.objects.count()
    assert len(frm.shops) == 1 + Shop.objects.count()
    for shop_id in (0, shop.id):
        for group_id in (0, group.id):
            assert "s_%d_g_%d" % (shop_id, group_id) in frm.fields

    form_data = get_form_data(frm)
    assert form_data["s_%d_g_0" % shop.id] == 250
    assert form_data["s_0_g_0"] == 200
Exemple #18
0
def test_basic_form_sanity():
    shop = get_default_shop()
    group = get_default_customer_group()
    product = _get_test_product()
    frm = SimplePricingForm(product=product, empty_permitted=True)
    assert len(frm.groups) == 1 + ContactGroup.objects.count()
    assert len(frm.shops) == 1 + Shop.objects.count()
    for shop_id in (0, shop.id):
        for group_id in (0, group.id):
            assert "s_%d_g_%d" % (shop_id, group_id) in frm.fields

    form_data = get_form_data(frm)
    assert form_data["s_%d_g_0" % shop.id] == 250
    assert form_data["s_0_g_0"] == 200
def test_cannot_add_product_to_own_package(rf):
    FormSet = formset_factory(PackageChildForm, PackageChildFormSet, extra=5, can_delete=True)
    parent = create_product(printable_gibberish())

    # No products in the package
    formset = FormSet(parent_product=parent)
    assert formset.initial_form_count() == 0 # No children yet

    assert not parent.get_all_package_children()

    # Try to add a product to its own package
    data = dict(get_form_data(formset, True), **{"form-0-child": parent.pk, "form-0-quantity": 2})
    formset = FormSet(parent_product=parent, data=data)
    formset.save()
    assert not parent.get_all_package_children()
def test_product_not_in_normal_mode():
    FormSet = formset_factory(PackageChildForm, PackageChildFormSet, extra=5, can_delete=True)
    parent = create_product(printable_gibberish())
    child_1 = create_product(printable_gibberish())
    child_1.link_to_parent(parent)
    child_2 = create_product(printable_gibberish())
    parent.verify_mode()

    assert parent.mode == ProductMode.SIMPLE_VARIATION_PARENT

    # Trying to create a package from a non-normal mode product
    with pytest.raises(Problem):
        formset = FormSet(parent_product=parent)
        data = dict(get_form_data(formset, True), **{"form-0-child": child_2.pk, "form-0-quantity": 2})
        formset = FormSet(parent_product=parent, data=data)
        formset.save()
Exemple #21
0
def test_category_form_saving(rf):
    with transaction.atomic():
        category = CategoryFactory()
        form_kwargs = dict(instance=category, languages=("sw",), default_language="sw")
        form = CategoryBaseForm(**form_kwargs)
        assert isinstance(form, CategoryBaseForm)
        form_data = get_form_data(form, prepared=True)
        for lang, field_map in form.trans_name_map.items():
            for dst_field in field_map.values():
                form_data[form.add_prefix(dst_field)] = "IJWEHGWOHKSL"
        form_kwargs["data"] = form_data
        form = CategoryBaseForm(**form_kwargs)
        form.full_clean()
        form.save()
        category = form.instance
        category.set_current_language("sw")
        assert category.name == "IJWEHGWOHKSL"
def test_product_attributes_form():
    # Come up with a decent product...
    product = create_product("TestProduct", shop=get_default_shop())
    assert product.type.attributes.count()
    product.set_attribute_value("bogomips", 6400)
    product.set_attribute_value("genre", "Kauhu", "fi")
    product.set_attribute_value("genre", "Horror", "en")
    product.set_attribute_value("time_to_finish", 12.05)
    product.set_attribute_value("release_date", VOGONY_DATE)
    assert product.get_attribute_value("bogomips") == 6400

    # English is missing on purpose from the languages list; it'll still be available
    # for `genre` as it has an extant value.
    paf = ProductAttributesForm(product=product, languages=("fi", "sv"))
    assert compare_partial_dicts(paf.initial, {  # Check that things get loaded.
        "bogomips": 6400,
        "genre__fi": "Kauhu",
        "genre__en": "Horror",
        "release_date": VOGONY_DATE,
        "time_to_finish": Decimal("12.05")
    })
    form_data = get_form_data(paf)
    form_data.update({  # Change, clear and add fields
        "genre__sv": "Skräck",
        "genre__en": "Terror",
        "bogomips": "",
        "release_date": "",
        "awesome": "True",
        "important_datetime": make_aware(datetime.datetime(2000, 1, 1, 1, 2, 3), utc)
    })
    paf = ProductAttributesForm(product=product, languages=("fi", "sv"), data=form_data)
    paf.full_clean()
    assert not paf.errors
    paf.save()

    for identifier in ("bogomips", "release_date"):
        # Value should be gone
        assert not product.get_attribute_value(identifier)
        # ... and so should the actual product attribute object
        assert not ProductAttribute.objects.filter(attribute__identifier=identifier, product=product).exists()

    # Those other values got updated, right?
    assert product.get_attribute_value("genre", "en") == form_data["genre__en"]
    assert product.get_attribute_value("genre", "sv") == form_data["genre__sv"]
    assert product.get_attribute_value("awesome") is True
    assert str(product.get_attribute_value("important_datetime")) == "2000-01-01 01:02:03+00:00"
Exemple #23
0
def test_page_form(rf):
    view = PageEditView(request=rf.get("/"))
    form_class = view.get_form_class()
    form_kwargs = view.get_form_kwargs()
    form = form_class(**form_kwargs)
    assert not form.is_bound

    data = get_form_data(form)
    data.update({
        "available_from": "",
        "available_to": "",
        "content__en": "",
        "content__fi": "suomi",
        "content__ja": "",
        "identifier": "",
        "title__en": "",
        "title__fi": "",
        "title__ja": "",
        "url__en": "",
        "url__fi": "suomi",
        "url__ja": "",
    })

    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()
    assert "title__fi" in form.errors  # We get an error because all of a given language's fields must be filled if any are
    data["title__fi"] = "suomi"
    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()
    assert not form.errors
    page = form.save()
    assert set(page.get_available_languages()) == {
        "fi"
    }  # The page should be only in Finnish
    # Let's edit that page
    data.update({
        "title__en": "englaish",
        "url__en": "errrnglish",
        "content__en": "ennnn ennnn ennnnnnn-nn-n-n"
    })
    form = form_class(**dict(form_kwargs, data=data, instance=page))
    form.full_clean()
    assert not form.errors
    page = form.save()
    assert set(page.get_available_languages()) == {"fi", "en"}  # English GET
def test_package_child_formset():
    FormSet = formset_factory(PackageChildForm, PackageChildFormSet, extra=5, can_delete=True)
    parent = create_product(printable_gibberish())
    child = create_product(printable_gibberish())

    # No products in the package
    formset = FormSet(parent_product=parent)
    assert formset.initial_form_count() == 0 # No children yet

    assert not parent.get_all_package_children()

    data = dict(get_form_data(formset, True), **{"form-0-child": child.pk, "form-0-quantity": 2})
    formset = FormSet(parent_product=parent, data=data)
    formset.save()
    assert parent.get_all_package_children()

    clear_existing_package(parent)
    assert not parent.get_all_package_children()
Exemple #25
0
def test_admin_basket_campaign_edit_view(rf, admin_user):
    activate("en")
    shop = get_default_shop()
    view = BasketCampaignEditView(
        request=apply_request_middleware(rf.get("/"), user=admin_user))
    form_class = view.get_form_class()
    form_kwargs = view.get_form_kwargs()
    form = form_class(**form_kwargs)

    assert not form.is_bound

    data = get_form_data(form)
    data.update({
        "shop": shop.pk,
        "name": "test",
        "public_name__en": "test",
    })
    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()
    assert "At least one effect must be defined." in form.errors["__all__"][0]

    data.update({"discount_amount_effect": "20"})
    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()

    # atleast 1 rule or coupon is required
    assert "You must set atleast one rule or discount code for this campaign." in form.errors[
        "__all__"][0]

    supplier = get_default_supplier()
    product = create_product(printable_gibberish(),
                             shop=shop,
                             supplier=supplier,
                             default_price="20")

    coupon = Coupon.objects.create(code="TEST-CODE", active=True)
    data.update({"coupon": coupon.pk})
    form = form_class(**dict(form_kwargs, data=data))

    form.full_clean()
    assert not form.errors

    campaign = form.save()
    assert campaign.coupon.pk == coupon.pk
def test_impossible_simple_variation():
    FormSet = formset_factory(SimpleVariationChildForm, SimpleVariationChildFormSet, extra=5, can_delete=True)
    parent = create_product(printable_gibberish())
    child = create_product(printable_gibberish())
    grandchild = create_product(printable_gibberish())
    grandchild.link_to_parent(child)
    assert child.variation_children.exists()

    formset = FormSet(parent_product=parent)
    data = dict(get_form_data(formset, True), **{"form-0-child": child.pk})
    formset = FormSet(parent_product=parent, data=data)
    assert formset.is_valid()  # It's technically valid, but...
    with pytest.raises(Problem) as ei:
        formset.save()

    if six.PY3:  # Can only test inner exceptions on Py3. Ah well.
        inner_exc = ei.value.__context__
        assert isinstance(inner_exc, ImpossibleProductModeException)
        assert inner_exc.code == "multilevel"
def test_category_form_saving(rf):
    with transaction.atomic():
        category = CategoryFactory()
        form_kwargs = dict(instance=category,
                           languages=("sw", ),
                           default_language="sw")
        form = CategoryBaseForm(**form_kwargs)
        assert isinstance(form, CategoryBaseForm)
        form_data = get_form_data(form, prepared=True)
        for lang, field_map in form.trans_name_map.items():
            for dst_field in field_map.values():
                form_data[form.add_prefix(dst_field)] = "IJWEHGWOHKSL"
        form_kwargs["data"] = form_data
        form = CategoryBaseForm(**form_kwargs)
        form.full_clean()
        form.save()
        category = form.instance
        category.set_current_language("sw")
        assert category.name == "IJWEHGWOHKSL"
Exemple #28
0
def test_change_shop_price():
    product = _get_test_product()
    shop = get_default_shop()
    frm = SimplePricingForm(product=product, empty_permitted=True)
    form_data = get_form_data(frm, prepared=True)
    # Price hike time!
    form_data["s_%d_g_0" % shop.id] = "4000"
    frm = SimplePricingForm(product=product, data=form_data, empty_permitted=True)
    frm.full_clean()
    frm.save()
    assert SimpleProductPrice.objects.get(product=product, shop=None).price == 200
    assert SimpleProductPrice.objects.get(product=product, shop=shop).price == 4000

    # Never mind actually, same price for all shops
    form_data["s_%d_g_0" % shop.id] = ""
    frm = SimplePricingForm(product=product, data=form_data, empty_permitted=True)
    frm.full_clean()
    frm.save()
    assert SimpleProductPrice.objects.get(product=product, shop=None).price == 200
    assert not SimpleProductPrice.objects.filter(product=product, shop=shop).exists()
Exemple #29
0
def test_cannot_add_product_to_own_package(rf):
    FormSet = formset_factory(PackageChildForm,
                              PackageChildFormSet,
                              extra=5,
                              can_delete=True)
    parent = create_product(printable_gibberish())

    # No products in the package
    formset = FormSet(parent_product=parent)
    assert formset.initial_form_count() == 0  # No children yet

    assert not parent.get_all_package_children()

    # Try to add a product to its own package
    data = dict(get_form_data(formset, True), **{
        "form-0-child": parent.pk,
        "form-0-quantity": 2
    })
    formset = FormSet(parent_product=parent, data=data)
    formset.save()
    assert not parent.get_all_package_children()
Exemple #30
0
def test_admin_basket_campaign_edit_view(rf, admin_user):
    activate("en")
    shop = get_default_shop()
    view = BasketCampaignEditView(request=apply_request_middleware(rf.get("/"), user=admin_user))
    form_class = view.get_form_class()
    form_kwargs = view.get_form_kwargs()
    form = form_class(**form_kwargs)

    assert not form.is_bound

    data = get_form_data(form)
    data.update({
        "shop": shop.pk,
        "name": "test",
        "public_name__en": "test",
    })
    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()
    assert "At least one effect must be defined." in form.errors["__all__"][0]

    data.update({"discount_amount_effect": "20"})
    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()

    # atleast 1 rule or coupon is required
    assert "You must set atleast one rule or discount code for this campaign." in form.errors["__all__"][0]

    supplier = get_default_supplier()
    product = create_product(printable_gibberish(), shop=shop, supplier=supplier, default_price="20")

    coupon = Coupon.objects.create(code="TEST-CODE", active=True)
    data.update({"coupon": coupon.pk})
    form = form_class(**dict(form_kwargs, data=data))

    form.full_clean()
    assert not form.errors

    campaign = form.save()
    assert campaign.coupon.pk == coupon.pk
Exemple #31
0
def test_product_not_in_normal_mode():
    FormSet = formset_factory(PackageChildForm,
                              PackageChildFormSet,
                              extra=5,
                              can_delete=True)
    parent = create_product(printable_gibberish())
    child_1 = create_product(printable_gibberish())
    child_1.link_to_parent(parent)
    child_2 = create_product(printable_gibberish())
    parent.verify_mode()

    assert parent.mode == ProductMode.SIMPLE_VARIATION_PARENT

    # Trying to create a package from a non-normal mode product
    with pytest.raises(Problem):
        formset = FormSet(parent_product=parent)
        data = dict(get_form_data(formset, True), **{
            "form-0-child": child_2.pk,
            "form-0-quantity": 2
        })
        formset = FormSet(parent_product=parent, data=data)
        formset.save()
Exemple #32
0
def test_change_shop_price():
    product = _get_test_product()
    shop = get_default_shop()

    form_field = "s_%d" % shop.id

    frm = DiscountPricingForm(product=product, empty_permitted=True)
    form_data = get_form_data(frm, prepared=True)
    # Price hike time!
    form_data[form_field] = "120"
    frm = DiscountPricingForm(product=product, data=form_data, empty_permitted=True)
    frm.full_clean()
    frm.save()
    assert DiscountedProductPrice.objects.get(product=product, shop=shop).price.value == 120

    # Never mind actually, same price for all shops
    form_data[form_field] = ""
    frm = DiscountPricingForm(product=product, data=form_data, empty_permitted=True)
    frm.full_clean()
    frm.save()

    assert not DiscountedProductPrice.objects.filter(product=product, shop=shop).exists()
Exemple #33
0
def test_editor_save(rf):
    layout, svc = get_test_layout_and_svc()

    with initialize_editor_view(svc.view_name, layout.placeholder_name) as view_obj:
        view_obj.request.GET.update({"x": 0, "y": 0})
        view_obj.dispatch(view_obj.request)
        assert view_obj.current_cell
        assert view_obj.form
        assert "general" in view_obj.form.forms
        assert "plugin" in view_obj.form.forms
        form_data = get_form_data(view_obj.form, prepared=True)

    new_text = printable_gibberish()
    form_data["plugin-text"] = new_text
    form_data["save"] = "1"
    request = rf.post("/pepe/", data=form_data)  # sort of rare pepe
    request.GET = dict(request.GET, x=0, y=0)
    with initialize_editor_view(svc.view_name, layout.placeholder_name, request) as view_obj:
        view_obj.dispatch(request)
        assert view_obj.form
        assert not view_obj.form.errors
        assert view_obj.current_cell.config["text"] == new_text
Exemple #34
0
def test_impossible_simple_variation():
    FormSet = formset_factory(SimpleVariationChildForm,
                              SimpleVariationChildFormSet,
                              extra=5,
                              can_delete=True)
    parent = create_product(printable_gibberish())
    child = create_product(printable_gibberish())
    grandchild = create_product(printable_gibberish())
    grandchild.link_to_parent(child)
    assert child.variation_children.exists()

    formset = FormSet(parent_product=parent)
    data = dict(get_form_data(formset, True), **{"form-0-child": child.pk})
    formset = FormSet(parent_product=parent, data=data)
    assert formset.is_valid()  # It's technically valid, but...
    with pytest.raises(Problem) as ei:
        formset.save()

    if six.PY3:  # Can only test inner exceptions on Py3. Ah well.
        inner_exc = ei.value.__context__
        assert isinstance(inner_exc, ImpossibleProductModeException)
        assert inner_exc.code == "multilevel"
Exemple #35
0
def test_page_form(rf):
    view = PageEditView(request=rf.get("/"))
    form_class = view.get_form_class()
    form_kwargs = view.get_form_kwargs()
    form = form_class(**form_kwargs)
    assert not form.is_bound

    data = get_form_data(form)
    data.update({
        "available_from": "",
        "available_to": "",
        "content__en": "",
        "content__fi": "suomi",
        "content__ja": "",
        "identifier": "",
        "title__en": "",
        "title__fi": "",
        "title__ja": "",
        "url__en": "",
        "url__fi": "suomi",
        "url__ja": "",
    })

    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()
    assert "title__fi" in form.errors  # We get an error because all of a given language's fields must be filled if any are
    data["title__fi"] = "suomi"
    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()
    assert not form.errors
    page = form.save()
    assert set(page.get_available_languages()) == {"fi"}  # The page should be only in Finnish
    # Let's edit that page
    data.update({"title__en": "englaish", "url__en": "errrnglish", "content__en": "ennnn ennnn ennnnnnn-nn-n-n"})
    form = form_class(**dict(form_kwargs, data=data, instance=page))
    form.full_clean()
    assert not form.errors
    page = form.save()
    assert set(page.get_available_languages()) == {"fi", "en"}  # English GET
Exemple #36
0
def test_rule_admin(rf, admin_user):
    shop = get_default_shop()

    tax = create_tax("test-1", rate=Decimal("0.12"))
    tax.save()

    tax_class = TaxClass.objects.create(name="test")

    view = TaxRuleEditView(
        request=apply_request_middleware(rf.get("/"), user=admin_user))
    form_class = view.get_form_class()
    form_kwargs = view.get_form_kwargs()
    form = form_class(**form_kwargs)
    data = get_form_data(form)
    data.update({
        "shop": shop.pk,
        "postal_codes_pattern":
        "99501-99511,99513-99524,99529-99530,99540,99590,99550,99567,99573,99577,99586-99588",
        "country_codes_pattern": "FI,CA,US,SE",
        "region_codes_pattern": "CA,AR,IA,AK,WY,TN",
        "tax": tax.id,
        "tax_classes": [tax_class.id]
    })
    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()

    assert not form.errors

    rule = form.save()

    assert rule._postal_codes_min == "99501"
    assert rule._postal_codes_max == "99590"

    postal_code = "99510"
    assert TaxRule.objects.may_match_postal_code(postal_code).count() == 1

    postal_code = "99500"
    assert not TaxRule.objects.may_match_postal_code(postal_code).count()
Exemple #37
0
def test_change_shop_price():
    product = _get_test_product()
    shop = get_default_shop()
    group = get_default_customer_group()
    price = shop.create_price

    form_field = "s_%d_g_%d" % (shop.id, group.id)

    frm = SimplePricingForm(product=product, empty_permitted=True)
    form_data = get_form_data(frm, prepared=True)
    # Price hike time!
    form_data[form_field] = "4000"
    frm = SimplePricingForm(product=product, data=form_data, empty_permitted=True)
    frm.full_clean()
    frm.save()
    assert SimpleProductPrice.objects.get(product=product, shop=shop, group=group).price == price(4000)

    # Never mind actually, same price for all shops
    form_data[form_field] = ""
    frm = SimplePricingForm(product=product, data=form_data, empty_permitted=True)
    frm.full_clean()
    frm.save()

    assert not SimpleProductPrice.objects.filter(product=product, shop=shop, group=group).exists()
Exemple #38
0
def test_protected_fields():
    activate("en")
    shop = Shop.objects.create(name="testshop",
                               identifier="testshop",
                               status=ShopStatus.ENABLED,
                               public_name="test shop",
                               domain="derp",
                               currency="EUR")
    assert shop.name == "testshop"
    assert shop.currency == "EUR"
    shop_form = ShopForm(instance=shop, languages=settings.LANGUAGES)
    assert not shop_form._get_protected_fields(
    )  # No protected fields just yet, right?
    data = get_form_data(shop_form, prepared=True)
    shop_form = ShopForm(data=data,
                         instance=shop,
                         languages=settings.LANGUAGES)
    _test_cleanliness(shop_form)
    shop_form.save()

    # Now let's make it protected!
    create_product(printable_gibberish(),
                   shop=shop,
                   supplier=get_default_supplier())
    order = create_random_order(customer=create_random_person(), shop=shop)
    assert order.shop == shop

    # And try again...
    data["currency"] = "XBT"  # Bitcoins!
    shop_form = ShopForm(data=data,
                         instance=shop,
                         languages=settings.LANGUAGES)
    assert shop_form._get_protected_fields()  # So protected!
    _test_cleanliness(shop_form)
    shop = shop_form.save()
    assert shop.currency == "EUR"  # But the shop form ignored the change . . .
Exemple #39
0
def test_package_child_formset():
    FormSet = formset_factory(PackageChildForm,
                              PackageChildFormSet,
                              extra=5,
                              can_delete=True)
    parent = create_product(printable_gibberish())
    child = create_product(printable_gibberish())

    # No products in the package
    formset = FormSet(parent_product=parent)
    assert formset.initial_form_count() == 0  # No children yet

    assert not parent.get_all_package_children()

    data = dict(get_form_data(formset, True), **{
        "form-0-child": child.pk,
        "form-0-quantity": 2
    })
    formset = FormSet(parent_product=parent, data=data)
    formset.save()
    assert parent.get_all_package_children()

    clear_existing_package(parent)
    assert not parent.get_all_package_children()
def test_rule_admin(rf, admin_user):
    shop = get_default_shop()

    tax = create_tax("test-1", rate=Decimal("0.12"))
    tax.save()

    tax_class = TaxClass.objects.create(name="test")

    view = TaxRuleEditView(request=apply_request_middleware(rf.get("/"), user=admin_user))
    form_class = view.get_form_class()
    form_kwargs = view.get_form_kwargs()
    form = form_class(**form_kwargs)
    data = get_form_data(form)
    data.update({
        "shop": shop.pk,
        "postal_codes_pattern": "99501-99511,99513-99524,99529-99530,99540,99590,99550,99567,99573,99577,99586-99588",
        "country_codes_pattern": "FI,CA,US,SE",
        "region_codes_pattern": "CA,AR,IA,AK,WY,TN",
        "tax": tax.id,
        "tax_classes": [tax_class.id]
    })
    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()

    assert not form.errors

    rule = form.save()

    assert rule._postal_codes_min == "99501"
    assert rule._postal_codes_max == "99590"

    postal_code = "99510"
    assert TaxRule.objects.may_match_postal_code(postal_code).count() == 1

    postal_code = "99500"
    assert not TaxRule.objects.may_match_postal_code(postal_code).count()
Exemple #41
0
def test_page_form(rf):
    view = PageEditView(request=rf.get("/"))
    form_class = view.get_form_class()
    form_kwargs = view.get_form_kwargs()
    form = form_class(**form_kwargs)
    assert not form.is_bound

    data = get_form_data(form)
    data.update({
        "available_from": "",
        "available_to": "",
        "content__en": "",
        "content__fi": "suomi",
        "content__ja": "",
        "identifier": "",
        "title__en": "",
        "title__fi": "",
        "title__ja": "",
        "url__en": "",
        "url__fi": "suomi",
        "url__ja": "",
    })

    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()
    assert "title__fi" in form.errors  # We get an error because all of a given language's fields must be filled if any are
    data["title__fi"] = "suomi"
    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()
    assert not form.errors
    page = form.save()
    assert set(page.get_available_languages()) == {"fi"}  # The page should be only in Finnish
    # Let's edit that page
    original_url = "errrnglish"
    data.update({"title__en": "englaish", "url__en": original_url, "content__en": "ennnn ennnn ennnnnnn-nn-n-n"})
    form = form_class(**dict(form_kwargs, data=data, instance=page))
    form.full_clean()
    assert not form.errors
    page = form.save()
    assert set(page.get_available_languages()) == {"fi", "en"}  # English GET

    # add dummy page with simple url, page is in english
    dummy = create_page(url="test")

    # edit page again and try to set duplicate url
    data.update({"title__en": "englaish", "url__en": "test", "content__en": "ennnn ennnn ennnnnnn-nn-n-n"})
    form = form_class(**dict(form_kwargs, data=data, instance=page))
    form.full_clean()

    assert len(form.errors) == 1
    assert "url__en" in form.errors
    assert form.errors["url__en"].as_data()[0].code == "invalid_url"

    # it should be possible to change back to the original url
    data.update({"title__en": "englaish", "url__en": original_url, "content__en": "ennnn ennnn ennnnnnn-nn-n-n"})
    form = form_class(**dict(form_kwargs, data=data, instance=page))
    form.full_clean()
    assert not form.errors
    page = form.save()

    # add finnish urls, it should not be possible to enter original url
    data.update({"title__fi": "englaish", "url__fi": original_url, "content__fi": "ennnn ennnn ennnnnnn-nn-n-n"})
    form = form_class(**dict(form_kwargs, data=data, instance=page))
    form.full_clean()
    assert len(form.errors) == 1
    assert "url__fi" in form.errors
    assert form.errors["url__fi"].as_data()[0].code == "invalid_url"
Exemple #42
0
def test_page_form(rf):
    view = PageEditView(request=rf.get("/"))
    form_class = view.get_form_class()
    form_kwargs = view.get_form_kwargs()
    form = form_class(**form_kwargs)
    assert not form.is_bound

    data = get_form_data(form)
    data.update({
        "available_from": "",
        "available_to": "",
        "content__en": "",
        "content__fi": "suomi",
        "content__ja": "",
        "identifier": "",
        "title__en": "",
        "title__fi": "",
        "title__ja": "",
        "url__en": "",
        "url__fi": "suomi",
        "url__ja": "",
    })

    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()
    assert "title__fi" in form.errors  # We get an error because all of a given language's fields must be filled if any are
    data["title__fi"] = "suomi"
    form = form_class(**dict(form_kwargs, data=data))
    form.full_clean()
    assert not form.errors
    page = form.save()
    assert set(page.get_available_languages()) == {
        "fi"
    }  # The page should be only in Finnish
    # Let's edit that page
    original_url = "errrnglish"
    data.update({
        "title__en": "englaish",
        "url__en": original_url,
        "content__en": "ennnn ennnn ennnnnnn-nn-n-n"
    })
    form = form_class(**dict(form_kwargs, data=data, instance=page))
    form.full_clean()
    assert not form.errors
    page = form.save()
    assert set(page.get_available_languages()) == {"fi", "en"}  # English GET

    # add dummy page with simple url, page is in english
    dummy = create_page(url="test")

    # edit page again and try to set duplicate url
    data.update({
        "title__en": "englaish",
        "url__en": "test",
        "content__en": "ennnn ennnn ennnnnnn-nn-n-n"
    })
    form = form_class(**dict(form_kwargs, data=data, instance=page))
    form.full_clean()

    assert len(form.errors) == 1
    assert "url__en" in form.errors
    assert form.errors["url__en"].as_data()[0].code == "invalid_url"

    # it should be possible to change back to the original url
    data.update({
        "title__en": "englaish",
        "url__en": original_url,
        "content__en": "ennnn ennnn ennnnnnn-nn-n-n"
    })
    form = form_class(**dict(form_kwargs, data=data, instance=page))
    form.full_clean()
    assert not form.errors
    page = form.save()

    # add finnish urls, it should not be possible to enter original url
    data.update({
        "title__fi": "englaish",
        "url__fi": original_url,
        "content__fi": "ennnn ennnn ennnnnnn-nn-n-n"
    })
    form = form_class(**dict(form_kwargs, data=data, instance=page))
    form.full_clean()
    assert len(form.errors) == 1
    assert "url__fi" in form.errors
    assert form.errors["url__fi"].as_data()[0].code == "invalid_url"