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
380 lines
12 KiB
Go
380 lines
12 KiB
Go
package model
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestContentFlaggingNotificationSettings_SetDefault(t *testing.T) {
|
|
t.Run("should set default event target mappings", func(t *testing.T) {
|
|
settings := &ContentFlaggingNotificationSettings{}
|
|
settings.SetDefaults()
|
|
|
|
require.Nil(t, settings.IsValid())
|
|
|
|
require.NotNil(t, settings.EventTargetMapping)
|
|
require.Equal(t, []NotificationTarget{TargetReviewers}, settings.EventTargetMapping[EventFlagged])
|
|
require.Equal(t, []NotificationTarget{TargetReviewers}, settings.EventTargetMapping[EventAssigned])
|
|
require.Equal(t, []NotificationTarget{TargetReviewers, TargetAuthor, TargetReporter}, settings.EventTargetMapping[EventContentRemoved])
|
|
require.Equal(t, []NotificationTarget{TargetReviewers, TargetReporter}, settings.EventTargetMapping[EventContentDismissed])
|
|
})
|
|
|
|
t.Run("should not override existing mappings", func(t *testing.T) {
|
|
settings := &ContentFlaggingNotificationSettings{
|
|
EventTargetMapping: map[ContentFlaggingEvent][]NotificationTarget{
|
|
EventFlagged: {TargetReviewers},
|
|
},
|
|
}
|
|
settings.SetDefaults()
|
|
|
|
require.Nil(t, settings.IsValid())
|
|
require.Equal(t, []NotificationTarget{TargetReviewers}, settings.EventTargetMapping[EventFlagged])
|
|
require.Equal(t, []NotificationTarget{TargetReviewers}, settings.EventTargetMapping[EventAssigned])
|
|
})
|
|
}
|
|
|
|
func TestContentFlaggingNotificationSettings_IsValid(t *testing.T) {
|
|
t.Run("should be valid when reviewers are notified for flagged events", func(t *testing.T) {
|
|
settings := &ContentFlaggingNotificationSettings{
|
|
EventTargetMapping: map[ContentFlaggingEvent][]NotificationTarget{
|
|
EventFlagged: {TargetReviewers, TargetAuthor},
|
|
},
|
|
}
|
|
|
|
err := settings.IsValid()
|
|
require.Nil(t, err)
|
|
})
|
|
|
|
t.Run("should be invalid when no targets for flagged events", func(t *testing.T) {
|
|
settings := &ContentFlaggingNotificationSettings{
|
|
EventTargetMapping: map[ContentFlaggingEvent][]NotificationTarget{
|
|
EventFlagged: {},
|
|
},
|
|
}
|
|
|
|
err := settings.IsValid()
|
|
require.NotNil(t, err)
|
|
require.Equal(t, "model.config.is_valid.notification_settings.reviewer_flagged_notification_disabled", err.Id)
|
|
})
|
|
|
|
t.Run("should be invalid when flagged event mapping is nil", func(t *testing.T) {
|
|
settings := &ContentFlaggingNotificationSettings{
|
|
EventTargetMapping: map[ContentFlaggingEvent][]NotificationTarget{},
|
|
}
|
|
|
|
err := settings.IsValid()
|
|
require.NotNil(t, err)
|
|
require.Equal(t, "model.config.is_valid.notification_settings.reviewer_flagged_notification_disabled", err.Id)
|
|
})
|
|
|
|
t.Run("should be invalid when reviewers not included in flagged event targets", func(t *testing.T) {
|
|
settings := &ContentFlaggingNotificationSettings{
|
|
EventTargetMapping: map[ContentFlaggingEvent][]NotificationTarget{
|
|
EventFlagged: {TargetAuthor, TargetReporter},
|
|
},
|
|
}
|
|
|
|
err := settings.IsValid()
|
|
require.NotNil(t, err)
|
|
require.Equal(t, "model.config.is_valid.notification_settings.reviewer_flagged_notification_disabled", err.Id)
|
|
})
|
|
|
|
t.Run("should be invalid when invalid events and targets are specified", func(t *testing.T) {
|
|
settings := &ContentFlaggingNotificationSettings{
|
|
EventTargetMapping: map[ContentFlaggingEvent][]NotificationTarget{
|
|
"invalid_event": {TargetAuthor, TargetReporter},
|
|
},
|
|
}
|
|
|
|
err := settings.IsValid()
|
|
require.NotNil(t, err)
|
|
require.Equal(t, "model.config.is_valid.notification_settings.invalid_event", err.Id)
|
|
|
|
settings = &ContentFlaggingNotificationSettings{
|
|
EventTargetMapping: map[ContentFlaggingEvent][]NotificationTarget{
|
|
EventFlagged: {"invalid_target_1", "invalid_target_2"},
|
|
},
|
|
}
|
|
|
|
err = settings.IsValid()
|
|
require.NotNil(t, err)
|
|
require.Equal(t, "model.config.is_valid.notification_settings.invalid_target", err.Id)
|
|
|
|
settings = &ContentFlaggingNotificationSettings{
|
|
EventTargetMapping: map[ContentFlaggingEvent][]NotificationTarget{
|
|
"invalid_event": {"invalid_target_1", "invalid_target_2"},
|
|
},
|
|
}
|
|
|
|
err = settings.IsValid()
|
|
require.NotNil(t, err)
|
|
require.Equal(t, "model.config.is_valid.notification_settings.invalid_event", err.Id)
|
|
})
|
|
}
|
|
|
|
func TestReviewerSettings_IsValid(t *testing.T) {
|
|
t.Run("should be valid when common reviewers enabled with reviewer IDs", func(t *testing.T) {
|
|
settings := &ReviewSettingsRequest{
|
|
ReviewerSettings: ReviewerSettings{
|
|
CommonReviewers: new(true),
|
|
SystemAdminsAsReviewers: new(false),
|
|
TeamAdminsAsReviewers: new(false),
|
|
},
|
|
ReviewerIDsSettings: ReviewerIDsSettings{
|
|
CommonReviewerIds: []string{"user1", "user2"},
|
|
TeamReviewersSetting: map[string]*TeamReviewerSetting{},
|
|
},
|
|
}
|
|
|
|
err := settings.IsValid()
|
|
require.Nil(t, err)
|
|
})
|
|
|
|
t.Run("should be valid when common reviewers enabled with Additional Reviewers", func(t *testing.T) {
|
|
settings := &ReviewSettingsRequest{
|
|
ReviewerSettings: ReviewerSettings{
|
|
CommonReviewers: new(true),
|
|
SystemAdminsAsReviewers: new(true),
|
|
TeamAdminsAsReviewers: new(false),
|
|
},
|
|
ReviewerIDsSettings: ReviewerIDsSettings{
|
|
CommonReviewerIds: []string{},
|
|
TeamReviewersSetting: map[string]*TeamReviewerSetting{},
|
|
},
|
|
}
|
|
|
|
err := settings.IsValid()
|
|
require.Nil(t, err)
|
|
})
|
|
|
|
t.Run("should be invalid when common reviewers enabled but no reviewers specified", func(t *testing.T) {
|
|
settings := &ReviewSettingsRequest{
|
|
ReviewerSettings: ReviewerSettings{
|
|
CommonReviewers: new(true),
|
|
SystemAdminsAsReviewers: new(false),
|
|
TeamAdminsAsReviewers: new(false),
|
|
},
|
|
ReviewerIDsSettings: ReviewerIDsSettings{
|
|
CommonReviewerIds: []string{},
|
|
TeamReviewersSetting: map[string]*TeamReviewerSetting{},
|
|
},
|
|
}
|
|
|
|
err := settings.IsValid()
|
|
require.NotNil(t, err)
|
|
require.Equal(t, "model.config.is_valid.content_flagging.common_reviewers_not_set.app_error", err.Id)
|
|
})
|
|
|
|
t.Run("should be valid when team reviewers enabled with reviewer IDs", func(t *testing.T) {
|
|
settings := &ReviewSettingsRequest{
|
|
ReviewerSettings: ReviewerSettings{
|
|
CommonReviewers: new(false),
|
|
SystemAdminsAsReviewers: new(false),
|
|
TeamAdminsAsReviewers: new(false),
|
|
},
|
|
ReviewerIDsSettings: ReviewerIDsSettings{
|
|
CommonReviewerIds: []string{},
|
|
TeamReviewersSetting: map[string]*TeamReviewerSetting{
|
|
"team1": {
|
|
Enabled: new(true),
|
|
ReviewerIds: []string{"user1"},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
err := settings.IsValid()
|
|
require.Nil(t, err)
|
|
})
|
|
|
|
t.Run("should be invalid when team reviewers enabled but no reviewer IDs", func(t *testing.T) {
|
|
settings := &ReviewSettingsRequest{
|
|
ReviewerSettings: ReviewerSettings{
|
|
CommonReviewers: new(false),
|
|
SystemAdminsAsReviewers: new(false),
|
|
TeamAdminsAsReviewers: new(false),
|
|
},
|
|
ReviewerIDsSettings: ReviewerIDsSettings{
|
|
CommonReviewerIds: []string{},
|
|
TeamReviewersSetting: map[string]*TeamReviewerSetting{
|
|
"team1": {
|
|
Enabled: new(true),
|
|
ReviewerIds: []string{},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
err := settings.IsValid()
|
|
require.NotNil(t, err)
|
|
require.Equal(t, "model.config.is_valid.content_flagging.team_reviewers_not_set.app_error", err.Id)
|
|
})
|
|
|
|
t.Run("should be valid when team reviewers enabled but no reviewer IDs with Additional Reviewers", func(t *testing.T) {
|
|
settings := &ReviewSettingsRequest{
|
|
ReviewerSettings: ReviewerSettings{
|
|
CommonReviewers: new(false),
|
|
SystemAdminsAsReviewers: new(true),
|
|
TeamAdminsAsReviewers: new(false),
|
|
},
|
|
ReviewerIDsSettings: ReviewerIDsSettings{
|
|
CommonReviewerIds: []string{},
|
|
TeamReviewersSetting: map[string]*TeamReviewerSetting{
|
|
"team1": {
|
|
Enabled: new(true),
|
|
ReviewerIds: []string{},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
err := settings.IsValid()
|
|
require.Nil(t, err)
|
|
})
|
|
}
|
|
|
|
func TestAdditionalContentFlaggingSettings_SetDefault(t *testing.T) {
|
|
t.Run("should not override existing values", func(t *testing.T) {
|
|
customReasons := []string{"Custom reason"}
|
|
settings := &AdditionalContentFlaggingSettings{
|
|
Reasons: &customReasons,
|
|
}
|
|
settings.SetDefaults()
|
|
|
|
require.Nil(t, settings.IsValid())
|
|
require.Equal(t, customReasons, *settings.Reasons)
|
|
})
|
|
}
|
|
|
|
func TestAdditionalContentFlaggingSettings_IsValid(t *testing.T) {
|
|
t.Run("should be valid when reasons are provided", func(t *testing.T) {
|
|
settings := &AdditionalContentFlaggingSettings{
|
|
Reasons: &[]string{"Reason 1", "Reason 2"},
|
|
}
|
|
|
|
err := settings.IsValid()
|
|
require.Nil(t, err)
|
|
})
|
|
|
|
t.Run("should be invalid when reasons are nil", func(t *testing.T) {
|
|
settings := &AdditionalContentFlaggingSettings{
|
|
Reasons: nil,
|
|
}
|
|
|
|
err := settings.IsValid()
|
|
require.NotNil(t, err)
|
|
require.Equal(t, "model.config.is_valid.content_flagging.reasons_not_set.app_error", err.Id)
|
|
})
|
|
|
|
t.Run("should be invalid when reasons are empty", func(t *testing.T) {
|
|
settings := &AdditionalContentFlaggingSettings{
|
|
Reasons: &[]string{},
|
|
}
|
|
|
|
err := settings.IsValid()
|
|
require.NotNil(t, err)
|
|
require.Equal(t, "model.config.is_valid.content_flagging.reasons_not_set.app_error", err.Id)
|
|
})
|
|
}
|
|
|
|
func TestContentFlaggingSettings_SetDefault(t *testing.T) {
|
|
t.Run("should not override existing values", func(t *testing.T) {
|
|
enabled := true
|
|
settings := &ContentFlaggingSettingsRequest{
|
|
ContentFlaggingSettingsBase: ContentFlaggingSettingsBase{
|
|
EnableContentFlagging: &enabled,
|
|
},
|
|
}
|
|
settings.SetDefaults()
|
|
|
|
require.Nil(t, settings.IsValid())
|
|
require.True(t, *settings.EnableContentFlagging)
|
|
})
|
|
}
|
|
|
|
func TestContentFlaggingSettings_IsValid(t *testing.T) {
|
|
t.Run("should be valid when all nested settings are valid", func(t *testing.T) {
|
|
settings := &ContentFlaggingSettings{}
|
|
settings.SetDefaults()
|
|
|
|
err := settings.IsValid()
|
|
require.Nil(t, err)
|
|
})
|
|
|
|
t.Run("should be invalid when notification settings are invalid", func(t *testing.T) {
|
|
settings := &ContentFlaggingSettingsRequest{
|
|
ContentFlaggingSettingsBase: ContentFlaggingSettingsBase{
|
|
NotificationSettings: &ContentFlaggingNotificationSettings{
|
|
EventTargetMapping: map[ContentFlaggingEvent][]NotificationTarget{
|
|
EventFlagged: {},
|
|
},
|
|
},
|
|
AdditionalSettings: &AdditionalContentFlaggingSettings{},
|
|
},
|
|
}
|
|
|
|
err := settings.IsValid()
|
|
require.NotNil(t, err)
|
|
require.Contains(t, err.Id, "notification_settings")
|
|
})
|
|
|
|
t.Run("should be invalid when reviewer settings are invalid", func(t *testing.T) {
|
|
settings := &ContentFlaggingSettingsRequest{
|
|
ContentFlaggingSettingsBase: ContentFlaggingSettingsBase{
|
|
NotificationSettings: &ContentFlaggingNotificationSettings{},
|
|
AdditionalSettings: &AdditionalContentFlaggingSettings{},
|
|
},
|
|
ReviewerSettings: &ReviewSettingsRequest{
|
|
ReviewerSettings: ReviewerSettings{
|
|
CommonReviewers: new(true),
|
|
SystemAdminsAsReviewers: new(false),
|
|
TeamAdminsAsReviewers: new(false),
|
|
},
|
|
ReviewerIDsSettings: ReviewerIDsSettings{
|
|
CommonReviewerIds: []string{},
|
|
TeamReviewersSetting: map[string]*TeamReviewerSetting{},
|
|
},
|
|
},
|
|
}
|
|
settings.NotificationSettings.SetDefaults()
|
|
settings.AdditionalSettings.SetDefaults()
|
|
|
|
err := settings.IsValid()
|
|
require.NotNil(t, err)
|
|
require.Contains(t, err.Id, "common_reviewers_not_set")
|
|
})
|
|
|
|
t.Run("should be invalid when additional settings are invalid", func(t *testing.T) {
|
|
settings := &ContentFlaggingSettingsRequest{
|
|
ContentFlaggingSettingsBase: ContentFlaggingSettingsBase{
|
|
NotificationSettings: &ContentFlaggingNotificationSettings{},
|
|
AdditionalSettings: &AdditionalContentFlaggingSettings{
|
|
Reasons: &[]string{},
|
|
},
|
|
},
|
|
}
|
|
|
|
settings.SetDefaults()
|
|
|
|
err := settings.IsValid()
|
|
require.NotNil(t, err)
|
|
require.Contains(t, err.Id, "reasons_not_set")
|
|
})
|
|
}
|
|
|
|
func TestContentFlaggingConstants(t *testing.T) {
|
|
t.Run("should have correct event constants", func(t *testing.T) {
|
|
require.Equal(t, ContentFlaggingEvent("flagged"), EventFlagged)
|
|
require.Equal(t, ContentFlaggingEvent("assigned"), EventAssigned)
|
|
require.Equal(t, ContentFlaggingEvent("removed"), EventContentRemoved)
|
|
require.Equal(t, ContentFlaggingEvent("dismissed"), EventContentDismissed)
|
|
})
|
|
|
|
t.Run("should have correct target constants", func(t *testing.T) {
|
|
require.Equal(t, NotificationTarget("reviewers"), TargetReviewers)
|
|
require.Equal(t, NotificationTarget("author"), TargetAuthor)
|
|
require.Equal(t, NotificationTarget("reporter"), TargetReporter)
|
|
})
|
|
}
|