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 Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
357 lines
9.8 KiB
Go
357 lines
9.8 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package model
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestIsValidPropertyOwnerType(t *testing.T) {
|
|
for _, ownerType := range []string{
|
|
PropertyOwnerTypePlugin,
|
|
PropertyOwnerTypeService,
|
|
PropertyOwnerTypeRole,
|
|
PropertyOwnerTypeUser,
|
|
} {
|
|
assert.True(t, IsValidPropertyOwnerType(ownerType), ownerType)
|
|
}
|
|
assert.False(t, IsValidPropertyOwnerType(""))
|
|
assert.False(t, IsValidPropertyOwnerType("bogus"))
|
|
}
|
|
|
|
func TestIsValidPropertyOwnerScope(t *testing.T) {
|
|
for _, scope := range []string{
|
|
"entra",
|
|
"Okta",
|
|
"entra:tenant-1",
|
|
"a.b_c-d",
|
|
"ldap",
|
|
} {
|
|
assert.True(t, IsValidPropertyOwnerScope(scope), scope)
|
|
}
|
|
|
|
for _, scope := range []string{
|
|
"",
|
|
" spaces",
|
|
"a b",
|
|
"a\nb",
|
|
"🔥",
|
|
"<script>",
|
|
} {
|
|
assert.False(t, IsValidPropertyOwnerScope(scope), scope)
|
|
}
|
|
}
|
|
|
|
func TestGetPropertyFieldOwners(t *testing.T) {
|
|
t.Run("returns nil when no attrs or no owners", func(t *testing.T) {
|
|
assert.Nil(t, GetPropertyFieldOwners(nil))
|
|
assert.Nil(t, GetPropertyFieldOwners(&PropertyField{}))
|
|
assert.False(t, HasPropertyFieldOwners(&PropertyField{}))
|
|
})
|
|
|
|
t.Run("reads the typed []PropertyOwner shape", func(t *testing.T) {
|
|
field := &PropertyField{Attrs: StringInterface{
|
|
PropertyAttrsOwners: []PropertyOwner{
|
|
{ID: "com.mattermost.scim", Type: PropertyOwnerTypePlugin, Scopes: []string{"entra"}},
|
|
},
|
|
}}
|
|
owners := GetPropertyFieldOwners(field)
|
|
require.Len(t, owners, 1)
|
|
assert.Equal(t, "com.mattermost.scim", owners[0].ID)
|
|
assert.True(t, HasPropertyFieldOwners(field))
|
|
})
|
|
|
|
t.Run("reads the generic JSON-round-tripped shape", func(t *testing.T) {
|
|
field := &PropertyField{Attrs: StringInterface{
|
|
PropertyAttrsOwners: []any{
|
|
map[string]any{"id": "com.mattermost.scim", "type": "plugin", "scopes": []any{"entra"}},
|
|
},
|
|
}}
|
|
owners := GetPropertyFieldOwners(field)
|
|
require.Len(t, owners, 1)
|
|
assert.Equal(t, "com.mattermost.scim", owners[0].ID)
|
|
assert.Equal(t, PropertyOwnerTypePlugin, owners[0].Type)
|
|
assert.Equal(t, []string{"entra"}, owners[0].Scopes)
|
|
})
|
|
}
|
|
|
|
func TestIsKnownPropertyAccessMode(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
accessMode string
|
|
expected bool
|
|
}{
|
|
{"empty string (public) is valid", PropertyAccessModePublic, true},
|
|
{"source_only is valid", PropertyAccessModeSourceOnly, true},
|
|
{"shared_only is valid", PropertyAccessModeSharedOnly, true},
|
|
{"unknown mode is invalid", "unknown", false},
|
|
{"random string is invalid", "random_mode", false},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := IsKnownPropertyAccessMode(tt.accessMode)
|
|
assert.Equal(t, tt.expected, result)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestIsPropertyFieldProtected(t *testing.T) {
|
|
t.Run("returns true when protected is true", func(t *testing.T) {
|
|
field := &PropertyField{
|
|
ID: NewId(),
|
|
GroupID: "test_group",
|
|
Name: "Protected Field",
|
|
Type: PropertyFieldTypeText,
|
|
Attrs: StringInterface{
|
|
PropertyAttrsProtected: true,
|
|
PropertyAttrsSourcePluginID: "plugin1",
|
|
},
|
|
}
|
|
|
|
require.True(t, IsPropertyFieldProtected(field))
|
|
})
|
|
|
|
t.Run("returns false when protected is false", func(t *testing.T) {
|
|
field := &PropertyField{
|
|
ID: NewId(),
|
|
GroupID: "test_group",
|
|
Name: "Non-Protected Field",
|
|
Type: PropertyFieldTypeText,
|
|
Attrs: StringInterface{
|
|
PropertyAttrsProtected: false,
|
|
PropertyAttrsSourcePluginID: "plugin1",
|
|
},
|
|
}
|
|
|
|
require.False(t, IsPropertyFieldProtected(field))
|
|
})
|
|
|
|
t.Run("returns false when protected is not set", func(t *testing.T) {
|
|
field := &PropertyField{
|
|
ID: NewId(),
|
|
GroupID: "test_group",
|
|
Name: "Field Without Protected",
|
|
Type: PropertyFieldTypeText,
|
|
Attrs: StringInterface{
|
|
PropertyAttrsSourcePluginID: "plugin1",
|
|
},
|
|
}
|
|
|
|
require.False(t, IsPropertyFieldProtected(field))
|
|
})
|
|
|
|
t.Run("returns false when attrs is nil", func(t *testing.T) {
|
|
field := &PropertyField{
|
|
ID: NewId(),
|
|
GroupID: "test_group",
|
|
Name: "Field Without Attrs",
|
|
Type: PropertyFieldTypeText,
|
|
Attrs: nil,
|
|
}
|
|
|
|
require.False(t, IsPropertyFieldProtected(field))
|
|
})
|
|
}
|
|
|
|
func TestPropertyFieldGetAccessMode(t *testing.T) {
|
|
t.Run("nil attrs returns public", func(t *testing.T) {
|
|
f := &PropertyField{Attrs: nil}
|
|
require.Equal(t, PropertyAccessModePublic, f.GetAccessMode())
|
|
})
|
|
t.Run("missing access_mode returns public", func(t *testing.T) {
|
|
f := &PropertyField{Attrs: StringInterface{}}
|
|
require.Equal(t, PropertyAccessModePublic, f.GetAccessMode())
|
|
})
|
|
t.Run("non-string access_mode returns public", func(t *testing.T) {
|
|
f := &PropertyField{Attrs: StringInterface{PropertyAttrsAccessMode: 123}}
|
|
require.Equal(t, PropertyAccessModePublic, f.GetAccessMode())
|
|
})
|
|
t.Run("shared_only returned as-is", func(t *testing.T) {
|
|
f := &PropertyField{Attrs: StringInterface{PropertyAttrsAccessMode: PropertyAccessModeSharedOnly}}
|
|
require.Equal(t, PropertyAccessModeSharedOnly, f.GetAccessMode())
|
|
})
|
|
t.Run("source_only returned as-is", func(t *testing.T) {
|
|
f := &PropertyField{Attrs: StringInterface{PropertyAttrsAccessMode: PropertyAccessModeSourceOnly}}
|
|
require.Equal(t, PropertyAccessModeSourceOnly, f.GetAccessMode())
|
|
})
|
|
}
|
|
|
|
func TestValidatePropertyFieldAccessMode(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
field *PropertyField
|
|
expectError bool
|
|
}{
|
|
{
|
|
name: "valid public (empty string) access mode",
|
|
field: &PropertyField{
|
|
Type: PropertyFieldTypeText,
|
|
Attrs: StringInterface{PropertyAttrsAccessMode: PropertyAccessModePublic},
|
|
},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "valid source_only access mode with protected",
|
|
field: &PropertyField{
|
|
Type: PropertyFieldTypeText,
|
|
Attrs: StringInterface{
|
|
PropertyAttrsAccessMode: PropertyAccessModeSourceOnly,
|
|
PropertyAttrsProtected: true,
|
|
},
|
|
},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "source_only access mode requires protected",
|
|
field: &PropertyField{
|
|
Type: PropertyFieldTypeText,
|
|
Attrs: StringInterface{PropertyAttrsAccessMode: PropertyAccessModeSourceOnly},
|
|
},
|
|
expectError: true,
|
|
},
|
|
{
|
|
name: "valid shared_only access mode with select field and protected",
|
|
field: &PropertyField{
|
|
Type: PropertyFieldTypeSelect,
|
|
Attrs: StringInterface{
|
|
PropertyAttrsAccessMode: PropertyAccessModeSharedOnly,
|
|
PropertyAttrsProtected: true,
|
|
},
|
|
},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "shared_only access mode with select field requires protected",
|
|
field: &PropertyField{
|
|
Type: PropertyFieldTypeSelect,
|
|
Attrs: StringInterface{PropertyAttrsAccessMode: PropertyAccessModeSharedOnly},
|
|
},
|
|
expectError: true,
|
|
},
|
|
{
|
|
name: "valid shared_only access mode with multiselect field and protected",
|
|
field: &PropertyField{
|
|
Type: PropertyFieldTypeMultiselect,
|
|
Attrs: StringInterface{
|
|
PropertyAttrsAccessMode: PropertyAccessModeSharedOnly,
|
|
PropertyAttrsProtected: true,
|
|
},
|
|
},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "shared_only access mode with multiselect field requires protected",
|
|
field: &PropertyField{
|
|
Type: PropertyFieldTypeMultiselect,
|
|
Attrs: StringInterface{PropertyAttrsAccessMode: PropertyAccessModeSharedOnly},
|
|
},
|
|
expectError: true,
|
|
},
|
|
{
|
|
name: "valid shared_only access mode with text field and protected",
|
|
field: &PropertyField{
|
|
Type: PropertyFieldTypeText,
|
|
Attrs: StringInterface{
|
|
PropertyAttrsAccessMode: PropertyAccessModeSharedOnly,
|
|
PropertyAttrsProtected: true,
|
|
},
|
|
},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "valid shared_only access mode with date field and protected",
|
|
field: &PropertyField{
|
|
Type: PropertyFieldTypeDate,
|
|
Attrs: StringInterface{
|
|
PropertyAttrsAccessMode: PropertyAccessModeSharedOnly,
|
|
PropertyAttrsProtected: true,
|
|
},
|
|
},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "valid shared_only access mode with user field and protected",
|
|
field: &PropertyField{
|
|
Type: PropertyFieldTypeUser,
|
|
Attrs: StringInterface{
|
|
PropertyAttrsAccessMode: PropertyAccessModeSharedOnly,
|
|
PropertyAttrsProtected: true,
|
|
},
|
|
},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "shared_only access mode with text field requires protected",
|
|
field: &PropertyField{
|
|
Type: PropertyFieldTypeText,
|
|
Attrs: StringInterface{PropertyAttrsAccessMode: PropertyAccessModeSharedOnly},
|
|
},
|
|
expectError: true,
|
|
},
|
|
{
|
|
name: "unknown access mode",
|
|
field: &PropertyField{
|
|
Type: PropertyFieldTypeText,
|
|
Attrs: StringInterface{PropertyAttrsAccessMode: "unknown_mode"},
|
|
},
|
|
expectError: true,
|
|
},
|
|
{
|
|
name: "shared_only rejected with member-writable permission_values",
|
|
field: &PropertyField{
|
|
Type: PropertyFieldTypeSelect,
|
|
Attrs: StringInterface{
|
|
PropertyAttrsAccessMode: PropertyAccessModeSharedOnly,
|
|
PropertyAttrsProtected: true,
|
|
},
|
|
PermissionValues: func() *PermissionLevel { p := PermissionLevelMember; return &p }(),
|
|
},
|
|
expectError: true,
|
|
},
|
|
{
|
|
name: "shared_only accepted with sysadmin permission_values",
|
|
field: &PropertyField{
|
|
Type: PropertyFieldTypeSelect,
|
|
Attrs: StringInterface{
|
|
PropertyAttrsAccessMode: PropertyAccessModeSharedOnly,
|
|
PropertyAttrsProtected: true,
|
|
},
|
|
PermissionValues: func() *PermissionLevel { p := PermissionLevelSysadmin; return &p }(),
|
|
},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "nil attrs should not error",
|
|
field: &PropertyField{
|
|
Type: PropertyFieldTypeText,
|
|
Attrs: nil,
|
|
},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "missing access_mode should not error",
|
|
field: &PropertyField{
|
|
Type: PropertyFieldTypeText,
|
|
Attrs: StringInterface{},
|
|
},
|
|
expectError: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := ValidatePropertyFieldAccessMode(tt.field)
|
|
if tt.expectError {
|
|
require.NotNil(t, err)
|
|
} else {
|
|
require.Nil(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|