Skip to content

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

Update expiry date validation

IKarbowiakc87441bb1464
diff --git a/saleor/core/utils/validators.py b/saleor/core/utils/validators.py
index 54f1de20df..49a39a4a8d 100644
--- a/saleor/core/utils/validators.py
+++ b/saleor/core/utils/validators.py
@@ -41,6 +41,6 @@ def user_is_valid(user: Optional[User]) -> bool:
return bool(user and not user.is_anonymous)
-def date_passed(given_date):
- """Return true when date has passed."""
- return given_date < date.today()
+def is_date_in_future(given_date):
+ """Return true when the date is in the future."""
+ return given_date > date.today()
diff --git a/saleor/graphql/giftcard/mutations.py b/saleor/graphql/giftcard/mutations.py
index 02494b98e5..5091a5e2cb 100644
--- a/saleor/graphql/giftcard/mutations.py
+++ b/saleor/graphql/giftcard/mutations.py
@@ -7,7 +7,7 @@ from django.core.validators import validate_email
from ...account.models import User
from ...core.permissions import GiftcardPermissions
from ...core.utils.promo_code import generate_promo_code
-from ...core.utils.validators import date_passed, user_is_valid
+from ...core.utils.validators import is_date_in_future, user_is_valid
from ...giftcard import events, models
from ...giftcard.error_codes import GiftCardErrorCode
from ...giftcard.notifications import send_gift_card_notification
@@ -137,11 +137,11 @@ class GiftCardCreate(ModelMutation):
@staticmethod
def clean_expiry_date(cleaned_input, instance):
expiry_date = cleaned_input.get("expiry_date")
- if expiry_date and date_passed(expiry_date):
+ if expiry_date and not is_date_in_future(expiry_date):
raise ValidationError(
{
"expiry_date": ValidationError(
- "Expiry date cannot be in the past.",
+ "Expiry date must be in the future.",
code=GiftCardErrorCode.INVALID.value,
)
}
diff --git a/saleor/graphql/giftcard/tests/mutations/test_gift_card_create.py b/saleor/graphql/giftcard/tests/mutations/test_gift_card_create.py
index b3b7f72230..1652731b1b 100644
--- a/saleor/graphql/giftcard/tests/mutations/test_gift_card_create.py
+++ b/saleor/graphql/giftcard/tests/mutations/test_gift_card_create.py
@@ -1,6 +1,8 @@
from datetime import date, timedelta
from unittest import mock
+import pytest
+
from .....giftcard import GiftCardEvents
from .....giftcard.error_codes import GiftCardErrorCode
from .....giftcard.models import GiftCard
@@ -550,7 +552,9 @@ def test_create_gift_card_with_expiry_date(
)
-def test_create_gift_card_with_expiry_date_type_date_in_past(
[email protected]("date_value", [date(1999, 1, 1), date.today()])
+def test_create_gift_card_with_expiry_date_type_invalid(
+ date_value,
staff_api_client,
customer_user,
permission_manage_gift_card,
@@ -560,7 +564,6 @@ def test_create_gift_card_with_expiry_date_type_date_in_past(
# given
initial_balance = 100
currency = "USD"
- date_value = date(1999, 1, 1)
tag = "gift-card-tag"
variables = {
"input": {