Files
upstream-mattermost/server/public/model/property_value_test.go
upstream-mirror 240db4e2a7
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Nightly Race / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 00:29:55 +05:00

379 lines
9.6 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package model
import (
"encoding/json"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPropertyValue_PreSave(t *testing.T) {
t.Run("sets ID if empty", func(t *testing.T) {
pv := &PropertyValue{}
pv.PreSave()
assert.NotEmpty(t, pv.ID)
assert.Len(t, pv.ID, 26) // Length of NewId()
})
t.Run("keeps existing ID", func(t *testing.T) {
pv := &PropertyValue{ID: "existing_id"}
pv.PreSave()
assert.Equal(t, "existing_id", pv.ID)
})
t.Run("sets CreateAt if zero", func(t *testing.T) {
pv := &PropertyValue{}
pv.PreSave()
assert.NotZero(t, pv.CreateAt)
})
t.Run("sets UpdateAt equal to CreateAt", func(t *testing.T) {
pv := &PropertyValue{}
pv.PreSave()
assert.Equal(t, pv.CreateAt, pv.UpdateAt)
})
}
func TestPropertyValue_IsValid(t *testing.T) {
t.Run("valid value", func(t *testing.T) {
value := json.RawMessage(`{"test": "value"}`)
pv := &PropertyValue{
ID: NewId(),
TargetID: NewId(),
TargetType: "test_type",
GroupID: NewId(),
FieldID: NewId(),
Value: value,
CreateAt: GetMillis(),
UpdateAt: GetMillis(),
}
require.NoError(t, pv.IsValid())
})
t.Run("invalid ID", func(t *testing.T) {
pv := &PropertyValue{
ID: "invalid",
TargetID: NewId(),
TargetType: "test_type",
GroupID: NewId(),
FieldID: NewId(),
CreateAt: GetMillis(),
UpdateAt: GetMillis(),
}
require.Error(t, pv.IsValid())
})
t.Run("invalid TargetID", func(t *testing.T) {
pv := &PropertyValue{
ID: NewId(),
TargetID: "invalid",
TargetType: "test_type",
GroupID: NewId(),
FieldID: NewId(),
CreateAt: GetMillis(),
UpdateAt: GetMillis(),
}
require.Error(t, pv.IsValid())
})
t.Run("empty TargetType", func(t *testing.T) {
pv := &PropertyValue{
ID: NewId(),
TargetID: NewId(),
TargetType: "",
GroupID: NewId(),
FieldID: NewId(),
CreateAt: GetMillis(),
UpdateAt: GetMillis(),
}
require.Error(t, pv.IsValid())
})
t.Run("invalid GroupID", func(t *testing.T) {
pv := &PropertyValue{
ID: NewId(),
TargetID: NewId(),
TargetType: "test_type",
GroupID: "invalid",
FieldID: NewId(),
CreateAt: GetMillis(),
UpdateAt: GetMillis(),
}
require.Error(t, pv.IsValid())
})
t.Run("invalid FieldID", func(t *testing.T) {
pv := &PropertyValue{
ID: NewId(),
TargetID: NewId(),
TargetType: "test_type",
GroupID: NewId(),
FieldID: "invalid",
CreateAt: GetMillis(),
UpdateAt: GetMillis(),
}
require.Error(t, pv.IsValid())
})
t.Run("zero CreateAt", func(t *testing.T) {
pv := &PropertyValue{
ID: NewId(),
TargetID: NewId(),
TargetType: "test_type",
GroupID: NewId(),
FieldID: NewId(),
CreateAt: 0,
UpdateAt: GetMillis(),
}
require.Error(t, pv.IsValid())
})
t.Run("zero UpdateAt", func(t *testing.T) {
pv := &PropertyValue{
ID: NewId(),
TargetID: NewId(),
TargetType: "test_type",
GroupID: NewId(),
FieldID: NewId(),
CreateAt: GetMillis(),
UpdateAt: 0,
}
require.Error(t, pv.IsValid())
})
t.Run("TargetType exceeds maximum length", func(t *testing.T) {
longTargetType := strings.Repeat("a", PropertyValueTargetTypeMaxRunes+1)
pv := &PropertyValue{
ID: NewId(),
TargetID: NewId(),
TargetType: longTargetType,
GroupID: NewId(),
FieldID: NewId(),
CreateAt: GetMillis(),
UpdateAt: GetMillis(),
}
require.Error(t, pv.IsValid())
})
t.Run("TargetType at maximum length is valid", func(t *testing.T) {
maxLengthTargetType := strings.Repeat("a", PropertyValueTargetTypeMaxRunes)
pv := &PropertyValue{
ID: NewId(),
TargetID: NewId(),
TargetType: maxLengthTargetType,
GroupID: NewId(),
FieldID: NewId(),
CreateAt: GetMillis(),
UpdateAt: GetMillis(),
}
require.NoError(t, pv.IsValid())
})
t.Run("system TargetType with system sentinel TargetID is valid", func(t *testing.T) {
pv := &PropertyValue{
ID: NewId(),
TargetID: PropertyValueSystemTargetID,
TargetType: PropertyValueTargetTypeSystem,
GroupID: NewId(),
FieldID: NewId(),
CreateAt: GetMillis(),
UpdateAt: GetMillis(),
}
require.NoError(t, pv.IsValid())
})
t.Run("system TargetType with arbitrary TargetID is invalid", func(t *testing.T) {
pv := &PropertyValue{
ID: NewId(),
TargetID: NewId(),
TargetType: PropertyValueTargetTypeSystem,
GroupID: NewId(),
FieldID: NewId(),
CreateAt: GetMillis(),
UpdateAt: GetMillis(),
}
require.Error(t, pv.IsValid())
})
t.Run("non-system TargetType with system sentinel TargetID is invalid", func(t *testing.T) {
pv := &PropertyValue{
ID: NewId(),
TargetID: PropertyValueSystemTargetID,
TargetType: PropertyValueTargetTypeChannel,
GroupID: NewId(),
FieldID: NewId(),
CreateAt: GetMillis(),
UpdateAt: GetMillis(),
}
require.Error(t, pv.IsValid())
})
}
func TestPropertyValueSearchCursor_IsValid(t *testing.T) {
t.Run("empty cursor is valid", func(t *testing.T) {
cursor := PropertyValueSearchCursor{}
assert.NoError(t, cursor.IsValid())
})
t.Run("valid cursor with CreateAt", func(t *testing.T) {
cursor := PropertyValueSearchCursor{
PropertyValueID: NewId(),
CreateAt: GetMillis(),
}
assert.NoError(t, cursor.IsValid())
})
t.Run("valid cursor with UpdateAt", func(t *testing.T) {
cursor := PropertyValueSearchCursor{
PropertyValueID: NewId(),
UpdateAt: GetMillis(),
}
assert.NoError(t, cursor.IsValid())
})
t.Run("invalid PropertyValueID", func(t *testing.T) {
cursor := PropertyValueSearchCursor{
PropertyValueID: "invalid",
CreateAt: GetMillis(),
}
assert.Error(t, cursor.IsValid())
})
t.Run("neither CreateAt nor UpdateAt set", func(t *testing.T) {
cursor := PropertyValueSearchCursor{
PropertyValueID: NewId(),
}
assert.Error(t, cursor.IsValid())
})
t.Run("both CreateAt and UpdateAt set", func(t *testing.T) {
cursor := PropertyValueSearchCursor{
PropertyValueID: NewId(),
CreateAt: GetMillis(),
UpdateAt: GetMillis(),
}
assert.Error(t, cursor.IsValid())
})
t.Run("negative CreateAt", func(t *testing.T) {
cursor := PropertyValueSearchCursor{
PropertyValueID: NewId(),
CreateAt: -1,
}
assert.Error(t, cursor.IsValid())
})
}
func TestPropertyValueSearchOpts_IsValid(t *testing.T) {
t.Run("empty opts is valid", func(t *testing.T) {
opts := PropertyValueSearchOpts{}
assert.NoError(t, opts.IsValid())
})
t.Run("since without cursor is valid", func(t *testing.T) {
opts := PropertyValueSearchOpts{SinceUpdateAt: 1000}
assert.NoError(t, opts.IsValid())
})
t.Run("delta mode with matching cursor_update_at is valid", func(t *testing.T) {
opts := PropertyValueSearchOpts{
SinceUpdateAt: 1000,
Cursor: PropertyValueSearchCursor{
PropertyValueID: NewId(),
UpdateAt: 1500,
},
}
assert.NoError(t, opts.IsValid())
})
t.Run("directory mode with matching cursor_create_at is valid", func(t *testing.T) {
opts := PropertyValueSearchOpts{
Cursor: PropertyValueSearchCursor{
PropertyValueID: NewId(),
CreateAt: 1500,
},
}
assert.NoError(t, opts.IsValid())
})
t.Run("delta mode with cursor_create_at is invalid", func(t *testing.T) {
opts := PropertyValueSearchOpts{
SinceUpdateAt: 1000,
Cursor: PropertyValueSearchCursor{
PropertyValueID: NewId(),
CreateAt: 1500,
},
}
assert.Error(t, opts.IsValid())
})
t.Run("directory mode with cursor_update_at is invalid", func(t *testing.T) {
opts := PropertyValueSearchOpts{
Cursor: PropertyValueSearchCursor{
PropertyValueID: NewId(),
UpdateAt: 1500,
},
}
assert.Error(t, opts.IsValid())
})
t.Run("invalid cursor surfaces error", func(t *testing.T) {
opts := PropertyValueSearchOpts{
Cursor: PropertyValueSearchCursor{
PropertyValueID: "invalid",
CreateAt: 1500,
},
}
assert.Error(t, opts.IsValid())
})
t.Run("cursor with ID but neither create_at nor update_at is invalid", func(t *testing.T) {
opts := PropertyValueSearchOpts{
Cursor: PropertyValueSearchCursor{
PropertyValueID: NewId(),
},
}
assert.Error(t, opts.IsValid())
})
}
func TestSanitizePropertyValue(t *testing.T) {
cases := []struct {
name string
in string
want string
}{
{"empty bytes", "", ""},
{"string trimmed", `" hello "`, `"hello"`},
{"string unchanged", `"hello"`, `"hello"`},
{"string all whitespace", `" "`, `""`},
{"string already empty", `""`, `""`},
{"string array trimmed and filtered", `[" a ", "", " ", "b"]`, `["a","b"]`},
{"string array unchanged", `["a","b"]`, `["a","b"]`},
{"string array all empty", `["", " ", ""]`, `[]`},
{"number passthrough", `42`, `42`},
{"boolean passthrough", `true`, `true`},
{"null passthrough", `null`, `null`},
{"object passthrough", `{"key":" val "}`, `{"key":" val "}`},
{"nested array passthrough", `[["a","b"]]`, `[["a","b"]]`},
{"mixed array passthrough", `["a",1]`, `["a",1]`},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := SanitizePropertyValue(json.RawMessage(tc.in))
assert.Equal(t, tc.want, string(got))
})
}
t.Run("returns identity when no change", func(t *testing.T) {
raw := json.RawMessage(`"hello"`)
got := SanitizePropertyValue(raw)
assert.Equal(t, &raw[0], &got[0], "expected same backing array when unchanged")
})
}