Example #1
0
def test_product_query(admin_user, regular_user):
    anon_contact = AnonymousContact()
    shop_product = get_default_shop_product()
    shop = shop_product.shop
    product = shop_product.product
    regular_contact = get_person_contact(regular_user)
    admin_contact = get_person_contact(admin_user)


    with modify(shop_product, save=True,
                listed=True,
                visible=True,
                visibility_limit=ProductVisibility.VISIBLE_TO_ALL
                ):
        assert Product.objects.list_visible(shop=shop, customer=anon_contact).filter(pk=product.pk).exists()

    with modify(shop_product, save=True,
                listed=False,
                visible=True,
                visibility_limit=ProductVisibility.VISIBLE_TO_ALL
                ):
        assert not Product.objects.list_visible(shop=shop, customer=anon_contact).filter(pk=product.pk).exists()
        assert not Product.objects.list_visible(shop=shop, customer=regular_contact).filter(pk=product.pk).exists()
        assert Product.objects.list_visible(shop=shop, customer=admin_contact).filter(pk=product.pk).exists()

    with modify(shop_product, save=True,
                listed=True,
                visible=True,
                visibility_limit=ProductVisibility.VISIBLE_TO_LOGGED_IN
                ):
        assert not Product.objects.list_visible(shop=shop, customer=anon_contact).filter(pk=product.pk).exists()
        assert Product.objects.list_visible(shop=shop, customer=regular_contact).filter(pk=product.pk).exists()

    product.soft_delete()
    assert not Product.objects.all_except_deleted().filter(pk=product.pk).exists()
Example #2
0
 def _set_person(self, request):
     request.person = get_person_contact(request.user)
     if not request.person.is_active:
         messages.add_message(request, messages.INFO, _("Logged out since this account is inactive."))
         logout(request)
         # Usually logout is connected to the `refresh_on_logout`
         # method via a signal and that already sets request.person
         # to anonymous, but set it explicitly too, just to be sure
         request.person = get_person_contact(None)
Example #3
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"
Example #4
0
def get_order_and_source(admin_user):
    # create original source to tamper with
    source = BasketishOrderSource(get_default_shop())
    source.status = get_initial_order_status()
    source.billing_address = MutableAddress.objects.create(
        name="Original Billing")
    source.shipping_address = MutableAddress.objects.create(
        name="Original Shipping")
    source.customer = get_person_contact(admin_user)
    source.payment_method = get_default_payment_method()
    source.shipping_method = get_default_shipping_method()
    source.add_line(
        type=OrderLineType.PRODUCT,
        product=get_default_product(),
        supplier=get_default_supplier(),
        quantity=1,
        base_unit_price=source.create_price(10),
    )
    source.add_line(
        type=OrderLineType.OTHER,
        quantity=1,
        base_unit_price=source.create_price(10),
        require_verification=True,
    )
    assert len(source.get_lines()) == 2
    source.creator = admin_user
    creator = OrderCreator()
    order = creator.create_order(source)
    return order, source
Example #5
0
def test_address_phase_authorized_user(rf, admin_user):
    request = apply_request_middleware(rf.get("/"),
                                       shop=get_default_shop(),
                                       customer=get_person_contact(admin_user))
    view_func = AddressesPhase.as_view()
    resp = view_func(request)
    assert 'company' not in resp.context_data['form'].form_defs
Example #6
0
def seed_source(user, shop):
    source = BasketishOrderSource(shop)
    source.status = get_initial_order_status()
    source.customer = get_person_contact(user)
    source.payment_method = get_default_payment_method()
    source.shipping_method = get_default_shipping_method()
    return source
Example #7
0
 def get_form(self, form_class):
     contact = get_person_contact(self.request.user)
     form_group = FormGroup(**self.get_form_kwargs())
     form_group.add_form_def("billing", AddressForm, kwargs={"instance": contact.default_billing_address})
     form_group.add_form_def("shipping", AddressForm, kwargs={"instance": contact.default_shipping_address})
     form_group.add_form_def("contact", PersonContactForm, kwargs={"instance": contact})
     return form_group
def get_order_and_source(admin_user):
    # create original source to tamper with
    source = BasketishOrderSource(get_default_shop())
    source.status = get_initial_order_status()
    source.billing_address = MutableAddress.objects.create(name="Original Billing")
    source.shipping_address = MutableAddress.objects.create(name="Original Shipping")
    source.customer = get_person_contact(admin_user)
    source.payment_method = get_default_payment_method()
    source.shipping_method = get_default_shipping_method()
    source.add_line(
        type=OrderLineType.PRODUCT,
        product=get_default_product(),
        supplier=get_default_supplier(),
        quantity=1,
        base_unit_price=source.create_price(10),
    )
    source.add_line(
        type=OrderLineType.OTHER,
        quantity=1,
        base_unit_price=source.create_price(10),
        require_verification=True,
    )
    assert len(source.get_lines()) == 2
    source.creator = admin_user
    creator = OrderCreator()
    order = creator.create_order(source)
    return order, source
Example #9
0
def seed_source(user, shop):
    source = BasketishOrderSource(shop)
    source.status = get_initial_order_status()
    source.customer = get_person_contact(user)
    source.payment_method = get_default_payment_method()
    source.shipping_method = get_default_shipping_method()
    return source
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))
Example #11
0
 def get_form(self, form_class):
     contact = get_person_contact(self.request.user)
     form_group = FormGroup(**self.get_form_kwargs())
     form_group.add_form_def("billing", AddressForm, kwargs={"instance": contact.default_billing_address})
     form_group.add_form_def("shipping", AddressForm, kwargs={"instance": contact.default_shipping_address})
     form_group.add_form_def("contact", PersonContactForm, kwargs={"instance": contact})
     return form_group
Example #12
0
def test_basic_order(rf, admin_user):
    request = rf.get('/')
    product = get_default_product()
    SimpleProductPrice.objects.create(product=product, group=None, price=Decimal("100.00"), includes_tax=False)
    customer = get_person_contact(admin_user)
    for x in range(10):
        create_order(request, creator=admin_user, customer=customer, product=product)
    assert Order.objects.filter(customer=customer).count() == 10
def test_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 #14
0
def test_get_company_contact(regular_user):
    person_contact = get_person_contact(regular_user)
    assert person_contact != AnonymousContact()
    assert not get_company_contact(regular_user)

    company_contact = create_random_company()
    company_contact.members.add(person_contact)
    assert get_company_contact(regular_user) == company_contact
Example #15
0
def test_companies(django_user_model):
    peons = [django_user_model.objects.create_user('Peon-%d' % x, '*****@*****.**' % x, 'password') for x in range(10)]
    for cx in range(10):
        company = CompanyContact.objects.create(name="Company %d" % cx, tax_number="FI2101%d" % cx)
        assert str(company)
        for x in range(5):
            off = (cx * 3 + x) % len(peons)
            contact = get_person_contact(user=peons[off])
            company.members.add(contact)
Example #16
0
def test_companies(django_user_model):
    peons = [django_user_model.objects.create_user('Peon-%d' % x, '*****@*****.**' % x, 'password') for x in range(10)]
    for cx in range(10):
        company = CompanyContact.objects.create(name="Company %d" % cx, vat_code="FI2101%d" % cx)
        assert str(company)
        for x in range(5):
            off = (cx * 3 + x) % len(peons)
            contact = get_person_contact(user=peons[off])
            company.members.add(contact)
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_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 #19
0
def test_intra_request_user_changing(rf, regular_user):
    get_default_shop()  # Create a shop
    mw = ShoopFrontMiddleware()
    request = apply_request_middleware(rf.get("/"), user=regular_user)
    mw.process_request(request)
    assert request.person == get_person_contact(regular_user)
    logout(request)
    assert request.user == AnonymousUser()
    assert request.person == AnonymousContact()
    assert request.customer == AnonymousContact()
Example #20
0
 def confirm_login_allowed(self, user):
     """
     Do not let user with inactive person contact to login.
     """
     if not get_person_contact(user).is_active:
         raise forms.ValidationError(
             self.error_messages['inactive'],
             code='inactive',
         )
     super(EmailAuthenticationForm, self).confirm_login_allowed(user)
Example #21
0
def test_intra_request_user_changing(rf, regular_user):
    get_default_shop()  # Create a shop
    mw = ShoopFrontMiddleware()
    request = apply_request_middleware(rf.get("/"), user=regular_user)
    mw.process_request(request)
    assert request.person == get_person_contact(regular_user)
    logout(request)
    assert request.user == AnonymousUser()
    assert request.person == AnonymousContact()
    assert request.customer == AnonymousContact()
Example #22
0
 def confirm_login_allowed(self, user):
     """
     Do not let user with inactive person contact to login.
     """
     if not get_person_contact(user).is_active:
         raise forms.ValidationError(
             self.error_messages['inactive'],
             code='inactive',
         )
     super(EmailAuthenticationForm, self).confirm_login_allowed(user)
Example #23
0
def test_basic_order(rf, admin_user):
    shop = get_default_shop()

    request = rf.get('/')
    request.shop = shop
    product = get_default_product()
    customer = get_person_contact(admin_user)
    for x in range(10):
        create_order(request, creator=admin_user, customer=customer, product=product)
    assert Order.objects.filter(customer=customer).count() == 10
Example #24
0
def test_product_query(admin_user, regular_user):
    anon_contact = AnonymousContact()
    shop_product = get_default_shop_product()
    shop = shop_product.shop
    product = shop_product.product
    regular_contact = get_person_contact(regular_user)
    admin_contact = get_person_contact(admin_user)

    with modify(shop_product,
                save=True,
                listed=True,
                visible=True,
                visibility_limit=ProductVisibility.VISIBLE_TO_ALL):
        assert Product.objects.list_visible(
            shop=shop, customer=anon_contact).filter(pk=product.pk).exists()

    with modify(shop_product,
                save=True,
                listed=False,
                visible=True,
                visibility_limit=ProductVisibility.VISIBLE_TO_ALL):
        assert not Product.objects.list_visible(
            shop=shop, customer=anon_contact).filter(pk=product.pk).exists()
        assert not Product.objects.list_visible(
            shop=shop,
            customer=regular_contact).filter(pk=product.pk).exists()
        assert Product.objects.list_visible(
            shop=shop, customer=admin_contact).filter(pk=product.pk).exists()

    with modify(shop_product,
                save=True,
                listed=True,
                visible=True,
                visibility_limit=ProductVisibility.VISIBLE_TO_LOGGED_IN):
        assert not Product.objects.list_visible(
            shop=shop, customer=anon_contact).filter(pk=product.pk).exists()
        assert Product.objects.list_visible(
            shop=shop,
            customer=regular_contact).filter(pk=product.pk).exists()

    product.soft_delete()
    assert not Product.objects.all_except_deleted().filter(
        pk=product.pk).exists()
Example #25
0
def test_customers(django_user_model):
    users = [django_user_model.objects.create_user('Joe-%d' % x, '*****@*****.**' % x, 'password') for x in range(10)]
    group = get_default_customer_group()
    assert str(group) == DEFAULT_NAME
    for user in users:
        contact = get_person_contact(user)
        group.members.add(contact)

    for user in users:
        assert PersonContact.objects.get(user=user).user_id == user.pk, "Customer profile found"
        assert tuple(user.contact.groups.values_list("identifier", flat=True)) == (DEFAULT_IDENTIFIER,), "Joe is now in the group"
Example #26
0
def test_customers(django_user_model):
    users = [django_user_model.objects.create_user('Joe-%d' % x, '*****@*****.**' % x, 'password') for x in range(10)]
    group = get_default_customer_group()
    assert str(group) == DEFAULT_NAME
    for user in users:
        contact = get_person_contact(user)
        group.members.add(contact)

    for user in users:
        assert PersonContact.objects.get(user=user).user_id == user.pk, "Customer profile found"
        assert tuple(user.contact.groups.values_list("identifier", flat=True)) == (DEFAULT_IDENTIFIER,), "Joe is now in the group"
Example #27
0
def test_basic_order(rf, admin_user, mode):
    prices_include_tax = mode == "taxful"
    shop = get_shop(prices_include_tax=prices_include_tax)

    request = rf.get("/")
    request.shop = shop
    product = get_default_product()
    customer = get_person_contact(admin_user)
    for x in range(10):
        create_order(request, creator=admin_user, customer=customer, product=product)
    assert Order.objects.filter(customer=customer).count() == 10
def test_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))
Example #29
0
def test_basic_order(rf, admin_user, mode):
    prices_include_tax = (mode == "taxful")
    shop = get_shop(prices_include_tax=prices_include_tax)

    request = rf.get('/')
    request.shop = shop
    apply_request_middleware(request)
    product = get_default_product()
    customer = get_person_contact(admin_user)
    for x in range(10):
        create_order(request, creator=admin_user, customer=customer, product=product)
    assert Order.objects.filter(customer=customer).count() == 10
Example #30
0
def seed_source(user):
    source = BasketishOrderSource(get_default_shop())
    billing_address = get_address()
    shipping_address = get_address(name="Shippy Doge")
    source.status = get_initial_order_status()
    source.billing_address = billing_address
    source.shipping_address = shipping_address
    source.customer = get_person_contact(user)
    source.payment_method = get_default_payment_method()
    source.shipping_method = get_default_shipping_method()
    assert source.payment_method_id == get_default_payment_method().id
    assert source.shipping_method_id == get_default_shipping_method().id
    return source
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 #32
0
def seed_source(user):
    source = BasketishOrderSource(get_default_shop())
    billing_address = get_address()
    shipping_address = get_address(name="Shippy Doge")
    source.status = get_initial_order_status()
    source.billing_address = billing_address
    source.shipping_address = shipping_address
    source.customer = get_person_contact(user)
    source.payment_method = get_default_payment_method()
    source.shipping_method = get_default_shipping_method()
    assert source.payment_method_id == get_default_payment_method().id
    assert source.shipping_method_id == get_default_shipping_method().id
    return source
Example #33
0
def test_category_visibility(admin_user, regular_user):
    visible_public_category = Category.objects.create(status=CategoryStatus.VISIBLE, visibility=CategoryVisibility.VISIBLE_TO_ALL, identifier="visible_public", name=DEFAULT_NAME)
    hidden_public_category = Category.objects.create(status=CategoryStatus.INVISIBLE, visibility=CategoryVisibility.VISIBLE_TO_ALL, identifier="hidden_public", name=DEFAULT_NAME)
    deleted_public_category = Category.objects.create(status=CategoryStatus.DELETED, visibility=CategoryVisibility.VISIBLE_TO_ALL, identifier="deleted_public", name=DEFAULT_NAME)
    logged_in_category = Category.objects.create(status=CategoryStatus.VISIBLE, visibility=CategoryVisibility.VISIBLE_TO_LOGGED_IN, identifier="visible_logged_in", name=DEFAULT_NAME)
    group_visible_category = Category.objects.create(status=CategoryStatus.VISIBLE, visibility=CategoryVisibility.VISIBLE_TO_GROUPS, identifier="visible_groups", name=DEFAULT_NAME)

    assert visible_public_category.name == DEFAULT_NAME
    assert str(visible_public_category) == DEFAULT_NAME

    anon_contact = AnonymousContact()
    regular_contact = get_person_contact(regular_user)
    admin_contact = get_person_contact(admin_user)

    for (customer, category, expect) in [
        (anon_contact, visible_public_category, True),
        (anon_contact, hidden_public_category, False),
        (anon_contact, deleted_public_category, False),
        (anon_contact, logged_in_category, False),
        (anon_contact, group_visible_category, False),

        (regular_contact, visible_public_category, True),
        (regular_contact, hidden_public_category, False),
        (regular_contact, deleted_public_category, False),
        (regular_contact, logged_in_category, True),
        (regular_contact, group_visible_category, False),

        (admin_contact, visible_public_category, True),
        (admin_contact, hidden_public_category, True),
        (admin_contact, deleted_public_category, False),
        (admin_contact, logged_in_category, True),
        (admin_contact, group_visible_category, True),
    ]:
        result = Category.objects.all_visible(customer=customer).filter(pk=category.pk).exists()
        assert result == expect, "Queryset visibility of %s for %s as expected" % (category.identifier, customer)
        assert category.is_visible(customer) == expect, "Direct visibility of %s for %s as expected" % (category.identifier, customer)

    assert not Category.objects.all_except_deleted().filter(pk=deleted_public_category.pk).exists(), "Deleted category does not show up in 'all_except_deleted'"
Example #34
0
def test_person_contact_creating_from_user(regular_user):
    user = regular_user
    user.first_name = 'Joe'
    user.last_name = 'Regular'

    # Preconditions
    assert user.get_full_name()
    assert not PersonContact.objects.filter(user=user).exists()

    # Actual test
    person = get_person_contact(user)
    assert person.is_active == user.is_active
    assert person.name == user.get_full_name()
    assert person.email == user.email
Example #35
0
def test_basic_order(rf, admin_user):
    request = rf.get('/')
    product = get_default_product()
    SimpleProductPrice.objects.create(product=product,
                                      group=None,
                                      price=Decimal("100.00"),
                                      includes_tax=False)
    customer = get_person_contact(admin_user)
    for x in range(10):
        create_order(request,
                     creator=admin_user,
                     customer=customer,
                     product=product)
    assert Order.objects.filter(customer=customer).count() == 10
Example #36
0
def test_person_contact_creating_from_user(regular_user):
    user = regular_user
    user.first_name = 'Joe'
    user.last_name = 'Regular'

    # Preconditions
    assert user.get_full_name()
    assert not PersonContact.objects.filter(user=user).exists()

    # Actual test
    person = get_person_contact(user)
    assert person.is_active == user.is_active
    assert person.name == user.get_full_name()
    assert person.email == user.email
Example #37
0
def test_with_inactive_contact(rf, regular_user, admin_user):
    get_default_shop()  # Create a shop
    # Get or create contact for regular user
    contact = get_person_contact(regular_user)
    assert contact.is_active
    contact.is_active = False
    contact.save()

    request = apply_request_middleware(rf.get("/"), user=regular_user)
    mw = ShoopFrontMiddleware()
    mw.process_request(request)

    assert request.user == AnonymousUser()
    assert request.person == AnonymousContact()
    assert request.customer == AnonymousContact()
Example #38
0
def test_with_inactive_contact(rf, regular_user, admin_user):
    get_default_shop()  # Create a shop
    # Get or create contact for regular user
    contact = get_person_contact(regular_user)
    assert contact.is_active
    contact.is_active = False
    contact.save()

    request = apply_request_middleware(rf.get("/"), user=regular_user)
    mw = ShoopFrontMiddleware()
    mw.process_request(request)

    assert request.user == AnonymousUser()
    assert request.person == AnonymousContact()
    assert request.customer == AnonymousContact()
Example #39
0
def test_methods(admin_user, country):
    contact = get_person_contact(admin_user)
    source = BasketishOrderSource(get_default_shop())
    source.add_line(
        type=OrderLineType.PRODUCT,
        product=get_default_product(),
        supplier=get_default_supplier(),
        quantity=1,
        base_unit_price=source.create_price(10),
        weight=Decimal("0.2"),
    )
    billing_address = get_address()
    shipping_address = get_address(name="Shippy Doge", country=country)
    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)

        for line in final_lines:
            if line.type == OrderLineType.SHIPPING:
                if country == "SE":  # We _are_ using Expenseefe-a Svedee Sheepping after all.
                    assert line.price == source.shop.create_price("5.00")
                else:
                    assert line.price == source.shop.create_price("4.00")
                assert line.text == u"Expenseefe-a Svedee Sheepping"
            if line.type == OrderLineType.PAYMENT:
                assert line.price == source.shop.create_price(4)
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 #41
0
def test_methods(admin_user, country):
    contact = get_person_contact(admin_user)
    source = BasketishOrderSource(get_default_shop())
    source.add_line(
        type=OrderLineType.PRODUCT,
        product=get_default_product(),
        supplier=get_default_supplier(),
        quantity=1,
        base_unit_price=source.create_price(10),
        weight=Decimal("0.2")
    )
    billing_address = get_address()
    shipping_address = get_address(name="Shippy Doge", country=country)
    source.billing_address = billing_address
    source.shipping_address = shipping_address
    source.customer = contact

    source.shipping_method = get_expensive_sweden_shipping_method()
    source.payment_method = get_payment_method(name="neat", price=4)
    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])
        assert [x.code for x in errors] == ["we_no_speak_finnish"]
        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)

    for line in final_lines:
        if line.type == OrderLineType.SHIPPING:
            if country == "SE":  # We _are_ using Expenseefe-a Svedee Sheepping after all.
                assert line.price == source.create_price("5.00")
            else:
                assert line.price == source.create_price("4.00")
            assert line.text == u"Expenseefe-a Svedee Sheepping"
        if line.type == OrderLineType.PAYMENT:
            assert line.price == source.create_price(4)
Example #42
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 #43
0
def test_contact_group_price_display_for_contact(regular_user):
    group = ContactGroup.objects.create(hide_prices=True)
    person = get_person_contact(regular_user)
    person.groups.add(group)

    options = person.get_price_display_options()
    assert options.hide_prices
    assert options.include_taxes is None

    default_group_for_person = person.get_default_group()
    default_group_for_person.show_prices_including_taxes = True
    default_group_for_person.save()

    # Now since default group has pricing options set these should be returned
    default_options = person.get_price_display_options()
    assert default_options.include_taxes
    assert not default_options.hide_prices
Example #44
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 #45
0
def test_company_edit_form_links_company(regular_user, rf):
    get_default_shop()
    person = get_person_contact(regular_user)
    assert not get_company_contact(regular_user)

    client = SmartClient()
    client.login(username=REGULAR_USER_USERNAME, password=REGULAR_USER_PASSWORD)
    company_edit_url = reverse("shoop:company_edit")
    soup = client.soup(company_edit_url)

    data = default_company_data()
    data.update(default_address_data("billing"))
    data.update(default_address_data("shipping"))

    response, soup = client.response_and_soup(company_edit_url, data, "post")

    assert response.status_code == 302
    assert get_company_contact(regular_user)
Example #46
0
def test_timezone_setting(regular_user):
    get_default_shop()  # Create a shop

    mw = ShoopFrontMiddleware()
    request = get_unprocessed_request()
    request.user = regular_user

    some_tz = ('US/Hawaii' if settings.TIME_ZONE == 'UTC' else 'UTC')

    person = get_person_contact(regular_user)
    person.timezone = some_tz
    person.save()

    assert timezone.get_current_timezone_name() != some_tz

    mw.process_request(request)

    assert timezone.get_current_timezone_name() == some_tz
Example #47
0
def test_timezone_setting(regular_user):
    get_default_shop()  # Create a shop

    mw = ShoopFrontMiddleware()
    request = get_unprocessed_request()
    request.user = regular_user

    some_tz = ('US/Hawaii' if settings.TIME_ZONE == 'UTC' else 'UTC')

    person = get_person_contact(regular_user)
    person.timezone = some_tz
    person.save()

    assert timezone.get_current_timezone_name() != some_tz

    mw.process_request(request)

    assert timezone.get_current_timezone_name() == some_tz
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 #49
0
    def form_valid(self, form):
        company = form["contact"].save()
        user = self.request.user
        person = get_person_contact(user)
        company.members.add(person)
        billing_address = form["billing"].save()
        shipping_address = form["shipping"].save()
        if billing_address.pk != company.default_billing_address_id:  # Identity changed due to immutability
            company.default_billing_address = billing_address
        if shipping_address.pk != company.default_shipping_address_id:  # Identity changed due to immutability
            company.default_shipping_address = shipping_address

        user.email = company.email
        user.first_name = company.name
        user.last_name = ""
        user.save()

        company.save()
        messages.success(self.request, _("Company information saved successfully."))
        return redirect("shoop:company_edit")
Example #50
0
def test_customer_company_member(regular_user):
    get_default_shop()  # Create a shop

    mw = ShoopFrontMiddleware()
    request = get_unprocessed_request()
    request.user = regular_user
    person = get_person_contact(regular_user)
    company = create_random_company()
    company.members.add(person)

    assert get_company_contact(regular_user) == company

    mw.process_request(request)

    check_request_attribute_basics(request)

    assert isinstance(request.person, PersonContact)
    assert isinstance(request.customer, CompanyContact)

    company = get_company_contact(request.user)
    assert company and (company == request.customer)
Example #51
0
def test_customer_company_member(regular_user):
    get_default_shop()  # Create a shop

    mw = ShoopFrontMiddleware()
    request = get_unprocessed_request()
    request.user = regular_user
    person = get_person_contact(regular_user)
    company = create_random_company()
    company.members.add(person)

    assert get_company_contact(regular_user) == company

    mw.process_request(request)

    check_request_attribute_basics(request)

    assert isinstance(request.person, PersonContact)
    assert isinstance(request.customer, CompanyContact)

    company = get_company_contact(request.user)
    assert company and (company == request.customer)
Example #52
0
def test_company_still_linked_if_customer_contact_edited(regular_user):
    get_default_shop()
    person = get_person_contact(regular_user)
    assert not get_company_contact(regular_user)

    company = CompanyContact()
    company.save()
    company.members.add(person)
    assert get_company_contact(regular_user)

    client = SmartClient()
    client.login(username=REGULAR_USER_USERNAME, password=REGULAR_USER_PASSWORD)
    customer_edit_url = reverse("shoop:customer_edit")
    soup = client.soup(customer_edit_url)

    data = default_customer_data()
    data.update(default_address_data("billing"))
    data.update(default_address_data("shipping"))

    response, soup = client.response_and_soup(customer_edit_url, data, "post")

    assert response.status_code == 302
    assert get_company_contact(regular_user)
Example #53
0
 def _set_person(self, request):
     request.person = get_person_contact(request.user)
Example #54
0
def test_anonymous_contact_vs_person(regular_user):
    anon = AnonymousContact()
    person = get_person_contact(regular_user)
    assert anon != person
    assert person != anon