Skip to content

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

Require email in checkout api

Michał Jelonekd5e23c928c4a
diff --git a/saleor/checkout/migrations/0017_auto_20190130_0207.py b/saleor/checkout/migrations/0017_auto_20190130_0207.py
new file mode 100644
index 0000000000..808fbc9cdd
--- /dev/null
+++ b/saleor/checkout/migrations/0017_auto_20190130_0207.py
@@ -0,0 +1,18 @@
+# Generated by Django 2.1.5 on 2019-01-30 08:07
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('checkout', '0016_auto_20190112_0506'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='cart',
+ name='email',
+ field=models.EmailField(max_length=254),
+ ),
+ ]
diff --git a/saleor/checkout/models.py b/saleor/checkout/models.py
index 32a91544e9..1e5f512e09 100644
--- a/saleor/checkout/models.py
+++ b/saleor/checkout/models.py
@@ -41,7 +41,7 @@ class Cart(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL, blank=True, null=True, related_name='carts',
on_delete=models.CASCADE)
- email = models.EmailField(blank=True, default='')
+ email = models.EmailField()
token = models.UUIDField(primary_key=True, default=uuid4, editable=False)
quantity = models.PositiveIntegerField(default=0)
billing_address = models.ForeignKey(
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.py
index 03b21753c7..b7f798c8b6 100644
--- a/saleor/graphql/checkout/mutations.py
+++ b/saleor/graphql/checkout/mutations.py
@@ -1,4 +1,6 @@
import graphene
+from django.core.exceptions import ValidationError
+from django.core.validators import validate_email
from django.db import transaction
from ...checkout import models
@@ -89,13 +91,14 @@ class CheckoutCreateInput(graphene.InputObjectType):
CheckoutLineInput,
description=(
'A list of checkout lines, each containing information about '
- 'an item in the checkout.'))
- email = graphene.String(description='The customer\'s email address.')
+ 'an item in the checkout.'), required=True)
+ email = graphene.String(
+ description='The customer\'s email address.', required=True)
shipping_address = AddressInput(
description=(
'The mailling address to where the checkout will be shipped.'))
billing_address = AddressInput(
- description=('Billing address of the customer.'))
+ description='Billing address of the customer.')
class CheckoutCreate(ModelMutation, I18nMixin):
@@ -404,6 +407,9 @@ class CheckoutEmailUpdate(BaseMutation):
info, checkout_id, errors, 'checkout_id', only_type=Checkout)
if checkout is not None:
checkout.email = email
+ cls.clean_instance(checkout, errors)
+ if errors:
+ return CheckoutEmailUpdate(errors=errors)
checkout.save(update_fields=['email'])
return CheckoutEmailUpdate(checkout=checkout, errors=errors)
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 1c2a04b5d9..f0b6d682cb 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -321,8 +321,8 @@ type CheckoutCreate {
}
input CheckoutCreateInput {
- lines: [CheckoutLineInput]
- email: String
+ lines: [CheckoutLineInput]!
+ email: String!
shippingAddress: AddressInput
billingAddress: AddressInput
}
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.py
index 8bfad3c460..edcf312dc4 100644
--- a/tests/api/test_checkout.py
+++ b/tests/api/test_checkout.py
@@ -65,11 +65,31 @@ def test_checkout_create(api_client, variant, graphql_address_data):
assert new_cart.shipping_address.city == shipping_address['city']
+def test_checkout_create_required_email(api_client, variant):
+ variant_id = graphene.Node.to_global_id('ProductVariant', variant.id)
+ variables = {
+ 'checkoutInput': {
+ 'lines': [{
+ 'quantity': 1,
+ 'variantId': variant_id}],
+ 'email': ''}}
+
+ response = api_client.post_graphql(
+ MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)
+
+ errors = content['data']['checkoutCreate']['errors']
+ assert errors
+ assert errors[0]['field'] == 'email'
+ assert errors[0]['message'] == 'This field cannot be blank.'
+
+
def test_checkout_create_logged_in_customer(
user_api_client, variant):
variant_id = graphene.Node.to_global_id('ProductVariant', variant.id)
variables = {
'checkoutInput': {
+ 'email': user_api_client.user.email,
'lines': [{
'quantity': 1,
'variantId': variant_id}]}}
@@ -86,6 +106,7 @@ def test_checkout_create_logged_in_customer(
assert customer.id == cart_user.id
assert customer.default_shipping_address_id == new_cart.shipping_address_id
assert customer.default_billing_address_id == new_cart.billing_address_id
+ assert customer.email == new_cart.email
def test_checkout_create_logged_in_customer_custom_email(
@@ -120,6 +141,7 @@ def test_checkout_create_logged_in_customer_custom_addresses(
billing_address = graphql_address_data
variables = {
'checkoutInput': {
+ 'email': user_api_client.user.email,
'lines': [{
'quantity': 1,
'variantId': variant_id}],
@@ -578,12 +600,7 @@ def test_checkout_billing_address_update(
assert cart.billing_address.city == billing_address['city']
-def test_checkout_email_update(user_api_client, cart_with_item):
- cart = cart_with_item
- assert not cart.email
- checkout_id = graphene.Node.to_global_id('Checkout', cart.pk)
-
- query = """
+CHECKOUT_EMAIL_UPDATE_MUTATION = """
mutation checkoutEmailUpdate($checkoutId: ID!, $email: String!) {
checkoutEmailUpdate(checkoutId: $checkoutId, email: $email) {
checkout {
@@ -596,11 +613,18 @@ def test_checkout_email_update(user_api_client, cart_with_item):
}
}
}
- """
+"""
+
+def test_checkout_email_update(user_api_client, cart_with_item):
+ cart = cart_with_item
+ assert not cart.email
+ checkout_id = graphene.Node.to_global_id('Checkout', cart.pk)
+
variables = {'checkoutId': checkout_id, 'email': email}
- response = user_api_client.post_graphql(query, variables)
+ response = user_api_client.post_graphql(
+ CHECKOUT_EMAIL_UPDATE_MUTATION, variables)
content = get_graphql_content(response)
data = content['data']['checkoutEmailUpdate']
assert not data['errors']
@@ -608,7 +632,21 @@ def test_checkout_email_update(user_api_client, cart_with_item):
assert cart.email == email
-MUTATION_CHECKOUT_COMPLEATE = """
+def test_checkout_email_update_validation(user_api_client, cart_with_item):
+ checkout_id = graphene.Node.to_global_id('Checkout', cart_with_item.pk)
+ variables = {'checkoutId': checkout_id, 'email': ''}
+
+ response = user_api_client.post_graphql(
+ CHECKOUT_EMAIL_UPDATE_MUTATION, variables)
+ content = get_graphql_content(response)
+
+ errors = content['data']['checkoutEmailUpdate']['errors']
+ assert errors
+ assert errors[0]['field'] == 'email'
+ assert errors[0]['message'] == 'This field cannot be blank.'
+
+
+MUTATION_CHECKOUT_COMPLETE = """
mutation checkoutComplete($checkoutId: ID!) {
checkoutComplete(checkoutId: $checkoutId) {
order {
@@ -645,7 +683,7 @@ def test_checkout_complete(
variables = {'checkoutId': checkout_id}
orders_count = Order.objects.count()
response = user_api_client.post_graphql(
- MUTATION_CHECKOUT_COMPLEATE, variables)
+ MUTATION_CHECKOUT_COMPLETE, variables)
content = get_graphql_content(response)
data = content['data']['checkoutComplete']
assert not data['errors']
@@ -671,7 +709,7 @@ def test_checkout_complete_invalid_checkout_id(user_api_client):
variables = {'checkoutId': checkout_id}
orders_count = Order.objects.count()
response = user_api_client.post_graphql(
- MUTATION_CHECKOUT_COMPLEATE, variables)
+ MUTATION_CHECKOUT_COMPLETE, variables)
content = get_graphql_content(response)
data = content['data']['checkoutComplete']
error_message = 'Couldn\'t resolve to a node: %s' % checkout_id
@@ -691,7 +729,7 @@ def test_checkout_complete_no_payment(
variables = {'checkoutId': checkout_id}
orders_count = Order.objects.count()
response = user_api_client.post_graphql(
- MUTATION_CHECKOUT_COMPLEATE, variables)
+ MUTATION_CHECKOUT_COMPLETE, variables)
content = get_graphql_content(response)
data = content['data']['checkoutComplete']
assert data['errors'][0]['message'] == 'Checkout is not fully paid'
@@ -721,7 +759,7 @@ def test_checkout_complete_insufficient_stock(
variables = {'checkoutId': checkout_id}
orders_count = Order.objects.count()
response = user_api_client.post_graphql(
- MUTATION_CHECKOUT_COMPLEATE, variables)
+ MUTATION_CHECKOUT_COMPLETE, variables)
content = get_graphql_content(response)
data = content['data']['checkoutComplete']
assert data['errors'][0]['message'] == 'Insufficient product stock.'