forked from abaschen/mattermost-xkcd-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_test.go
115 lines (103 loc) · 3.26 KB
/
plugin_test.go
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/plugin"
"github.com/mattermost/mattermost-server/plugin/plugintest"
"github.com/mattermost/mattermost-server/plugin/plugintest/mock"
)
type TestConfiguration struct {
StrictTrigger bool
Debug bool
Client *http.Client
}
func TestPlugin(t *testing.T) {
jsonComic := `{"month": "10", "num": 2057, "link": "", "year": "2018", "news": "", "safe_title": "Internal Monologues", "transcript": "", "alt": "Haha, just kidding, everyone's already been hacked. I wonder if today's the day we find out about it.", "img": "https://imgs.xkcd.com/comics/internal_monologues.png", "title": "Internal Monologues", "day": "10"}`
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, jsonComic)
}))
defer ts.Close()
client := ts.Client()
validConfiguration := TestConfiguration{
StrictTrigger: false,
Debug: true,
Client: client,
}
validConfigurationStrict := TestConfiguration{
StrictTrigger: true,
Debug: true,
Client: client,
}
for name, tc := range map[string]struct {
Configuration TestConfiguration
Message string
ExpectedComicNum int
ExpectedComic bool
}{
"ValidConfiguration": {
Configuration: validConfiguration,
Message: "foo https://xkcd.com/2057/ bar",
ExpectedComicNum: 2057,
ExpectedComic: true,
},
"BadUrl": {
Configuration: validConfiguration,
Message: "foo https://xdkcd.com/2057/ bar",
ExpectedComic: false,
},
"GoodUrlStrictSkip": {
Configuration: validConfigurationStrict,
Message: "foo https://xkcd.com/2057/ bar",
ExpectedComic: false,
},
"GoodUrlStrictSkipTail": {
Configuration: validConfigurationStrict,
Message: "https://xkcd.com/2057/ foooooo",
ExpectedComic: false,
},
"GoodUrlStrictSkipHead": {
Configuration: validConfigurationStrict,
Message: "Some head https://xkcd.com/2057/",
ExpectedComic: false,
},
"GoodUrlStrict": {
Configuration: validConfigurationStrict,
Message: "https://xkcd.com/2057/",
ExpectedComicNum: 2057,
ExpectedComic: true,
},
} {
t.Run(name, func(t *testing.T) {
api := &plugintest.API{}
api.On("CreatePost", mock.AnythingOfType("*model.Post")).Return(&model.Post{})
p := XKCDPlugin{}
p.Debug = tc.Configuration.Debug
p.StrictTrigger = tc.Configuration.StrictTrigger
p.SetAPI(api)
post := model.Post{
Message: tc.Message,
}
result, sErr := p.MessageWillBePosted(&plugin.Context{}, &post)
assert.Equal(t, sErr, "")
if tc.ExpectedComic {
assert.NotNil(t, result)
assert.Equal(t, tc.ExpectedComic, result.Props["xkcd"] != nil)
attachments := result.Attachments()
assert.NotNil(t, attachments)
for _, a := range attachments {
if a == nil {
continue
}
assert.Equal(t, "https://xkcd.com/"+strconv.Itoa(tc.ExpectedComicNum)+"/", a.TitleLink)
}
} else {
assert.Nil(t, result)
}
})
}
}