diff options
author | Tulir Asokan <tulir@maunium.net> | 2018-11-14 01:11:40 +0200 |
---|---|---|
committer | Tulir Asokan <tulir@maunium.net> | 2018-11-14 01:11:40 +0200 |
commit | 912bf309d1f102f8f10c96c718784f01901dad4d (patch) | |
tree | d39b4dc6723884d49e6576411f494d56f492df30 /matrix/pushrules | |
parent | a0815a6f3de6ed4063fcdcf6c7af3f946bbf52f8 (diff) |
Fix tests
Diffstat (limited to 'matrix/pushrules')
-rw-r--r-- | matrix/pushrules/condition.go | 3 | ||||
-rw-r--r-- | matrix/pushrules/condition_displayname_test.go | 25 | ||||
-rw-r--r-- | matrix/pushrules/condition_eventmatch_test.go | 33 | ||||
-rw-r--r-- | matrix/pushrules/condition_test.go | 38 | ||||
-rw-r--r-- | matrix/pushrules/pushrules_test.go | 13 | ||||
-rw-r--r-- | matrix/pushrules/rule_array_test.go | 31 | ||||
-rw-r--r-- | matrix/pushrules/rule_test.go | 65 |
7 files changed, 112 insertions, 96 deletions
diff --git a/matrix/pushrules/condition.go b/matrix/pushrules/condition.go index bacab56..d547a3f 100644 --- a/matrix/pushrules/condition.go +++ b/matrix/pushrules/condition.go @@ -112,6 +112,9 @@ func (cond *PushCondition) matchDisplayName(room Room, event *mautrix.Event) boo return false } member := room.GetMember(ownerID) + if member == nil { + return false + } return strings.Contains(event.Content.Body, member.Displayname) } diff --git a/matrix/pushrules/condition_displayname_test.go b/matrix/pushrules/condition_displayname_test.go index e859ff8..0ae85d1 100644 --- a/matrix/pushrules/condition_displayname_test.go +++ b/matrix/pushrules/condition_displayname_test.go @@ -17,42 +17,43 @@ package pushrules_test import ( + "maunium.net/go/mautrix" "testing" "github.com/stretchr/testify/assert" ) func TestPushCondition_Match_DisplayName(t *testing.T) { - event := newFakeEvent("m.room.message", map[string]interface{}{ - "msgtype": "m.text", - "body": "tulir: test mention", + event := newFakeEvent(mautrix.EventMessage, mautrix.Content{ + MsgType: mautrix.MsgText, + Body: "tulir: test mention", }) event.Sender = "@someone_else:matrix.org" assert.True(t, displaynamePushCondition.Match(displaynameTestRoom, event)) } func TestPushCondition_Match_DisplayName_Fail(t *testing.T) { - event := newFakeEvent("m.room.message", map[string]interface{}{ - "msgtype": "m.text", - "body": "not a mention", + event := newFakeEvent(mautrix.EventMessage, mautrix.Content{ + MsgType: mautrix.MsgText, + Body: "not a mention", }) event.Sender = "@someone_else:matrix.org" assert.False(t, displaynamePushCondition.Match(displaynameTestRoom, event)) } func TestPushCondition_Match_DisplayName_CantHighlightSelf(t *testing.T) { - event := newFakeEvent("m.room.message", map[string]interface{}{ - "msgtype": "m.text", - "body": "tulir: I can't highlight myself", + event := newFakeEvent(mautrix.EventMessage, mautrix.Content{ + MsgType: mautrix.MsgText, + Body: "tulir: I can't highlight myself", }) assert.False(t, displaynamePushCondition.Match(displaynameTestRoom, event)) } func TestPushCondition_Match_DisplayName_FailsOnEmptyRoom(t *testing.T) { emptyRoom := newFakeRoom(0) - event := newFakeEvent("m.room.message", map[string]interface{}{ - "msgtype": "m.text", - "body": "tulir: this room doesn't have the owner Member available, so it fails.", + event := newFakeEvent(mautrix.EventMessage, mautrix.Content{ + MsgType: mautrix.MsgText, + Body: "tulir: this room doesn't have the owner Member available, so it fails.", }) event.Sender = "@someone_else:matrix.org" assert.False(t, displaynamePushCondition.Match(emptyRoom, event)) diff --git a/matrix/pushrules/condition_eventmatch_test.go b/matrix/pushrules/condition_eventmatch_test.go index 160edd5..0591656 100644 --- a/matrix/pushrules/condition_eventmatch_test.go +++ b/matrix/pushrules/condition_eventmatch_test.go @@ -17,6 +17,7 @@ package pushrules_test import ( + "maunium.net/go/mautrix" "testing" "github.com/stretchr/testify/assert" @@ -24,9 +25,11 @@ import ( func TestPushCondition_Match_KindEvent_MsgType(t *testing.T) { condition := newMatchPushCondition("content.msgtype", "m.emote") - event := newFakeEvent("m.room.message", map[string]interface{}{ - "msgtype": "m.emote", - "body": "tests gomuks pushconditions", + event := newFakeEvent(mautrix.EventMessage, mautrix.Content{ + Raw: map[string]interface{}{ + "msgtype": "m.emote", + "body": "tests gomuks pushconditions", + }, }) assert.True(t, condition.Match(blankTestRoom, event)) } @@ -34,58 +37,60 @@ func TestPushCondition_Match_KindEvent_MsgType(t *testing.T) { func TestPushCondition_Match_KindEvent_MsgType_Fail(t *testing.T) { condition := newMatchPushCondition("content.msgtype", "m.emote") - event := newFakeEvent("m.room.message", map[string]interface{}{ - "msgtype": "m.text", - "body": "I'm testing gomuks pushconditions", + event := newFakeEvent(mautrix.EventMessage, mautrix.Content{ + Raw: map[string]interface{}{ + "msgtype": "m.text", + "body": "I'm testing gomuks pushconditions", + }, }) assert.False(t, condition.Match(blankTestRoom, event)) } func TestPushCondition_Match_KindEvent_EventType(t *testing.T) { condition := newMatchPushCondition("type", "m.room.foo") - event := newFakeEvent("m.room.foo", map[string]interface{}{}) + event := newFakeEvent(mautrix.NewEventType("m.room.foo"), mautrix.Content{}) 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{}{}) + event := newFakeEvent(mautrix.NewEventType("m.room.invalid_glob"), mautrix.Content{}) 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{}{}) + event := newFakeEvent(mautrix.NewEventType("m.room.foo"), mautrix.Content{}) assert.False(t, condition.Match(blankTestRoom, event)) } func TestPushCondition_Match_KindEvent_RoomID(t *testing.T) { condition := newMatchPushCondition("room_id", "!fakeroom:maunium.net") - event := newFakeEvent("", map[string]interface{}{}) + event := newFakeEvent(mautrix.NewEventType(""), mautrix.Content{}) assert.True(t, condition.Match(blankTestRoom, event)) } func TestPushCondition_Match_KindEvent_BlankStateKey(t *testing.T) { condition := newMatchPushCondition("state_key", "") - event := newFakeEvent("m.room.foo", map[string]interface{}{}) + event := newFakeEvent(mautrix.NewEventType("m.room.foo"), mautrix.Content{}) assert.True(t, condition.Match(blankTestRoom, event)) } func TestPushCondition_Match_KindEvent_BlankStateKey_Fail(t *testing.T) { condition := newMatchPushCondition("state_key", "not blank") - event := newFakeEvent("m.room.foo", map[string]interface{}{}) + event := newFakeEvent(mautrix.NewEventType("m.room.foo"), mautrix.Content{}) assert.False(t, condition.Match(blankTestRoom, event)) } func TestPushCondition_Match_KindEvent_NonBlankStateKey(t *testing.T) { condition := newMatchPushCondition("state_key", "*:maunium.net") - event := newFakeEvent("m.room.foo", map[string]interface{}{}) + event := newFakeEvent(mautrix.NewEventType("m.room.foo"), mautrix.Content{}) event.StateKey = &event.Sender assert.True(t, condition.Match(blankTestRoom, event)) } func TestPushCondition_Match_KindEvent_UnknownKey(t *testing.T) { condition := newMatchPushCondition("non-existent key", "doesn't affect anything") - event := newFakeEvent("m.room.foo", map[string]interface{}{}) + event := newFakeEvent(mautrix.NewEventType("m.room.foo"), mautrix.Content{}) assert.False(t, condition.Match(blankTestRoom, event)) } diff --git a/matrix/pushrules/condition_test.go b/matrix/pushrules/condition_test.go index 750f2c7..4e8e8a1 100644 --- a/matrix/pushrules/condition_test.go +++ b/matrix/pushrules/condition_test.go @@ -40,13 +40,13 @@ func init() { countConditionTestEvent = &mautrix.Event{ Sender: "@tulir:maunium.net", - Type: "m.room.message", + Type: mautrix.EventMessage, Timestamp: 1523791120, ID: "$123:maunium.net", RoomID: "!fakeroom:maunium.net", - Content: map[string]interface{}{ - "msgtype": "m.text", - "body": "test", + Content: mautrix.Content{ + MsgType: mautrix.MsgText, + Body: "test", }, } @@ -56,7 +56,7 @@ func init() { } } -func newFakeEvent(evtType string, content map[string]interface{}) *mautrix.Event { +func newFakeEvent(evtType mautrix.EventType, content mautrix.Content) *mautrix.Event { return &mautrix.Event{ Sender: "@tulir:maunium.net", Type: evtType, @@ -86,49 +86,47 @@ func TestPushCondition_Match_InvalidKind(t *testing.T) { condition := &pushrules.PushCondition{ Kind: pushrules.PushCondKind("invalid"), } - event := newFakeEvent("m.room.foobar", map[string]interface{}{}) + event := newFakeEvent(mautrix.EventType{Type: "m.room.foobar"}, mautrix.Content{}) assert.False(t, condition.Match(blankTestRoom, event)) } type FakeRoom struct { - members map[string]*rooms.Member + members map[string]*mautrix.Member owner string } func newFakeRoom(memberCount int) *FakeRoom { room := &FakeRoom{ owner: "@tulir:maunium.net", - members: make(map[string]*rooms.Member), + members: make(map[string]*mautrix.Member), } if memberCount >= 1 { - room.members["@tulir:maunium.net"] = &rooms.Member{ - UserID: "@tulir:maunium.net", - Membership: rooms.MembershipJoin, - DisplayName: "tulir", + room.members["@tulir:maunium.net"] = &mautrix.Member{ + Membership: mautrix.MembershipJoin, + Displayname: "tulir", } } for i := 0; i < memberCount-1; i++ { mxid := fmt.Sprintf("@extrauser_%d:matrix.org", i) - room.members[mxid] = &rooms.Member{ - UserID: mxid, - Membership: rooms.MembershipJoin, - DisplayName: fmt.Sprintf("Extra User %d", i), + room.members[mxid] = &mautrix.Member{ + Membership: mautrix.MembershipJoin, + Displayname: fmt.Sprintf("Extra User %d", i), } } return room } -func (fr *FakeRoom) GetMember(mxid string) *rooms.Member { +func (fr *FakeRoom) GetMember(mxid string) *mautrix.Member { return fr.members[mxid] } -func (fr *FakeRoom) GetSessionOwner() *rooms.Member { - return fr.members[fr.owner] +func (fr *FakeRoom) GetSessionOwner() string { + return fr.owner } -func (fr *FakeRoom) GetMembers() map[string]*rooms.Member { +func (fr *FakeRoom) GetMembers() map[string]*mautrix.Member { return fr.members } diff --git a/matrix/pushrules/pushrules_test.go b/matrix/pushrules/pushrules_test.go index 73fa787..c56fd10 100644 --- a/matrix/pushrules/pushrules_test.go +++ b/matrix/pushrules/pushrules_test.go @@ -25,18 +25,13 @@ import ( "maunium.net/go/gomuks/matrix/pushrules" ) -var mapExamplePushRules map[string]interface{} - -func init() { - mapExamplePushRules = make(map[string]interface{}) - json.Unmarshal([]byte(JSONExamplePushRules), &mapExamplePushRules) -} - func TestEventToPushRules(t *testing.T) { event := &mautrix.Event{ - Type: "m.push_rules", + Type: mautrix.AccountDataPushRules, Timestamp: 1523380910, - Content: mapExamplePushRules, + Content: mautrix.Content{ + VeryRaw: json.RawMessage(JSONExamplePushRules), + }, } pushRuleset, err := pushrules.EventToPushRules(event) assert.Nil(t, err) diff --git a/matrix/pushrules/rule_array_test.go b/matrix/pushrules/rule_array_test.go index 9396411..5a5bd3d 100644 --- a/matrix/pushrules/rule_array_test.go +++ b/matrix/pushrules/rule_array_test.go @@ -19,6 +19,7 @@ package pushrules_test import ( "github.com/stretchr/testify/assert" "maunium.net/go/gomuks/matrix/pushrules" + "maunium.net/go/mautrix" "testing" ) @@ -61,9 +62,9 @@ func TestPushRuleArray_GetActions_FirstMatchReturns(t *testing.T) { rules := pushrules.PushRuleArray{rule1, rule2, rule3} - event := newFakeEvent("m.room.message", map[string]interface{}{ - "msgtype": "m.emote", - "body": "is testing pushrules", + event := newFakeEvent(mautrix.EventMessage, mautrix.Content{ + MsgType: mautrix.MsgEmote, + Body: "is testing pushrules", }) assert.Equal(t, rules.GetActions(blankTestRoom, event), actions2) } @@ -107,9 +108,9 @@ func TestPushRuleArray_GetActions_NoMatchesIsNil(t *testing.T) { rules := pushrules.PushRuleArray{rule1, rule2, rule3} - event := newFakeEvent("m.room.message", map[string]interface{}{ - "msgtype": "m.emote", - "body": "is testing pushrules", + event := newFakeEvent(mautrix.EventMessage, mautrix.Content{ + MsgType: mautrix.MsgEmote, + Body: "is testing pushrules", }) assert.Nil(t, rules.GetActions(blankTestRoom, event)) } @@ -156,9 +157,9 @@ func TestPushRuleMap_GetActions_RoomRuleExists(t *testing.T) { Type: pushrules.RoomRule, } - event := newFakeEvent("m.room.message", map[string]interface{}{ - "msgtype": "m.emote", - "body": "is testing pushrules", + event := newFakeEvent(mautrix.EventMessage, mautrix.Content{ + MsgType: mautrix.MsgEmote, + Body: "is testing pushrules", }) assert.Equal(t, rules.GetActions(blankTestRoom, event), actions3) } @@ -194,9 +195,9 @@ func TestPushRuleMap_GetActions_RoomRuleDoesntExist(t *testing.T) { Type: pushrules.RoomRule, } - event := newFakeEvent("m.room.message", map[string]interface{}{ - "msgtype": "m.emote", - "body": "is testing pushrules", + event := newFakeEvent(mautrix.EventMessage, mautrix.Content{ + MsgType: mautrix.MsgEmote, + Body: "is testing pushrules", }) assert.Nil(t, rules.GetActions(blankTestRoom, event)) } @@ -243,9 +244,9 @@ func TestPushRuleMap_GetActions_SenderRuleExists(t *testing.T) { Type: pushrules.SenderRule, } - event := newFakeEvent("m.room.message", map[string]interface{}{ - "msgtype": "m.emote", - "body": "is testing pushrules", + event := newFakeEvent(mautrix.EventMessage, mautrix.Content{ + MsgType: mautrix.MsgEmote, + Body: "is testing pushrules", }) assert.Equal(t, rules.GetActions(blankTestRoom, event), actions1) } diff --git a/matrix/pushrules/rule_test.go b/matrix/pushrules/rule_test.go index bedcfca..aad6a77 100644 --- a/matrix/pushrules/rule_test.go +++ b/matrix/pushrules/rule_test.go @@ -19,6 +19,7 @@ package pushrules_test import ( "github.com/stretchr/testify/assert" "maunium.net/go/gomuks/matrix/pushrules" + "maunium.net/go/mautrix" "testing" ) @@ -31,9 +32,13 @@ func TestPushRule_Match_Conditions(t *testing.T) { Conditions: []*pushrules.PushCondition{cond1, cond2}, } - event := newFakeEvent("m.room.message", map[string]interface{}{ - "msgtype": "m.emote", - "body": "is testing pushrules", + event := newFakeEvent(mautrix.EventMessage, mautrix.Content{ + Raw: map[string]interface{}{ + "msgtype": "m.emote", + "body": "is testing pushrules", + }, + MsgType: mautrix.MsgEmote, + Body: "is testing pushrules", }) assert.True(t, rule.Match(blankTestRoom, event)) } @@ -47,9 +52,13 @@ func TestPushRule_Match_Conditions_Disabled(t *testing.T) { Conditions: []*pushrules.PushCondition{cond1, cond2}, } - event := newFakeEvent("m.room.message", map[string]interface{}{ - "msgtype": "m.emote", - "body": "is testing pushrules", + event := newFakeEvent(mautrix.EventMessage, mautrix.Content{ + Raw: map[string]interface{}{ + "msgtype": "m.emote", + "body": "is testing pushrules", + }, + MsgType: mautrix.MsgEmote, + Body: "is testing pushrules", }) assert.False(t, rule.Match(blankTestRoom, event)) } @@ -63,9 +72,13 @@ func TestPushRule_Match_Conditions_FailIfOneFails(t *testing.T) { Conditions: []*pushrules.PushCondition{cond1, cond2}, } - event := newFakeEvent("m.room.message", map[string]interface{}{ - "msgtype": "m.text", - "body": "I'm testing pushrules", + event := newFakeEvent(mautrix.EventMessage, mautrix.Content{ + Raw: map[string]interface{}{ + "msgtype": "m.text", + "body": "I'm testing pushrules", + }, + MsgType: mautrix.MsgText, + Body: "I'm testing pushrules", }) assert.False(t, rule.Match(blankTestRoom, event)) } @@ -77,9 +90,9 @@ func TestPushRule_Match_Content(t *testing.T) { Pattern: "is testing*", } - event := newFakeEvent("m.room.message", map[string]interface{}{ - "msgtype": "m.emote", - "body": "is testing pushrules", + event := newFakeEvent(mautrix.EventMessage, mautrix.Content{ + MsgType: mautrix.MsgEmote, + Body: "is testing pushrules", }) assert.True(t, rule.Match(blankTestRoom, event)) } @@ -91,9 +104,9 @@ func TestPushRule_Match_Content_Fail(t *testing.T) { Pattern: "is testing*", } - event := newFakeEvent("m.room.message", map[string]interface{}{ - "msgtype": "m.emote", - "body": "is not testing pushrules", + event := newFakeEvent(mautrix.EventMessage, mautrix.Content{ + MsgType: mautrix.MsgEmote, + Body: "is not testing pushrules", }) assert.False(t, rule.Match(blankTestRoom, event)) } @@ -105,9 +118,9 @@ func TestPushRule_Match_Content_ImplicitGlob(t *testing.T) { Pattern: "testing", } - event := newFakeEvent("m.room.message", map[string]interface{}{ - "msgtype": "m.emote", - "body": "is not testing pushrules", + event := newFakeEvent(mautrix.EventMessage, mautrix.Content{ + MsgType: mautrix.MsgEmote, + Body: "is not testing pushrules", }) assert.True(t, rule.Match(blankTestRoom, event)) } @@ -119,9 +132,9 @@ func TestPushRule_Match_Content_IllegalGlob(t *testing.T) { 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", + event := newFakeEvent(mautrix.EventMessage, mautrix.Content{ + MsgType: mautrix.MsgEmote, + Body: "this is not a valid glob", }) assert.False(t, rule.Match(blankTestRoom, event)) } @@ -133,7 +146,7 @@ func TestPushRule_Match_Room(t *testing.T) { RuleID: "!fakeroom:maunium.net", } - event := newFakeEvent("m.room.message", map[string]interface{}{}) + event := newFakeEvent(mautrix.EventMessage, mautrix.Content{}) assert.True(t, rule.Match(blankTestRoom, event)) } @@ -144,7 +157,7 @@ func TestPushRule_Match_Room_Fail(t *testing.T) { RuleID: "!otherroom:maunium.net", } - event := newFakeEvent("m.room.message", map[string]interface{}{}) + event := newFakeEvent(mautrix.EventMessage, mautrix.Content{}) assert.False(t, rule.Match(blankTestRoom, event)) } @@ -155,7 +168,7 @@ func TestPushRule_Match_Sender(t *testing.T) { RuleID: "@tulir:maunium.net", } - event := newFakeEvent("m.room.message", map[string]interface{}{}) + event := newFakeEvent(mautrix.EventMessage, mautrix.Content{}) assert.True(t, rule.Match(blankTestRoom, event)) } @@ -166,7 +179,7 @@ func TestPushRule_Match_Sender_Fail(t *testing.T) { RuleID: "@someone:matrix.org", } - event := newFakeEvent("m.room.message", map[string]interface{}{}) + event := newFakeEvent(mautrix.EventMessage, mautrix.Content{}) assert.False(t, rule.Match(blankTestRoom, event)) } @@ -177,6 +190,6 @@ func TestPushRule_Match_UnknownTypeAlwaysFail(t *testing.T) { RuleID: "@someone:matrix.org", } - event := newFakeEvent("m.room.message", map[string]interface{}{}) + event := newFakeEvent(mautrix.EventMessage, mautrix.Content{}) assert.False(t, rule.Match(blankTestRoom, event)) } |