aboutsummaryrefslogtreecommitdiff
path: root/internal/pushrules/validate_test.go
blob: b276eb55115325f927ed07f6416eeb6883c883ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package pushrules

import (
	"strings"
	"testing"
)

func TestValidateRuleNegatives(t *testing.T) {
	tsts := []struct {
		Name          string
		Kind          Kind
		Rule          Rule
		WantErrString string
	}{
		{"emptyRuleID", OverrideKind, Rule{}, "invalid rule ID"},
		{"invalidKind", Kind("something else"), Rule{}, "invalid rule kind"},
		{"ruleIDBackslash", OverrideKind, Rule{RuleID: "#foo\\:example.com"}, "invalid rule ID"},
		{"noActions", OverrideKind, Rule{}, "missing actions"},
		{"invalidAction", OverrideKind, Rule{Actions: []*Action{{}}}, "invalid rule action kind"},
		{"invalidCondition", OverrideKind, Rule{Conditions: []*Condition{{}}}, "invalid rule condition kind"},
		{"overrideNoCondition", OverrideKind, Rule{}, "missing rule conditions"},
		{"underrideNoCondition", UnderrideKind, Rule{}, "missing rule conditions"},
		{"contentNoPattern", ContentKind, Rule{}, "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)
				}
			}
		})
	}
}