gomuks/matrix/pushrules/pushrules_test.go

250 lines
6.1 KiB
Go
Raw Normal View History

2018-04-10 19:50:37 +02:00
// gomuks - A terminal Matrix client written in Go.
2019-01-17 13:13:25 +01:00
// Copyright (C) 2019 Tulir Asokan
2018-04-10 19:50:37 +02:00
//
// This program is free software: you can redistribute it and/or modify
2019-01-17 13:13:25 +01:00
// it under the terms of the GNU Affero General Public License as published by
2018-04-10 19:50:37 +02:00
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2019-01-17 13:13:25 +01:00
// GNU Affero General Public License for more details.
2018-04-10 19:50:37 +02:00
//
2019-01-17 13:13:25 +01:00
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-04-10 19:50:37 +02:00
package pushrules_test
2018-04-10 19:50:37 +02:00
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
2018-11-13 23:00:35 +01:00
"maunium.net/go/mautrix"
"maunium.net/go/gomuks/matrix/pushrules"
2018-04-10 19:50:37 +02:00
)
func TestEventToPushRules(t *testing.T) {
2018-11-13 23:00:35 +01:00
event := &mautrix.Event{
2018-11-14 00:11:40 +01:00
Type: mautrix.AccountDataPushRules,
2018-04-10 19:50:37 +02:00
Timestamp: 1523380910,
2018-11-14 00:11:40 +01:00
Content: mautrix.Content{
VeryRaw: json.RawMessage(JSONExamplePushRules),
},
2018-04-10 19:50:37 +02:00
}
pushRuleset, err := pushrules.EventToPushRules(event)
2018-04-10 19:50:37 +02:00
assert.Nil(t, err)
assert.NotNil(t, pushRuleset)
assert.IsType(t, pushRuleset.Override, pushrules.PushRuleArray{})
assert.IsType(t, pushRuleset.Content, pushrules.PushRuleArray{})
assert.IsType(t, pushRuleset.Room, pushrules.PushRuleMap{})
assert.IsType(t, pushRuleset.Sender, pushrules.PushRuleMap{})
assert.IsType(t, pushRuleset.Underride, pushrules.PushRuleArray{})
2018-04-10 19:50:37 +02:00
assert.Len(t, pushRuleset.Override, 2)
assert.Len(t, pushRuleset.Content, 1)
assert.Empty(t, pushRuleset.Room.Map)
assert.Empty(t, pushRuleset.Sender.Map)
assert.Len(t, pushRuleset.Underride, 6)
assert.Len(t, pushRuleset.Content[0].Actions, 3)
assert.True(t, pushRuleset.Content[0].Default)
assert.True(t, pushRuleset.Content[0].Enabled)
assert.Empty(t, pushRuleset.Content[0].Conditions)
assert.Equal(t, "alice", pushRuleset.Content[0].Pattern)
assert.Equal(t, ".m.rule.contains_user_name", pushRuleset.Content[0].RuleID)
assert.False(t, pushRuleset.Override[0].Actions.Should().Notify)
assert.True(t, pushRuleset.Override[0].Actions.Should().NotifySpecified)
}
const JSONExamplePushRules = `{
"global": {
"content": [
{
"actions": [
"notify",
{
"set_tweak": "sound",
"value": "default"
},
{
"set_tweak": "highlight"
}
],
"default": true,
"enabled": true,
"pattern": "alice",
"rule_id": ".m.rule.contains_user_name"
}
],
"override": [
{
"actions": [
"dont_notify"
],
"conditions": [],
"default": true,
"enabled": false,
"rule_id": ".m.rule.master"
},
{
"actions": [
"dont_notify"
],
"conditions": [
{
"key": "content.msgtype",
"kind": "event_match",
"pattern": "m.notice"
}
],
"default": true,
"enabled": true,
"rule_id": ".m.rule.suppress_notices"
}
],
"room": [],
"sender": [],
"underride": [
{
"actions": [
"notify",
{
"set_tweak": "sound",
"value": "ring"
},
{
"set_tweak": "highlight",
"value": false
}
],
"conditions": [
{
"key": "type",
"kind": "event_match",
"pattern": "m.call.invite"
}
],
"default": true,
"enabled": true,
"rule_id": ".m.rule.call"
},
{
"actions": [
"notify",
{
"set_tweak": "sound",
"value": "default"
},
{
"set_tweak": "highlight"
}
],
"conditions": [
{
"kind": "contains_display_name"
}
],
"default": true,
"enabled": true,
"rule_id": ".m.rule.contains_display_name"
},
{
"actions": [
"notify",
{
"set_tweak": "sound",
"value": "default"
},
{
"set_tweak": "highlight",
"value": false
}
],
"conditions": [
{
"is": "2",
"kind": "room_member_count"
}
],
"default": true,
"enabled": true,
"rule_id": ".m.rule.room_one_to_one"
},
{
"actions": [
"notify",
{
"set_tweak": "sound",
"value": "default"
},
{
"set_tweak": "highlight",
"value": false
}
],
"conditions": [
{
"key": "type",
"kind": "event_match",
"pattern": "m.room.member"
},
{
"key": "content.membership",
"kind": "event_match",
"pattern": "invite"
},
{
"key": "state_key",
"kind": "event_match",
"pattern": "@alice:example.com"
}
],
"default": true,
"enabled": true,
"rule_id": ".m.rule.invite_for_me"
},
{
"actions": [
"notify",
{
"set_tweak": "highlight",
"value": false
}
],
"conditions": [
{
"key": "type",
"kind": "event_match",
"pattern": "m.room.member"
}
],
"default": true,
"enabled": true,
"rule_id": ".m.rule.member_event"
},
{
"actions": [
"notify",
{
"set_tweak": "highlight",
"value": false
}
],
"conditions": [
{
"key": "type",
"kind": "event_match",
"pattern": "m.room.message"
}
],
"default": true,
"enabled": true,
"rule_id": ".m.rule.message"
}
]
}
}`