def test_basket_contact_condition(rf): product_price_value, campaign_discount_value = 2, 1 request = get_request_for_contact_tests(rf) random_person = create_random_person() request.customer = random_person condition = ContactBasketCondition.objects.create() condition.contacts.add(random_person) basket, original_line_count, original_price = create_basket_and_campaign( request, [condition], product_price_value, campaign_discount_value) # random_person should get this campaign assert basket.customer == random_person assert_discounted_basket(basket, original_line_count, original_price, campaign_discount_value) another_random_person = create_random_person() basket.customer = another_random_person # another random person shouldn't assert_non_discounted_basket(basket, original_line_count, original_price) # Add another random person for the rule and see if he get's the discount condition.contacts.add(another_random_person) condition.save() assert_discounted_basket(basket, original_line_count, original_price, campaign_discount_value) assert basket.customer == another_random_person # Remove random person from rule and see the discount disappear condition.contacts.remove(random_person) condition.save() basket.customer = random_person assert_non_discounted_basket(basket, original_line_count, original_price) assert basket.customer == random_person
def test_context_contact_condition(rf): original_price_value, discount_value = 2, 1 request = get_request_for_contact_tests(rf) random_person = create_random_person() condition = ContactCondition.objects.create() condition.contacts.add(random_person) product = create_random_product_and_campaign(request.shop, [condition], original_price_value, discount_value) # random_person should get this campaign discounted_value = original_price_value - discount_value assert_product_price_value_with_customer(request, random_person, product, discounted_value) another_random_person = create_random_person() # another random person shouldn't assert_product_price_value_with_customer(request, another_random_person, product, original_price_value) # Add another random person for the rule and see if he get's the discount condition.contacts.add(another_random_person) assert_product_price_value_with_customer(request, another_random_person, product, discounted_value) # Remove random person from rule and see the discount disappear condition.contacts.remove(random_person) condition.save() assert_product_price_value_with_customer(request, random_person, product, original_price_value)
def test_category_links_plugin_with_customer(rf, show_all_categories): """ Test plugin for categories that is visible for certain group """ shop = get_default_shop() group = get_default_customer_group() customer = create_random_person() customer.groups.add(group) customer.save() request = rf.get("/") request.shop = get_default_shop() apply_request_middleware(request) request.customer = customer category = get_default_category() category.status = CategoryStatus.VISIBLE category.visibility = CategoryVisibility.VISIBLE_TO_GROUPS category.visibility_groups.add(group) category.shops.add(shop) category.save() vars = {"request": request} context = get_jinja_context(**vars) plugin = CategoryLinksPlugin({"categories": [category.pk], "show_all_categories": show_all_categories}) assert category.is_visible(customer) assert category in plugin.get_context_data(context)["categories"] customer_without_groups = create_random_person() customer_without_groups.groups.clear() assert not category.is_visible(customer_without_groups) request.customer = customer_without_groups context = get_jinja_context(**vars) assert category not in plugin.get_context_data(context)["categories"]
def test_user_detail_contact_seed(rf): get_default_shop() contact = create_random_person() view_func = UserDetailView.as_view() # Check that fields populate . . . request = apply_request_middleware(rf.get("/", {"contact_id": contact.pk})) response = view_func(request) response.render() content = force_text(response.content) assert force_text(contact.first_name) in content assert force_text(contact.last_name) in content assert force_text(contact.email) in content # POST the password too to create the user . . . post = extract_form_fields(BeautifulSoup(content)) post["password"] = "******" request.method = "POST" request.POST = post response = view_func(request) assert response.status_code < 500 # Check this new user is visible in the details now user = Contact.objects.get(pk=contact.pk).user request = apply_request_middleware(rf.get("/", {"contact_id": contact.pk})) response = view_func(request, pk=user.pk) response.render() content = force_text(response.content) assert force_text(contact.first_name) in content assert force_text(contact.last_name) in content assert force_text(contact.email) in content
def test_coupon_amount_limit(): coupon = Coupon.objects.create(code="TEST", active=True) get_default_campaign(coupon) contact = create_random_person() shop = get_default_shop() product = create_product("test", shop=shop, supplier=get_default_supplier(), default_price="12") order = create_random_order(customer=contact) for x in range(50): coupon.use(order) assert coupon.usages.count() == 50 coupon.increase_usage_limit_by(5) coupon.save() assert coupon.usage_limit == 55 assert coupon.can_use_code(contact) for x in range(5): coupon.use(order) assert coupon.usages.count() == 55 assert not Coupon.is_usable(coupon.code, order.customer) assert coupon.usages.count() == 55 # no change, limit met
def test_contact_details_view_with_many_groups(rf, admin_user): get_default_shop() person = create_random_person() person.groups.add( ContactGroup.objects.create(name="Czz Group"), ContactGroup.objects.create(name="Azz Group"), ContactGroup.objects.create(name="Bzz Group"), ContactGroup.objects.language('fi').create(name="Dzz ryhmä"), ) # Group with name in two languages grp_e = ContactGroup.objects.language('en').create(name="Ezz Group") grp_e.set_current_language('fi') grp_e.name = "Ezz ryhmä" grp_e.save() person.groups.add(grp_e) request = apply_request_middleware(rf.get("/"), user=admin_user) with translation.override('en'): view_func = ContactDetailView.as_view() response = view_func(request, pk=person.pk) content = response.render().content.decode('utf-8') assert "Azz Group" in content assert "Bzz Group" in content assert "Czz Group" in content assert "Dzz ryhmä" in content, "no name in active language, still present" assert "Ezz Group" in content, "rendered with active language" positions = [content.index(x + "zz ") for x in 'ABCDE'] assert positions == sorted(positions), "Groups are sorted" assert response.status_code == 200
def test_order_creator_source_data(rf, admin_user): get_initial_order_status() # Needed for the API contact = create_random_person(locale="en_US", minimum_name_comp_len=5) request = get_frontend_request_for_command(get_frontend_order_state(contact), "source_data", admin_user) response = OrderCreateView.as_view()(request) data = json.loads(response.content.decode("utf8")) assert len(data.get("orderLines")) == 5
def test_user_detail_contact_seed(rf): get_default_shop() contact = create_random_person() # Using random data for name and email would need escaping when # checking if it is rendered, therefore use something very basic instead contact.name = "Matti Perustyyppi" contact.email = "*****@*****.**" contact.save() view_func = UserDetailView.as_view() # Check that fields populate . . . request = apply_request_middleware(rf.get("/", {"contact_id": contact.pk})) response = view_func(request) response.render() content = force_text(response.content) assert force_text(contact.first_name) in content assert force_text(contact.last_name) in content assert force_text(contact.email) in content # POST the password too to create the user . . . post = extract_form_fields(BeautifulSoup(content)) post["password"] = "******" request.method = "POST" request.POST = post response = view_func(request) assert response.status_code < 500 # Check this new user is visible in the details now user = Contact.objects.get(pk=contact.pk).user request = apply_request_middleware(rf.get("/", {"contact_id": contact.pk})) response = view_func(request, pk=user.pk) response.render() content = force_text(response.content) assert force_text(contact.first_name) in content assert force_text(contact.last_name) in content assert force_text(contact.email) in content
def test_min_amount_is_not_included(): shop = get_default_shop() supplier = get_default_supplier() person = create_random_person() initial_group_count = person.groups.count() sales_ranges = [ ("silver", 0, 50), ("gold", 50, 100), ("diamond", 100, 1000), ("reverse_diamond", 1000, 100) ] for identifier, min, max in sales_ranges: create_sales_range(identifier, shop, min, max) payment = create_fully_paid_order(shop, person, supplier, "sku1", 50) assert _get_total_sales(shop, person) == 50 update_customers_groups(Payment, payment) assert person.groups.count() == (initial_group_count + 1) assert bool([group for group in person.groups.all() if group.identifier == "gold"]) assert not bool([group for group in person.groups.all() if group.identifier in ["silver", "diamond"]]) payment = create_fully_paid_order(shop, person, supplier, "sku2", 50) assert _get_total_sales(shop, person) == 100 update_customers_groups(Payment, payment) assert person.groups.count() == (initial_group_count + 1) assert bool([group for group in person.groups.all() if group.identifier == "diamond"]) assert not bool([group for group in person.groups.all() if group.identifier == "reverse_diamond"]) assert not bool([group for group in person.groups.all() if group.identifier in ["silver", "gold"]])
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_campaign_with_non_active_coupon(rf): initial_status = get_initial_order_status() request, shop, group = initialize_test(rf, include_tax=False) order = _get_order_with_coupon(request, initial_status) coupon = order.coupon_usages.first().coupon coupon.active = False coupon.save() modifier = UserFactory() contact = create_random_person(locale="en_US", minimum_name_comp_len=5) assert order.customer != contact state = _get_frontend_order_state(shop, contact) assert order.shop.id == state["shop"]["selected"]["id"] request = get_frontend_request_for_command(state, "finalize", modifier) response = OrderEditView.as_view()(request, pk=order.pk) assert_contains(response, "orderIdentifier") data = json.loads(response.content.decode("utf8")) edited_order = Order.objects.get(pk=order.pk) assert edited_order.identifier == data["orderIdentifier"] == order.identifier assert edited_order.pk == order.pk assert edited_order.lines.count() == 3 assert OrderLineType.DISCOUNT not in [l.type for l in edited_order.lines.all()] assert edited_order.coupon_usages.count() == 0
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()
def test_order_creator_source_data(rf, admin_user): get_initial_order_status() # Needed for the API contact = create_random_person(locale="en_US", minimum_name_comp_len=5) request = get_frontend_request_for_command(get_frontend_order_state(contact), "source_data", admin_user) response =OrderEditView.as_view()(request) data = json.loads(response.content.decode("utf8")) assert len(data.get("orderLines")) == 5
def test_order_creator_view_for_customer(rf, admin_user): get_default_shop() contact = create_random_person(locale="en_US", minimum_name_comp_len=5) request = apply_request_middleware(rf.get("/", {"contact_id": contact.id}), user=admin_user) response = OrderEditView.as_view()(request) assert_contains(response, "customerData") # in the config assert_contains(response, "isCompany") # in the config
def test_campaign_with_non_active_coupon(rf): initial_status = get_initial_order_status() request, shop, group = initialize_test(rf, include_tax=False) order = _get_order_with_coupon(request, initial_status) coupon = order.coupon_usages.first().coupon coupon.active = False coupon.save() modifier = UserFactory() contact = create_random_person(locale="en_US", minimum_name_comp_len=5) assert order.customer != contact state = _get_frontend_order_state(shop, contact) assert order.shop.id == state["shop"]["selected"]["id"] request = get_frontend_request_for_command(state, "finalize", modifier) response = OrderEditView.as_view()(request, pk=order.pk) assert_contains(response, "orderIdentifier") data = json.loads(response.content.decode("utf8")) edited_order = Order.objects.get(pk=order.pk) assert edited_order.identifier == data[ "orderIdentifier"] == order.identifier assert edited_order.pk == order.pk assert edited_order.lines.count() == 3 assert OrderLineType.DISCOUNT not in [ l.type for l in edited_order.lines.all() ] assert edited_order.coupon_usages.count() == 0
def test_sales_between_ranges(): shop = get_default_shop() supplier = get_default_supplier() person = create_random_person() initial_group_count = person.groups.count() sales_ranges = [ ("wood", 15, 0), ("silver", 0, 50), ("diamond", 100, None) ] for identifier, min, max in sales_ranges: create_sales_range(identifier, shop, min, max) payment = create_fully_paid_order(shop, person, supplier, "sku1", 10) assert get_total_sales(shop, person) == 10 update_customers_groups(Payment, payment) assert person.groups.count() == (initial_group_count + 1) assert bool([group for group in person.groups.all() if group.identifier == "silver"]) assert not bool([group for group in person.groups.all() if group.identifier == "wood"]) payment = create_fully_paid_order(shop, person, supplier, "sku2", 50) assert get_total_sales(shop, person) == 60 update_customers_groups(Payment, payment) assert person.groups.count() == initial_group_count assert not bool([group for group in person.groups.all() if group.identifier in ["silver", "gold", "diamond"]]) payment = create_fully_paid_order(shop, person, supplier, "sku3", 200) assert get_total_sales(shop, person) == 260 update_customers_groups(Payment, payment) assert person.groups.count() == (initial_group_count + 1) assert bool([group for group in person.groups.all() if group.identifier == "diamond"])
def test_sales_ranges_basic(): shop = get_default_shop() supplier = get_default_supplier() person = create_random_person() initial_group_count = person.groups.count() sales_ranges = [("silver", 0, 50), ("gold", 50, 100), ("diamond", 100, 1000)] for identifier, min, max in sales_ranges: create_sales_level(identifier, shop, min, max) payment = create_fully_paid_order(shop, person, supplier, "sku1", 10) assert _get_total_sales(shop, person) == 10 update_customers_groups(Payment, payment) assert person.groups.count() == (initial_group_count + 1) assert bool([ group for group in person.groups.all() if group.identifier == "silver" ]) payment = create_fully_paid_order(shop, person, supplier, "sku2", 50) assert _get_total_sales(shop, person) == 60 update_customers_groups(Payment, payment) assert person.groups.count() == (initial_group_count + 1) assert bool( [group for group in person.groups.all() if group.identifier == "gold"]) payment = create_fully_paid_order(shop, person, supplier, "sku3", 200) assert _get_total_sales(shop, person) == 260 update_customers_groups(Payment, payment) assert person.groups.count() == (initial_group_count + 1) assert bool([ group for group in person.groups.all() if group.identifier == "diamond" ])
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 . . .
def get_initialized_test_event(): get_default_product() customer = create_random_person() order = create_random_order(customer) return ATestEvent(order_language=order.language, order=order, just_some_text=random_title())
def test_min_amount_is_not_included(): shop = get_default_shop() supplier = get_default_supplier() person = create_random_person() initial_group_count = person.groups.count() sales_ranges = [ ("silver", 0, 50), ("gold", 50, 100), ("diamond", 100, 1000), ("reverse_diamond", 1000, 100) ] for identifier, min, max in sales_ranges: create_sales_range(identifier, shop, min, max) payment = create_fully_paid_order(shop, person, supplier, "sku1", 50) assert get_total_sales(shop, person) == 50 update_customers_groups(Payment, payment) assert person.groups.count() == (initial_group_count + 1) assert bool([group for group in person.groups.all() if group.identifier == "gold"]) assert not bool([group for group in person.groups.all() if group.identifier in ["silver", "diamond"]]) payment = create_fully_paid_order(shop, person, supplier, "sku2", 50) assert get_total_sales(shop, person) == 100 update_customers_groups(Payment, payment) assert person.groups.count() == (initial_group_count + 1) assert bool([group for group in person.groups.all() if group.identifier == "diamond"]) assert not bool([group for group in person.groups.all() if group.identifier == "reverse_diamond"]) assert not bool([group for group in person.groups.all() if group.identifier in ["silver", "gold"]])
def test_contact_module_search(rf): cm = ContactModule() # This test has a chance to fail if the random person is from a strange locale # and the database does not like it. Therefore, use `en_US` here... contact = create_random_person(locale="en_US", minimum_name_comp_len=5) request = rf.get("/") assert not empty_iterable(cm.get_search_results(request, query=contact.email)) assert not empty_iterable(cm.get_search_results(request, query=contact.first_name)) assert empty_iterable(cm.get_search_results(request, query=contact.email[0]))
def get_initialized_test_event(): get_default_product() customer = create_random_person() order = create_random_order(customer) return ATestEvent( order_language=order.language, order=order, just_some_text=random_title() )
def test_without_groups(admin_user): payment_method = get_default_payment_method() _assign_component_for_service(payment_method, []) person = create_random_person() person.user = admin_user person.save() source = _get_source_for_contact(admin_user, payment_method) assert source.customer == person _test_service_availability(source, payment_method, False)
def test_order_creator_customer_data(rf, admin_user): get_default_shop() contact = create_random_person(locale="en_US", minimum_name_comp_len=5) request = apply_request_middleware(rf.get("/", { "command": "customer_data", "id": contact.id }), user=admin_user) response = OrderCreateView.as_view()(request) assert_contains(response, "name") assert_contains(response, contact.name)
def test_order_creator_customer_data(rf, admin_user): get_default_shop() contact = create_random_person(locale="en_US", minimum_name_comp_len=5) request = apply_request_middleware(rf.get("/", { "command": "customer_data", "id": contact.id }), user=admin_user) response =OrderEditView.as_view()(request) assert_contains(response, "name") assert_contains(response, contact.name)
def test_add_order_log_entry(admin_user, rf): order = create_random_order(customer=create_random_person(), products=(get_default_product(),)) assert not OrderLogEntry.objects.filter(target=order).exists() view = NewLogEntryView.as_view() test_message = "test_order" request = apply_request_middleware(rf.post("/", {"message": test_message}), user=admin_user) response = view(request, pk=order.pk) assert response.status_code < 400 assert OrderLogEntry.objects.filter(target=order).exists() assert OrderLogEntry.objects.filter(target=order).first().message == test_message
def test_editing_existing_order(rf, admin_user): modifier = UserFactory() get_initial_order_status() # Needed for the API contact = create_random_person(locale="en_US", minimum_name_comp_len=5) state = get_frontend_order_state(contact=contact) shop = get_default_shop() order = create_empty_order(shop=shop) order.payment_data = {"payment_data": True} order.shipping_data = {"shipping_data": True} order.extra_data = {"external_id": "123"} order.save() assert order.lines.count() == 0 assert order.pk is not None assert order.modified_by == order.creator request = get_frontend_request_for_command(state, "finalize", modifier) response = OrderEditView.as_view()(request, pk=order.pk) assert_contains( response, "orderIdentifier") # this checks for status codes as a side effect data = json.loads(response.content.decode("utf8")) edited_order = Order.objects.get(pk=order.pk) # Re fetch the initial order # Check that identifiers has not changed assert edited_order.identifier == data[ "orderIdentifier"] == order.identifier assert edited_order.pk == order.pk # Check that the product content is updated based on state assert edited_order.lines.count() == 5 assert edited_order.customer == contact # Check that product line have right taxes for line in edited_order.lines.all(): if line.type == OrderLineType.PRODUCT: assert [line_tax.tax.code for line_tax in line.taxes.all()] == ["test_code"] assert line.taxful_price.amount > line.taxless_price.amount # Make sure order modification information is correct assert edited_order.modified_by != order.modified_by assert edited_order.modified_by == modifier assert edited_order.modified_on > order.modified_on # Make sure all non handled attributes is preserved from original order assert edited_order.creator == order.creator assert edited_order.ip_address == order.ip_address assert edited_order.orderer == order.orderer assert edited_order.customer_comment == order.customer_comment assert edited_order.marketing_permission == order.marketing_permission assert edited_order.order_date == order.order_date assert edited_order.status == order.status assert edited_order.payment_data == order.payment_data assert edited_order.shipping_data == order.shipping_data assert edited_order.extra_data == order.extra_data
def test_order_creator_valid(rf, admin_user): get_initial_order_status() # Needed for the API contact = create_random_person(locale="en_US", minimum_name_comp_len=5) request = get_frontend_request_for_command(get_frontend_order_state(contact), "create", admin_user) response = OrderCreateView.as_view()(request) assert_contains(response, "orderIdentifier") # this checks for status codes as a side effect data = json.loads(response.content.decode("utf8")) order = Order.objects.get(identifier=data["orderIdentifier"]) assert order.lines.count() == 5 # 3 submitted, two for the shipping and payment method assert order.creator == admin_user assert order.customer == contact
def initialize_test(rf, include_tax=False): shop = get_shop_with_tax(include_tax=include_tax) group = get_default_customer_group() customer = create_random_person() customer.groups.add(group) customer.save() request = rf.get("/") request.shop = shop request.customer = customer return request, shop, group
def test_order_set_status_works(admin_user, rf): order = create_random_order(customer=create_random_person(), products=(get_default_product(),)) order.create_shipment_of_all_products() # Need to be shipped to set complete assert order.status.role == OrderStatusRole.INITIAL complete_status = OrderStatus.objects.get_default_complete() view = OrderSetStatusView.as_view() request = apply_request_middleware(rf.post("/", {"status": complete_status.pk}), user=admin_user) response = view(request, pk=order.pk) assert response.status_code < 400 order = Order.objects.get(pk=order.pk) assert order.status_id == complete_status.id assert order.log_entries.filter(identifier="status_change").exists()
def initialize_test(rf, include_tax=False): shop = get_shop(prices_include_tax=include_tax) group = get_default_customer_group() customer = create_random_person() customer.groups.add(group) customer.save() request = apply_request_middleware(rf.get("/")) request.shop = shop request.customer = customer return request, shop, group
def test_order_set_status_canceled_works(admin_user, rf): order = create_random_order(customer=create_random_person(), products=(get_default_product(),)) assert order.shipping_status == ShippingStatus.NOT_SHIPPED assert order.status.role == OrderStatusRole.INITIAL canceled_status = OrderStatus.objects.get_default_canceled() view = OrderSetStatusView.as_view() request = apply_request_middleware(rf.post("/", {"status": canceled_status.pk}), user=admin_user) response = view(request, pk=order.pk) assert response.status_code < 400 order = Order.objects.get(pk=order.pk) assert order.status_id == canceled_status.id assert order.log_entries.filter(identifier="status_change").exists()
def test_order_set_status_completed_works(admin_user, rf): order = create_random_order(customer=create_random_person(), products=(get_default_product(),)) order.create_shipment_of_all_products() # Need to be shipped to set complete assert order.status.role == OrderStatusRole.INITIAL complete_status = OrderStatus.objects.get_default_complete() view = OrderSetStatusView.as_view() request = apply_request_middleware(rf.post("/", {"status": complete_status.pk}), user=admin_user) response = view(request, pk=order.pk) assert response.status_code < 400 order = Order.objects.get(pk=order.pk) assert order.status_id == complete_status.id assert order.log_entries.filter(identifier="status_change").exists()
def test_order_creator_invalid_line_data(rf, admin_user): get_initial_order_status() # Needed for the API contact = create_random_person(locale="en_US", minimum_name_comp_len=5) state = get_frontend_order_state(contact=contact, valid_lines=False) request = get_frontend_request_for_command(state, "create", admin_user) response = OrderCreateView.as_view()(request) # Let's see that we get a cornucopia of trouble: assert_contains(response, "does not exist", status_code=400) assert_contains(response, "does not have a product", status_code=400) assert_contains(response, "is not available", status_code=400) assert_contains(response, "The price", status_code=400) assert_contains(response, "The quantity", status_code=400)
def test_admin_recovers_clients_password(rf, admin_user): get_default_shop() person = create_random_person() person.user = get_user_model().objects.create_user( username="******", password="******", email="*****@*****.**") person.save() request = apply_request_middleware(rf.post("/"), user=admin_user) view_func = ContactResetPasswordView.as_view() n_outbox_pre = len(mail.outbox) view_func(request, pk=person.pk) # The response doesn't actually matter. assert (len(mail.outbox) == n_outbox_pre + 1), "Sending recovery email has failed"
def test_order_creator_valid(rf, admin_user): get_initial_order_status() # Needed for the API contact = create_random_person(locale="en_US", minimum_name_comp_len=5) order = get_order_from_state(get_frontend_order_state(contact), admin_user) assert order.lines.count() == 5 # 3 submitted, two for the shipping and payment method assert order.creator == admin_user assert order.customer == contact # Check that product line have right taxes for line in order.lines.all(): if line.type == OrderLineType.PRODUCT: assert [line_tax.tax.code for line_tax in line.taxes.all()] == ["test_code"] assert line.taxful_price.amount > line.taxless_price.amount
def test_add_order_log_entry(admin_user, rf): order = create_random_order(customer=create_random_person(), products=(get_default_product(), )) assert not OrderLogEntry.objects.filter(target=order).exists() view = NewLogEntryView.as_view() test_message = "test_order" request = apply_request_middleware(rf.post("/", {"message": test_message}), user=admin_user) response = view(request, pk=order.pk) assert response.status_code < 400 assert OrderLogEntry.objects.filter(target=order).exists() assert OrderLogEntry.objects.filter( target=order).first().message == test_message
def test_order_creator_invalid_line_data(rf, admin_user): get_initial_order_status() # Needed for the API contact = create_random_person(locale="en_US", minimum_name_comp_len=5) state = get_frontend_order_state(contact=contact, valid_lines=False) request = get_frontend_request_for_command(state, "create", admin_user) response = OrderCreateView.as_view()(request) # Let's see that we get a cornucopia of trouble: assert_contains(response, "does not exist", status_code=400) assert_contains(response, "does not have a product", status_code=400) assert_contains(response, "is not available", status_code=400) assert_contains(response, "The price", status_code=400) assert_contains(response, "The quantity", status_code=400) assert_contains(response, "not visible", status_code=400)
def test_with_multiple_groups(admin_user): payment_method = get_default_payment_method() group = get_default_customer_group() person = create_random_person() groups = [group, person.get_default_group()] _assign_component_for_service(payment_method, groups) person.user = admin_user person.save() source = _get_source_for_contact(admin_user, payment_method) assert source.customer == person assert len([group for group in person.groups.all() if group in groups]) == 1 _test_service_availability(source, payment_method, True)
def test_get_suppliable_products(): customer = create_random_person() shop_product = get_default_shop_product() shop = get_default_shop() supplier = get_default_supplier() # Check for default orderable shop product with unstocked behavior assert len(list(supplier.get_suppliable_products(shop, customer=customer))) == 1 product = shop_product.product product.stock_behavior = StockBehavior.STOCKED product.save() # Make sure supplier now omits unorderable product assert not list(supplier.get_suppliable_products(shop, customer=customer))
def test_sales_ranges_around_zero(): shop = get_default_shop() person = create_random_person() initial_group_count = person.groups.count() sales_ranges = [("silver", 0, 0), ("gold", 0, None), ("diamond", None, 0)] for identifier, min, max in sales_ranges: create_sales_level(identifier, shop, min, max) assert _get_total_sales(shop, person) == 0 _assign_to_group_based_on_sales(shop, person) assert person.groups.count() == (initial_group_count + 1) assert bool( [group for group in person.groups.all() if group.identifier == "gold"])
def test_admin_recovers_clients_password(rf, admin_user): get_default_shop() person = create_random_person() person.user = get_user_model().objects.create_user( username="******", password="******", email="*****@*****.**" ) person.save() request = apply_request_middleware(rf.post("/"), user=admin_user) view_func = ContactResetPasswordView.as_view() n_outbox_pre = len(mail.outbox) view_func(request, pk=person.pk) # The response doesn't actually matter. assert (len(mail.outbox) == n_outbox_pre + 1), "Sending recovery email has failed"
def test_editing_existing_order(rf, admin_user): modifier = UserFactory() get_initial_order_status() # Needed for the API contact = create_random_person(locale="en_US", minimum_name_comp_len=5) state = get_frontend_order_state(contact=contact) shop = get_default_shop() order = create_empty_order(shop=shop) order.payment_data = {"payment_data": True} order.shipping_data = {"shipping_data": True} order.extra_data = {"external_id": "123"} order.save() assert order.lines.count() == 0 assert order.pk is not None assert order.modified_by == order.creator request = get_frontend_request_for_command(state, "finalize", modifier) response = OrderEditView.as_view()(request, pk=order.pk) assert_contains(response, "orderIdentifier") # this checks for status codes as a side effect data = json.loads(response.content.decode("utf8")) edited_order = Order.objects.get(pk=order.pk) # Re fetch the initial order # Check that identifiers has not changed assert edited_order.identifier == data["orderIdentifier"] == order.identifier assert edited_order.pk == order.pk # Check that the product content is updated based on state assert edited_order.lines.count() == 5 assert edited_order.customer == contact # Check that product line have right taxes for line in edited_order.lines.all(): if line.type == OrderLineType.PRODUCT: assert [line_tax.tax.code for line_tax in line.taxes.all()] == ["test_code"] assert line.taxful_price.amount > line.taxless_price.amount # Make sure order modification information is correct assert edited_order.modified_by != order.modified_by assert edited_order.modified_by == modifier assert edited_order.modified_on > order.modified_on # Make sure all non handled attributes is preserved from original order assert edited_order.creator == order.creator assert edited_order.ip_address == order.ip_address assert edited_order.orderer == order.orderer assert edited_order.customer_comment == order.customer_comment assert edited_order.marketing_permission == order.marketing_permission assert edited_order.order_date == order.order_date assert edited_order.status == order.status assert edited_order.payment_data == order.payment_data assert edited_order.shipping_data == order.shipping_data assert edited_order.extra_data == order.extra_data
def test_contact_set_is_active(rf, admin_user): get_default_shop() contact = create_random_person(locale="en_US", minimum_name_comp_len=5) assert contact.is_active request = apply_request_middleware(rf.post("/", {"set_is_active": "0"}), user=admin_user) view_func = ContactDetailView.as_view() response = view_func(request, pk=contact.pk) assert response.status_code < 500 and not Contact.objects.get(pk=contact.pk).is_active request = apply_request_middleware(rf.post("/", {"set_is_active": "1"}), user=admin_user) view_func = ContactDetailView.as_view() response = view_func(request, pk=contact.pk) assert response.status_code < 500 and Contact.objects.get(pk=contact.pk).is_active
def test_order_set_status_canceled_works(admin_user, rf): order = create_random_order(customer=create_random_person(), products=(get_default_product(), )) assert order.shipping_status == ShippingStatus.NOT_SHIPPED assert order.status.role == OrderStatusRole.INITIAL canceled_status = OrderStatus.objects.get_default_canceled() view = OrderSetStatusView.as_view() request = apply_request_middleware(rf.post("/", {"status": canceled_status.pk}), user=admin_user) response = view(request, pk=order.pk) assert response.status_code < 400 order = Order.objects.get(pk=order.pk) assert order.status_id == canceled_status.id assert order.log_entries.filter(identifier="status_change").exists()