2017-07-25 18:11:52 +00:00
|
|
|
package gateway
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-02-23 21:51:27 +00:00
|
|
|
"io/ioutil"
|
2018-06-09 10:47:40 +00:00
|
|
|
"strconv"
|
2018-12-26 14:16:09 +00:00
|
|
|
"testing"
|
2018-06-09 10:47:40 +00:00
|
|
|
|
2017-07-25 18:11:52 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2018-11-30 22:53:00 +00:00
|
|
|
"github.com/42wim/matterbridge/gateway/bridgemap"
|
2019-02-23 21:51:27 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-07-25 18:11:52 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-02-23 21:51:27 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2017-07-25 18:11:52 +00:00
|
|
|
)
|
|
|
|
|
2018-03-04 23:30:46 +00:00
|
|
|
var testconfig = []byte(`
|
2017-07-25 18:11:52 +00:00
|
|
|
[irc.freenode]
|
2019-09-09 21:48:00 +00:00
|
|
|
server=""
|
2017-07-25 18:11:52 +00:00
|
|
|
[mattermost.test]
|
2019-09-09 21:48:00 +00:00
|
|
|
server=""
|
2017-07-25 18:11:52 +00:00
|
|
|
[gitter.42wim]
|
2019-09-09 21:48:00 +00:00
|
|
|
server=""
|
2017-07-25 18:11:52 +00:00
|
|
|
[discord.test]
|
2019-09-09 21:48:00 +00:00
|
|
|
server=""
|
2017-07-25 18:11:52 +00:00
|
|
|
[slack.test]
|
2019-09-09 21:48:00 +00:00
|
|
|
server=""
|
2017-07-25 18:11:52 +00:00
|
|
|
|
|
|
|
[[gateway]]
|
|
|
|
name = "bridge1"
|
|
|
|
enable=true
|
|
|
|
|
|
|
|
[[gateway.inout]]
|
|
|
|
account = "irc.freenode"
|
|
|
|
channel = "#wimtesting"
|
|
|
|
|
|
|
|
[[gateway.inout]]
|
|
|
|
account="gitter.42wim"
|
|
|
|
channel="42wim/testroom"
|
|
|
|
#channel="matterbridge/Lobby"
|
|
|
|
|
|
|
|
[[gateway.inout]]
|
|
|
|
account = "discord.test"
|
|
|
|
channel = "general"
|
|
|
|
|
|
|
|
[[gateway.inout]]
|
|
|
|
account="slack.test"
|
|
|
|
channel="testing"
|
2018-03-04 23:30:46 +00:00
|
|
|
`)
|
2017-07-25 18:11:52 +00:00
|
|
|
|
2018-03-04 23:30:46 +00:00
|
|
|
var testconfig2 = []byte(`
|
2017-07-25 18:11:52 +00:00
|
|
|
[irc.freenode]
|
2019-09-09 21:48:00 +00:00
|
|
|
server=""
|
2017-07-25 18:11:52 +00:00
|
|
|
[mattermost.test]
|
2019-09-09 21:48:00 +00:00
|
|
|
server=""
|
2017-07-25 18:11:52 +00:00
|
|
|
[gitter.42wim]
|
2019-09-09 21:48:00 +00:00
|
|
|
server=""
|
2017-07-25 18:11:52 +00:00
|
|
|
[discord.test]
|
2019-09-09 21:48:00 +00:00
|
|
|
server=""
|
2017-07-25 18:11:52 +00:00
|
|
|
[slack.test]
|
2019-09-09 21:48:00 +00:00
|
|
|
server=""
|
2017-07-25 18:11:52 +00:00
|
|
|
|
|
|
|
[[gateway]]
|
|
|
|
name = "bridge1"
|
|
|
|
enable=true
|
|
|
|
|
|
|
|
[[gateway.in]]
|
|
|
|
account = "irc.freenode"
|
|
|
|
channel = "#wimtesting"
|
|
|
|
|
|
|
|
[[gateway.in]]
|
|
|
|
account="gitter.42wim"
|
|
|
|
channel="42wim/testroom"
|
|
|
|
|
|
|
|
[[gateway.inout]]
|
|
|
|
account = "discord.test"
|
|
|
|
channel = "general"
|
|
|
|
|
|
|
|
[[gateway.out]]
|
|
|
|
account="slack.test"
|
|
|
|
channel="testing"
|
|
|
|
[[gateway]]
|
|
|
|
name = "bridge2"
|
|
|
|
enable=true
|
|
|
|
|
|
|
|
[[gateway.in]]
|
|
|
|
account = "irc.freenode"
|
|
|
|
channel = "#wimtesting2"
|
|
|
|
|
|
|
|
[[gateway.out]]
|
|
|
|
account="gitter.42wim"
|
|
|
|
channel="42wim/testroom"
|
|
|
|
|
|
|
|
[[gateway.out]]
|
|
|
|
account = "discord.test"
|
|
|
|
channel = "general2"
|
2018-03-04 23:30:46 +00:00
|
|
|
`)
|
|
|
|
|
|
|
|
var testconfig3 = []byte(`
|
2017-07-25 18:11:52 +00:00
|
|
|
[irc.zzz]
|
2019-09-09 21:48:00 +00:00
|
|
|
server=""
|
2017-07-25 18:11:52 +00:00
|
|
|
[telegram.zzz]
|
2019-09-09 21:48:00 +00:00
|
|
|
server=""
|
2017-07-25 18:11:52 +00:00
|
|
|
[slack.zzz]
|
2019-09-09 21:48:00 +00:00
|
|
|
server=""
|
2017-07-25 18:11:52 +00:00
|
|
|
[[gateway]]
|
|
|
|
name="bridge"
|
|
|
|
enable=true
|
|
|
|
|
|
|
|
[[gateway.inout]]
|
|
|
|
account="irc.zzz"
|
|
|
|
channel="#main"
|
|
|
|
|
|
|
|
[[gateway.inout]]
|
|
|
|
account="telegram.zzz"
|
|
|
|
channel="-1111111111111"
|
|
|
|
|
|
|
|
[[gateway.inout]]
|
|
|
|
account="slack.zzz"
|
|
|
|
channel="irc"
|
|
|
|
|
|
|
|
[[gateway]]
|
|
|
|
name="announcements"
|
|
|
|
enable=true
|
|
|
|
|
|
|
|
[[gateway.in]]
|
|
|
|
account="telegram.zzz"
|
|
|
|
channel="-2222222222222"
|
|
|
|
|
|
|
|
[[gateway.out]]
|
|
|
|
account="irc.zzz"
|
|
|
|
channel="#main"
|
|
|
|
|
|
|
|
[[gateway.out]]
|
|
|
|
account="irc.zzz"
|
|
|
|
channel="#main-help"
|
|
|
|
|
|
|
|
[[gateway.out]]
|
|
|
|
account="telegram.zzz"
|
|
|
|
channel="--333333333333"
|
|
|
|
|
|
|
|
[[gateway.out]]
|
|
|
|
account="slack.zzz"
|
|
|
|
channel="general"
|
|
|
|
|
|
|
|
[[gateway]]
|
|
|
|
name="bridge2"
|
|
|
|
enable=true
|
|
|
|
|
|
|
|
[[gateway.inout]]
|
|
|
|
account="irc.zzz"
|
|
|
|
channel="#main-help"
|
|
|
|
|
|
|
|
[[gateway.inout]]
|
|
|
|
account="telegram.zzz"
|
|
|
|
channel="--444444444444"
|
|
|
|
|
|
|
|
|
|
|
|
[[gateway]]
|
|
|
|
name="bridge3"
|
|
|
|
enable=true
|
|
|
|
|
|
|
|
[[gateway.inout]]
|
|
|
|
account="irc.zzz"
|
|
|
|
channel="#main-telegram"
|
|
|
|
|
|
|
|
[[gateway.inout]]
|
|
|
|
account="telegram.zzz"
|
|
|
|
channel="--333333333333"
|
2018-03-04 23:30:46 +00:00
|
|
|
`)
|
2017-07-25 18:11:52 +00:00
|
|
|
|
2018-11-08 21:20:03 +00:00
|
|
|
const (
|
|
|
|
ircTestAccount = "irc.zzz"
|
|
|
|
tgTestAccount = "telegram.zzz"
|
|
|
|
slackTestAccount = "slack.zzz"
|
|
|
|
)
|
|
|
|
|
2018-03-04 23:30:46 +00:00
|
|
|
func maketestRouter(input []byte) *Router {
|
2019-02-23 21:51:27 +00:00
|
|
|
logger := logrus.New()
|
|
|
|
logger.SetOutput(ioutil.Discard)
|
|
|
|
cfg := config.NewConfigFromString(logger, input)
|
|
|
|
r, err := NewRouter(logger, cfg, bridgemap.FullMap)
|
2017-07-25 18:11:52 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
func TestNewRouter(t *testing.T) {
|
2018-03-04 23:30:46 +00:00
|
|
|
r := maketestRouter(testconfig)
|
2017-07-25 18:11:52 +00:00
|
|
|
assert.Equal(t, 1, len(r.Gateways))
|
|
|
|
assert.Equal(t, 4, len(r.Gateways["bridge1"].Bridges))
|
|
|
|
assert.Equal(t, 4, len(r.Gateways["bridge1"].Channels))
|
|
|
|
r = maketestRouter(testconfig2)
|
|
|
|
assert.Equal(t, 2, len(r.Gateways))
|
|
|
|
assert.Equal(t, 4, len(r.Gateways["bridge1"].Bridges))
|
|
|
|
assert.Equal(t, 3, len(r.Gateways["bridge2"].Bridges))
|
|
|
|
assert.Equal(t, 4, len(r.Gateways["bridge1"].Channels))
|
|
|
|
assert.Equal(t, 3, len(r.Gateways["bridge2"].Channels))
|
2018-10-12 22:47:18 +00:00
|
|
|
assert.Equal(t, &config.ChannelInfo{
|
|
|
|
Name: "42wim/testroom",
|
|
|
|
Direction: "out",
|
|
|
|
ID: "42wim/testroomgitter.42wim",
|
|
|
|
Account: "gitter.42wim",
|
|
|
|
SameChannel: map[string]bool{"bridge2": false},
|
|
|
|
}, r.Gateways["bridge2"].Channels["42wim/testroomgitter.42wim"])
|
|
|
|
assert.Equal(t, &config.ChannelInfo{
|
|
|
|
Name: "42wim/testroom",
|
|
|
|
Direction: "in",
|
|
|
|
ID: "42wim/testroomgitter.42wim",
|
|
|
|
Account: "gitter.42wim",
|
|
|
|
SameChannel: map[string]bool{"bridge1": false},
|
|
|
|
}, r.Gateways["bridge1"].Channels["42wim/testroomgitter.42wim"])
|
|
|
|
assert.Equal(t, &config.ChannelInfo{
|
|
|
|
Name: "general",
|
|
|
|
Direction: "inout",
|
|
|
|
ID: "generaldiscord.test",
|
|
|
|
Account: "discord.test",
|
|
|
|
SameChannel: map[string]bool{"bridge1": false},
|
|
|
|
}, r.Gateways["bridge1"].Channels["generaldiscord.test"])
|
2017-07-25 18:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetDestChannel(t *testing.T) {
|
|
|
|
r := maketestRouter(testconfig2)
|
|
|
|
msg := &config.Message{Text: "test", Channel: "general", Account: "discord.test", Gateway: "bridge1", Protocol: "discord", Username: "test"}
|
|
|
|
for _, br := range r.Gateways["bridge1"].Bridges {
|
|
|
|
switch br.Account {
|
|
|
|
case "discord.test":
|
2018-10-12 22:47:18 +00:00
|
|
|
assert.Equal(t, []config.ChannelInfo{{
|
|
|
|
Name: "general",
|
|
|
|
Account: "discord.test",
|
|
|
|
Direction: "inout",
|
|
|
|
ID: "generaldiscord.test",
|
|
|
|
SameChannel: map[string]bool{"bridge1": false},
|
|
|
|
Options: config.ChannelOptions{Key: ""},
|
|
|
|
}}, r.Gateways["bridge1"].getDestChannel(msg, *br))
|
2017-07-25 18:11:52 +00:00
|
|
|
case "slack.test":
|
2018-10-12 22:47:18 +00:00
|
|
|
assert.Equal(t, []config.ChannelInfo{{
|
|
|
|
Name: "testing",
|
|
|
|
Account: "slack.test",
|
|
|
|
Direction: "out",
|
|
|
|
ID: "testingslack.test",
|
|
|
|
SameChannel: map[string]bool{"bridge1": false},
|
|
|
|
Options: config.ChannelOptions{Key: ""},
|
|
|
|
}}, r.Gateways["bridge1"].getDestChannel(msg, *br))
|
2017-07-25 18:11:52 +00:00
|
|
|
case "gitter.42wim":
|
|
|
|
assert.Equal(t, []config.ChannelInfo(nil), r.Gateways["bridge1"].getDestChannel(msg, *br))
|
|
|
|
case "irc.freenode":
|
|
|
|
assert.Equal(t, []config.ChannelInfo(nil), r.Gateways["bridge1"].getDestChannel(msg, *br))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetDestChannelAdvanced(t *testing.T) {
|
|
|
|
r := maketestRouter(testconfig3)
|
|
|
|
var msgs []*config.Message
|
|
|
|
i := 0
|
|
|
|
for _, gw := range r.Gateways {
|
|
|
|
for _, channel := range gw.Channels {
|
|
|
|
msgs = append(msgs, &config.Message{Text: "text" + strconv.Itoa(i), Channel: channel.Name, Account: channel.Account, Gateway: gw.Name, Username: "user" + strconv.Itoa(i)})
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hits := make(map[string]int)
|
|
|
|
for _, gw := range r.Gateways {
|
|
|
|
for _, br := range gw.Bridges {
|
|
|
|
for _, msg := range msgs {
|
|
|
|
channels := gw.getDestChannel(msg, *br)
|
|
|
|
if gw.Name != msg.Gateway {
|
|
|
|
assert.Equal(t, []config.ChannelInfo(nil), channels)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch gw.Name {
|
|
|
|
case "bridge":
|
2018-10-12 22:47:18 +00:00
|
|
|
if (msg.Channel == "#main" || msg.Channel == "-1111111111111" || msg.Channel == "irc") &&
|
2018-11-08 21:20:03 +00:00
|
|
|
(msg.Account == ircTestAccount || msg.Account == tgTestAccount || msg.Account == slackTestAccount) {
|
2017-07-25 18:11:52 +00:00
|
|
|
hits[gw.Name]++
|
|
|
|
switch br.Account {
|
2018-11-08 21:20:03 +00:00
|
|
|
case ircTestAccount:
|
2018-10-12 22:47:18 +00:00
|
|
|
assert.Equal(t, []config.ChannelInfo{{
|
|
|
|
Name: "#main",
|
2018-11-08 21:20:03 +00:00
|
|
|
Account: ircTestAccount,
|
2018-10-12 22:47:18 +00:00
|
|
|
Direction: "inout",
|
|
|
|
ID: "#mainirc.zzz",
|
|
|
|
SameChannel: map[string]bool{"bridge": false},
|
|
|
|
Options: config.ChannelOptions{Key: ""},
|
|
|
|
}}, channels)
|
2018-11-08 21:20:03 +00:00
|
|
|
case tgTestAccount:
|
2018-10-12 22:47:18 +00:00
|
|
|
assert.Equal(t, []config.ChannelInfo{{
|
|
|
|
Name: "-1111111111111",
|
2018-11-08 21:20:03 +00:00
|
|
|
Account: tgTestAccount,
|
2018-10-12 22:47:18 +00:00
|
|
|
Direction: "inout",
|
|
|
|
ID: "-1111111111111telegram.zzz",
|
|
|
|
SameChannel: map[string]bool{"bridge": false},
|
|
|
|
Options: config.ChannelOptions{Key: ""},
|
|
|
|
}}, channels)
|
2018-11-08 21:20:03 +00:00
|
|
|
case slackTestAccount:
|
2018-10-12 22:47:18 +00:00
|
|
|
assert.Equal(t, []config.ChannelInfo{{
|
|
|
|
Name: "irc",
|
2018-11-08 21:20:03 +00:00
|
|
|
Account: slackTestAccount,
|
2018-10-12 22:47:18 +00:00
|
|
|
Direction: "inout",
|
|
|
|
ID: "ircslack.zzz",
|
|
|
|
SameChannel: map[string]bool{"bridge": false},
|
|
|
|
Options: config.ChannelOptions{Key: ""},
|
|
|
|
}}, channels)
|
2017-07-25 18:11:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case "bridge2":
|
2018-10-12 22:47:18 +00:00
|
|
|
if (msg.Channel == "#main-help" || msg.Channel == "--444444444444") &&
|
2018-11-08 21:20:03 +00:00
|
|
|
(msg.Account == ircTestAccount || msg.Account == tgTestAccount) {
|
2017-07-25 18:11:52 +00:00
|
|
|
hits[gw.Name]++
|
|
|
|
switch br.Account {
|
2018-11-08 21:20:03 +00:00
|
|
|
case ircTestAccount:
|
2018-10-12 22:47:18 +00:00
|
|
|
assert.Equal(t, []config.ChannelInfo{{
|
|
|
|
Name: "#main-help",
|
2018-11-08 21:20:03 +00:00
|
|
|
Account: ircTestAccount,
|
2018-10-12 22:47:18 +00:00
|
|
|
Direction: "inout",
|
|
|
|
ID: "#main-helpirc.zzz",
|
|
|
|
SameChannel: map[string]bool{"bridge2": false},
|
|
|
|
Options: config.ChannelOptions{Key: ""},
|
|
|
|
}}, channels)
|
2018-11-08 21:20:03 +00:00
|
|
|
case tgTestAccount:
|
2018-10-12 22:47:18 +00:00
|
|
|
assert.Equal(t, []config.ChannelInfo{{
|
|
|
|
Name: "--444444444444",
|
2018-11-08 21:20:03 +00:00
|
|
|
Account: tgTestAccount,
|
2018-10-12 22:47:18 +00:00
|
|
|
Direction: "inout",
|
|
|
|
ID: "--444444444444telegram.zzz",
|
|
|
|
SameChannel: map[string]bool{"bridge2": false},
|
|
|
|
Options: config.ChannelOptions{Key: ""},
|
|
|
|
}}, channels)
|
2017-07-25 18:11:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case "bridge3":
|
2018-10-12 22:47:18 +00:00
|
|
|
if (msg.Channel == "#main-telegram" || msg.Channel == "--333333333333") &&
|
2018-11-08 21:20:03 +00:00
|
|
|
(msg.Account == ircTestAccount || msg.Account == tgTestAccount) {
|
2017-07-25 18:11:52 +00:00
|
|
|
hits[gw.Name]++
|
|
|
|
switch br.Account {
|
2018-11-08 21:20:03 +00:00
|
|
|
case ircTestAccount:
|
2018-10-12 22:47:18 +00:00
|
|
|
assert.Equal(t, []config.ChannelInfo{{
|
|
|
|
Name: "#main-telegram",
|
2018-11-08 21:20:03 +00:00
|
|
|
Account: ircTestAccount,
|
2018-10-12 22:47:18 +00:00
|
|
|
Direction: "inout",
|
|
|
|
ID: "#main-telegramirc.zzz",
|
|
|
|
SameChannel: map[string]bool{"bridge3": false},
|
|
|
|
Options: config.ChannelOptions{Key: ""},
|
|
|
|
}}, channels)
|
2018-11-08 21:20:03 +00:00
|
|
|
case tgTestAccount:
|
2018-10-12 22:47:18 +00:00
|
|
|
assert.Equal(t, []config.ChannelInfo{{
|
|
|
|
Name: "--333333333333",
|
2018-11-08 21:20:03 +00:00
|
|
|
Account: tgTestAccount,
|
2018-10-12 22:47:18 +00:00
|
|
|
Direction: "inout",
|
|
|
|
ID: "--333333333333telegram.zzz",
|
|
|
|
SameChannel: map[string]bool{"bridge3": false},
|
|
|
|
Options: config.ChannelOptions{Key: ""},
|
|
|
|
}}, channels)
|
2017-07-25 18:11:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case "announcements":
|
|
|
|
if msg.Channel != "-2222222222222" && msg.Account != "telegram" {
|
|
|
|
assert.Equal(t, []config.ChannelInfo(nil), channels)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
hits[gw.Name]++
|
|
|
|
switch br.Account {
|
2018-11-08 21:20:03 +00:00
|
|
|
case ircTestAccount:
|
2018-10-12 22:47:18 +00:00
|
|
|
assert.Len(t, channels, 2)
|
|
|
|
assert.Contains(t, channels, config.ChannelInfo{
|
|
|
|
Name: "#main",
|
2018-11-08 21:20:03 +00:00
|
|
|
Account: ircTestAccount,
|
2018-10-12 22:47:18 +00:00
|
|
|
Direction: "out",
|
|
|
|
ID: "#mainirc.zzz",
|
|
|
|
SameChannel: map[string]bool{"announcements": false},
|
|
|
|
Options: config.ChannelOptions{Key: ""},
|
|
|
|
})
|
|
|
|
assert.Contains(t, channels, config.ChannelInfo{
|
|
|
|
Name: "#main-help",
|
2018-11-08 21:20:03 +00:00
|
|
|
Account: ircTestAccount,
|
2018-10-12 22:47:18 +00:00
|
|
|
Direction: "out",
|
|
|
|
ID: "#main-helpirc.zzz",
|
|
|
|
SameChannel: map[string]bool{"announcements": false},
|
|
|
|
Options: config.ChannelOptions{Key: ""},
|
|
|
|
})
|
2018-11-08 21:20:03 +00:00
|
|
|
case slackTestAccount:
|
2018-10-12 22:47:18 +00:00
|
|
|
assert.Equal(t, []config.ChannelInfo{{
|
|
|
|
Name: "general",
|
2018-11-08 21:20:03 +00:00
|
|
|
Account: slackTestAccount,
|
2018-10-12 22:47:18 +00:00
|
|
|
Direction: "out",
|
|
|
|
ID: "generalslack.zzz",
|
|
|
|
SameChannel: map[string]bool{"announcements": false},
|
|
|
|
Options: config.ChannelOptions{Key: ""},
|
|
|
|
}}, channels)
|
2018-11-08 21:20:03 +00:00
|
|
|
case tgTestAccount:
|
2018-10-12 22:47:18 +00:00
|
|
|
assert.Equal(t, []config.ChannelInfo{{
|
|
|
|
Name: "--333333333333",
|
2018-11-08 21:20:03 +00:00
|
|
|
Account: tgTestAccount,
|
2018-10-12 22:47:18 +00:00
|
|
|
Direction: "out",
|
|
|
|
ID: "--333333333333telegram.zzz",
|
|
|
|
SameChannel: map[string]bool{"announcements": false},
|
|
|
|
Options: config.ChannelOptions{Key: ""},
|
|
|
|
}}, channels)
|
2017-07-25 18:11:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Equal(t, map[string]int{"bridge3": 4, "bridge": 9, "announcements": 3, "bridge2": 4}, hits)
|
|
|
|
}
|
2018-12-12 22:57:17 +00:00
|
|
|
|
2019-02-23 21:51:27 +00:00
|
|
|
type ignoreTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
gw *Gateway
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIgnoreSuite(t *testing.T) {
|
|
|
|
s := &ignoreTestSuite{}
|
|
|
|
suite.Run(t, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ignoreTestSuite) SetupSuite() {
|
|
|
|
logger := logrus.New()
|
|
|
|
logger.SetOutput(ioutil.Discard)
|
|
|
|
s.gw = &Gateway{logger: logrus.NewEntry(logger)}
|
|
|
|
}
|
|
|
|
func (s *ignoreTestSuite) TestIgnoreTextEmpty() {
|
2018-12-12 22:57:17 +00:00
|
|
|
extraFile := make(map[string][]interface{})
|
|
|
|
extraAttach := make(map[string][]interface{})
|
|
|
|
extraFailure := make(map[string][]interface{})
|
|
|
|
extraFile["file"] = append(extraFile["file"], config.FileInfo{})
|
|
|
|
extraAttach["attachments"] = append(extraAttach["attachments"], []string{})
|
|
|
|
extraFailure[config.EventFileFailureSize] = append(extraFailure[config.EventFileFailureSize], config.FileInfo{})
|
|
|
|
|
|
|
|
msgTests := map[string]struct {
|
|
|
|
input *config.Message
|
|
|
|
output bool
|
|
|
|
}{
|
|
|
|
"usertyping": {
|
|
|
|
input: &config.Message{Event: config.EventUserTyping},
|
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
"file attach": {
|
|
|
|
input: &config.Message{Extra: extraFile},
|
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
"attachments": {
|
|
|
|
input: &config.Message{Extra: extraAttach},
|
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
config.EventFileFailureSize: {
|
|
|
|
input: &config.Message{Extra: extraFailure},
|
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
"nil extra": {
|
|
|
|
input: &config.Message{Extra: nil},
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
"empty": {
|
|
|
|
input: &config.Message{},
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for testname, testcase := range msgTests {
|
2019-02-23 21:51:27 +00:00
|
|
|
output := s.gw.ignoreTextEmpty(testcase.input)
|
|
|
|
s.Assert().Equalf(testcase.output, output, "case '%s' failed", testname)
|
2018-12-12 22:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-02-23 21:51:27 +00:00
|
|
|
func (s *ignoreTestSuite) TestIgnoreTexts() {
|
2018-12-12 22:57:17 +00:00
|
|
|
msgTests := map[string]struct {
|
2019-02-17 20:49:28 +00:00
|
|
|
input string
|
2018-12-12 22:57:17 +00:00
|
|
|
re []string
|
|
|
|
output bool
|
|
|
|
}{
|
|
|
|
"no regex": {
|
2019-02-17 20:49:28 +00:00
|
|
|
input: "a text message",
|
2018-12-12 22:57:17 +00:00
|
|
|
re: []string{},
|
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
"simple regex": {
|
2019-02-17 20:49:28 +00:00
|
|
|
input: "a text message",
|
2018-12-12 22:57:17 +00:00
|
|
|
re: []string{"text"},
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
"multiple regex fail": {
|
2019-02-17 20:49:28 +00:00
|
|
|
input: "a text message",
|
2018-12-12 22:57:17 +00:00
|
|
|
re: []string{"abc", "123$"},
|
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
"multiple regex pass": {
|
2019-02-17 20:49:28 +00:00
|
|
|
input: "a text message",
|
2018-12-12 22:57:17 +00:00
|
|
|
re: []string{"lala", "sage$"},
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for testname, testcase := range msgTests {
|
2019-02-23 21:51:27 +00:00
|
|
|
output := s.gw.ignoreText(testcase.input, testcase.re)
|
|
|
|
s.Assert().Equalf(testcase.output, output, "case '%s' failed", testname)
|
2018-12-12 22:57:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 21:51:27 +00:00
|
|
|
func (s *ignoreTestSuite) TestIgnoreNicks() {
|
2018-12-12 22:57:17 +00:00
|
|
|
msgTests := map[string]struct {
|
2019-02-17 20:49:28 +00:00
|
|
|
input string
|
2018-12-12 22:57:17 +00:00
|
|
|
re []string
|
|
|
|
output bool
|
|
|
|
}{
|
|
|
|
"no entry": {
|
2019-02-17 20:49:28 +00:00
|
|
|
input: "user",
|
2018-12-12 22:57:17 +00:00
|
|
|
re: []string{},
|
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
"one entry": {
|
2019-02-17 20:49:28 +00:00
|
|
|
input: "user",
|
2018-12-12 22:57:17 +00:00
|
|
|
re: []string{"user"},
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
"multiple entries": {
|
2019-02-17 20:49:28 +00:00
|
|
|
input: "user",
|
2018-12-12 22:57:17 +00:00
|
|
|
re: []string{"abc", "user"},
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
"multiple entries fail": {
|
2019-02-17 20:49:28 +00:00
|
|
|
input: "user",
|
2018-12-12 22:57:17 +00:00
|
|
|
re: []string{"abc", "def"},
|
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for testname, testcase := range msgTests {
|
2019-02-23 21:51:27 +00:00
|
|
|
output := s.gw.ignoreText(testcase.input, testcase.re)
|
|
|
|
s.Assert().Equalf(testcase.output, output, "case '%s' failed", testname)
|
2018-12-12 22:57:17 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-23 15:39:44 +00:00
|
|
|
|
|
|
|
func BenchmarkTengo(b *testing.B) {
|
|
|
|
msg := &config.Message{Username: "user", Text: "blah testing", Account: "protocol.account", Channel: "mychannel"}
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
err := modifyMessageTengo("bench.tengo", msg)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|