-
Notifications
You must be signed in to change notification settings - Fork 4
/
plugin_test.go
183 lines (151 loc) · 4.38 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func TestMissingDefaultConfig(t *testing.T) {
var plugin Plugin
err := plugin.Exec()
assert.NotNil(t, err)
}
func TestDefaultMessageFormat(t *testing.T) {
plugin := Plugin{
Repo: Repo{
FullName: "appleboy/go-hello",
Name: "go-hello",
Namespace: "appleboy",
},
Commit: Commit{
Sha: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2",
Author: "Bo-Yi Wu",
Branch: "master",
Message: "update travis by drone plugin",
Email: "[email protected]",
},
Build: Build{
Tag: "1.0.0",
Number: 101,
Status: "success",
Link: "https://github.com/appleboy/go-hello",
},
}
message := plugin.Message()
assert.Equal(t, []string{"[success] <https://github.com/appleboy/go-hello> (master)『update travis by drone plugin』by Bo-Yi Wu"}, message)
}
func TestErrorTemplate(t *testing.T) {
plugin := Plugin{
Config: Config{
PageToken: os.Getenv("FB_PAGE_TOKEN"),
VerifyToken: os.Getenv("FB_VERIFY_TOKEN"),
Verify: false,
To: []string{"1234567890"},
Message: []string{"file://xxxxx/xxxxx"},
},
}
err := plugin.Exec()
assert.NoError(t, err)
}
func TestSendMessage(t *testing.T) {
plugin := Plugin{
Repo: Repo{
FullName: "appleboy/go-hello",
Name: "go-hello",
Namespace: "appleboy",
},
Commit: Commit{
Sha: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2",
Author: "Bo-Yi Wu",
Branch: "master",
Message: "update travis by drone plugin",
Email: "[email protected]",
},
Build: Build{
Tag: "1.0.0",
Number: 101,
Status: "success",
Link: "https://github.com/appleboy/go-hello",
},
Config: Config{
PageToken: os.Getenv("FB_PAGE_TOKEN"),
VerifyToken: os.Getenv("FB_VERIFY_TOKEN"),
Verify: false,
To: []string{os.Getenv("FB_TO")},
Message: []string{"Test Facebook Bot From Travis or Local from {{ build.author }}", " "},
Image: []string{"https://cdn3.iconfinder.com/data/icons/picons-social/57/16-apple-256.png"},
// Audio: []string{"https://ia802508.us.archive.org/5/items/testmp3testfile/mpthreetest.mp3"},
// Video: []string{"https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4"},
// File: []string{"http://open.qiniudn.com/where-can-you-use-golang.pdf"},
},
}
err := plugin.Exec()
assert.Nil(t, err)
// disable message
plugin.Config.Message = []string{}
err = plugin.Exec()
assert.Nil(t, err)
}
func TestDefaultMessageFormatFromGitHub(t *testing.T) {
plugin := Plugin{
Config: Config{
GitHub: true,
},
Repo: Repo{
FullName: "appleboy/go-hello",
Name: "go-hello",
Namespace: "appleboy",
},
GitHub: GitHub{
Workflow: "test-workflow",
Action: "send notification",
EventName: "push",
},
}
message := plugin.Message()
assert.Equal(t, []string{"appleboy/go-hello/test-workflow triggered by appleboy (push)"}, message)
}
func TestTrimElement(t *testing.T) {
var input, result []string
input = []string{"1", " ", "3"}
result = []string{"1", "3"}
assert.Equal(t, result, trimElement(input))
input = []string{"1", "2"}
result = []string{"1", "2"}
assert.Equal(t, result, trimElement(input))
}
func TestParseTo(t *testing.T) {
input := []string{"0", "1:[email protected]", "2:[email protected]", "3:[email protected]", "4", "5"}
ids := parseTo(input, "[email protected]", false)
assert.Equal(t, []int64{0, 4, 5, 1}, ids)
ids = parseTo(input, "[email protected]", true)
assert.Equal(t, []int64{1}, ids)
ids = parseTo(input, "[email protected]", false)
assert.Equal(t, []int64{0, 4, 5}, ids)
ids = parseTo(input, "[email protected]", true)
assert.Equal(t, []int64{0, 4, 5}, ids)
// test empty ids
ids = parseTo([]string{"", " ", " "}, "[email protected]", true)
assert.Equal(t, 0, len(ids))
}
func performRequest(r http.Handler, method, url string) *httptest.ResponseRecorder {
req, _ := http.NewRequest(method, url, nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
}
func TestDefaultRouter(t *testing.T) {
p := Plugin{
Config: Config{
PageToken: os.Getenv("FB_PAGE_TOKEN"),
VerifyToken: os.Getenv("FB_VERIFY_TOKEN"),
Verify: false,
},
}
router := p.serveMux()
w := performRequest(router, "GET", "/")
assert.Equal(t, "Welcome to facebook webhook page.\n", w.Body.String())
w = performRequest(router, "GET", "/metrics")
assert.Equal(t, 200, w.Code)
}