diff --git a/server/public/model/custom_profile_attributes_test.go b/server/public/model/custom_profile_attributes_test.go index 67ace328766..c2a35a41946 100644 --- a/server/public/model/custom_profile_attributes_test.go +++ b/server/public/model/custom_profile_attributes_test.go @@ -7,8 +7,193 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) +func TestNewCPAFieldFromPropertyField(t *testing.T) { + tests := []struct { + name string + propertyField *PropertyField + wantAttrs CPAAttrs + wantErr bool + }{ + { + name: "valid property field with all attributes", + propertyField: &PropertyField{ + ID: NewId(), + GroupID: CustomProfileAttributesPropertyGroupName, + Name: "Test Field", + Type: PropertyFieldTypeSelect, + Attrs: StringInterface{ + CustomProfileAttributesPropertyAttrsVisibility: CustomProfileAttributesVisibilityAlways, + CustomProfileAttributesPropertyAttrsSortOrder: "1", + CustomProfileAttributesPropertyAttrsValueType: CustomProfileAttributesValueTypeEmail, + PropertyFieldAttributeOptions: []*CustomProfileAttributesSelectOption{ + { + ID: NewId(), + Name: "Option 1", + Color: "#FF0000", + }, + }, + }, + CreateAt: GetMillis(), + UpdateAt: GetMillis(), + }, + wantAttrs: CPAAttrs{ + Visibility: CustomProfileAttributesVisibilityAlways, + SortOrder: "1", + ValueType: CustomProfileAttributesValueTypeEmail, + Options: []*CustomProfileAttributesSelectOption{ + { + ID: "", // ID will be different in each test run + Name: "Option 1", + Color: "#FF0000", + }, + }, + }, + wantErr: false, + }, + { + name: "valid property field with minimal attributes", + propertyField: &PropertyField{ + ID: NewId(), + GroupID: CustomProfileAttributesPropertyGroupName, + Name: "Test Field", + Type: PropertyFieldTypeText, + Attrs: StringInterface{ + CustomProfileAttributesPropertyAttrsVisibility: CustomProfileAttributesVisibilityWhenSet, + CustomProfileAttributesPropertyAttrsSortOrder: "2", + }, + CreateAt: GetMillis(), + UpdateAt: GetMillis(), + }, + wantAttrs: CPAAttrs{ + Visibility: CustomProfileAttributesVisibilityWhenSet, + SortOrder: "2", + ValueType: "", + Options: nil, + }, + wantErr: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cpaField, err := NewCPAFieldFromPropertyField(tt.propertyField) + + if tt.wantErr { + require.Error(t, err) + return + } + + require.NoError(t, err) + require.NotNil(t, cpaField) + + // Check that the PropertyField was copied correctly + assert.Equal(t, tt.propertyField.ID, cpaField.ID) + assert.Equal(t, tt.propertyField.GroupID, cpaField.GroupID) + assert.Equal(t, tt.propertyField.Name, cpaField.Name) + assert.Equal(t, tt.propertyField.Type, cpaField.Type) + + // Check that the attributes were parsed correctly + assert.Equal(t, tt.wantAttrs.Visibility, cpaField.Attrs.Visibility) + assert.Equal(t, tt.wantAttrs.SortOrder, cpaField.Attrs.SortOrder) + assert.Equal(t, tt.wantAttrs.ValueType, cpaField.Attrs.ValueType) + + // For options, we need to check length since IDs will be different + if tt.wantAttrs.Options != nil { + require.NotNil(t, cpaField.Attrs.Options) + assert.Equal(t, len(tt.wantAttrs.Options), len(cpaField.Attrs.Options)) + + if len(tt.wantAttrs.Options) > 0 { + assert.Equal(t, tt.wantAttrs.Options[0].Name, cpaField.Attrs.Options[0].Name) + assert.Equal(t, tt.wantAttrs.Options[0].Color, cpaField.Attrs.Options[0].Color) + } + } else { + assert.Nil(t, cpaField.Attrs.Options) + } + }) + } +} + +func TestCPAFieldToPropertyField(t *testing.T) { + tests := []struct { + name string + cpaField *CPAField + }{ + { + name: "convert CPA field with all attributes", + cpaField: &CPAField{ + PropertyField: PropertyField{ + ID: NewId(), + GroupID: CustomProfileAttributesPropertyGroupName, + Name: "Test Field", + Type: PropertyFieldTypeSelect, + CreateAt: GetMillis(), + UpdateAt: GetMillis(), + }, + Attrs: CPAAttrs{ + Visibility: CustomProfileAttributesVisibilityAlways, + SortOrder: "1", + ValueType: CustomProfileAttributesValueTypeEmail, + Options: []*CustomProfileAttributesSelectOption{ + { + ID: NewId(), + Name: "Option 1", + Color: "#FF0000", + }, + }, + }, + }, + }, + { + name: "convert CPA field with minimal attributes", + cpaField: &CPAField{ + PropertyField: PropertyField{ + ID: NewId(), + GroupID: CustomProfileAttributesPropertyGroupName, + Name: "Test Field", + Type: PropertyFieldTypeText, + CreateAt: GetMillis(), + UpdateAt: GetMillis(), + }, + Attrs: CPAAttrs{ + Visibility: CustomProfileAttributesVisibilityWhenSet, + SortOrder: "2", + }, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + pf := tt.cpaField.ToPropertyField() + + require.NotNil(t, pf) + + // Check that the PropertyField was copied correctly + assert.Equal(t, tt.cpaField.ID, pf.ID) + assert.Equal(t, tt.cpaField.GroupID, pf.GroupID) + assert.Equal(t, tt.cpaField.Name, pf.Name) + assert.Equal(t, tt.cpaField.Type, pf.Type) + + // Check that the attributes were converted correctly + assert.Equal(t, tt.cpaField.Attrs.Visibility, pf.Attrs[CustomProfileAttributesPropertyAttrsVisibility]) + assert.Equal(t, tt.cpaField.Attrs.SortOrder, pf.Attrs[CustomProfileAttributesPropertyAttrsSortOrder]) + assert.Equal(t, tt.cpaField.Attrs.ValueType, pf.Attrs[CustomProfileAttributesPropertyAttrsValueType]) + + // Check options + options, ok := pf.Attrs[PropertyFieldAttributeOptions] + if tt.cpaField.Attrs.Options != nil { + require.True(t, ok) + optionsSlice, ok := options.(PropertyOptions[*CustomProfileAttributesSelectOption]) + require.True(t, ok) + assert.Equal(t, len(tt.cpaField.Attrs.Options), len(optionsSlice)) + } + }) + } +} + func TestCustomProfileAttributeSelectOptionIsValid(t *testing.T) { tests := []struct { name string