diff --git a/src/pretix/api/serializers/organizer.py b/src/pretix/api/serializers/organizer.py index e3cd8c0753..68bdb6bf49 100644 --- a/src/pretix/api/serializers/organizer.py +++ b/src/pretix/api/serializers/organizer.py @@ -94,6 +94,14 @@ class CustomerSerializer(I18nAwareModelSerializer): data['name_parts']['_scheme'] = self.context['request'].organizer.settings.name_scheme return data + def validate_email(self, value): + qs = Customer.objects.filter(organizer=self.context['organizer'], email__iexact=value) + if self.instance and self.instance.pk: + qs = qs.exclude(pk=self.instance.pk) + if qs.exists(): + raise ValidationError(_("An account with this email address is already registered.")) + return value + class CustomerCreateSerializer(CustomerSerializer): send_email = serializers.BooleanField(default=False, required=False, allow_null=True) diff --git a/src/tests/api/test_customers.py b/src/tests/api/test_customers.py index 06d3f76eab..0b0b5bc361 100644 --- a/src/tests/api/test_customers.py +++ b/src/tests/api/test_customers.py @@ -104,6 +104,34 @@ def test_customer_create(token_client, organizer): assert len(djmail.outbox) == 0 +@pytest.mark.django_db +def test_customer_create_email_unique(token_client, organizer): + resp = token_client.post( + '/api/v1/organizers/{}/customers/'.format(organizer.slug), + format='json', + data={ + 'identifier': 'IGNORED', + 'email': 'bar@example.com', + 'password': 'foobar', + 'is_active': True, + 'is_verified': True, + } + ) + assert resp.status_code == 201 + resp = token_client.post( + '/api/v1/organizers/{}/customers/'.format(organizer.slug), + format='json', + data={ + 'identifier': 'IGNORED', + 'email': 'bar@example.com', + 'password': 'foobar', + 'is_active': True, + 'is_verified': True, + } + ) + assert resp.status_code == 400 + + @pytest.mark.django_db def test_customer_create_send_email(token_client, organizer): resp = token_client.post(