[MM-62552] Custom Profile Attributes: use json.RawMessage for the value. (#29989)
* refactor: Move property value sanitization to model layer * feat: Add value sanitization for custom profile attributes * refactor: Update custom profile attributes to use json.RawMessage * refactor: Update patchCustomProfileAttribute to handle json.RawMessage directly * refactor: Refactor custom profile attributes handler with improved validation * refactor: Rename `patchCustomProfileAttribute` to `patchCPAValues` * refactor: Replace ReturnJSON with json.NewEncoder and add error logging * feat: Add encoding/json import to property_value.go * refactor: Update property value tests to use json.RawMessage * fix: Convert string value to json.RawMessage in property value test * fix: Convert string literals to json.RawMessage in property value tests * fix: Add missing encoding/json import in custom_profile_attributes.go * fix: Preserve JSON RawMessage type in listCPAValues function * fix: Update custom profile attributes test to use json.RawMessage * feat: Add json import to custom_profile_attributes_test.go * refactor: Update ListCPAValues and PatchCPAValues to use json.RawMessage * refactor: Rename `actualValue` to `updatedValue` in custom profile attributes test * refactor: Improve user permission and audit logging for custom profile attributes patch * refactor: Optimize CPA field lookup by using ListCPAFields() and map * fix: Correct user ID reference in custom profile attributes patch endpoint * refactor: Change patchCPAValues to use map[string]json.RawMessage for results * refactor: format and fix tests * test: Add comprehensive unit tests for sanitizePropertyValue function * test: Add test case for invalid property value type * feat: Use `model.NewId()` to generate valid IDs in custom profile attributes tests * refactor: Replace hardcoded IDs with dynamic variables in custom profile attributes test * refactor: restore variable name * refactor: drop undesired changes * chore: refresh app layers * feat: Update API definition to support string or string array values for custom profile attributes * test: Add test cases for multiselect custom profile attribute values * test: Add tests for multiselect custom profile attribute values * test: Isolate array value test in separate t.Run * test: Add test case for multiselect array values in custom profile attributes * refactor: Move array value test from TestCreateCPAField to TestPatchCPAValue * test: Update custom profile attributes test assertions * test: add test case for handling array values in GetCPAValue * test: Add array value tests for property value store * refactor(store): no need to convert to json the rawmessage * chore: lint * i18n * use model to interface with sqlx * fix: Allow empty strings for text, date, and select profile attributes * refactor: Filter out empty strings in multiselect and multiuser fields * refactor: Update multiuser field sanitization to validate and error on invalid IDs * refactor: Simplify sanitizePropertyValue function with reduced code duplication * fix: Allow empty user ID in custom profile attribute sanitization * refactor: Convert comment-based subtests to nested t.Run in TestSanitizePropertyValue * refactor: Convert comment-based subtests to nested t.Run tests in TestSanitizePropertyValue --------- Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
@@ -4,6 +4,7 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
@@ -191,7 +192,7 @@ func (a *App) GetCPAValue(valueID string) (*model.PropertyValue, *model.AppError
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func (a *App) PatchCPAValue(userID string, fieldID string, value string) (*model.PropertyValue, *model.AppError) {
|
||||
func (a *App) PatchCPAValue(userID string, fieldID string, value json.RawMessage) (*model.PropertyValue, *model.AppError) {
|
||||
groupID, err := a.cpaGroupID()
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("PatchCPAValues", "app.custom_profile_attributes.cpa_group_id.app_error", nil, "", http.StatusInternalServerError).Wrap(err)
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -295,7 +296,7 @@ func TestDeleteCPAField(t *testing.T) {
|
||||
TargetType: "user",
|
||||
GroupID: cpaGroupID,
|
||||
FieldID: createdField.ID,
|
||||
Value: fmt.Sprintf("Value %d", i),
|
||||
Value: json.RawMessage(fmt.Sprintf(`"Value %d"`, i)),
|
||||
}
|
||||
value, err := th.App.Srv().propertyService.CreatePropertyValue(newValue)
|
||||
require.NoError(t, err)
|
||||
@@ -375,7 +376,7 @@ func TestGetCPAValue(t *testing.T) {
|
||||
TargetType: "user",
|
||||
GroupID: model.NewId(),
|
||||
FieldID: fieldID,
|
||||
Value: "Value",
|
||||
Value: json.RawMessage(`"Value"`),
|
||||
}
|
||||
propertyValue, err := th.App.Srv().propertyService.CreatePropertyValue(propertyValue)
|
||||
require.NoError(t, err)
|
||||
@@ -391,7 +392,7 @@ func TestGetCPAValue(t *testing.T) {
|
||||
TargetType: "user",
|
||||
GroupID: cpaGroupID,
|
||||
FieldID: fieldID,
|
||||
Value: "Value",
|
||||
Value: json.RawMessage(`"Value"`),
|
||||
}
|
||||
propertyValue, err := th.App.Srv().propertyService.CreatePropertyValue(propertyValue)
|
||||
require.NoError(t, err)
|
||||
@@ -400,6 +401,33 @@ func TestGetCPAValue(t *testing.T) {
|
||||
require.Nil(t, appErr)
|
||||
require.NotNil(t, pv)
|
||||
})
|
||||
|
||||
t.Run("should handle array values correctly", func(t *testing.T) {
|
||||
arrayField := &model.PropertyField{
|
||||
GroupID: cpaGroupID,
|
||||
Name: model.NewId(),
|
||||
Type: model.PropertyFieldTypeMultiselect,
|
||||
}
|
||||
createdField, err := th.App.Srv().propertyService.CreatePropertyField(arrayField)
|
||||
require.NoError(t, err)
|
||||
|
||||
propertyValue := &model.PropertyValue{
|
||||
TargetID: model.NewId(),
|
||||
TargetType: "user",
|
||||
GroupID: cpaGroupID,
|
||||
FieldID: createdField.ID,
|
||||
Value: json.RawMessage(`["option1", "option2", "option3"]`),
|
||||
}
|
||||
propertyValue, err = th.App.Srv().propertyService.CreatePropertyValue(propertyValue)
|
||||
require.NoError(t, err)
|
||||
|
||||
pv, appErr := th.App.GetCPAValue(propertyValue.ID)
|
||||
require.Nil(t, appErr)
|
||||
require.NotNil(t, pv)
|
||||
var arrayValues []string
|
||||
require.NoError(t, json.Unmarshal(pv.Value, &arrayValues))
|
||||
require.Equal(t, []string{"option1", "option2", "option3"}, arrayValues)
|
||||
})
|
||||
}
|
||||
|
||||
func TestPatchCPAValue(t *testing.T) {
|
||||
@@ -413,7 +441,7 @@ func TestPatchCPAValue(t *testing.T) {
|
||||
|
||||
t.Run("should fail if the field doesn't exist", func(t *testing.T) {
|
||||
invalidFieldID := model.NewId()
|
||||
_, appErr := th.App.PatchCPAValue(model.NewId(), invalidFieldID, "fieldValue")
|
||||
_, appErr := th.App.PatchCPAValue(model.NewId(), invalidFieldID, json.RawMessage(`"fieldValue"`))
|
||||
require.NotNil(t, appErr)
|
||||
})
|
||||
|
||||
@@ -427,18 +455,18 @@ func TestPatchCPAValue(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
userID := model.NewId()
|
||||
patchedValue, appErr := th.App.PatchCPAValue(userID, createdField.ID, "test value")
|
||||
patchedValue, appErr := th.App.PatchCPAValue(userID, createdField.ID, json.RawMessage(`"test value"`))
|
||||
require.Nil(t, appErr)
|
||||
require.NotNil(t, patchedValue)
|
||||
require.Equal(t, "test value", patchedValue.Value)
|
||||
require.Equal(t, json.RawMessage(`"test value"`), patchedValue.Value)
|
||||
require.Equal(t, userID, patchedValue.TargetID)
|
||||
|
||||
t.Run("should correctly patch the CPA property value", func(t *testing.T) {
|
||||
patch2, appErr := th.App.PatchCPAValue(userID, createdField.ID, "new patched value")
|
||||
patch2, appErr := th.App.PatchCPAValue(userID, createdField.ID, json.RawMessage(`"new patched value"`))
|
||||
require.Nil(t, appErr)
|
||||
require.NotNil(t, patch2)
|
||||
require.Equal(t, patchedValue.ID, patch2.ID)
|
||||
require.Equal(t, "new patched value", patch2.Value)
|
||||
require.Equal(t, json.RawMessage(`"new patched value"`), patch2.Value)
|
||||
require.Equal(t, userID, patch2.TargetID)
|
||||
})
|
||||
})
|
||||
@@ -455,8 +483,37 @@ func TestPatchCPAValue(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
userID := model.NewId()
|
||||
patchedValue, appErr := th.App.PatchCPAValue(userID, createdField.ID, "test value")
|
||||
patchedValue, appErr := th.App.PatchCPAValue(userID, createdField.ID, json.RawMessage(`"test value"`))
|
||||
require.NotNil(t, appErr)
|
||||
require.Nil(t, patchedValue)
|
||||
})
|
||||
|
||||
t.Run("should handle array values correctly", func(t *testing.T) {
|
||||
arrayField := &model.PropertyField{
|
||||
GroupID: cpaGroupID,
|
||||
Name: model.NewId(),
|
||||
Type: model.PropertyFieldTypeMultiselect,
|
||||
}
|
||||
createdField, err := th.App.Srv().propertyService.CreatePropertyField(arrayField)
|
||||
require.NoError(t, err)
|
||||
|
||||
userID := model.NewId()
|
||||
patchedValue, appErr := th.App.PatchCPAValue(userID, createdField.ID, json.RawMessage(`["option1", "option2", "option3"]`))
|
||||
require.Nil(t, appErr)
|
||||
require.NotNil(t, patchedValue)
|
||||
var arrayValues []string
|
||||
require.NoError(t, json.Unmarshal(patchedValue.Value, &arrayValues))
|
||||
require.Equal(t, []string{"option1", "option2", "option3"}, arrayValues)
|
||||
require.Equal(t, userID, patchedValue.TargetID)
|
||||
|
||||
// Update array values
|
||||
updatedValue, appErr := th.App.PatchCPAValue(userID, createdField.ID, json.RawMessage(`["newOption1", "newOption2"]`))
|
||||
require.Nil(t, appErr)
|
||||
require.NotNil(t, updatedValue)
|
||||
require.Equal(t, patchedValue.ID, updatedValue.ID)
|
||||
arrayValues = nil
|
||||
require.NoError(t, json.Unmarshal(updatedValue.Value, &arrayValues))
|
||||
require.Equal(t, []string{"newOption1", "newOption2"}, arrayValues)
|
||||
require.Equal(t, userID, updatedValue.TargetID)
|
||||
})
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user