forked from CGM_Public/pretix_original
API: validate payment_info (#5944)
* API: validate payment_info * improve dict-check * Apply suggestions from code review Co-authored-by: Raphael Michel <michel@pretix.eu> --------- Co-authored-by: Raphael Michel <michel@pretix.eu>
This commit is contained in:
committed by
GitHub
parent
876ddf1321
commit
959e926a67
@@ -19,6 +19,7 @@
|
|||||||
# You should have received a copy of the GNU Affero General Public License along with this program. If not, see
|
# You should have received a copy of the GNU Affero General Public License along with this program. If not, see
|
||||||
# <https://www.gnu.org/licenses/>.
|
# <https://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
from collections import Counter, defaultdict
|
from collections import Counter, defaultdict
|
||||||
@@ -1215,6 +1216,18 @@ class OrderCreateSerializer(I18nAwareModelSerializer):
|
|||||||
raise ValidationError('The given payment provider is not known.')
|
raise ValidationError('The given payment provider is not known.')
|
||||||
return pp
|
return pp
|
||||||
|
|
||||||
|
def validate_payment_info(self, info):
|
||||||
|
if info:
|
||||||
|
try:
|
||||||
|
obj = json.loads(info)
|
||||||
|
except ValueError:
|
||||||
|
raise ValidationError('payment_info must be valid JSON.')
|
||||||
|
|
||||||
|
if not isinstance(obj, dict):
|
||||||
|
# only objects are allowed
|
||||||
|
raise ValidationError('payment_info must be a JSON object.')
|
||||||
|
return info
|
||||||
|
|
||||||
def validate_expires(self, expires):
|
def validate_expires(self, expires):
|
||||||
if expires < now():
|
if expires < now():
|
||||||
raise ValidationError('Expiration date must be in the future.')
|
raise ValidationError('Expiration date must be in the future.')
|
||||||
|
|||||||
@@ -895,6 +895,41 @@ def test_order_create_payment_info_optional(token_client, organizer, event, item
|
|||||||
assert json.loads(p.info) == res['payment_info']
|
assert json.loads(p.info) == res['payment_info']
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_order_create_payment_info_valid_object(token_client, organizer, event, item, quota, question):
|
||||||
|
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)
|
||||||
|
res['positions'][0]['item'] = item.pk
|
||||||
|
res['positions'][0]['answers'][0]['question'] = question.pk
|
||||||
|
|
||||||
|
res["payment_info"] = [{"should": "fail"}]
|
||||||
|
resp = token_client.post(
|
||||||
|
'/api/v1/organizers/{}/events/{}/orders/'.format(
|
||||||
|
organizer.slug, event.slug
|
||||||
|
), format='json', data=res
|
||||||
|
)
|
||||||
|
assert resp.status_code == 400
|
||||||
|
|
||||||
|
res['payment_info'] = {
|
||||||
|
'foo': {
|
||||||
|
'bar': [1, 2],
|
||||||
|
'test': False
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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'])
|
||||||
|
|
||||||
|
p = o.payments.first()
|
||||||
|
assert p.provider == "banktransfer"
|
||||||
|
assert p.amount == o.total
|
||||||
|
assert json.loads(p.info) == res['payment_info']
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_order_create_position_secret_optional(token_client, organizer, event, item, quota, question):
|
def test_order_create_position_secret_optional(token_client, organizer, event, item, quota, question):
|
||||||
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)
|
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)
|
||||||
|
|||||||
Reference in New Issue
Block a user