Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add simple voucher validation
Adam Janik6a698fa5ba08
diff --git a/saleor/graphql/discount/mutations.py b/saleor/graphql/discount/mutations.pyindex 0b10149d46..1d082d2cdf 100644--- a/saleor/graphql/discount/mutations.py+++ b/saleor/graphql/discount/mutations.py@@ -1,10 +1,22 @@import graphene-from ...discount import models+from ...discount import VoucherType, modelsfrom ..core.mutations import ModelDeleteMutation, ModelMutationfrom ..core.types import Decimal+def validate_voucher(voucher_data):+ voucher_type = voucher_data.get('type')+ errors = []+ if voucher_type == VoucherType.PRODUCT:+ if not voucher_data.get('product'):+ errors.append(('product', 'This field is required.'))+ elif voucher_type == VoucherType.CATEGORY:+ if not voucher_data.get('category'):+ errors.append(('category', 'This field is required.'))+ return errors++class VoucherInput(graphene.InputObjectType):type = graphene.String(description='Voucher type: product, category shipping or value.')@@ -38,6 +50,14 @@ class VoucherCreate(ModelMutation):def user_is_allowed(cls, user, input):return user.has_perm('discount.edit_voucher')+ @classmethod+ def clean_input(cls, info, instance, input, errors):+ cleaned_input = super().clean_input(info, instance, input, errors)+ voucher_errors = validate_voucher(cleaned_input)+ for err in voucher_errors:+ cls.add_error(errors=errors, field=err[0], message=err[1])+ return cleaned_input+class VoucherUpdate(ModelMutation):class Arguments:@@ -55,6 +75,14 @@ class VoucherUpdate(ModelMutation):def user_is_allowed(cls, user, input):return user.has_perm('discount.edit_voucher')+ @classmethod+ def clean_input(cls, info, instance, input, errors):+ cleaned_input = super().clean_input(info, instance, input, errors)+ voucher_errors = validate_voucher(cleaned_input)+ for err in voucher_errors:+ cls.add_error(errors=errors, field=err[0], message=err[1])+ return cleaned_input+class VoucherDelete(ModelDeleteMutation):class Arguments:diff --git a/tests/api/test_discount.py b/tests/api/test_discount.pyindex d7b0dcd86d..818b2581ad 100644--- a/tests/api/test_discount.py+++ b/tests/api/test_discount.py@@ -4,11 +4,12 @@ import grapheneimport pytestfrom django.shortcuts import reversefrom tests.utils import get_graphql_content-from .utils import assert_no_permissionfrom saleor.discount import (DiscountValueType, VoucherApplyToProduct, VoucherType)+from .utils import assert_no_permission+def test_voucher_permissions(staff_api_client, staff_group, staff_user, permission_view_voucher):@@ -76,7 +77,7 @@ def test_voucher_query(def test_sale_query(- admin_api_client, sale):+ admin_api_client, sale):query = """query sales{sales(first: 1) {@@ -126,13 +127,13 @@ def test_create_voucher(user_api_client, admin_api_client):}"""variables = json.dumps({- 'name': 'test voucher',- 'type': VoucherType.VALUE,- 'code': 'testcode123',- 'applyTo': VoucherApplyToProduct.ALL_PRODUCTS,- 'discountValueType': DiscountValueType.FIXED,- 'discountValue': '10.12',- 'limit': '1.12'})+ 'name': 'test voucher',+ 'type': VoucherType.VALUE,+ 'code': 'testcode123',+ 'applyTo': VoucherApplyToProduct.ALL_PRODUCTS,+ 'discountValueType': DiscountValueType.FIXED,+ 'discountValue': '10.12',+ 'limit': '1.12'})response = user_api_client.post(reverse('api'), {'query': query, 'variables': variables})assert_no_permission(response)@@ -172,9 +173,9 @@ def test_update_voucher(user_api_client, admin_api_client, voucher):voucher.save()assert voucher.code != 'testcode123'variables = json.dumps({- 'id': graphene.Node.to_global_id('Voucher', voucher.id),- 'code': 'testcode123',- 'discountValueType': DiscountValueType.PERCENTAGE})+ 'id': graphene.Node.to_global_id('Voucher', voucher.id),+ 'code': 'testcode123',+ 'discountValueType': DiscountValueType.PERCENTAGE})response = user_api_client.post(reverse('api'), {'query': query, 'variables': variables})@@ -239,9 +240,9 @@ def test_create_sale(user_api_client, admin_api_client):}"""variables = json.dumps({- 'name': 'test sale',- 'type': DiscountValueType.FIXED,- 'value': '10.12'})+ 'name': 'test sale',+ 'type': DiscountValueType.FIXED,+ 'value': '10.12'})response = user_api_client.post(reverse('api'), {'query': query, 'variables': variables})assert_no_permission(response)@@ -320,3 +321,35 @@ def test_sale_delete_mutation(user_api_client, admin_api_client, sale):assert data['sale']['name'] == sale.namewith pytest.raises(sale._meta.model.DoesNotExist):sale.refresh_from_db()+++def test_validate_voucher(voucher, admin_api_client, product):+ query = """+ mutation voucherUpdate($product_id: ID, $id: ID!, $type: String) {+ voucherUpdate(+ id: $id, input: {product: $product_id, type: $type}) {+ errors {+ field+ message+ }+ voucher {+ product {+ name+ }+ }+ }+ }+ """++ assert not voucher.product+ variables = json.dumps({+ 'type': VoucherType.PRODUCT,+ 'id': graphene.Node.to_global_id('Voucher', voucher.id),+ 'product': graphene.Node.to_global_id('Product', product.id)})++ response = admin_api_client.post(+ reverse('api'), {'query': query, 'variables': variables})+ content = get_graphql_content(response)+ data = content['data']['voucherUpdate']['errors'][0]+ assert data['field'] == 'product'+ assert data['message'] == 'This field is required.'