Files
upstream-mattermost/server/public/pluginapi/property_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 Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 00:29:55 +05:00

507 lines
12 KiB
Go

package pluginapi
import (
"encoding/json"
"testing"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/plugin/plugintest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPropertyFieldAPI(t *testing.T) {
t.Run("CreatePropertyField", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
field := &model.PropertyField{
ID: "field1",
GroupID: "group1",
Name: "Test Field",
Type: model.PropertyFieldTypeText,
}
api.On("CreatePropertyField", field).Return(field, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.CreatePropertyField(field)
// Verify the results
assert.NoError(t, err)
assert.Equal(t, field, result)
api.AssertExpectations(t)
})
t.Run("GetPropertyField", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
field := &model.PropertyField{
ID: "field1",
GroupID: "group1",
Name: "Test Field",
Type: model.PropertyFieldTypeText,
}
api.On("GetPropertyField", "group1", "field1").Return(field, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.GetPropertyField("group1", "field1")
// Verify the results
assert.NoError(t, err)
assert.Equal(t, field, result)
api.AssertExpectations(t)
})
t.Run("GetPropertyFields", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
fields := []*model.PropertyField{
{
ID: "field1",
GroupID: "group1",
Name: "Test Field 1",
Type: model.PropertyFieldTypeText,
},
{
ID: "field2",
GroupID: "group1",
Name: "Test Field 2",
Type: model.PropertyFieldTypeSelect,
},
}
api.On("GetPropertyFields", "group1", []string{"field1", "field2"}).Return(fields, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.GetPropertyFields("group1", []string{"field1", "field2"})
// Verify the results
assert.NoError(t, err)
assert.Equal(t, fields, result)
api.AssertExpectations(t)
})
t.Run("UpdatePropertyField", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
field := &model.PropertyField{
ID: "field1",
GroupID: "group1",
Name: "Updated Field",
Type: model.PropertyFieldTypeText,
}
api.On("UpdatePropertyField", "group1", field).Return(field, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.UpdatePropertyField("group1", field)
// Verify the results
assert.NoError(t, err)
assert.Equal(t, field, result)
api.AssertExpectations(t)
})
t.Run("DeletePropertyField", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
api.On("DeletePropertyField", "group1", "field1").Return(nil)
// Create the client
client := NewClient(api, nil)
// Call the method
err := client.Property.DeletePropertyField("group1", "field1")
// Verify the results
assert.NoError(t, err)
api.AssertExpectations(t)
})
t.Run("SearchPropertyFields", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
opts := model.PropertyFieldSearchOpts{
PerPage: 10,
TargetIDs: []string{"target1"},
}
fields := []*model.PropertyField{
{
ID: "field1",
GroupID: "group1",
Name: "Test Field 1",
Type: model.PropertyFieldTypeText,
},
{
ID: "field2",
GroupID: "group1",
Name: "Test Field 2",
Type: model.PropertyFieldTypeSelect,
},
}
api.On("SearchPropertyFields", "group1", opts).Return(fields, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.SearchPropertyFields("group1", opts)
// Verify the results
require.NoError(t, err)
assert.Equal(t, fields, result)
api.AssertExpectations(t)
})
t.Run("CountPropertyFields", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call for active fields only
api.On("CountPropertyFields", "group1", false).Return(int64(5), nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.CountPropertyFields("group1", false)
// Verify the results
require.NoError(t, err)
assert.Equal(t, int64(5), result)
api.AssertExpectations(t)
})
t.Run("CountPropertyFields with deleted", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call for all fields including deleted
api.On("CountPropertyFields", "group1", true).Return(int64(8), nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.CountPropertyFields("group1", true)
// Verify the results
require.NoError(t, err)
assert.Equal(t, int64(8), result)
api.AssertExpectations(t)
})
t.Run("CountPropertyFieldsForTarget", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call for active fields for a specific target
api.On("CountPropertyFieldsForTarget", "group1", "user", "target123", false).Return(int64(3), nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.CountPropertyFieldsForTarget("group1", "user", "target123", false)
// Verify the results
require.NoError(t, err)
assert.Equal(t, int64(3), result)
api.AssertExpectations(t)
})
t.Run("CountPropertyFieldsForTarget with deleted", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call for all fields including deleted for a specific target
api.On("CountPropertyFieldsForTarget", "group1", "user", "target123", true).Return(int64(5), nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.CountPropertyFieldsForTarget("group1", "user", "target123", true)
// Verify the results
require.NoError(t, err)
assert.Equal(t, int64(5), result)
api.AssertExpectations(t)
})
}
func TestPropertyValueAPI(t *testing.T) {
t.Run("CreatePropertyValue", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
value := &model.PropertyValue{
ID: "value1",
GroupID: "group1",
FieldID: "field1",
TargetID: "target1",
TargetType: "post",
Value: json.RawMessage(`"Test Value"`),
}
api.On("CreatePropertyValue", value).Return(value, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.CreatePropertyValue(value)
// Verify the results
assert.NoError(t, err)
assert.Equal(t, value, result)
api.AssertExpectations(t)
})
t.Run("GetPropertyValue", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
value := &model.PropertyValue{
ID: "value1",
GroupID: "group1",
FieldID: "field1",
TargetID: "target1",
TargetType: "post",
Value: json.RawMessage(`"Test Value"`),
}
api.On("GetPropertyValue", "group1", "value1").Return(value, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.GetPropertyValue("group1", "value1")
// Verify the results
assert.NoError(t, err)
assert.Equal(t, value, result)
api.AssertExpectations(t)
})
t.Run("GetPropertyValues", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
values := []*model.PropertyValue{
{
ID: "value1",
GroupID: "group1",
FieldID: "field1",
TargetID: "target1",
TargetType: "post",
Value: json.RawMessage(`"Test Value 1"`),
},
{
ID: "value2",
GroupID: "group1",
FieldID: "field2",
TargetID: "target1",
TargetType: "post",
Value: json.RawMessage(`"Test Value 2"`),
},
}
api.On("GetPropertyValues", "group1", []string{"value1", "value2"}).Return(values, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.GetPropertyValues("group1", []string{"value1", "value2"})
// Verify the results
assert.NoError(t, err)
assert.Equal(t, values, result)
api.AssertExpectations(t)
})
t.Run("UpdatePropertyValue", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
value := &model.PropertyValue{
ID: "value1",
GroupID: "group1",
FieldID: "field1",
TargetID: "target1",
TargetType: "post",
Value: json.RawMessage(`"Updated Value"`),
}
api.On("UpdatePropertyValue", "group1", value).Return(value, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.UpdatePropertyValue("group1", value)
// Verify the results
assert.NoError(t, err)
assert.Equal(t, value, result)
api.AssertExpectations(t)
})
t.Run("UpsertPropertyValue", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
value := &model.PropertyValue{
ID: "value1",
GroupID: "group1",
FieldID: "field1",
TargetID: "target1",
TargetType: "post",
Value: json.RawMessage(`"Upsert Value"`),
}
api.On("UpsertPropertyValue", value).Return(value, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.UpsertPropertyValue(value)
// Verify the results
assert.NoError(t, err)
assert.Equal(t, value, result)
api.AssertExpectations(t)
})
t.Run("DeletePropertyValue", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
api.On("DeletePropertyValue", "group1", "value1").Return(nil)
// Create the client
client := NewClient(api, nil)
// Call the method
err := client.Property.DeletePropertyValue("group1", "value1")
// Verify the results
assert.NoError(t, err)
api.AssertExpectations(t)
})
t.Run("SearchPropertyValues", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
opts := model.PropertyValueSearchOpts{
PerPage: 10,
TargetIDs: []string{"target1"},
}
values := []*model.PropertyValue{
{
ID: "value1",
GroupID: "group1",
FieldID: "field1",
TargetID: "target1",
TargetType: "post",
Value: json.RawMessage(`"Test Value 1"`),
},
{
ID: "value2",
GroupID: "group1",
FieldID: "field2",
TargetID: "target1",
TargetType: "post",
Value: json.RawMessage(`"Test Value 2"`),
},
}
api.On("SearchPropertyValues", "group1", opts).Return(values, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.SearchPropertyValues("group1", opts)
// Verify the results
require.NoError(t, err)
assert.Equal(t, values, result)
api.AssertExpectations(t)
})
}
func TestPropertyGroupAPI(t *testing.T) {
t.Run("RegisterPropertyGroup", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
group := &model.PropertyGroup{
ID: "group1",
Name: "Test Group",
}
api.On("RegisterPropertyGroup", "Test Group").Return(group, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.RegisterPropertyGroup("Test Group")
// Verify the results
assert.NoError(t, err)
assert.Equal(t, group, result)
api.AssertExpectations(t)
})
t.Run("GetPropertyGroup", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
group := &model.PropertyGroup{
ID: "group1",
Name: "Test Group",
}
api.On("GetPropertyGroup", "Test Group").Return(group, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.GetPropertyGroup("Test Group")
// Verify the results
assert.NoError(t, err)
assert.Equal(t, group, result)
api.AssertExpectations(t)
})
}