diff --git a/server/public/model/custom_profile_attributes.go b/server/public/model/custom_profile_attributes.go index 77d0bf44bc6..ba38f73ebbb 100644 --- a/server/public/model/custom_profile_attributes.go +++ b/server/public/model/custom_profile_attributes.go @@ -64,3 +64,20 @@ func (c CustomProfileAttributesSelectOption) IsValid() error { } type CustomProfileAttributesSelectOptions []CustomProfileAttributesSelectOption + +func (c CustomProfileAttributesSelectOptions) IsValid() error { + seenNames := make(map[string]bool) + + for _, option := range c { + if err := option.IsValid(); err != nil { + return err + } + + if seenNames[option.Name] { + return errors.New("duplicate option name found") + } + seenNames[option.Name] = true + } + + return nil +} diff --git a/server/public/model/custom_profile_attributes_test.go b/server/public/model/custom_profile_attributes_test.go index 13b0557f907..906239ebe3a 100644 --- a/server/public/model/custom_profile_attributes_test.go +++ b/server/public/model/custom_profile_attributes_test.go @@ -91,3 +91,76 @@ func TestCustomProfileAttributeSelectOptionIsValid(t *testing.T) { }) } } + +func TestCustomProfileAttributesSelectOptionsIsValid(t *testing.T) { + tests := []struct { + name string + options CustomProfileAttributesSelectOptions + wantErr string + }{ + { + name: "empty options", + options: CustomProfileAttributesSelectOptions{}, + wantErr: "", + }, + { + name: "valid options", + options: CustomProfileAttributesSelectOptions{ + { + ID: NewId(), + Name: "Option 1", + Color: "#FF0000", + }, + { + ID: NewId(), + Name: "Option 2", + Color: "#00FF00", + }, + }, + wantErr: "", + }, + { + name: "invalid option", + options: CustomProfileAttributesSelectOptions{ + { + ID: NewId(), + Name: "Option 1", + Color: "#FF0000", + }, + { + ID: "", + Name: "Option 2", + Color: "#00FF00", + }, + }, + wantErr: "id cannot be empty", + }, + { + name: "duplicate names", + options: CustomProfileAttributesSelectOptions{ + { + ID: NewId(), + Name: "Option 1", + Color: "#FF0000", + }, + { + ID: NewId(), + Name: "Option 1", + Color: "#00FF00", + }, + }, + wantErr: "duplicate option name found", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := tt.options.IsValid() + if tt.wantErr != "" { + assert.EqualError(t, err, tt.wantErr) + } else { + assert.NoError(t, err) + } + }) + } +}