def test_order_creator_account_manager(): company = create_random_company() shop = get_shop(identifier="random-shop", enabled=True) source = seed_source(create_random_user(), shop) source.customer = company source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), supplier=get_default_supplier(), quantity=1, base_unit_price=source.create_price(10), ) creator = OrderCreator() order = creator.create_order(source) assert order.account_manager is None # Company contact doesn't have account manager field person = create_random_person() person.account_manager = create_random_person() person.save() source = seed_source(create_random_user(), shop) source.customer = person source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), supplier=get_default_supplier(), quantity=1, base_unit_price=source.create_price(10), ) creator = OrderCreator() order = creator.create_order(source) assert order.account_manager is not None assert order.account_manager == person.account_manager with pytest.raises(ProtectedError): person.account_manager.delete()
def test_sales_ranges_update_after_range_update(): shop = get_default_shop() supplier = get_default_supplier() person = create_random_person() company = create_random_company() create_fully_paid_order(shop, person, supplier, "sku1", 50) create_fully_paid_order(shop, company, supplier, "sku2", 100) assert get_total_sales(shop, person) == 50 assert get_total_sales(shop, company) == 100 sales_range = create_sales_range("gold", shop, 10, 90) assert sales_range.group in person.groups.all() assert sales_range.group not in company.groups.all() sales_range.max_value = None sales_range.save() assert sales_range.group in person.groups.all() assert sales_range.group in company.groups.all() # Make sure customers is actually removed when range changes sales_range.max_value = 60 sales_range.save() assert sales_range.group in person.groups.all() assert sales_range.group not in company.groups.all() # Inactive ranges shouldn't update group members sales_range.min_value = None sales_range.save() assert sales_range.group in person.groups.all() assert sales_range.group not in company.groups.all()
def test_set_non_shop_member_customer(rf): """ Set some customer to the basket that is not member of the shop """ with override_settings(**CORE_BASKET_SETTINGS): shop = factories.get_shop(False) assert shop != factories.get_default_shop() user = factories.create_random_user() request = apply_request_middleware(rf.get("/"), user=user) basket = get_basket(request, "basket") basket.customer = get_person_contact(user) assert basket.shop == factories.get_default_shop() person = factories.create_random_person() person.shops.add(shop) company = factories.create_random_company() company.shops.add(shop) for customer in [person, company]: with pytest.raises(ValidationError) as exc: basket_commands.handle_set_customer(request, basket, customer) assert exc.value.code == "invalid_customer_shop" assert basket.customer == get_person_contact(user)
def test_contact_company_list_multishop(rf): with override_settings(WSHOP_MANAGE_CONTACTS_PER_SHOP=True, WSHOP_ENABLE_MULTIPLE_SHOPS=True): staff_user = create_random_user(is_staff=True) shop1 = get_shop(identifier="shop-1", enabled=True) shop2 = get_shop(identifier="shop-2", enabled=True) shop1.staff_members.add(staff_user) shop2.staff_members.add(staff_user) # only available in shop2 contact = create_random_person(locale="en_US", minimum_name_comp_len=5) contact.shops.add(shop2) # only available in shop1 company = create_random_company() company.shops.add(shop1) view = ContactListView() request = apply_request_middleware(rf.get("/"), user=staff_user, shop=shop1) view.request = request assert company in view.get_queryset() assert contact not in view.get_queryset() request = apply_request_middleware(rf.get("/"), user=staff_user, shop=shop2) view.request = request assert contact in view.get_queryset() assert company not in view.get_queryset()
def test_company_contact_creation(rf, admin_user): get_initial_order_status() contact = create_random_company() test_tax_number = "1234567-1" contact.tax_number = test_tax_number contact.save() contact.default_billing_address.tax_number = test_tax_number contact.default_billing_address.save() state = get_frontend_order_state(contact=contact) state["customer"] = { "id": None, "name": None, "billingAddress": encode_address(contact.default_billing_address), "shipToBillingAddress": True, "saveAddress": True, "isCompany": True } order = get_order_from_state(state, admin_user) assert order.lines.count() == 5 assert order.customer.id != contact.id assert order.customer.name == contact.name assert order.customer.tax_number == test_tax_number assert order.billing_address.tax_number == contact.default_billing_address.tax_number assert order.billing_address.street == contact.default_billing_address.street assert order.billing_address.street == order.shipping_address.street
def test_encode_address(): contact = create_random_company() address = contact.default_billing_address address.save() assert not address.tax_number encoded = encode_address(address) for field, value in encoded.items(): assert getattr(address, field) == value # If no tax number, use tax_number argument encoded = encode_address(address, tax_number="12345") for field, value in encoded.items(): if field == "tax_number": assert value == "12345" else: assert getattr(address, field) == value address.tax_number = "67890" address.save() # If tax number exists, use existing tax number encoded = encode_address(address, tax_number="12345") for field, value in encoded.items(): if field == "tax_number": assert value == "67890" else: assert getattr(address, field) == value
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
def test_company_contact_edit_form(rf, admin_user): company = create_random_company() request = apply_request_middleware(rf.post("/"), user=admin_user) new_company_name = "test company" contact_base_form = CompanyContactBaseForm(request=request, instance=company, data={ "name": new_company_name, }) assert contact_base_form.is_valid(), contact_base_form.errors contact = contact_base_form.save() assert isinstance(contact, CompanyContact) assert isinstance(contact_base_form.fields["members"], Select2MultipleField) assert contact.name == new_company_name
def test_set_company_customer(rf): """ Set a company as the basket customer """ with override_settings(**CORE_BASKET_SETTINGS): user = factories.create_random_user() request = apply_request_middleware(rf.get("/"), user=user) basket = get_basket(request, "basket") basket.customer = get_person_contact(user) person = factories.create_random_person() company = factories.create_random_company() # no orderer provided with pytest.raises(ValidationError) as exc: basket_commands.handle_set_customer(request, basket, company) assert exc.value.code == "invalid_orderer" assert basket.customer == get_person_contact(user) # orderer provided but not member of the company with pytest.raises(ValidationError) as exc: basket_commands.handle_set_customer(request, basket, company, person) assert exc.value.code == "orderer_not_company_member" assert basket.customer == get_person_contact(user) # orderer provided but user not member of the company company.members.add(person) with pytest.raises(ValidationError) as exc: basket_commands.handle_set_customer(request, basket, company, person) assert exc.value.code == "not_company_member" assert basket.customer == get_person_contact(user) # staff and admin can add any the company and orderer without being member of the company superuser = factories.create_random_user(is_superuser=True) staff = factories.create_random_user(is_staff=True) basket.shop.staff_members.add(staff) for user in [superuser, staff]: basket.customer = None basket.orderer = None request = apply_request_middleware(rf.get("/"), user=user) assert basket_commands.handle_set_customer(request, basket, company, person)["ok"] is True assert basket.customer == company assert basket.orderer == person
def test_anonymous_set_company_customer(rf): """ Set a company as the basket customer """ with override_settings(**CORE_BASKET_SETTINGS): user = factories.create_random_user() request = apply_request_middleware(rf.get("/"), user=user) basket = get_basket(request, "basket") basket.customer = AnonymousContact() person = factories.create_random_person() company = factories.create_random_company() company.members.add(person) with pytest.raises(ValidationError) as exc: basket_commands.handle_set_customer(request, basket, company, person) assert exc.value.code == "not_company_member" assert basket.customer == AnonymousContact()
def test_order_creator_company_multishop(): with override_settings(WSHOP_MANAGE_CONTACTS_PER_SHOP=True, WSHOP_ENABLE_MULTIPLE_SHOPS=True): company = create_random_company() shop = get_shop(identifier="random-shop", enabled=True) source = seed_source(create_random_user(), shop) source.customer = company source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), supplier=get_default_supplier(), quantity=1, base_unit_price=source.create_price(10), ) creator = OrderCreator() creator.create_order(source) company.refresh_from_db() assert shop in company.shops.all()
def test_order_creator_customer_details(rf, admin_user): shop = get_default_shop() contact = create_random_person(locale="en_US", minimum_name_comp_len=5) company = create_random_company() group = get_default_customer_group() contact.groups.add(group) contact.company_memberships.add(company) contact.save() product = create_product(sku=printable_gibberish(), supplier=get_default_supplier(), shop=shop) order = create_random_order(contact, products=[product]) request = apply_request_middleware(rf.get( "/", { "command": "customer_details", "id": contact.id }), user=admin_user) response = OrderEditView.as_view()(request) data = json.loads(response.content.decode("utf8")) assert "customer_info" in data assert "order_summary" in data assert "recent_orders" in data assert data["customer_info"]["name"] == contact.full_name assert data["customer_info"]["phone_no"] == contact.phone assert data["customer_info"]["email"] == contact.email assert company.full_name in data["customer_info"]["companies"] assert group.name in data["customer_info"]["groups"] assert data["customer_info"]["merchant_notes"] == contact.merchant_notes assert len(data["order_summary"]) == 1 assert data["order_summary"][0]["year"] == order.order_date.year assert data["order_summary"][0]["total"] == format_money( order.taxful_total_price) assert len(data["recent_orders"]) == 1 assert data["recent_orders"][0]["status"] == order.get_status_display() assert data["recent_orders"][0]["total"] == format_money( order.taxful_total_price) assert data["recent_orders"][0]["payment_status"] == force_text( order.payment_status.label) assert data["recent_orders"][0]["shipment_status"] == force_text( order.shipping_status.label)
def test_customer_company_member(regular_user): get_default_shop() # Create a shop mw = WshopFrontMiddleware() 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)
def test_address_assigned_for_multiple_contact(): person1 = create_random_person() person2 = create_random_company() address = create_random_address() person1.default_billing_address = address person1.save() person2.default_shipping_address = address person2.save() assert person1.default_billing_address.id == address.id assert person2.default_shipping_address.id == address.id form = MutableAddressForm(instance=address) data = get_form_data(form, prepared=True) form = MutableAddressForm(data=data, instance=address) form.full_clean() assert not form.errors assert form.cleaned_data form.save() address.refresh_from_db() assert person1.default_billing_address_id != address.id assert person2.default_shipping_address_id != address.id
def test_set_not_active_customer(rf): """ Set a not active customer to the basket """ with override_settings(**CORE_BASKET_SETTINGS): user = factories.create_random_user() request = apply_request_middleware(rf.get("/"), user=user) basket = get_basket(request, "basket") basket.customer = get_person_contact(user) person = factories.create_random_person() person.is_active = False person.save() company = factories.create_random_company() company.is_active = False company.save() for customer in [person, company]: with pytest.raises(ValidationError) as exc: basket_commands.handle_set_customer(request, basket, customer) assert exc.value.code == "invalid_customer" assert basket.customer == get_person_contact(user)
def test_contact_company_edit_multishop(rf): with override_settings(WSHOP_MANAGE_CONTACTS_PER_SHOP=True, WSHOP_ENABLE_MULTIPLE_SHOPS=True): staff_user = create_random_user(is_staff=True) shop1 = get_shop(identifier="shop-1", enabled=True) shop2 = get_shop(identifier="shop-2", enabled=True) shop1.staff_members.add(staff_user) shop2.staff_members.add(staff_user) # only available in shop2 contact = create_random_person(locale="en_US", minimum_name_comp_len=5) contact.shops.add(shop2) # only available in shop1 company = create_random_company() company.shops.add(shop1) view = ContactEditView.as_view() # permission denied for contact and shop1 request = apply_request_middleware(rf.get("/"), user=staff_user, shop=shop1) with pytest.raises(PermissionDenied): response = view(request, pk=contact.id) # permission granted for contact and shop2 request = apply_request_middleware(rf.get("/"), user=staff_user, shop=shop2) response = view(request, pk=contact.id) assert response.status_code == 200 # permission denied for company and shop2 request = apply_request_middleware(rf.get("/"), user=staff_user, shop=shop2) with pytest.raises(PermissionDenied): response = view(request, pk=company.id) # permission granted for company and shop1 request = apply_request_middleware(rf.get("/"), user=staff_user, shop=shop1) response = view(request, pk=company.id) assert response.status_code == 200
def test_company_contact_detail_multishop(rf): with override_settings(WSHOP_MANAGE_CONTACTS_PER_SHOP=True, WSHOP_ENABLE_MULTIPLE_SHOPS=True): staff_user = create_random_user(is_staff=True) shop1 = get_shop(identifier="shop-1", enabled=True) shop2 = get_shop(identifier="shop-2", enabled=True) shop1.staff_members.add(staff_user) shop2.staff_members.add(staff_user) company = create_random_company() # only available in shop1 company.shops.add(shop1) view = ContactDetailView.as_view() # company not found for this shop request = apply_request_middleware(rf.get("/"), user=staff_user, shop=shop2) with pytest.raises(PermissionDenied): response = view(request, pk=company.id) request = apply_request_middleware(rf.get("/"), user=staff_user, shop=shop1) response = view(request, pk=company.id) assert response.status_code == 200
def mock_company(self): """ Create a random company """ return create_random_company()
def test_create_order(admin_user, target_customer): with override_settings(**REQUIRED_SETTINGS): set_configuration() factories.create_default_order_statuses() shop = factories.get_default_shop() client = _get_client(admin_user) # Create basket for target customer payload = {"shop": shop.pk} target = orderer = get_person_contact(admin_user) if target_customer == "other_person": target = orderer = factories.create_random_person() payload["customer"] = target.pk elif target_customer == "company": target = factories.create_random_company() orderer = factories.create_random_person() payload.update({"customer": target.pk, "orderer": orderer.pk}) target.members.add(orderer) response = client.post("/api/wshop/basket/new/", payload) assert response.status_code == status.HTTP_201_CREATED basket_data = json.loads(response.content.decode("utf-8")) basket = Basket.objects.first() assert basket.key == basket_data["uuid"].split("-")[1] assert basket.customer == target assert basket.orderer == orderer shop_product = factories.get_default_shop_product() shop_product.default_price = TaxfulPrice(1, shop.currency) shop_product.save() # Add shop product to basket payload = {"shop_product": shop_product.id} response = client.post( "/api/wshop/basket/{}-{}/add/".format(shop.pk, basket.key), payload) assert response.status_code == status.HTTP_200_OK response_data = json.loads(response.content.decode("utf-8")) assert len(response_data["items"]) == 1 # Create order from basket response = client.post( "/api/wshop/basket/{}-{}/create_order/".format( shop.pk, basket.key), payload) assert response.status_code == status.HTTP_400_BAD_REQUEST response_data = json.loads(response.content.decode("utf-8")) assert "errors" in response_data factories.get_default_payment_method() factories.get_default_shipping_method() response = client.post( "/api/wshop/basket/{}-{}/create_order/".format( shop.pk, basket.key), payload) assert response.status_code == status.HTTP_201_CREATED response_data = json.loads(response.content.decode("utf-8")) basket.refresh_from_db() assert basket.finished order = Order.objects.get( reference_number=response_data["reference_number"]) assert order.status == OrderStatus.objects.get_default_initial() assert order.payment_status == PaymentStatus.NOT_PAID assert order.shipping_status == ShippingStatus.NOT_SHIPPED assert not order.payment_method assert not order.shipping_method assert float(order.taxful_total_price_value) == 1 assert order.customer == target assert order.orderer == orderer assert order.creator == admin_user assert not order.billing_address assert not order.shipping_address
def _create_customer_for_day(shop, day): company = create_random_company() company.created_on = day company.save()