Fix tests

This commit is contained in:
Tulir Asokan 2018-11-14 01:11:40 +02:00
parent a0815a6f3d
commit 912bf309d1
13 changed files with 181 additions and 178 deletions

4
Gopkg.lock generated
View File

@ -164,14 +164,14 @@
[[projects]] [[projects]]
branch = "master" branch = "master"
digest = "1:6dd6ac30401547ebcd1c0ee7a08e74e9ae872eba28607309979e9ed9776cee21" digest = "1:f0a9c6bdcbf2bbaa02f4f913b1956cc722daf92bec1715b51e001e53ba93b3a2"
name = "maunium.net/go/mautrix" name = "maunium.net/go/mautrix"
packages = [ packages = [
".", ".",
"format", "format",
] ]
pruneopts = "UT" pruneopts = "UT"
revision = "e8080dcf484d1db9021d2019fee132ffc9e37e3c" revision = "5012a3c49b63ccb3c72cd01d2fd3442759c3ea3c"
[[projects]] [[projects]]
branch = "master" branch = "master"

View File

@ -21,8 +21,8 @@ import (
"fmt" "fmt"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"io/ioutil" "io/ioutil"
"maunium.net/go/mautrix"
"maunium.net/go/gomuks/config" "maunium.net/go/gomuks/config"
"maunium.net/go/mautrix"
"net/http" "net/http"
"os" "os"
"strings" "strings"
@ -69,7 +69,7 @@ func TestContainer_SendMarkdownMessage_WithMarkdown(t *testing.T) {
body := parseBody(req) body := parseBody(req)
assert.Equal(t, "m.text", body["msgtype"]) assert.Equal(t, "m.text", body["msgtype"])
assert.Equal(t, "**formatted** <u>test</u> _message_", body["body"]) assert.Equal(t, "**formatted** test _message_", body["body"])
assert.Equal(t, "<strong>formatted</strong> <u>test</u> <em>message</em>", body["formatted_body"]) assert.Equal(t, "<strong>formatted</strong> <u>test</u> <em>message</em>", body["formatted_body"])
return mockResponse(http.StatusOK, `{"event_id": "!foobar2:example.com"}`), nil return mockResponse(http.StatusOK, `{"event_id": "!foobar2:example.com"}`), nil
})} })}

View File

@ -112,6 +112,9 @@ func (cond *PushCondition) matchDisplayName(room Room, event *mautrix.Event) boo
return false return false
} }
member := room.GetMember(ownerID) member := room.GetMember(ownerID)
if member == nil {
return false
}
return strings.Contains(event.Content.Body, member.Displayname) return strings.Contains(event.Content.Body, member.Displayname)
} }

View File

@ -17,42 +17,43 @@
package pushrules_test package pushrules_test
import ( import (
"maunium.net/go/mautrix"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestPushCondition_Match_DisplayName(t *testing.T) { func TestPushCondition_Match_DisplayName(t *testing.T) {
event := newFakeEvent("m.room.message", map[string]interface{}{ event := newFakeEvent(mautrix.EventMessage, mautrix.Content{
"msgtype": "m.text", MsgType: mautrix.MsgText,
"body": "tulir: test mention", Body: "tulir: test mention",
}) })
event.Sender = "@someone_else:matrix.org" event.Sender = "@someone_else:matrix.org"
assert.True(t, displaynamePushCondition.Match(displaynameTestRoom, event)) assert.True(t, displaynamePushCondition.Match(displaynameTestRoom, event))
} }
func TestPushCondition_Match_DisplayName_Fail(t *testing.T) { func TestPushCondition_Match_DisplayName_Fail(t *testing.T) {
event := newFakeEvent("m.room.message", map[string]interface{}{ event := newFakeEvent(mautrix.EventMessage, mautrix.Content{
"msgtype": "m.text", MsgType: mautrix.MsgText,
"body": "not a mention", Body: "not a mention",
}) })
event.Sender = "@someone_else:matrix.org" event.Sender = "@someone_else:matrix.org"
assert.False(t, displaynamePushCondition.Match(displaynameTestRoom, event)) assert.False(t, displaynamePushCondition.Match(displaynameTestRoom, event))
} }
func TestPushCondition_Match_DisplayName_CantHighlightSelf(t *testing.T) { func TestPushCondition_Match_DisplayName_CantHighlightSelf(t *testing.T) {
event := newFakeEvent("m.room.message", map[string]interface{}{ event := newFakeEvent(mautrix.EventMessage, mautrix.Content{
"msgtype": "m.text", MsgType: mautrix.MsgText,
"body": "tulir: I can't highlight myself", Body: "tulir: I can't highlight myself",
}) })
assert.False(t, displaynamePushCondition.Match(displaynameTestRoom, event)) assert.False(t, displaynamePushCondition.Match(displaynameTestRoom, event))
} }
func TestPushCondition_Match_DisplayName_FailsOnEmptyRoom(t *testing.T) { func TestPushCondition_Match_DisplayName_FailsOnEmptyRoom(t *testing.T) {
emptyRoom := newFakeRoom(0) emptyRoom := newFakeRoom(0)
event := newFakeEvent("m.room.message", map[string]interface{}{ event := newFakeEvent(mautrix.EventMessage, mautrix.Content{
"msgtype": "m.text", MsgType: mautrix.MsgText,
"body": "tulir: this room doesn't have the owner Member available, so it fails.", Body: "tulir: this room doesn't have the owner Member available, so it fails.",
}) })
event.Sender = "@someone_else:matrix.org" event.Sender = "@someone_else:matrix.org"
assert.False(t, displaynamePushCondition.Match(emptyRoom, event)) assert.False(t, displaynamePushCondition.Match(emptyRoom, event))

View File

@ -17,6 +17,7 @@
package pushrules_test package pushrules_test
import ( import (
"maunium.net/go/mautrix"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -24,9 +25,11 @@ import (
func TestPushCondition_Match_KindEvent_MsgType(t *testing.T) { func TestPushCondition_Match_KindEvent_MsgType(t *testing.T) {
condition := newMatchPushCondition("content.msgtype", "m.emote") condition := newMatchPushCondition("content.msgtype", "m.emote")
event := newFakeEvent("m.room.message", map[string]interface{}{ event := newFakeEvent(mautrix.EventMessage, mautrix.Content{
"msgtype": "m.emote", Raw: map[string]interface{}{
"body": "tests gomuks pushconditions", "msgtype": "m.emote",
"body": "tests gomuks pushconditions",
},
}) })
assert.True(t, condition.Match(blankTestRoom, event)) 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) { func TestPushCondition_Match_KindEvent_MsgType_Fail(t *testing.T) {
condition := newMatchPushCondition("content.msgtype", "m.emote") condition := newMatchPushCondition("content.msgtype", "m.emote")
event := newFakeEvent("m.room.message", map[string]interface{}{ event := newFakeEvent(mautrix.EventMessage, mautrix.Content{
"msgtype": "m.text", Raw: map[string]interface{}{
"body": "I'm testing gomuks pushconditions", "msgtype": "m.text",
"body": "I'm testing gomuks pushconditions",
},
}) })
assert.False(t, condition.Match(blankTestRoom, event)) assert.False(t, condition.Match(blankTestRoom, event))
} }
func TestPushCondition_Match_KindEvent_EventType(t *testing.T) { func TestPushCondition_Match_KindEvent_EventType(t *testing.T) {
condition := newMatchPushCondition("type", "m.room.foo") 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)) assert.True(t, condition.Match(blankTestRoom, event))
} }
func TestPushCondition_Match_KindEvent_EventType_IllegalGlob(t *testing.T) { func TestPushCondition_Match_KindEvent_EventType_IllegalGlob(t *testing.T) {
condition := newMatchPushCondition("type", "m.room.invalid_glo[b") 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)) assert.False(t, condition.Match(blankTestRoom, event))
} }
func TestPushCondition_Match_KindEvent_Sender_Fail(t *testing.T) { func TestPushCondition_Match_KindEvent_Sender_Fail(t *testing.T) {
condition := newMatchPushCondition("sender", "@foo:maunium.net") 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)) assert.False(t, condition.Match(blankTestRoom, event))
} }
func TestPushCondition_Match_KindEvent_RoomID(t *testing.T) { func TestPushCondition_Match_KindEvent_RoomID(t *testing.T) {
condition := newMatchPushCondition("room_id", "!fakeroom:maunium.net") 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)) assert.True(t, condition.Match(blankTestRoom, event))
} }
func TestPushCondition_Match_KindEvent_BlankStateKey(t *testing.T) { func TestPushCondition_Match_KindEvent_BlankStateKey(t *testing.T) {
condition := newMatchPushCondition("state_key", "") 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)) assert.True(t, condition.Match(blankTestRoom, event))
} }
func TestPushCondition_Match_KindEvent_BlankStateKey_Fail(t *testing.T) { func TestPushCondition_Match_KindEvent_BlankStateKey_Fail(t *testing.T) {
condition := newMatchPushCondition("state_key", "not blank") 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)) assert.False(t, condition.Match(blankTestRoom, event))
} }
func TestPushCondition_Match_KindEvent_NonBlankStateKey(t *testing.T) { func TestPushCondition_Match_KindEvent_NonBlankStateKey(t *testing.T) {
condition := newMatchPushCondition("state_key", "*:maunium.net") 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 event.StateKey = &event.Sender
assert.True(t, condition.Match(blankTestRoom, event)) assert.True(t, condition.Match(blankTestRoom, event))
} }
func TestPushCondition_Match_KindEvent_UnknownKey(t *testing.T) { func TestPushCondition_Match_KindEvent_UnknownKey(t *testing.T) {
condition := newMatchPushCondition("non-existent key", "doesn't affect anything") 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)) assert.False(t, condition.Match(blankTestRoom, event))
} }

View File

@ -40,13 +40,13 @@ func init() {
countConditionTestEvent = &mautrix.Event{ countConditionTestEvent = &mautrix.Event{
Sender: "@tulir:maunium.net", Sender: "@tulir:maunium.net",
Type: "m.room.message", Type: mautrix.EventMessage,
Timestamp: 1523791120, Timestamp: 1523791120,
ID: "$123:maunium.net", ID: "$123:maunium.net",
RoomID: "!fakeroom:maunium.net", RoomID: "!fakeroom:maunium.net",
Content: map[string]interface{}{ Content: mautrix.Content{
"msgtype": "m.text", MsgType: mautrix.MsgText,
"body": "test", 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{ return &mautrix.Event{
Sender: "@tulir:maunium.net", Sender: "@tulir:maunium.net",
Type: evtType, Type: evtType,
@ -86,49 +86,47 @@ func TestPushCondition_Match_InvalidKind(t *testing.T) {
condition := &pushrules.PushCondition{ condition := &pushrules.PushCondition{
Kind: pushrules.PushCondKind("invalid"), 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)) assert.False(t, condition.Match(blankTestRoom, event))
} }
type FakeRoom struct { type FakeRoom struct {
members map[string]*rooms.Member members map[string]*mautrix.Member
owner string owner string
} }
func newFakeRoom(memberCount int) *FakeRoom { func newFakeRoom(memberCount int) *FakeRoom {
room := &FakeRoom{ room := &FakeRoom{
owner: "@tulir:maunium.net", owner: "@tulir:maunium.net",
members: make(map[string]*rooms.Member), members: make(map[string]*mautrix.Member),
} }
if memberCount >= 1 { if memberCount >= 1 {
room.members["@tulir:maunium.net"] = &rooms.Member{ room.members["@tulir:maunium.net"] = &mautrix.Member{
UserID: "@tulir:maunium.net", Membership: mautrix.MembershipJoin,
Membership: rooms.MembershipJoin, Displayname: "tulir",
DisplayName: "tulir",
} }
} }
for i := 0; i < memberCount-1; i++ { for i := 0; i < memberCount-1; i++ {
mxid := fmt.Sprintf("@extrauser_%d:matrix.org", i) mxid := fmt.Sprintf("@extrauser_%d:matrix.org", i)
room.members[mxid] = &rooms.Member{ room.members[mxid] = &mautrix.Member{
UserID: mxid, Membership: mautrix.MembershipJoin,
Membership: rooms.MembershipJoin, Displayname: fmt.Sprintf("Extra User %d", i),
DisplayName: fmt.Sprintf("Extra User %d", i),
} }
} }
return room return room
} }
func (fr *FakeRoom) GetMember(mxid string) *rooms.Member { func (fr *FakeRoom) GetMember(mxid string) *mautrix.Member {
return fr.members[mxid] return fr.members[mxid]
} }
func (fr *FakeRoom) GetSessionOwner() *rooms.Member { func (fr *FakeRoom) GetSessionOwner() string {
return fr.members[fr.owner] return fr.owner
} }
func (fr *FakeRoom) GetMembers() map[string]*rooms.Member { func (fr *FakeRoom) GetMembers() map[string]*mautrix.Member {
return fr.members return fr.members
} }

View File

@ -25,18 +25,13 @@ import (
"maunium.net/go/gomuks/matrix/pushrules" "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) { func TestEventToPushRules(t *testing.T) {
event := &mautrix.Event{ event := &mautrix.Event{
Type: "m.push_rules", Type: mautrix.AccountDataPushRules,
Timestamp: 1523380910, Timestamp: 1523380910,
Content: mapExamplePushRules, Content: mautrix.Content{
VeryRaw: json.RawMessage(JSONExamplePushRules),
},
} }
pushRuleset, err := pushrules.EventToPushRules(event) pushRuleset, err := pushrules.EventToPushRules(event)
assert.Nil(t, err) assert.Nil(t, err)

View File

@ -19,6 +19,7 @@ package pushrules_test
import ( import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"maunium.net/go/gomuks/matrix/pushrules" "maunium.net/go/gomuks/matrix/pushrules"
"maunium.net/go/mautrix"
"testing" "testing"
) )
@ -61,9 +62,9 @@ func TestPushRuleArray_GetActions_FirstMatchReturns(t *testing.T) {
rules := pushrules.PushRuleArray{rule1, rule2, rule3} rules := pushrules.PushRuleArray{rule1, rule2, rule3}
event := newFakeEvent("m.room.message", map[string]interface{}{ event := newFakeEvent(mautrix.EventMessage, mautrix.Content{
"msgtype": "m.emote", MsgType: mautrix.MsgEmote,
"body": "is testing pushrules", Body: "is testing pushrules",
}) })
assert.Equal(t, rules.GetActions(blankTestRoom, event), actions2) 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} rules := pushrules.PushRuleArray{rule1, rule2, rule3}
event := newFakeEvent("m.room.message", map[string]interface{}{ event := newFakeEvent(mautrix.EventMessage, mautrix.Content{
"msgtype": "m.emote", MsgType: mautrix.MsgEmote,
"body": "is testing pushrules", Body: "is testing pushrules",
}) })
assert.Nil(t, rules.GetActions(blankTestRoom, event)) assert.Nil(t, rules.GetActions(blankTestRoom, event))
} }
@ -156,9 +157,9 @@ func TestPushRuleMap_GetActions_RoomRuleExists(t *testing.T) {
Type: pushrules.RoomRule, Type: pushrules.RoomRule,
} }
event := newFakeEvent("m.room.message", map[string]interface{}{ event := newFakeEvent(mautrix.EventMessage, mautrix.Content{
"msgtype": "m.emote", MsgType: mautrix.MsgEmote,
"body": "is testing pushrules", Body: "is testing pushrules",
}) })
assert.Equal(t, rules.GetActions(blankTestRoom, event), actions3) assert.Equal(t, rules.GetActions(blankTestRoom, event), actions3)
} }
@ -194,9 +195,9 @@ func TestPushRuleMap_GetActions_RoomRuleDoesntExist(t *testing.T) {
Type: pushrules.RoomRule, Type: pushrules.RoomRule,
} }
event := newFakeEvent("m.room.message", map[string]interface{}{ event := newFakeEvent(mautrix.EventMessage, mautrix.Content{
"msgtype": "m.emote", MsgType: mautrix.MsgEmote,
"body": "is testing pushrules", Body: "is testing pushrules",
}) })
assert.Nil(t, rules.GetActions(blankTestRoom, event)) assert.Nil(t, rules.GetActions(blankTestRoom, event))
} }
@ -243,9 +244,9 @@ func TestPushRuleMap_GetActions_SenderRuleExists(t *testing.T) {
Type: pushrules.SenderRule, Type: pushrules.SenderRule,
} }
event := newFakeEvent("m.room.message", map[string]interface{}{ event := newFakeEvent(mautrix.EventMessage, mautrix.Content{
"msgtype": "m.emote", MsgType: mautrix.MsgEmote,
"body": "is testing pushrules", Body: "is testing pushrules",
}) })
assert.Equal(t, rules.GetActions(blankTestRoom, event), actions1) assert.Equal(t, rules.GetActions(blankTestRoom, event), actions1)
} }

View File

@ -19,6 +19,7 @@ package pushrules_test
import ( import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"maunium.net/go/gomuks/matrix/pushrules" "maunium.net/go/gomuks/matrix/pushrules"
"maunium.net/go/mautrix"
"testing" "testing"
) )
@ -31,9 +32,13 @@ func TestPushRule_Match_Conditions(t *testing.T) {
Conditions: []*pushrules.PushCondition{cond1, cond2}, Conditions: []*pushrules.PushCondition{cond1, cond2},
} }
event := newFakeEvent("m.room.message", map[string]interface{}{ event := newFakeEvent(mautrix.EventMessage, mautrix.Content{
"msgtype": "m.emote", Raw: map[string]interface{}{
"body": "is testing pushrules", "msgtype": "m.emote",
"body": "is testing pushrules",
},
MsgType: mautrix.MsgEmote,
Body: "is testing pushrules",
}) })
assert.True(t, rule.Match(blankTestRoom, event)) assert.True(t, rule.Match(blankTestRoom, event))
} }
@ -47,9 +52,13 @@ func TestPushRule_Match_Conditions_Disabled(t *testing.T) {
Conditions: []*pushrules.PushCondition{cond1, cond2}, Conditions: []*pushrules.PushCondition{cond1, cond2},
} }
event := newFakeEvent("m.room.message", map[string]interface{}{ event := newFakeEvent(mautrix.EventMessage, mautrix.Content{
"msgtype": "m.emote", Raw: map[string]interface{}{
"body": "is testing pushrules", "msgtype": "m.emote",
"body": "is testing pushrules",
},
MsgType: mautrix.MsgEmote,
Body: "is testing pushrules",
}) })
assert.False(t, rule.Match(blankTestRoom, event)) assert.False(t, rule.Match(blankTestRoom, event))
} }
@ -63,9 +72,13 @@ func TestPushRule_Match_Conditions_FailIfOneFails(t *testing.T) {
Conditions: []*pushrules.PushCondition{cond1, cond2}, Conditions: []*pushrules.PushCondition{cond1, cond2},
} }
event := newFakeEvent("m.room.message", map[string]interface{}{ event := newFakeEvent(mautrix.EventMessage, mautrix.Content{
"msgtype": "m.text", Raw: map[string]interface{}{
"body": "I'm testing pushrules", "msgtype": "m.text",
"body": "I'm testing pushrules",
},
MsgType: mautrix.MsgText,
Body: "I'm testing pushrules",
}) })
assert.False(t, rule.Match(blankTestRoom, event)) assert.False(t, rule.Match(blankTestRoom, event))
} }
@ -77,9 +90,9 @@ func TestPushRule_Match_Content(t *testing.T) {
Pattern: "is testing*", Pattern: "is testing*",
} }
event := newFakeEvent("m.room.message", map[string]interface{}{ event := newFakeEvent(mautrix.EventMessage, mautrix.Content{
"msgtype": "m.emote", MsgType: mautrix.MsgEmote,
"body": "is testing pushrules", Body: "is testing pushrules",
}) })
assert.True(t, rule.Match(blankTestRoom, event)) assert.True(t, rule.Match(blankTestRoom, event))
} }
@ -91,9 +104,9 @@ func TestPushRule_Match_Content_Fail(t *testing.T) {
Pattern: "is testing*", Pattern: "is testing*",
} }
event := newFakeEvent("m.room.message", map[string]interface{}{ event := newFakeEvent(mautrix.EventMessage, mautrix.Content{
"msgtype": "m.emote", MsgType: mautrix.MsgEmote,
"body": "is not testing pushrules", Body: "is not testing pushrules",
}) })
assert.False(t, rule.Match(blankTestRoom, event)) assert.False(t, rule.Match(blankTestRoom, event))
} }
@ -105,9 +118,9 @@ func TestPushRule_Match_Content_ImplicitGlob(t *testing.T) {
Pattern: "testing", Pattern: "testing",
} }
event := newFakeEvent("m.room.message", map[string]interface{}{ event := newFakeEvent(mautrix.EventMessage, mautrix.Content{
"msgtype": "m.emote", MsgType: mautrix.MsgEmote,
"body": "is not testing pushrules", Body: "is not testing pushrules",
}) })
assert.True(t, rule.Match(blankTestRoom, event)) 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", Pattern: "this is not a valid glo[b",
} }
event := newFakeEvent("m.room.message", map[string]interface{}{ event := newFakeEvent(mautrix.EventMessage, mautrix.Content{
"msgtype": "m.emote", MsgType: mautrix.MsgEmote,
"body": "this is not a valid glob", Body: "this is not a valid glob",
}) })
assert.False(t, rule.Match(blankTestRoom, event)) assert.False(t, rule.Match(blankTestRoom, event))
} }
@ -133,7 +146,7 @@ func TestPushRule_Match_Room(t *testing.T) {
RuleID: "!fakeroom:maunium.net", 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)) assert.True(t, rule.Match(blankTestRoom, event))
} }
@ -144,7 +157,7 @@ func TestPushRule_Match_Room_Fail(t *testing.T) {
RuleID: "!otherroom:maunium.net", 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)) assert.False(t, rule.Match(blankTestRoom, event))
} }
@ -155,7 +168,7 @@ func TestPushRule_Match_Sender(t *testing.T) {
RuleID: "@tulir:maunium.net", 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)) assert.True(t, rule.Match(blankTestRoom, event))
} }
@ -166,7 +179,7 @@ func TestPushRule_Match_Sender_Fail(t *testing.T) {
RuleID: "@someone:matrix.org", 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)) assert.False(t, rule.Match(blankTestRoom, event))
} }
@ -177,6 +190,6 @@ func TestPushRule_Match_UnknownTypeAlwaysFail(t *testing.T) {
RuleID: "@someone:matrix.org", 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)) assert.False(t, rule.Match(blankTestRoom, event))
} }

View File

@ -424,7 +424,7 @@ func (room *Room) GetMember(userID string) *mautrix.Member {
return member return member
} }
// GetSessionOwner returns the Member instance of the user whose session this room was created for. // GetSessionOwner returns the ID of the user whose session this room was created for.
func (room *Room) GetSessionOwner() string { func (room *Room) GetSessionOwner() string {
return room.SessionUserID return room.SessionUserID
} }

View File

@ -21,8 +21,8 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"maunium.net/go/mautrix"
"maunium.net/go/gomuks/matrix/rooms" "maunium.net/go/gomuks/matrix/rooms"
"maunium.net/go/mautrix"
) )
func TestNewRoom_DefaultValues(t *testing.T) { func TestNewRoom_DefaultValues(t *testing.T) {
@ -34,15 +34,15 @@ func TestNewRoom_DefaultValues(t *testing.T) {
assert.Empty(t, room.GetAliases()) assert.Empty(t, room.GetAliases())
assert.Empty(t, room.GetCanonicalAlias()) assert.Empty(t, room.GetCanonicalAlias())
assert.Empty(t, room.GetTopic()) assert.Empty(t, room.GetTopic())
assert.Nil(t, room.GetSessionOwner()) assert.Nil(t, room.GetMember(room.GetSessionOwner()))
} }
func TestRoom_GetCanonicalAlias(t *testing.T) { func TestRoom_GetCanonicalAlias(t *testing.T) {
room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net") room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net")
room.UpdateState(&mautrix.Event{ room.UpdateState(&mautrix.Event{
Type: "m.room.canonical_alias", Type: mautrix.StateCanonicalAlias,
Content: map[string]interface{}{ Content: mautrix.Content{
"alias": "#foo:maunium.net", Alias: "#foo:maunium.net",
}, },
}) })
assert.Equal(t, "#foo:maunium.net", room.GetCanonicalAlias()) assert.Equal(t, "#foo:maunium.net", room.GetCanonicalAlias())
@ -51,9 +51,9 @@ func TestRoom_GetCanonicalAlias(t *testing.T) {
func TestRoom_GetTopic(t *testing.T) { func TestRoom_GetTopic(t *testing.T) {
room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net") room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net")
room.UpdateState(&mautrix.Event{ room.UpdateState(&mautrix.Event{
Type: "m.room.topic", Type: mautrix.StateTopic,
Content: map[string]interface{}{ Content: mautrix.Content{
"topic": "test topic", Topic: "test topic",
}, },
}) })
assert.Equal(t, "test topic", room.GetTopic()) assert.Equal(t, "test topic", room.GetTopic())
@ -88,18 +88,18 @@ func TestRoom_GetAliases(t *testing.T) {
func addName(room *rooms.Room) { func addName(room *rooms.Room) {
room.UpdateState(&mautrix.Event{ room.UpdateState(&mautrix.Event{
Type: "m.room.name", Type: mautrix.StateRoomName,
Content: map[string]interface{}{ Content: mautrix.Content{
"name": "Test room", Name: "Test room",
}, },
}) })
} }
func addCanonicalAlias(room *rooms.Room) { func addCanonicalAlias(room *rooms.Room) {
room.UpdateState(&mautrix.Event{ room.UpdateState(&mautrix.Event{
Type: "m.room.canonical_alias", Type: mautrix.StateCanonicalAlias,
Content: map[string]interface{}{ Content: mautrix.Content{
"alias": "#foo:maunium.net", Alias: "#foo:maunium.net",
}, },
}) })
} }
@ -107,19 +107,19 @@ func addCanonicalAlias(room *rooms.Room) {
func addAliases(room *rooms.Room) { func addAliases(room *rooms.Room) {
server1 := "maunium.net" server1 := "maunium.net"
room.UpdateState(&mautrix.Event{ room.UpdateState(&mautrix.Event{
Type: "m.room.aliases", Type: mautrix.StateAliases,
StateKey: &server1, StateKey: &server1,
Content: map[string]interface{}{ Content: mautrix.Content{
"aliases": []interface{}{"#bar:maunium.net", "#test:maunium.net", "#foo:maunium.net"}, Aliases: []string{"#bar:maunium.net", "#test:maunium.net", "#foo:maunium.net"},
}, },
}) })
server2 := "matrix.org" server2 := "matrix.org"
room.UpdateState(&mautrix.Event{ room.UpdateState(&mautrix.Event{
Type: "m.room.aliases", Type: mautrix.StateAliases,
StateKey: &server2, StateKey: &server2,
Content: map[string]interface{}{ Content: mautrix.Content{
"aliases": []interface{}{"#foo:matrix.org", "#test:matrix.org"}, Aliases: []string{"#foo:matrix.org", "#test:matrix.org"},
}, },
}) })
} }
@ -127,27 +127,31 @@ func addAliases(room *rooms.Room) {
func addMembers(room *rooms.Room, count int) { func addMembers(room *rooms.Room, count int) {
user1 := "@tulir:maunium.net" user1 := "@tulir:maunium.net"
room.UpdateState(&mautrix.Event{ room.UpdateState(&mautrix.Event{
Type: "m.room.member", Type: mautrix.StateMember,
StateKey: &user1, StateKey: &user1,
Content: map[string]interface{}{ Content: mautrix.Content{
"displayname": "tulir", Member: mautrix.Member{
"membership": "join", Displayname: "tulir",
Membership: mautrix.MembershipJoin,
},
}, },
}) })
for i := 1; i < count; i++ { for i := 1; i < count; i++ {
userN := fmt.Sprintf("@user_%d:matrix.org", i+1) userN := fmt.Sprintf("@user_%d:matrix.org", i+1)
content := map[string]interface{}{ content := mautrix.Content{
"membership": "join", Member: mautrix.Member{
Membership: mautrix.MembershipJoin,
},
} }
if i%2 == 1 { if i%2 == 1 {
content["displayname"] = fmt.Sprintf("User #%d", i+1) content.Displayname = fmt.Sprintf("User #%d", i+1)
} }
if i%5 == 0 { if i%5 == 0 {
content["membership"] = "invite" content.Membership = mautrix.MembershipInvite
} }
room.UpdateState(&mautrix.Event{ room.UpdateState(&mautrix.Event{
Type: "m.room.member", Type: mautrix.StateMember,
StateKey: &userN, StateKey: &userN,
Content: content, Content: content,
}) })
@ -168,7 +172,7 @@ func TestRoom_GetMember(t *testing.T) {
assert.NotNil(t, room.GetMember("@user_2:matrix.org")) assert.NotNil(t, room.GetMember("@user_2:matrix.org"))
assert.NotNil(t, room.GetMember("@tulir:maunium.net")) assert.NotNil(t, room.GetMember("@tulir:maunium.net"))
assert.Equal(t, room.GetMember("@tulir:maunium.net"), room.GetSessionOwner()) assert.Equal(t, "@tulir:maunium.net", room.GetSessionOwner())
} }
func TestRoom_GetTitle_ExplicitName(t *testing.T) { func TestRoom_GetTitle_ExplicitName(t *testing.T) {

View File

@ -20,9 +20,9 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"maunium.net/go/mautrix"
"maunium.net/go/gomuks/matrix" "maunium.net/go/gomuks/matrix"
"maunium.net/go/gomuks/matrix/rooms" "maunium.net/go/gomuks/matrix/rooms"
"maunium.net/go/mautrix"
) )
func TestGomuksSyncer_ProcessResponse_Initial(t *testing.T) { func TestGomuksSyncer_ProcessResponse_Initial(t *testing.T) {
@ -54,47 +54,47 @@ func TestGomuksSyncer_ProcessResponse(t *testing.T) {
} }
ml := &mockListener{} ml := &mockListener{}
syncer := matrix.NewGomuksSyncer(mss) syncer := matrix.NewGomuksSyncer(mss)
syncer.OnEventType("m.room.member", ml.receive) syncer.OnEventType(mautrix.EventMessage, ml.receive)
syncer.OnEventType("m.room.message", ml.receive) syncer.OnEventType(mautrix.StateMember, ml.receive)
syncer.GetFilterJSON("@tulir:maunium.net") syncer.GetFilterJSON("@tulir:maunium.net")
joinEvt := &mautrix.Event{ joinEvt := &mautrix.Event{
ID: "!join:maunium.net", ID: "!join:maunium.net",
Type: "m.room.member", Type: mautrix.StateMember,
Sender: "@tulir:maunium.net", Sender: "@tulir:maunium.net",
StateKey: ptr("̣@tulir:maunium.net"), StateKey: ptr("̣@tulir:maunium.net"),
Content: map[string]interface{}{ Content: mautrix.Content{
"membership": "join", Membership: mautrix.MembershipJoin,
}, },
} }
messageEvt := &mautrix.Event{ messageEvt := &mautrix.Event{
ID: "!msg:maunium.net", ID: "!msg:maunium.net",
Type: "m.room.message", Type: mautrix.EventMessage,
Content: map[string]interface{}{ Content: mautrix.Content{
"body": "foo", Body: "foo",
"msgtype": "m.text", MsgType: mautrix.MsgText,
}, },
} }
unhandledEvt := &mautrix.Event{ unhandledEvt := &mautrix.Event{
ID: "!unhandled:maunium.net", ID: "!unhandled:maunium.net",
Type: "m.room.unhandled_event", Type: mautrix.EventType{Type: "m.room.unhandled_event"},
} }
inviteEvt := &mautrix.Event{ inviteEvt := &mautrix.Event{
ID: "!invite:matrix.org", ID: "!invite:matrix.org",
Type: "m.room.member", Type: mautrix.StateMember,
Sender: "@you:matrix.org", Sender: "@you:matrix.org",
StateKey: ptr("̣@tulir:maunium.net"), StateKey: ptr("̣@tulir:maunium.net"),
Content: map[string]interface{}{ Content: mautrix.Content{
"membership": "invite", Membership: mautrix.MembershipInvite,
}, },
} }
leaveEvt := &mautrix.Event{ leaveEvt := &mautrix.Event{
ID: "!leave:matrix.org", ID: "!leave:matrix.org",
Type: "m.room.member", Type: mautrix.StateMember,
Sender: "@you:matrix.org", Sender: "@you:matrix.org",
StateKey: ptr("̣@tulir:maunium.net"), StateKey: ptr("̣@tulir:maunium.net"),
Content: map[string]interface{}{ Content: mautrix.Content{
"membership": "leave", Membership: mautrix.MembershipLeave,
}, },
} }
@ -116,8 +116,8 @@ func TestGomuksSyncer_ProcessResponse(t *testing.T) {
} `json:"state"` } `json:"state"`
Timeline struct { Timeline struct {
Events []*mautrix.Event `json:"events"` Events []*mautrix.Event `json:"events"`
Limited bool `json:"limited"` Limited bool `json:"limited"`
PrevBatch string `json:"prev_batch"` PrevBatch string `json:"prev_batch"`
} `json:"timeline"` } `json:"timeline"`
}{ }{
State: events{Events: []*mautrix.Event{leaveEvt}}, State: events{Events: []*mautrix.Event{leaveEvt}},
@ -150,8 +150,8 @@ type events struct {
type timeline struct { type timeline struct {
Events []*mautrix.Event `json:"events"` Events []*mautrix.Event `json:"events"`
Limited bool `json:"limited"` Limited bool `json:"limited"`
PrevBatch string `json:"prev_batch"` PrevBatch string `json:"prev_batch"`
} }
type join struct { type join struct {
State struct { State struct {
@ -159,8 +159,8 @@ type join struct {
} `json:"state"` } `json:"state"`
Timeline struct { Timeline struct {
Events []*mautrix.Event `json:"events"` Events []*mautrix.Event `json:"events"`
Limited bool `json:"limited"` Limited bool `json:"limited"`
PrevBatch string `json:"prev_batch"` PrevBatch string `json:"prev_batch"`
} `json:"timeline"` } `json:"timeline"`
Ephemeral struct { Ephemeral struct {
Events []*mautrix.Event `json:"events"` Events []*mautrix.Event `json:"events"`
@ -190,8 +190,8 @@ func newRespSync() *mautrix.RespSync {
} `json:"state"` } `json:"state"`
Timeline struct { Timeline struct {
Events []*mautrix.Event `json:"events"` Events []*mautrix.Event `json:"events"`
Limited bool `json:"limited"` Limited bool `json:"limited"`
PrevBatch string `json:"prev_batch"` PrevBatch string `json:"prev_batch"`
} `json:"timeline"` } `json:"timeline"`
Ephemeral struct { Ephemeral struct {
Events []*mautrix.Event `json:"events"` Events []*mautrix.Event `json:"events"`
@ -211,8 +211,8 @@ func newRespSync() *mautrix.RespSync {
} `json:"state"` } `json:"state"`
Timeline struct { Timeline struct {
Events []*mautrix.Event `json:"events"` Events []*mautrix.Event `json:"events"`
Limited bool `json:"limited"` Limited bool `json:"limited"`
PrevBatch string `json:"prev_batch"` PrevBatch string `json:"prev_batch"`
} `json:"timeline"` } `json:"timeline"`
}) })
return resp return resp

View File

@ -200,9 +200,9 @@ type Content struct {
PowerLevels PowerLevels
Member Member
Aliases []string `json:"aliases,omitempty"` Aliases []string `json:"aliases,omitempty"`
CanonicalAlias Alias string `json:"alias,omitempty"`
RoomName Name string `json:"name,omitempty"`
RoomTopic Topic string `json:"name,omitempty"`
RoomTags Tags `json:"tags,omitempty"` RoomTags Tags `json:"tags,omitempty"`
TypingUserIDs []string `json:"user_ids,omitempty"` TypingUserIDs []string `json:"user_ids,omitempty"`
@ -244,11 +244,6 @@ func (content *Content) UnmarshalMember() (m Member, err error) {
return return
} }
func (content *Content) UnmarshalCanonicalAlias() (ca CanonicalAlias, err error) {
err = json.Unmarshal(content.VeryRaw, &ca)
return
}
func (content *Content) GetInfo() *FileInfo { func (content *Content) GetInfo() *FileInfo {
if content.Info == nil { if content.Info == nil {
content.Info = &FileInfo{} content.Info = &FileInfo{}
@ -260,14 +255,6 @@ type Tags map[string]struct {
Order json.Number `json:"order"` Order json.Number `json:"order"`
} }
type RoomName struct {
Name string `json:"name,omitempty"`
}
type RoomTopic struct {
Topic string `json:"topic,omitempty"`
}
// Membership is an enum specifying the membership state of a room member. // Membership is an enum specifying the membership state of a room member.
type Membership string type Membership string
@ -297,10 +284,6 @@ type ThirdPartyInvite struct {
} }
} }
type CanonicalAlias struct {
Alias string `json:"alias,omitempty"`
}
type PowerLevels struct { type PowerLevels struct {
usersLock sync.RWMutex `json:"-"` usersLock sync.RWMutex `json:"-"`
Users map[string]int `json:"users,omitempty"` Users map[string]int `json:"users,omitempty"`