API: Trust discounts assigned by pretixPOS, do not assign differently (#5531)

This commit is contained in:
Raphael Michel
2025-10-21 18:35:06 +02:00
committed by GitHub
parent 5563183255
commit 40db7d939f
3 changed files with 148 additions and 14 deletions

View File

@@ -3134,3 +3134,128 @@ def test_order_create_create_medium(token_client, organizer, event, item, quota,
m = organizer.reusable_media.get(identifier=i)
assert m.linked_orderposition == o.positions.first()
assert m.type == "barcode"
@pytest.mark.django_db
def test_order_create_auto_pricing_discount(token_client, organizer, event, item, quota, question, taxrule):
with scopes_disabled():
event.discounts.create(
condition_min_count=2,
benefit_discount_matching_percent=50,
benefit_only_apply_to_cheapest_n_matches=1,
)
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)
res['positions'][0]['item'] = item.pk
res['positions'][0]['answers'][0]['question'] = question.pk
del res['positions'][0]['positionid']
del res['positions'][0]['price']
res['positions'].append(dict(res['positions'][0]))
resp = token_client.post(
'/api/v1/organizers/{}/events/{}/orders/'.format(
organizer.slug, event.slug
), format='json', data=res
)
assert resp.status_code == 201
with scopes_disabled():
o = Order.objects.get(code=resp.data['code'])
p1 = o.positions.first()
p2 = o.positions.last()
assert p1.price == Decimal('23')
assert p2.price == Decimal('11.50')
assert o.total == Decimal('34.75')
@pytest.mark.django_db
def test_order_create_auto_pricing_do_not_discount_if_price_explcitly_set(token_client, organizer, event, item, quota, question, taxrule):
with scopes_disabled():
event.discounts.create(
condition_min_count=2,
benefit_discount_matching_percent=50,
benefit_only_apply_to_cheapest_n_matches=1,
)
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)
res['positions'][0]['item'] = item.pk
res['positions'][0]['answers'][0]['question'] = question.pk
del res['positions'][0]['positionid']
res['positions'].append(dict(res['positions'][0]))
resp = token_client.post(
'/api/v1/organizers/{}/events/{}/orders/'.format(
organizer.slug, event.slug
), format='json', data=res
)
assert resp.status_code == 201
with scopes_disabled():
o = Order.objects.get(code=resp.data['code'])
p1 = o.positions.first()
p2 = o.positions.last()
assert p1.price == Decimal('23.00')
assert p2.price == Decimal('23.00')
assert o.total == Decimal('46.25')
@pytest.mark.django_db
def test_order_create_auto_pricing_believe_wrong_discounts_by_client(token_client, organizer, event, item, quota, question, taxrule):
with scopes_disabled():
discount = event.discounts.create(
condition_min_count=2,
benefit_discount_matching_percent=50,
benefit_only_apply_to_cheapest_n_matches=1,
)
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)
res['positions'][0]['item'] = item.pk
res['positions'][0]['answers'][0]['question'] = question.pk
del res['positions'][0]['positionid']
res['positions'].append(dict(res['positions'][0]))
res['positions'][0]['price'] = Decimal("10.00")
res['positions'][1]['price'] = Decimal("7.00")
res['positions'][1]['discount'] = discount.pk
resp = token_client.post(
'/api/v1/organizers/{}/events/{}/orders/'.format(
organizer.slug, event.slug
), format='json', data=res
)
assert resp.status_code == 201
with scopes_disabled():
o = Order.objects.get(code=resp.data['code'])
p1 = o.positions.first()
p2 = o.positions.last()
assert p1.price == Decimal('10.00')
assert p1.discount is None
assert p2.price == Decimal('7.00')
assert p2.discount == discount
assert o.total == Decimal('17.25')
@pytest.mark.django_db
def test_order_create_auto_pricing_explicit_discount_not_allowed(token_client, organizer, event, item, quota, question, taxrule):
with scopes_disabled():
discount = event.discounts.create(
condition_min_count=2,
benefit_discount_matching_percent=50,
benefit_only_apply_to_cheapest_n_matches=1,
)
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)
res['positions'][0]['item'] = item.pk
res['positions'][0]['answers'][0]['question'] = question.pk
del res['positions'][0]['positionid']
del res['positions'][0]['price']
res['positions'].append(dict(res['positions'][0]))
res['positions'][1]['discount'] = discount.pk
resp = token_client.post(
'/api/v1/organizers/{}/events/{}/orders/'.format(
organizer.slug, event.slug
), format='json', data=res
)
assert resp.status_code == 400
assert resp.data == {
"positions": [
{},
{
"discount": ["You can only specify a discount if you do the price computation, but price is not set."]
}
]
}