api: adapt validation to get/update_or_create (#34619)
This commit is contained in:
parent
b9962e721a
commit
f7e5f2a700
|
@ -474,8 +474,14 @@ class BaseUserSerializer(api_mixins.GetOrCreateModelSerializer,
|
|||
if 'ou' in data and not ou:
|
||||
ou = data['ou']
|
||||
|
||||
get_or_create_fields = self.context['view'].request.GET.getlist('get_or_create')
|
||||
update_or_create_fields = self.context['view'].request.GET.getlist('update_or_create')
|
||||
|
||||
already_used = False
|
||||
if data.get('email') and (not self.instance or data.get('email') != self.instance.email):
|
||||
if ('email' not in get_or_create_fields
|
||||
and 'email' not in update_or_create_fields
|
||||
and data.get('email')
|
||||
and (not self.instance or data.get('email') != self.instance.email)):
|
||||
if app_settings.A2_EMAIL_IS_UNIQUE and qs.filter(email=data['email']).exists():
|
||||
already_used = True
|
||||
if ou and ou.email_is_unique and qs.filter(ou=ou, email=data['email']).exists():
|
||||
|
|
|
@ -1176,6 +1176,32 @@ def test_api_users_get_or_create(settings, app, admin):
|
|||
assert User.objects.get(id=id).last_name == 'Doe'
|
||||
|
||||
|
||||
def test_api_users_get_or_create_email_is_unique(settings, app, admin):
|
||||
settings.A2_EMAIL_IS_UNIQUE = True
|
||||
app.authorization = ('Basic', (admin.username, admin.username))
|
||||
# test missing first_name
|
||||
payload = {
|
||||
'email': 'john.doe@example.net',
|
||||
'first_name': 'John',
|
||||
'last_name': 'Doe',
|
||||
}
|
||||
resp = app.post_json('/api/users/?get_or_create=email', params=payload, status=201)
|
||||
id = resp.json['id']
|
||||
assert User.objects.get(id=id).first_name == 'John'
|
||||
assert User.objects.get(id=id).last_name == 'Doe'
|
||||
|
||||
resp = app.post_json('/api/users/?get_or_create=email', params=payload, status=201)
|
||||
assert id == resp.json['id']
|
||||
assert User.objects.get(id=id).first_name == 'John'
|
||||
assert User.objects.get(id=id).last_name == 'Doe'
|
||||
|
||||
payload['first_name'] = 'Jane'
|
||||
resp = app.post_json('/api/users/?update_or_create=email', params=payload, status=201)
|
||||
assert id == resp.json['id']
|
||||
assert User.objects.get(id=id).first_name == 'Jane'
|
||||
assert User.objects.get(id=id).last_name == 'Doe'
|
||||
|
||||
|
||||
def test_api_users_get_or_create_multi_key(settings, app, admin):
|
||||
app.authorization = ('Basic', (admin.username, admin.username))
|
||||
# test missing first_name
|
||||
|
|
Loading…
Reference in New Issue