custom_profile_fields: Don't allow leading/trailing whitespaces.

Allowing such whitespaces can lead to hard to debug issues e.g. with
ldap sync.
This commit is contained in:
Mateusz Mandera 2020-09-30 22:49:33 +02:00 committed by Tim Abbott
parent bd61b570aa
commit 6e83bcc0d5
2 changed files with 20 additions and 2 deletions

View File

@ -70,6 +70,15 @@ class CreateCustomProfileFieldTest(CustomProfileFieldTestCase):
field = CustomProfileField.objects.get(name="Phone", realm=realm) field = CustomProfileField.objects.get(name="Phone", realm=realm)
self.assertEqual(field.id, field.order) self.assertEqual(field.id, field.order)
data["name"] = "Name "
data["hint"] = "Some name"
data["field_type"] = CustomProfileField.SHORT_TEXT
result = self.client_post("/json/realm/profile_fields", info=data)
self.assert_json_success(result)
field = CustomProfileField.objects.get(name="Name", realm=realm)
self.assertEqual(field.id, field.order)
result = self.client_post("/json/realm/profile_fields", info=data) result = self.client_post("/json/realm/profile_fields", info=data)
self.assert_json_error(result, self.assert_json_error(result,
'A field with that label already exists.') 'A field with that label already exists.')
@ -413,6 +422,15 @@ class UpdateCustomProfileFieldTest(CustomProfileFieldTestCase):
self.assertEqual(field.hint, 'New contact number') self.assertEqual(field.hint, 'New contact number')
self.assertEqual(field.field_type, CustomProfileField.SHORT_TEXT) self.assertEqual(field.field_type, CustomProfileField.SHORT_TEXT)
result = self.client_patch(
f"/json/realm/profile_fields/{field.id}",
info={'name': 'Name ',
'field_type': CustomProfileField.SHORT_TEXT},
)
self.assert_json_success(result)
field.refresh_from_db()
self.assertEqual(field.name, 'Name')
field = CustomProfileField.objects.get(name="Favorite editor", realm=realm) field = CustomProfileField.objects.get(name="Favorite editor", realm=realm)
result = self.client_patch( result = self.client_patch(
f"/json/realm/profile_fields/{field.id}", f"/json/realm/profile_fields/{field.id}",

View File

@ -90,7 +90,7 @@ def validate_custom_profile_field(name: str, hint: str, field_type: int,
@has_request_variables @has_request_variables
def create_realm_custom_profile_field(request: HttpRequest, def create_realm_custom_profile_field(request: HttpRequest,
user_profile: UserProfile, user_profile: UserProfile,
name: str=REQ(default=''), name: str=REQ(default='', converter=lambda x: x.strip()),
hint: str=REQ(default=''), hint: str=REQ(default=''),
field_data: ProfileFieldData=REQ(default={}, field_data: ProfileFieldData=REQ(default={},
converter=orjson.loads), converter=orjson.loads),
@ -133,7 +133,7 @@ def delete_realm_custom_profile_field(request: HttpRequest, user_profile: UserPr
@has_request_variables @has_request_variables
def update_realm_custom_profile_field(request: HttpRequest, user_profile: UserProfile, def update_realm_custom_profile_field(request: HttpRequest, user_profile: UserProfile,
field_id: int, field_id: int,
name: str=REQ(default=''), name: str=REQ(default='', converter=lambda x: x.strip()),
hint: str=REQ(default=''), hint: str=REQ(default=''),
field_data: ProfileFieldData=REQ(default={}, field_data: ProfileFieldData=REQ(default={},
converter=orjson.loads), converter=orjson.loads),