API: Fix validation of duplicate customer email addresses

This commit is contained in:
Raphael Michel
2023-08-30 14:25:42 +02:00
parent 2e8447486c
commit 53e84dfb08
2 changed files with 36 additions and 0 deletions

View File

@@ -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)

View File

@@ -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(