Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add test for unique code for Voucher and Gift Card

fowczarek0e80addae553
diff --git a/tests/api/test_discount.py b/tests/api/test_discount.py
index 8eb1139e9d..46d61ebd14 100644
--- a/tests/api/test_discount.py
+++ b/tests/api/test_discount.py
@@ -3,11 +3,12 @@ from datetime import date, timedelta
import graphene
import pytest
from django_countries import countries
+from tests.api.utils import get_graphql_content
from saleor.discount import DiscountValueType, VoucherType
from saleor.discount.models import Sale, Voucher
-from saleor.graphql.discount.enums import DiscountValueTypeEnum, VoucherTypeEnum
-from tests.api.utils import get_graphql_content
+from saleor.graphql.discount.enums import (DiscountValueTypeEnum,
+ VoucherTypeEnum)
@pytest.fixture
@@ -217,6 +218,61 @@ def test_create_voucher_with_empty_code(staff_api_client, permission_manage_disc
assert data["code"] != ""
+def test_create_voucher_with_existing_gift_card_code(
+ staff_api_client, gift_card, permission_manage_discounts
+):
+ start_date = date(day=1, month=1, year=2018)
+ end_date = date(day=1, month=1, year=2019)
+ variables = {
+ "name": "test voucher",
+ "type": VoucherTypeEnum.VALUE.name,
+ "code": gift_card.code,
+ "discountValueType": DiscountValueTypeEnum.FIXED.name,
+ "discountValue": 10.12,
+ "minAmountSpent": 1.12,
+ "startDate": start_date.isoformat(),
+ "endDate": end_date.isoformat(),
+ }
+
+ response = staff_api_client.post_graphql(
+ CREATE_VOUCHER_MUTATION, variables, permissions=[permission_manage_discounts]
+ )
+ content = get_graphql_content(response)
+ assert content["data"]["voucherCreate"]["errors"]
+ errors = content["data"]["voucherCreate"]["errors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "code"
+ assert errors[0]["message"] == "Voucher with this code is not avaible."
+
+
+def test_create_voucher_with_existing_voucher_code(
+ staff_api_client, voucher_shipping_type, permission_manage_discounts
+):
+ start_date = date(day=1, month=1, year=2018)
+ end_date = date(day=1, month=1, year=2019)
+ variables = {
+ "name": "test voucher",
+ "type": VoucherTypeEnum.VALUE.name,
+ "code": voucher_shipping_type.code,
+ "discountValueType": DiscountValueTypeEnum.FIXED.name,
+ "discountValue": 10.12,
+ "minAmountSpent": 1.12,
+ "startDate": start_date.isoformat(),
+ "endDate": end_date.isoformat(),
+ }
+ response = staff_api_client.post_graphql(
+ CREATE_VOUCHER_MUTATION, variables, permissions=[permission_manage_discounts]
+ )
+ from pprint import pprint
+ content = get_graphql_content(response)
+ pprint(content)
+ assert content["data"]["voucherCreate"]["errors"]
+ errors = content["data"]["voucherCreate"]["errors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "code"
+ assert errors[0]["message"] == "Voucher with this code is not avaible."
+
+
def test_update_voucher(staff_api_client, voucher, permission_manage_discounts):
query = """
mutation voucherUpdate($code: String,
diff --git a/tests/api/test_gift_card.py b/tests/api/test_gift_card.py
index 56ac2463ff..a9f233be6e 100644
--- a/tests/api/test_gift_card.py
+++ b/tests/api/test_gift_card.py
@@ -136,7 +136,7 @@ def test_query_own_gift_cards(user_api_client, gift_card, gift_card_created_by_s
CREATE_GIFT_CARD_MUTATION = """
mutation giftCardCreate(
$code: String, $startDate: Date, $expirationDate: Date,
- $balance: Decimal, $buyerEmail: String!) {
+ $balance: Decimal!, $buyerEmail: String) {
giftCardCreate(input: {
code: $code, startDate: $startDate,
expirationDate: $expirationDate,
@@ -219,6 +219,56 @@ def test_create_gift_card_with_empty_code(
assert data["code"] != ""
+def test_create_gift_card_without_code(staff_api_client, permission_manage_gift_card):
+ start_date = date(day=1, month=1, year=2018)
+ expiration_date = date(day=1, month=1, year=2019)
+ initial_balance = 123
+ variables = {
+ "startDate": start_date.isoformat(),
+ "expirationDate": expiration_date.isoformat(),
+ "balance": initial_balance,
+ "buyerEmail": staff_api_client.user.email,
+ }
+ response = staff_api_client.post_graphql(
+ CREATE_GIFT_CARD_MUTATION, variables, permissions=[permission_manage_gift_card]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["giftCardCreate"]["giftCard"]
+ assert data["code"] != ""
+
+
+def test_create_gift_card_with_existing_voucher_code(
+ staff_api_client, voucher, permission_manage_gift_card
+):
+ initial_balance = 123
+ variables = {"code": voucher.code, "balance": initial_balance}
+ response = staff_api_client.post_graphql(
+ CREATE_GIFT_CARD_MUTATION, variables, permissions=[permission_manage_gift_card]
+ )
+ content = get_graphql_content(response)
+ assert content["data"]["giftCardCreate"]["errors"]
+ errors = content["data"]["giftCardCreate"]["errors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "code"
+ assert errors[0]["message"] == "Gift card with this code is not avaible."
+
+
+def test_create_gift_card_with_existing_gift_card_code(
+ staff_api_client, gift_card, permission_manage_gift_card
+):
+ initial_balance = 123
+ variables = {"code": gift_card.code, "balance": initial_balance}
+ response = staff_api_client.post_graphql(
+ CREATE_GIFT_CARD_MUTATION, variables, permissions=[permission_manage_gift_card]
+ )
+ content = get_graphql_content(response)
+ assert content["data"]["giftCardCreate"]["errors"]
+ errors = content["data"]["giftCardCreate"]["errors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "code"
+ assert errors[0]["message"] == "Gift card with this code is not avaible."
+
+
def test_create_gift_card_without_buyer(staff_api_client, permission_manage_gift_card):
code = "mirumee1"
start_date = date(day=1, month=1, year=2018)
diff --git a/tests/conftest.py b/tests/conftest.py
index 2b7d0ff194..e9cdbedeb0 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -619,7 +619,7 @@ def order_line(order, variant, vatlayer):
@pytest.fixture
def gift_card(customer_user, staff_user):
return GiftCard.objects.create(
- code='mirumee', buyer=customer_user, initial_balance=10,
+ code='mirumee_giftcard', buyer=customer_user, initial_balance=10,
current_balance=10, creator=staff_user)