0
0
Fork 0
mirror of https://github.com/matrix-org/dendrite synced 2024-11-16 14:50:52 +01:00
dendrite/internal/pushrules/validate_test.go
Till f47515e38b
Pushrule tweaks, make pattern non-optional on EventMatchCondition (#2918)
This should fix https://github.com/matrix-org/dendrite/issues/2882
(Tested with FluffyChat 1.7.1)
Also adds tests that the predefined push rules (as per the spec) is what
we have in Dendrite.
2022-12-23 12:52:47 +01:00

164 lines
5.3 KiB
Go

package pushrules
import (
"strings"
"testing"
)
func TestValidateRuleNegatives(t *testing.T) {
tsts := []struct {
Name string
Kind Kind
Rule Rule
WantErrString string
}{
{Name: "emptyRuleID", Kind: OverrideKind, Rule: Rule{}, WantErrString: "invalid rule ID"},
{Name: "invalidKind", Kind: Kind("something else"), Rule: Rule{}, WantErrString: "invalid rule kind"},
{Name: "ruleIDBackslash", Kind: OverrideKind, Rule: Rule{RuleID: "#foo\\:example.com"}, WantErrString: "invalid rule ID"},
{Name: "noActions", Kind: OverrideKind, Rule: Rule{}, WantErrString: "missing actions"},
{Name: "invalidAction", Kind: OverrideKind, Rule: Rule{Actions: []*Action{{}}}, WantErrString: "invalid rule action kind"},
{Name: "invalidCondition", Kind: OverrideKind, Rule: Rule{Conditions: []*Condition{{}}}, WantErrString: "invalid rule condition kind"},
{Name: "overrideNoCondition", Kind: OverrideKind, Rule: Rule{}, WantErrString: "missing rule conditions"},
{Name: "underrideNoCondition", Kind: UnderrideKind, Rule: Rule{}, WantErrString: "missing rule conditions"},
{Name: "contentNoPattern", Kind: ContentKind, Rule: Rule{}, WantErrString: "missing content rule pattern"},
{Name: "contentEmptyPattern", Kind: ContentKind, Rule: Rule{Pattern: pointer("")}, WantErrString: "missing content rule pattern"},
}
for _, tst := range tsts {
t.Run(tst.Name, func(t *testing.T) {
errs := ValidateRule(tst.Kind, &tst.Rule)
var foundErr error
for _, err := range errs {
t.Logf("Got error %#v", err)
if strings.Contains(err.Error(), tst.WantErrString) {
foundErr = err
}
}
if foundErr == nil {
t.Errorf("errs: got %#v, want containing %q", errs, tst.WantErrString)
}
})
}
}
func TestValidateRulePositives(t *testing.T) {
tsts := []struct {
Name string
Kind Kind
Rule Rule
WantNoErrString string
}{
{"invalidKind", OverrideKind, Rule{}, "invalid rule kind"},
{"invalidActionNoActions", OverrideKind, Rule{}, "invalid rule action kind"},
{"invalidConditionNoConditions", OverrideKind, Rule{}, "invalid rule condition kind"},
{"contentNoCondition", ContentKind, Rule{}, "missing rule conditions"},
{"roomNoCondition", RoomKind, Rule{}, "missing rule conditions"},
{"senderNoCondition", SenderKind, Rule{}, "missing rule conditions"},
{"overrideNoPattern", OverrideKind, Rule{}, "missing content rule pattern"},
{"overrideEmptyConditions", OverrideKind, Rule{Conditions: []*Condition{}}, "missing rule conditions"},
}
for _, tst := range tsts {
t.Run(tst.Name, func(t *testing.T) {
errs := ValidateRule(tst.Kind, &tst.Rule)
for _, err := range errs {
t.Logf("Got error %#v", err)
if strings.Contains(err.Error(), tst.WantNoErrString) {
t.Errorf("errs: got %#v, want none containing %q", errs, tst.WantNoErrString)
}
}
})
}
}
func TestValidateActionNegatives(t *testing.T) {
tsts := []struct {
Name string
Action Action
WantErrString string
}{
{"emptyKind", Action{}, "invalid rule action kind"},
{"invalidKind", Action{Kind: ActionKind("something else")}, "invalid rule action kind"},
}
for _, tst := range tsts {
t.Run(tst.Name, func(t *testing.T) {
errs := validateAction(&tst.Action)
var foundErr error
for _, err := range errs {
t.Logf("Got error %#v", err)
if strings.Contains(err.Error(), tst.WantErrString) {
foundErr = err
}
}
if foundErr == nil {
t.Errorf("errs: got %#v, want containing %q", errs, tst.WantErrString)
}
})
}
}
func TestValidateActionPositives(t *testing.T) {
tsts := []struct {
Name string
Action Action
WantNoErrString string
}{
{"invalidKind", Action{Kind: NotifyAction}, "invalid rule action kind"},
}
for _, tst := range tsts {
t.Run(tst.Name, func(t *testing.T) {
errs := validateAction(&tst.Action)
for _, err := range errs {
t.Logf("Got error %#v", err)
if strings.Contains(err.Error(), tst.WantNoErrString) {
t.Errorf("errs: got %#v, want none containing %q", errs, tst.WantNoErrString)
}
}
})
}
}
func TestValidateConditionNegatives(t *testing.T) {
tsts := []struct {
Name string
Condition Condition
WantErrString string
}{
{"emptyKind", Condition{}, "invalid rule condition kind"},
{"invalidKind", Condition{Kind: ConditionKind("something else")}, "invalid rule condition kind"},
}
for _, tst := range tsts {
t.Run(tst.Name, func(t *testing.T) {
errs := validateCondition(&tst.Condition)
var foundErr error
for _, err := range errs {
t.Logf("Got error %#v", err)
if strings.Contains(err.Error(), tst.WantErrString) {
foundErr = err
}
}
if foundErr == nil {
t.Errorf("errs: got %#v, want containing %q", errs, tst.WantErrString)
}
})
}
}
func TestValidateConditionPositives(t *testing.T) {
tsts := []struct {
Name string
Condition Condition
WantNoErrString string
}{
{"invalidKind", Condition{Kind: EventMatchCondition}, "invalid rule condition kind"},
}
for _, tst := range tsts {
t.Run(tst.Name, func(t *testing.T) {
errs := validateCondition(&tst.Condition)
for _, err := range errs {
t.Logf("Got error %#v", err)
if strings.Contains(err.Error(), tst.WantNoErrString) {
t.Errorf("errs: got %#v, want none containing %q", errs, tst.WantNoErrString)
}
}
})
}
}