From aec3b8d204dd8b4f9308f536e9b5eefcf966f86e Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 2 May 2018 22:30:43 +0300 Subject: Add tests for PushRule.Match and fork glob to make it compatible with the spec --- matrix/pushrules/condition.go | 7 +- matrix/pushrules/condition_eventmatch_test.go | 6 + matrix/pushrules/rule.go | 2 +- matrix/pushrules/rule_test.go | 166 ++++++++++++++++++++++++++ 4 files changed, 178 insertions(+), 3 deletions(-) (limited to 'matrix/pushrules') diff --git a/matrix/pushrules/condition.go b/matrix/pushrules/condition.go index 4d17695..6607323 100644 --- a/matrix/pushrules/condition.go +++ b/matrix/pushrules/condition.go @@ -21,7 +21,7 @@ import ( "strconv" "strings" - "github.com/zyedidia/glob" + "maunium.net/go/gomuks/lib/glob" "maunium.net/go/gomatrix" "maunium.net/go/gomuks/matrix/rooms" ) @@ -82,7 +82,10 @@ func (cond *PushCondition) matchValue(room Room, event *gomatrix.Event) bool { key = key[0:index] } - pattern, _ := glob.Compile(cond.Pattern) + pattern, err := glob.Compile(cond.Pattern) + if err != nil { + return false + } switch key { case "type": diff --git a/matrix/pushrules/condition_eventmatch_test.go b/matrix/pushrules/condition_eventmatch_test.go index 2fcd054..160edd5 100644 --- a/matrix/pushrules/condition_eventmatch_test.go +++ b/matrix/pushrules/condition_eventmatch_test.go @@ -47,6 +47,12 @@ func TestPushCondition_Match_KindEvent_EventType(t *testing.T) { assert.True(t, condition.Match(blankTestRoom, event)) } +func TestPushCondition_Match_KindEvent_EventType_IllegalGlob(t *testing.T) { + condition := newMatchPushCondition("type", "m.room.invalid_glo[b") + event := newFakeEvent("m.room.invalid_glob", map[string]interface{}{}) + assert.False(t, condition.Match(blankTestRoom, event)) +} + func TestPushCondition_Match_KindEvent_Sender_Fail(t *testing.T) { condition := newMatchPushCondition("sender", "@foo:maunium.net") event := newFakeEvent("m.room.foo", map[string]interface{}{}) diff --git a/matrix/pushrules/rule.go b/matrix/pushrules/rule.go index 0caa13d..5c32a05 100644 --- a/matrix/pushrules/rule.go +++ b/matrix/pushrules/rule.go @@ -17,7 +17,7 @@ package pushrules import ( - "github.com/zyedidia/glob" + "maunium.net/go/gomuks/lib/glob" "maunium.net/go/gomatrix" ) diff --git a/matrix/pushrules/rule_test.go b/matrix/pushrules/rule_test.go index e8b56f4..3d3f03c 100644 --- a/matrix/pushrules/rule_test.go +++ b/matrix/pushrules/rule_test.go @@ -15,3 +15,169 @@ // along with this program. If not, see . package pushrules_test + +import ( + "testing" + "github.com/stretchr/testify/assert" + "maunium.net/go/gomuks/matrix/pushrules" +) + +func TestPushRule_Match_Conditions(t *testing.T) { + cond1 := newMatchPushCondition("content.msgtype", "m.emote") + cond2 := newMatchPushCondition("content.body", "*pushrules") + rule := &pushrules.PushRule{ + Type: pushrules.OverrideRule, + Enabled: true, + Conditions: []*pushrules.PushCondition{cond1, cond2}, + } + + event := newFakeEvent("m.room.message", map[string]interface{}{ + "msgtype": "m.emote", + "body": "is testing pushrules", + }) + assert.True(t, rule.Match(blankTestRoom, event)) +} + +func TestPushRule_Match_Conditions_Disabled(t *testing.T) { + cond1 := newMatchPushCondition("content.msgtype", "m.emote") + cond2 := newMatchPushCondition("content.body", "*pushrules") + rule := &pushrules.PushRule{ + Type: pushrules.OverrideRule, + Enabled: false, + Conditions: []*pushrules.PushCondition{cond1, cond2}, + } + + event := newFakeEvent("m.room.message", map[string]interface{}{ + "msgtype": "m.emote", + "body": "is testing pushrules", + }) + assert.False(t, rule.Match(blankTestRoom, event)) +} + +func TestPushRule_Match_Conditions_FailIfOneFails(t *testing.T) { + cond1 := newMatchPushCondition("content.msgtype", "m.emote") + cond2 := newMatchPushCondition("content.body", "*pushrules") + rule := &pushrules.PushRule{ + Type: pushrules.OverrideRule, + Enabled: true, + Conditions: []*pushrules.PushCondition{cond1, cond2}, + } + + event := newFakeEvent("m.room.message", map[string]interface{}{ + "msgtype": "m.text", + "body": "I'm testing pushrules", + }) + assert.False(t, rule.Match(blankTestRoom, event)) +} + +func TestPushRule_Match_Content(t *testing.T) { + rule := &pushrules.PushRule{ + Type: pushrules.ContentRule, + Enabled: true, + Pattern: "is testing*", + } + + event := newFakeEvent("m.room.message", map[string]interface{}{ + "msgtype": "m.emote", + "body": "is testing pushrules", + }) + assert.True(t, rule.Match(blankTestRoom, event)) +} + +func TestPushRule_Match_Content_Fail(t *testing.T) { + rule := &pushrules.PushRule{ + Type: pushrules.ContentRule, + Enabled: true, + Pattern: "is testing*", + } + + event := newFakeEvent("m.room.message", map[string]interface{}{ + "msgtype": "m.emote", + "body": "is not testing pushrules", + }) + assert.False(t, rule.Match(blankTestRoom, event)) +} + +func TestPushRule_Match_Content_ImplicitGlob(t *testing.T) { + rule := &pushrules.PushRule{ + Type: pushrules.ContentRule, + Enabled: true, + Pattern: "testing", + } + + event := newFakeEvent("m.room.message", map[string]interface{}{ + "msgtype": "m.emote", + "body": "is not testing pushrules", + }) + assert.True(t, rule.Match(blankTestRoom, event)) +} + +func TestPushRule_Match_Content_IllegalGlob(t *testing.T) { + rule := &pushrules.PushRule{ + Type: pushrules.ContentRule, + Enabled: true, + Pattern: "this is not a valid glo[b", + } + + event := newFakeEvent("m.room.message", map[string]interface{}{ + "msgtype": "m.emote", + "body": "this is not a valid glob", + }) + assert.False(t, rule.Match(blankTestRoom, event)) +} + +func TestPushRule_Match_Room(t *testing.T) { + rule := &pushrules.PushRule{ + Type: pushrules.RoomRule, + Enabled: true, + RuleID: "!fakeroom:maunium.net", + } + + event := newFakeEvent("m.room.message", map[string]interface{}{}) + assert.True(t, rule.Match(blankTestRoom, event)) +} + +func TestPushRule_Match_Room_Fail(t *testing.T) { + rule := &pushrules.PushRule{ + Type: pushrules.RoomRule, + Enabled: true, + RuleID: "!otherroom:maunium.net", + } + + event := newFakeEvent("m.room.message", map[string]interface{}{}) + assert.False(t, rule.Match(blankTestRoom, event)) +} + + +func TestPushRule_Match_Sender(t *testing.T) { + rule := &pushrules.PushRule{ + Type: pushrules.SenderRule, + Enabled: true, + RuleID: "@tulir:maunium.net", + } + + event := newFakeEvent("m.room.message", map[string]interface{}{}) + assert.True(t, rule.Match(blankTestRoom, event)) +} + +func TestPushRule_Match_Sender_Fail(t *testing.T) { + rule := &pushrules.PushRule{ + Type: pushrules.RoomRule, + Enabled: true, + RuleID: "@someone:matrix.org", + } + + event := newFakeEvent("m.room.message", map[string]interface{}{}) + assert.False(t, rule.Match(blankTestRoom, event)) +} + +func TestPushRule_Match_UnknownTypeAlwaysFail(t *testing.T) { + rule := &pushrules.PushRule{ + Type: pushrules.PushRuleType("foobar"), + Enabled: true, + RuleID: "@someone:matrix.org", + } + + event := newFakeEvent("m.room.message", map[string]interface{}{}) + assert.False(t, rule.Match(blankTestRoom, event)) +} -- cgit v1.2.3