summaryrefslogtreecommitdiffstats
path: root/gateway/handlers_test.go
blob: db7988a9d324bb67dcc9fb33b3a43754549b8a9c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package gateway

import (
	"github.com/42wim/matterbridge/bridge"
	"github.com/42wim/matterbridge/bridge/config"
	"github.com/stretchr/testify/assert"

	"testing"
)

func TestIgnoreEvent(t *testing.T) {
	eventTests := map[string]struct {
		input  string
		dest   *bridge.Bridge
		output bool
	}{
		"avatar mattermost": {
			input:  config.EventAvatarDownload,
			dest:   &bridge.Bridge{Protocol: "mattermost"},
			output: false,
		},
		"avatar slack": {
			input:  config.EventAvatarDownload,
			dest:   &bridge.Bridge{Protocol: "slack"},
			output: true,
		},
		"avatar telegram": {
			input:  config.EventAvatarDownload,
			dest:   &bridge.Bridge{Protocol: "telegram"},
			output: false,
		},
	}
	gw := &Gateway{}
	for testname, testcase := range eventTests {
		output := gw.ignoreEvent(testcase.input, testcase.dest)
		assert.Equalf(t, testcase.output, output, "case '%s' failed", testname)
	}

}

func TestExtractNick(t *testing.T) {
	eventTests := map[string]struct {
		search         string
		extract        string
		username       string
		text           string
		resultUsername string
		resultText     string
	}{
		"test1": {
			search:         "fromgitter",
			extract:        "<(.*?)>\\s+",
			username:       "fromgitter",
			text:           "<userx> blahblah",
			resultUsername: "userx",
			resultText:     "blahblah",
		},
		"test2": {
			search: "<.*?bot>",
			//extract:        `\((.*?)\)\s+`,
			extract:        "\\((.*?)\\)\\s+",
			username:       "<matterbot>",
			text:           "(userx) blahblah (abc) test",
			resultUsername: "userx",
			resultText:     "blahblah (abc) test",
		},
	}
	//	gw := &Gateway{}
	for testname, testcase := range eventTests {
		resultUsername, resultText, _ := extractNick(testcase.search, testcase.extract, testcase.username, testcase.text)
		assert.Equalf(t, testcase.resultUsername, resultUsername, "case '%s' failed", testname)
		assert.Equalf(t, testcase.resultText, resultText, "case '%s' failed", testname)
	}

}