Example #1
0
def test_address_ownership(admin_user):
    address = get_address()
    address.save()
    saved = SavedAddress(address=address)
    saved.owner = get_person_contact(admin_user)
    assert saved.get_title(), u"get_title does what it should even if there is no explicit title"
    saved.title = u"My favorite address"
    assert saved.get_title() == saved.title, u"get_title does what it should when there is an explicit title"
    assert six.text_type(saved) == saved.get_title(), u"str() is an alias for .get_title()"
    saved.full_clean()
    saved.save()
    assert SavedAddress.objects.for_owner(get_person_contact(admin_user)).filter(address=address).exists(), \
        "contacts can save addresses"
    assert SavedAddress.objects.for_owner(None).count() == 0, "Ownerless saved addresses aren't a real thing"
def test_product_unsupplied(admin_user):
    shop_product = get_default_shop_product()
    fake_supplier = Supplier.objects.create(identifier="fake")
    admin_contact = get_person_contact(admin_user)

    with modify(shop_product, visibility_limit=ProductVisibility.VISIBLE_TO_ALL, orderable=True):
        assert any(ve.code == "invalid_supplier" for ve in shop_product.get_orderability_errors(supplier=fake_supplier, customer=admin_contact, quantity=1))
def test_product_minimum_order_quantity(admin_user):
    shop_product = get_default_shop_product()
    supplier = get_default_supplier()
    admin_contact = get_person_contact(admin_user)

    with modify(shop_product, visibility_limit=ProductVisibility.VISIBLE_TO_ALL, orderable=True, minimum_purchase_quantity=10):
        assert any(ve.code == "purchase_quantity_not_met" for ve in shop_product.get_orderability_errors(supplier=supplier, customer=admin_contact, quantity=1))
        assert not any(ve.code == "purchase_quantity_not_met" for ve in shop_product.get_orderability_errors(supplier=supplier, customer=admin_contact, quantity=15))
def test_product_order_multiple(admin_user):
    shop_product = get_default_shop_product()
    supplier = get_default_supplier()
    admin_contact = get_person_contact(admin_user)

    with modify(shop_product, visibility_limit=ProductVisibility.VISIBLE_TO_ALL, orderable=True, purchase_multiple=7):
        assert any(ve.code == "invalid_purchase_multiple" for ve in shop_product.get_orderability_errors(supplier=supplier, customer=admin_contact, quantity=4))
        assert any(ve.code == "invalid_purchase_multiple" for ve in shop_product.get_orderability_errors(supplier=supplier, customer=admin_contact, quantity=25))
        assert not any(ve.code == "invalid_purchase_multiple" for ve in shop_product.get_orderability_errors(supplier=supplier, customer=admin_contact, quantity=49))
Example #5
0
def test_methods(admin_user, country):
    contact = get_person_contact(admin_user)
    source = BasketishOrderSource(
        lines=[
            SourceLine(
                type=OrderLineType.PRODUCT,
                product=get_default_product(),
                supplier=get_default_supplier(),
                quantity=1,
                unit_price=TaxlessPrice(10),
                weight=Decimal("0.2"),
            )
        ]
    )
    billing_address = get_address()
    shipping_address = get_address(name="Shippy Doge", country=country)
    source.shop = get_default_shop()
    source.billing_address = billing_address
    source.shipping_address = shipping_address
    source.customer = contact

    with override_provides_for_expensive_sweden_shipping_method():
        source.shipping_method = get_expensive_sweden_shipping_method()
        source.payment_method = PaymentMethod.objects.create(
            identifier="neat", module_data={"price": 4}, tax_class=get_default_tax_class()
        )
        assert source.shipping_method_id
        assert source.payment_method_id

        errors = list(source.get_validation_errors())

        if (
            country == "FI"
        ):  # "Expenseefe-a Svedee Sheepping" will not allow shipping to Finland, let's see if that holds true
            assert any([ve.code == "we_no_speak_finnish" for ve in errors])
            return  # Shouldn't try the rest if we got an error here
        else:
            assert not errors

        final_lines = list(source.get_final_lines())

        assert any(line.type == OrderLineType.SHIPPING for line in final_lines)

        # TODO: (TAX) for some reason SourceLine.taxless_total_price property has been removed
        # I think it should be implemented back like in OrderLine / janne

        for line in final_lines:
            if line.type == OrderLineType.SHIPPING:
                if country == "SE":  # We _are_ using Expenseefe-a Svedee Sheepping after all.
                    assert line.taxless_total_price == TaxlessPrice("5.00")
                else:
                    assert line.taxless_total_price == TaxlessPrice("4.00")
                assert line.text == u"Expenseefe-a Svedee Sheepping"
            if line.type == OrderLineType.PAYMENT:
                assert line.taxless_total_price == TaxlessPrice("4")
Example #6
0
def test_product_unsupplied(admin_user):
    shop_product = get_default_shop_product()
    fake_supplier = Supplier.objects.create(identifier="fake")
    admin_contact = get_person_contact(admin_user)

    with modify(shop_product,
                visibility_limit=ProductVisibility.VISIBLE_TO_ALL,
                orderable=True):
        assert any(
            ve.code == "invalid_supplier"
            for ve in shop_product.get_orderability_errors(
                supplier=fake_supplier, customer=admin_contact, quantity=1))
def test_product_visibility(rf, admin_user, regular_user):
    anon_contact = get_person_contact(AnonymousUser())
    shop_product = get_default_shop_product()
    admin_contact = get_person_contact(admin_user)
    regular_contact = get_person_contact(regular_user)


    with modify(shop_product.product, deleted=True):  # NB: assigning to `product` here works because `get_shop_instance` populates `_product_cache`
        assert error_exists(shop_product.get_visibility_errors(customer=anon_contact), "product_deleted")
        assert error_exists(shop_product.get_visibility_errors(customer=admin_contact), "product_deleted")
        with pytest.raises(ProductNotVisibleProblem):
            shop_product.raise_if_not_visible(anon_contact)
        assert not shop_product.is_list_visible()

    with modify(shop_product, visibility_limit=ProductVisibility.VISIBLE_TO_ALL, visible=False):
        assert error_exists(shop_product.get_visibility_errors(customer=anon_contact), "product_not_visible")
        assert error_does_not_exist(shop_product.get_visibility_errors(customer=admin_contact), "product_not_visible")
        assert not shop_product.is_list_visible()

    with modify(shop_product, visibility_limit=ProductVisibility.VISIBLE_TO_LOGGED_IN, visible=True):
        assert error_exists(shop_product.get_visibility_errors(customer=anon_contact), "product_not_visible_to_anonymous")
        assert error_does_not_exist(shop_product.get_visibility_errors(customer=admin_contact), "product_not_visible_to_anonymous")

    customer_group = get_default_customer_group()
    grouped_user = get_user_model().objects.create_user(username=printable_gibberish(20))
    grouped_contact = get_person_contact(grouped_user)
    with modify(shop_product, visibility_limit=ProductVisibility.VISIBLE_TO_GROUPS, visible=True):
        shop_product.visibility_groups.add(customer_group)
        customer_group.members.add(grouped_contact)
        customer_group.members.remove(get_person_contact(regular_user))
        assert error_does_not_exist(shop_product.get_visibility_errors(customer=grouped_contact), "product_not_visible_to_group")
        assert error_does_not_exist(shop_product.get_visibility_errors(customer=admin_contact), "product_not_visible_to_group")
        assert error_exists(shop_product.get_visibility_errors(customer=regular_contact), "product_not_visible_to_group")

    with modify(shop_product, listed=False):
        assert not shop_product.is_list_visible()

    with modify(shop_product, listed=True):
        assert shop_product.is_list_visible()
Example #8
0
def test_methods(admin_user, country):
    contact = get_person_contact(admin_user)
    source = BasketishOrderSource(lines=[
        SourceLine(type=OrderLineType.PRODUCT,
                   product=get_default_product(),
                   supplier=get_default_supplier(),
                   quantity=1,
                   unit_price=TaxlessPrice(10),
                   weight=Decimal("0.2"))
    ])
    billing_address = get_address()
    shipping_address = get_address(name="Shippy Doge", country=country)
    source.shop = get_default_shop()
    source.billing_address = billing_address
    source.shipping_address = shipping_address
    source.customer = contact

    with override_provides_for_expensive_sweden_shipping_method():
        source.shipping_method = get_expensive_sweden_shipping_method()
        source.payment_method = PaymentMethod.objects.create(
            identifier="neat",
            module_data={"price": 4},
            tax_class=get_default_tax_class())
        assert source.shipping_method_id
        assert source.payment_method_id

        errors = list(source.get_validation_errors())

        if country == "FI":  # "Expenseefe-a Svedee Sheepping" will not allow shipping to Finland, let's see if that holds true
            assert any([ve.code == "we_no_speak_finnish" for ve in errors])
            return  # Shouldn't try the rest if we got an error here
        else:
            assert not errors

        final_lines = list(source.get_final_lines())

        assert any(line.type == OrderLineType.SHIPPING for line in final_lines)

        # TODO: (TAX) for some reason SourceLine.taxless_total_price property has been removed
        # I think it should be implemented back like in OrderLine / janne

        for line in final_lines:
            if line.type == OrderLineType.SHIPPING:
                if country == "SE":  # We _are_ using Expenseefe-a Svedee Sheepping after all.
                    assert line.taxless_total_price == TaxlessPrice("5.00")
                else:
                    assert line.taxless_total_price == TaxlessPrice("4.00")
                assert line.text == u"Expenseefe-a Svedee Sheepping"
            if line.type == OrderLineType.PAYMENT:
                assert line.taxless_total_price == TaxlessPrice("4")
Example #9
0
def test_product_minimum_order_quantity(admin_user):
    shop_product = get_default_shop_product()
    supplier = get_default_supplier()
    admin_contact = get_person_contact(admin_user)

    with modify(shop_product,
                visibility_limit=ProductVisibility.VISIBLE_TO_ALL,
                orderable=True,
                minimum_purchase_quantity=10):
        assert any(ve.code == "purchase_quantity_not_met"
                   for ve in shop_product.get_orderability_errors(
                       supplier=supplier, customer=admin_contact, quantity=1))
        assert not any(
            ve.code == "purchase_quantity_not_met"
            for ve in shop_product.get_orderability_errors(
                supplier=supplier, customer=admin_contact, quantity=15))
Example #10
0
def test_contact_edit_form():
    user = get_user_model().objects.create_user(
        username=printable_gibberish(),
        first_name=printable_gibberish(),
        last_name=printable_gibberish(),
    )
    contact_base_form = ContactBaseForm(bind_user=user, data={
        "name": "herf durr",
        "gender": Gender.UNDISCLOSED.value
    })
    assert contact_base_form.bind_user == user
    assert contact_base_form.contact_class == PersonContact
    assert contact_base_form.is_valid(), contact_base_form.errors
    contact = contact_base_form.save()
    assert isinstance(contact, PersonContact)
    assert contact.user == user
    assert get_person_contact(user) == contact
Example #11
0
def test_contact_edit_form():
    user = get_user_model().objects.create_user(
        username=printable_gibberish(),
        first_name=printable_gibberish(),
        last_name=printable_gibberish(),
    )
    contact_base_form = ContactBaseForm(bind_user=user,
                                        data={
                                            "name": "herf durr",
                                            "gender": Gender.UNDISCLOSED.value
                                        })
    assert contact_base_form.bind_user == user
    assert contact_base_form.contact_class == PersonContact
    assert contact_base_form.is_valid(), contact_base_form.errors
    contact = contact_base_form.save()
    assert isinstance(contact, PersonContact)
    assert contact.user == user
    assert get_person_contact(user) == contact
Example #12
0
def test_product_order_multiple(admin_user):
    shop_product = get_default_shop_product()
    supplier = get_default_supplier()
    admin_contact = get_person_contact(admin_user)

    with modify(shop_product,
                visibility_limit=ProductVisibility.VISIBLE_TO_ALL,
                orderable=True,
                purchase_multiple=7):
        assert any(ve.code == "invalid_purchase_multiple"
                   for ve in shop_product.get_orderability_errors(
                       supplier=supplier, customer=admin_contact, quantity=4))
        assert any(ve.code == "invalid_purchase_multiple"
                   for ve in shop_product.get_orderability_errors(
                       supplier=supplier, customer=admin_contact, quantity=25))
        assert not any(
            ve.code == "invalid_purchase_multiple"
            for ve in shop_product.get_orderability_errors(
                supplier=supplier, customer=admin_contact, quantity=49))
Example #13
0
 def _set_person(self, request):
     request.person = get_person_contact(request.user)
Example #14
0
 def _set_person(self, request):
     request.person = get_person_contact(request.user)
Example #15
0
def test_product_visibility(rf, admin_user, regular_user):
    anon_contact = get_person_contact(AnonymousUser())
    shop_product = get_default_shop_product()
    admin_contact = get_person_contact(admin_user)
    regular_contact = get_person_contact(regular_user)

    with modify(
            shop_product.product, deleted=True
    ):  # NB: assigning to `product` here works because `get_shop_instance` populates `_product_cache`
        assert error_exists(
            shop_product.get_visibility_errors(customer=anon_contact),
            "product_deleted")
        assert error_exists(
            shop_product.get_visibility_errors(customer=admin_contact),
            "product_deleted")
        with pytest.raises(ProductNotVisibleProblem):
            shop_product.raise_if_not_visible(anon_contact)
        assert not shop_product.is_list_visible()

    with modify(shop_product,
                visibility_limit=ProductVisibility.VISIBLE_TO_ALL,
                visible=False):
        assert error_exists(
            shop_product.get_visibility_errors(customer=anon_contact),
            "product_not_visible")
        assert error_does_not_exist(
            shop_product.get_visibility_errors(customer=admin_contact),
            "product_not_visible")
        assert not shop_product.is_list_visible()

    with modify(shop_product,
                visibility_limit=ProductVisibility.VISIBLE_TO_LOGGED_IN,
                visible=True):
        assert error_exists(
            shop_product.get_visibility_errors(customer=anon_contact),
            "product_not_visible_to_anonymous")
        assert error_does_not_exist(
            shop_product.get_visibility_errors(customer=admin_contact),
            "product_not_visible_to_anonymous")

    customer_group = get_default_customer_group()
    grouped_user = get_user_model().objects.create_user(
        username=printable_gibberish(20))
    grouped_contact = get_person_contact(grouped_user)
    with modify(shop_product,
                visibility_limit=ProductVisibility.VISIBLE_TO_GROUPS,
                visible=True):
        shop_product.visibility_groups.add(customer_group)
        customer_group.members.add(grouped_contact)
        customer_group.members.remove(get_person_contact(regular_user))
        assert error_does_not_exist(
            shop_product.get_visibility_errors(customer=grouped_contact),
            "product_not_visible_to_group")
        assert error_does_not_exist(
            shop_product.get_visibility_errors(customer=admin_contact),
            "product_not_visible_to_group")
        assert error_exists(
            shop_product.get_visibility_errors(customer=regular_contact),
            "product_not_visible_to_group")

    with modify(shop_product, listed=False):
        assert not shop_product.is_list_visible()

    with modify(shop_product, listed=True):
        assert shop_product.is_list_visible()