Debian Codegraph @

pushrules/rule_test.go

raw
1 // Copyright (c) 2020 Tulir Asokan
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 package pushrules_test
8
9 import (
10 "github.com/stretchr/testify/assert"
11
12 "maunium.net/go/mautrix/event"
13 "maunium.net/go/mautrix/pushrules"
14
15 "testing"
16 )
17
18 func TestPushRule_Match_Conditions(t *testing.T) {
19 cond1 := newMatchPushCondition("content.msgtype", "m.emote")
20 cond2 := newMatchPushCondition("content.body", "*pushrules")
21 rule := &pushrules.PushRule{
22 Type: pushrules.OverrideRule,
23 Enabled: true,
24 Conditions: []*pushrules.PushCondition{cond1, cond2},
25 }
26
27 evt := newFakeEvent(event.EventMessage, &event.MessageEventContent{
28 MsgType: event.MsgEmote,
29 Body: "is testing pushrules",
30 })
31 assert.True(t, rule.Match(blankTestRoom, evt))
32 }
33
34 func TestPushRule_Match_Conditions_Disabled(t *testing.T) {
35 cond1 := newMatchPushCondition("content.msgtype", "m.emote")
36 cond2 := newMatchPushCondition("content.body", "*pushrules")
37 rule := &pushrules.PushRule{
38 Type: pushrules.OverrideRule,
39 Enabled: false,
40 Conditions: []*pushrules.PushCondition{cond1, cond2},
41 }
42
43 evt := newFakeEvent(event.EventMessage, &event.MessageEventContent{
44 MsgType: event.MsgEmote,
45 Body: "is testing pushrules",
46 })
47 assert.False(t, rule.Match(blankTestRoom, evt))
48 }
49
50 func TestPushRule_Match_Conditions_FailIfOneFails(t *testing.T) {
51 cond1 := newMatchPushCondition("content.msgtype", "m.emote")
52 cond2 := newMatchPushCondition("content.body", "*pushrules")
53 rule := &pushrules.PushRule{
54 Type: pushrules.OverrideRule,
55 Enabled: true,
56 Conditions: []*pushrules.PushCondition{cond1, cond2},
57 }
58
59 evt := newFakeEvent(event.EventMessage, &event.MessageEventContent{
60 MsgType: event.MsgText,
61 Body: "I'm testing pushrules",
62 })
63 assert.False(t, rule.Match(blankTestRoom, evt))
64 }
65
66 func TestPushRule_Match_Content(t *testing.T) {
67 rule := &pushrules.PushRule{
68 Type: pushrules.ContentRule,
69 Enabled: true,
70 Pattern: "is testing*",
71 }
72
73 evt := newFakeEvent(event.EventMessage, &event.MessageEventContent{
74 MsgType: event.MsgEmote,
75 Body: "is testing pushrules",
76 })
77 assert.True(t, rule.Match(blankTestRoom, evt))
78 }
79
80 func TestPushRule_Match_Content_Fail(t *testing.T) {
81 rule := &pushrules.PushRule{
82 Type: pushrules.ContentRule,
83 Enabled: true,
84 Pattern: "is testing*",
85 }
86
87 evt := newFakeEvent(event.EventMessage, &event.MessageEventContent{
88 MsgType: event.MsgEmote,
89 Body: "is not testing pushrules",
90 })
91 assert.False(t, rule.Match(blankTestRoom, evt))
92 }
93
94 func TestPushRule_Match_Content_ImplicitGlob(t *testing.T) {
95 rule := &pushrules.PushRule{
96 Type: pushrules.ContentRule,
97 Enabled: true,
98 Pattern: "testing",
99 }
100
101 evt := newFakeEvent(event.EventMessage, &event.MessageEventContent{
102 MsgType: event.MsgEmote,
103 Body: "is not testing pushrules",
104 })
105 assert.True(t, rule.Match(blankTestRoom, evt))
106 }
107
108 func TestPushRule_Match_Content_IllegalGlob(t *testing.T) {
109 rule := &pushrules.PushRule{
110 Type: pushrules.ContentRule,
111 Enabled: true,
112 Pattern: "this is not a valid glo[b",
113 }
114
115 evt := newFakeEvent(event.EventMessage, &event.MessageEventContent{
116 MsgType: event.MsgEmote,
117 Body: "this is not a valid glob",
118 })
119 assert.False(t, rule.Match(blankTestRoom, evt))
120 }
121
122 func TestPushRule_Match_Room(t *testing.T) {
123 rule := &pushrules.PushRule{
124 Type: pushrules.RoomRule,
125 Enabled: true,
126 RuleID: "!fakeroom:maunium.net",
127 }
128
129 evt := newFakeEvent(event.EventMessage, &struct{}{})
130 assert.True(t, rule.Match(blankTestRoom, evt))
131 }
132
133 func TestPushRule_Match_Room_Fail(t *testing.T) {
134 rule := &pushrules.PushRule{
135 Type: pushrules.RoomRule,
136 Enabled: true,
137 RuleID: "!otherroom:maunium.net",
138 }
139
140 evt := newFakeEvent(event.EventMessage, &struct{}{})
141 assert.False(t, rule.Match(blankTestRoom, evt))
142 }
143
144 func TestPushRule_Match_Sender(t *testing.T) {
145 rule := &pushrules.PushRule{
146 Type: pushrules.SenderRule,
147 Enabled: true,
148 RuleID: "@tulir:maunium.net",
149 }
150
151 evt := newFakeEvent(event.EventMessage, &struct{}{})
152 assert.True(t, rule.Match(blankTestRoom, evt))
153 }
154
155 func TestPushRule_Match_Sender_Fail(t *testing.T) {
156 rule := &pushrules.PushRule{
157 Type: pushrules.RoomRule,
158 Enabled: true,
159 RuleID: "@someone:matrix.org",
160 }
161
162 evt := newFakeEvent(event.EventMessage, &struct{}{})
163 assert.False(t, rule.Match(blankTestRoom, evt))
164 }
165
166 func TestPushRule_Match_UnknownTypeAlwaysFail(t *testing.T) {
167 rule := &pushrules.PushRule{
168 Type: pushrules.PushRuleType("foobar"),
169 Enabled: true,
170 RuleID: "@someone:matrix.org",
171 }
172
173 evt := newFakeEvent(event.EventMessage, &struct{}{})
174 assert.False(t, rule.Match(blankTestRoom, evt))
175 }