Debian Codegraph @

pushrules/condition_eventmatch_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 "maunium.net/go/mautrix/event"
11
12 "testing"
13
14 "github.com/stretchr/testify/assert"
15 )
16
17 func TestPushCondition_Match_KindEvent_MsgType(t *testing.T) {
18 condition := newMatchPushCondition("content.msgtype", "m.emote")
19 evt := newFakeEvent(event.EventMessage, &event.MessageEventContent{
20 MsgType: event.MsgEmote,
21 Body: "tests gomuks pushconditions",
22 })
23 assert.True(t, condition.Match(blankTestRoom, evt))
24 }
25
26 func TestPushCondition_Match_KindEvent_MsgType_Fail(t *testing.T) {
27 condition := newMatchPushCondition("content.msgtype", "m.emote")
28
29 evt := newFakeEvent(event.EventMessage, &event.MessageEventContent{
30 MsgType: event.MsgText,
31 Body: "I'm testing gomuks pushconditions",
32 })
33 assert.False(t, condition.Match(blankTestRoom, evt))
34 }
35
36 func TestPushCondition_Match_KindEvent_EventType(t *testing.T) {
37 condition := newMatchPushCondition("type", "m.room.foo")
38 evt := newFakeEvent(event.NewEventType("m.room.foo"), &struct{}{})
39 assert.True(t, condition.Match(blankTestRoom, evt))
40 }
41
42 func TestPushCondition_Match_KindEvent_EventType_IllegalGlob(t *testing.T) {
43 condition := newMatchPushCondition("type", "m.room.invalid_glo[b")
44 evt := newFakeEvent(event.NewEventType("m.room.invalid_glob"), &struct{}{})
45 assert.False(t, condition.Match(blankTestRoom, evt))
46 }
47
48 func TestPushCondition_Match_KindEvent_Sender_Fail(t *testing.T) {
49 condition := newMatchPushCondition("sender", "@foo:maunium.net")
50 evt := newFakeEvent(event.NewEventType("m.room.foo"), &struct{}{})
51 assert.False(t, condition.Match(blankTestRoom, evt))
52 }
53
54 func TestPushCondition_Match_KindEvent_RoomID(t *testing.T) {
55 condition := newMatchPushCondition("room_id", "!fakeroom:maunium.net")
56 evt := newFakeEvent(event.NewEventType(""), &struct{}{})
57 assert.True(t, condition.Match(blankTestRoom, evt))
58 }
59
60 func TestPushCondition_Match_KindEvent_BlankStateKey(t *testing.T) {
61 condition := newMatchPushCondition("state_key", "")
62 evt := newFakeEvent(event.NewEventType("m.room.foo"), &struct{}{})
63 assert.True(t, condition.Match(blankTestRoom, evt))
64 }
65
66 func TestPushCondition_Match_KindEvent_BlankStateKey_Fail(t *testing.T) {
67 condition := newMatchPushCondition("state_key", "not blank")
68 evt := newFakeEvent(event.NewEventType("m.room.foo"), &struct{}{})
69 assert.False(t, condition.Match(blankTestRoom, evt))
70 }
71
72 func TestPushCondition_Match_KindEvent_NonBlankStateKey(t *testing.T) {
73 condition := newMatchPushCondition("state_key", "*:maunium.net")
74 evt := newFakeEvent(event.NewEventType("m.room.foo"), &struct{}{})
75 evt.StateKey = (*string)(&evt.Sender)
76 assert.True(t, condition.Match(blankTestRoom, evt))
77 }
78
79 func TestPushCondition_Match_KindEvent_UnknownKey(t *testing.T) {
80 condition := newMatchPushCondition("non-existent key", "doesn't affect anything")
81 evt := newFakeEvent(event.NewEventType("m.room.foo"), &struct{}{})
82 assert.False(t, condition.Match(blankTestRoom, evt))
83 }