Debian Codegraph @

pushrules/condition_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 "encoding/json"
11 "fmt"
12 "testing"
13
14 "github.com/stretchr/testify/assert"
15
16 "maunium.net/go/mautrix/event"
17 "maunium.net/go/mautrix/pushrules"
18 )
19
20 var (
21 blankTestRoom pushrules.Room
22 displaynameTestRoom pushrules.Room
23
24 countConditionTestEvent *event.Event
25
26 displaynamePushCondition *pushrules.PushCondition
27 )
28
29 func init() {
30 blankTestRoom = newFakeRoom(1)
31
32 countConditionTestEvent = &event.Event{
33 Sender: "@tulir:maunium.net",
34 Type: event.EventMessage,
35 Timestamp: 1523791120,
36 ID: "$123:maunium.net",
37 RoomID: "!fakeroom:maunium.net",
38 Content: event.Content{
39 Raw: map[string]interface{}{
40 "msgtype": "m.text",
41 "body": "test",
42 },
43 Parsed: &event.MessageEventContent{
44 MsgType: event.MsgText,
45 Body: "test",
46 },
47 },
48 }
49
50 displaynameTestRoom = newFakeRoom(4)
51 displaynamePushCondition = &pushrules.PushCondition{
52 Kind: pushrules.KindContainsDisplayName,
53 }
54 }
55
56 func newFakeEvent(evtType event.Type, parsed interface{}) *event.Event {
57 data, err := json.Marshal(parsed)
58 if err != nil {
59 panic(err)
60 }
61 var raw map[string]interface{}
62 err = json.Unmarshal(data, &raw)
63 if err != nil {
64 panic(err)
65 }
66 content := event.Content{
67 VeryRaw: data,
68 Raw: raw,
69 Parsed: parsed,
70 }
71 return &event.Event{
72 Sender: "@tulir:maunium.net",
73 Type: evtType,
74 Timestamp: 1523791120,
75 ID: "$123:maunium.net",
76 RoomID: "!fakeroom:maunium.net",
77 Content: content,
78 }
79 }
80
81 func newCountPushCondition(condition string) *pushrules.PushCondition {
82 return &pushrules.PushCondition{
83 Kind: pushrules.KindRoomMemberCount,
84 MemberCountCondition: condition,
85 }
86 }
87
88 func newMatchPushCondition(key, pattern string) *pushrules.PushCondition {
89 return &pushrules.PushCondition{
90 Kind: pushrules.KindEventMatch,
91 Key: key,
92 Pattern: pattern,
93 }
94 }
95
96 func TestPushCondition_Match_InvalidKind(t *testing.T) {
97 condition := &pushrules.PushCondition{
98 Kind: pushrules.PushCondKind("invalid"),
99 }
100 evt := newFakeEvent(event.Type{Type: "m.room.foobar"}, &struct{}{})
101 assert.False(t, condition.Match(blankTestRoom, evt))
102 }
103
104 type FakeRoom struct {
105 members map[string]*event.MemberEventContent
106 owner string
107 }
108
109 func newFakeRoom(memberCount int) *FakeRoom {
110 room := &FakeRoom{
111 owner: "@tulir:maunium.net",
112 members: make(map[string]*event.MemberEventContent),
113 }
114
115 if memberCount >= 1 {
116 room.members["@tulir:maunium.net"] = &event.MemberEventContent{
117 Membership: event.MembershipJoin,
118 Displayname: "tulir",
119 }
120 }
121
122 for i := 0; i < memberCount-1; i++ {
123 mxid := fmt.Sprintf("@extrauser_%d:matrix.org", i)
124 room.members[mxid] = &event.MemberEventContent{
125 Membership: event.MembershipJoin,
126 Displayname: fmt.Sprintf("Extra User %d", i),
127 }
128 }
129
130 return room
131 }
132
133 func (fr *FakeRoom) GetMemberCount() int {
134 return len(fr.members)
135 }
136
137 func (fr *FakeRoom) GetOwnDisplayname() string {
138 member, ok := fr.members[fr.owner]
139 if ok {
140 return member.Displayname
141 }
142 return ""
143 }