2016-10-04 16:45:49 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
2017-07-04 07:11:48 +03:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2016-10-04 16:45:49 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMissingDefaultConfig(t *testing.T) {
|
|
|
|
var plugin Plugin
|
|
|
|
|
|
|
|
err := plugin.Exec()
|
|
|
|
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMissingUserConfig(t *testing.T) {
|
|
|
|
plugin := Plugin{
|
|
|
|
Config: Config{
|
|
|
|
Token: "123456789",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := plugin.Exec()
|
|
|
|
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultMessageFormat(t *testing.T) {
|
|
|
|
plugin := Plugin{
|
|
|
|
Repo: Repo{
|
2019-12-11 02:24:10 +03:00
|
|
|
FullName: "appleboy/go-hello",
|
2019-02-17 10:39:47 +03:00
|
|
|
Name: "go-hello",
|
|
|
|
Namespace: "appleboy",
|
2016-10-04 16:45:49 +03:00
|
|
|
},
|
2017-09-11 07:34:47 +03:00
|
|
|
Commit: Commit{
|
|
|
|
Sha: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2",
|
2016-10-04 16:45:49 +03:00
|
|
|
Author: "Bo-Yi Wu",
|
|
|
|
Branch: "master",
|
|
|
|
Message: "update travis",
|
2017-09-11 07:34:47 +03:00
|
|
|
},
|
|
|
|
Build: Build{
|
|
|
|
Number: 101,
|
|
|
|
Status: "success",
|
|
|
|
Link: "https://github.com/appleboy/go-hello",
|
2016-10-04 16:45:49 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-02-17 10:44:29 +03:00
|
|
|
message := plugin.Message()
|
2016-10-04 16:45:49 +03:00
|
|
|
|
2019-12-11 02:34:42 +03:00
|
|
|
assert.Equal(t, []string{"✅ *success*: [appleboy/go-hello](https://github.com/appleboy/go-hello)『update travis』by Bo-Yi Wu"}, message)
|
2016-10-04 16:45:49 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 11:03:10 +03:00
|
|
|
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",
|
|
|
|
},
|
|
|
|
}
|
2019-02-17 10:44:29 +03:00
|
|
|
|
2019-02-17 11:03:10 +03:00
|
|
|
message := plugin.Message()
|
2019-02-17 10:44:29 +03:00
|
|
|
|
2019-02-17 11:03:10 +03:00
|
|
|
assert.Equal(t, []string{"appleboy/go-hello/test-workflow triggered by appleboy (push)"}, message)
|
|
|
|
}
|
2019-02-17 10:44:29 +03:00
|
|
|
|
2016-10-04 16:45:49 +03:00
|
|
|
func TestSendMessage(t *testing.T) {
|
|
|
|
plugin := Plugin{
|
|
|
|
Repo: Repo{
|
2019-02-17 10:39:47 +03:00
|
|
|
Name: "go-hello",
|
|
|
|
Namespace: "appleboy",
|
2016-10-04 16:45:49 +03:00
|
|
|
},
|
2017-09-11 07:34:47 +03:00
|
|
|
Commit: Commit{
|
|
|
|
Sha: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2",
|
2016-10-04 16:45:49 +03:00
|
|
|
Author: "Bo-Yi Wu",
|
|
|
|
Branch: "master",
|
|
|
|
Message: "update travis by drone plugin",
|
2016-11-04 04:43:21 +03:00
|
|
|
Email: "test@gmail.com",
|
2016-10-04 16:45:49 +03:00
|
|
|
},
|
2017-09-11 07:34:47 +03:00
|
|
|
Build: Build{
|
|
|
|
Tag: "1.0.0",
|
|
|
|
Number: 101,
|
|
|
|
Status: "success",
|
|
|
|
Link: "https://github.com/appleboy/go-hello",
|
|
|
|
},
|
2016-10-04 16:45:49 +03:00
|
|
|
|
|
|
|
Config: Config{
|
2016-10-07 16:50:42 +03:00
|
|
|
Token: os.Getenv("TELEGRAM_TOKEN"),
|
2016-11-04 06:39:30 +03:00
|
|
|
To: []string{os.Getenv("TELEGRAM_TO"), os.Getenv("TELEGRAM_TO") + ":appleboy@gmail.com", "中文ID", "1234567890"},
|
2016-10-31 04:57:34 +03:00
|
|
|
Message: []string{"Test Telegram Chat Bot From Travis or Local", "commit message: 『{{ build.message }}』", " "},
|
2016-10-07 16:50:42 +03:00
|
|
|
Photo: []string{"tests/github.png", "1234", " "},
|
|
|
|
Document: []string{"tests/gophercolor.png", "1234", " "},
|
2016-10-09 04:56:31 +03:00
|
|
|
Sticker: []string{"tests/github-logo.png", "tests/github.png", "1234", " "},
|
2016-10-14 10:36:50 +03:00
|
|
|
Audio: []string{"tests/audio.mp3", "1234", " "},
|
|
|
|
Voice: []string{"tests/voice.ogg", "1234", " "},
|
2019-09-25 16:40:51 +03:00
|
|
|
Location: []string{"24.9163213 121.1424972", "1", " "},
|
|
|
|
Venue: []string{"35.661777 139.704051 竹北體育館 新竹縣竹北市", "24.9163213 121.1424972", "1", " "},
|
2016-10-14 10:36:50 +03:00
|
|
|
Video: []string{"tests/video.mp4", "1234", " "},
|
2016-10-07 16:50:42 +03:00
|
|
|
Debug: false,
|
2016-10-04 16:45:49 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := plugin.Exec()
|
2017-12-18 10:16:10 +03:00
|
|
|
assert.NotNil(t, err)
|
2016-10-04 16:45:49 +03:00
|
|
|
|
2019-10-30 14:50:31 +03:00
|
|
|
plugin.Config.Format = formatMarkdown
|
2017-07-04 14:03:14 +03:00
|
|
|
plugin.Config.Message = []string{"Test escape under_score"}
|
|
|
|
err = plugin.Exec()
|
2017-12-18 10:16:10 +03:00
|
|
|
assert.NotNil(t, err)
|
2017-07-04 14:03:14 +03:00
|
|
|
|
2016-10-04 16:45:49 +03:00
|
|
|
// disable message
|
|
|
|
plugin.Config.Message = []string{}
|
|
|
|
err = plugin.Exec()
|
2017-12-18 10:16:10 +03:00
|
|
|
assert.NotNil(t, err)
|
2016-10-04 16:45:49 +03:00
|
|
|
}
|
|
|
|
|
2016-10-04 17:09:10 +03:00
|
|
|
func TestBotError(t *testing.T) {
|
|
|
|
plugin := Plugin{
|
|
|
|
Repo: Repo{
|
2019-02-17 10:39:47 +03:00
|
|
|
Name: "go-hello",
|
|
|
|
Namespace: "appleboy",
|
2016-10-04 17:09:10 +03:00
|
|
|
},
|
2017-09-11 07:34:47 +03:00
|
|
|
Commit: Commit{
|
|
|
|
Sha: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2",
|
2016-10-04 17:09:10 +03:00
|
|
|
Author: "Bo-Yi Wu",
|
|
|
|
Branch: "master",
|
|
|
|
Message: "update travis by drone plugin",
|
2017-09-11 07:34:47 +03:00
|
|
|
},
|
|
|
|
Build: Build{
|
|
|
|
Number: 101,
|
|
|
|
Status: "success",
|
|
|
|
Link: "https://github.com/appleboy/go-hello",
|
2016-10-04 17:09:10 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
Config: Config{
|
|
|
|
Token: "appleboy",
|
|
|
|
To: []string{os.Getenv("TELEGRAM_TO"), "中文ID", "1234567890"},
|
|
|
|
Message: []string{"Test Telegram Chat Bot From Travis or Local", " "},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := plugin.Exec()
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
2016-10-04 16:45:49 +03:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2017-07-04 14:03:14 +03:00
|
|
|
func TestEscapeMarkdown(t *testing.T) {
|
|
|
|
provider := [][][]string{
|
|
|
|
{
|
|
|
|
{"user", "repo"},
|
|
|
|
{"user", "repo"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{"user_name", "repo_name"},
|
|
|
|
{`user\_name`, `repo\_name`},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{"user_name_long", "user_name_long"},
|
|
|
|
{`user\_name\_long`, `user\_name\_long`},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{`user\_name\_long`, `repo\_name\_long`},
|
|
|
|
{`user\_name\_long`, `repo\_name\_long`},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{`user\_name\_long`, `repo\_name\_long`, ""},
|
|
|
|
{`user\_name\_long`, `repo\_name\_long`},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range provider {
|
|
|
|
assert.Equal(t, testCase[1], escapeMarkdown(testCase[0]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEscapeMarkdownOne(t *testing.T) {
|
|
|
|
provider := [][]string{
|
|
|
|
{"user", "user"},
|
|
|
|
{"user_name", `user\_name`},
|
|
|
|
{"user_name_long", `user\_name\_long`},
|
|
|
|
{`user\_name\_escaped`, `user\_name\_escaped`},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range provider {
|
|
|
|
assert.Equal(t, testCase[1], escapeMarkdownOne(testCase[0]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-04 04:38:21 +03:00
|
|
|
func TestParseTo(t *testing.T) {
|
2016-11-09 07:00:36 +03:00
|
|
|
input := []string{"0", "1:1@gmail.com", "2:2@gmail.com", "3:3@gmail.com", "4", "5"}
|
2016-10-04 16:45:49 +03:00
|
|
|
|
2016-11-09 07:00:36 +03:00
|
|
|
ids := parseTo(input, "1@gmail.com", false)
|
|
|
|
assert.Equal(t, []int64{0, 4, 5, 1}, ids)
|
2016-10-04 16:45:49 +03:00
|
|
|
|
2016-11-09 07:00:36 +03:00
|
|
|
ids = parseTo(input, "1@gmail.com", true)
|
|
|
|
assert.Equal(t, []int64{1}, ids)
|
2016-10-04 16:45:49 +03:00
|
|
|
|
2016-11-09 07:00:36 +03:00
|
|
|
ids = parseTo(input, "a@gmail.com", false)
|
|
|
|
assert.Equal(t, []int64{0, 4, 5}, ids)
|
2016-11-04 04:38:21 +03:00
|
|
|
|
2016-11-09 07:00:36 +03:00
|
|
|
ids = parseTo(input, "a@gmail.com", true)
|
|
|
|
assert.Equal(t, []int64{0, 4, 5}, ids)
|
2016-11-17 08:13:43 +03:00
|
|
|
|
|
|
|
// test empty ids
|
|
|
|
ids = parseTo([]string{"", " ", " "}, "a@gmail.com", true)
|
|
|
|
assert.Equal(t, 0, len(ids))
|
2016-10-04 16:45:49 +03:00
|
|
|
}
|
2016-10-07 09:34:31 +03:00
|
|
|
|
2018-04-06 05:28:55 +03:00
|
|
|
func TestGlobList(t *testing.T) {
|
2016-10-07 09:34:31 +03:00
|
|
|
var input []string
|
|
|
|
var result []string
|
|
|
|
|
|
|
|
input = []string{"tests/gophercolor.png", "測試", "3"}
|
|
|
|
result = []string{"tests/gophercolor.png"}
|
2018-04-06 05:28:55 +03:00
|
|
|
assert.Equal(t, result, globList(input))
|
2016-10-07 09:34:31 +03:00
|
|
|
|
2018-04-06 05:28:55 +03:00
|
|
|
input = []string{"tests/*.mp3"}
|
|
|
|
result = []string{"tests/audio.mp3"}
|
|
|
|
assert.Equal(t, result, globList(input))
|
2016-10-07 09:34:31 +03:00
|
|
|
}
|
2016-10-14 09:27:34 +03:00
|
|
|
|
|
|
|
func TestConvertLocation(t *testing.T) {
|
|
|
|
var input string
|
|
|
|
var result Location
|
|
|
|
var empty bool
|
|
|
|
|
|
|
|
input = "1"
|
|
|
|
result, empty = convertLocation(input)
|
|
|
|
|
|
|
|
assert.Equal(t, true, empty)
|
|
|
|
assert.Equal(t, Location{}, result)
|
|
|
|
|
|
|
|
// strconv.ParseInt: parsing "測試": invalid syntax
|
2019-09-25 16:40:51 +03:00
|
|
|
input = "測試 139.704051"
|
2016-10-14 09:27:34 +03:00
|
|
|
result, empty = convertLocation(input)
|
|
|
|
|
|
|
|
assert.Equal(t, true, empty)
|
|
|
|
assert.Equal(t, Location{}, result)
|
|
|
|
|
|
|
|
// strconv.ParseInt: parsing "測試": invalid syntax
|
2019-09-25 16:40:51 +03:00
|
|
|
input = "35.661777 測試"
|
2016-10-14 09:27:34 +03:00
|
|
|
result, empty = convertLocation(input)
|
|
|
|
|
|
|
|
assert.Equal(t, true, empty)
|
|
|
|
assert.Equal(t, Location{}, result)
|
|
|
|
|
2019-09-25 16:40:51 +03:00
|
|
|
input = "35.661777 139.704051"
|
2016-10-14 09:27:34 +03:00
|
|
|
result, empty = convertLocation(input)
|
|
|
|
|
|
|
|
assert.Equal(t, false, empty)
|
|
|
|
assert.Equal(t, Location{
|
|
|
|
Latitude: float64(35.661777),
|
|
|
|
Longitude: float64(139.704051),
|
|
|
|
}, result)
|
2016-10-14 11:28:40 +03:00
|
|
|
|
2019-09-25 16:40:51 +03:00
|
|
|
input = "35.661777 139.704051 title"
|
2016-10-14 11:28:40 +03:00
|
|
|
result, empty = convertLocation(input)
|
|
|
|
|
|
|
|
assert.Equal(t, false, empty)
|
|
|
|
assert.Equal(t, Location{
|
|
|
|
Title: "title",
|
|
|
|
Address: "",
|
|
|
|
Latitude: float64(35.661777),
|
|
|
|
Longitude: float64(139.704051),
|
|
|
|
}, result)
|
|
|
|
|
2019-09-25 16:59:11 +03:00
|
|
|
input = "35.661777 139.704051 title address"
|
2016-10-14 11:28:40 +03:00
|
|
|
result, empty = convertLocation(input)
|
|
|
|
|
|
|
|
assert.Equal(t, false, empty)
|
|
|
|
assert.Equal(t, Location{
|
|
|
|
Title: "title",
|
|
|
|
Address: "address",
|
|
|
|
Latitude: float64(35.661777),
|
|
|
|
Longitude: float64(139.704051),
|
|
|
|
}, result)
|
2016-10-14 09:27:34 +03:00
|
|
|
}
|
2017-12-02 12:30:28 +03:00
|
|
|
|
|
|
|
func TestHTMLMessage(t *testing.T) {
|
|
|
|
plugin := Plugin{
|
|
|
|
Repo: Repo{
|
2019-02-17 10:39:47 +03:00
|
|
|
Name: "go-hello",
|
|
|
|
Namespace: "appleboy",
|
2017-12-02 12:30:28 +03:00
|
|
|
},
|
|
|
|
Commit: Commit{
|
|
|
|
Sha: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2",
|
|
|
|
Author: "Bo-Yi Wu",
|
|
|
|
Branch: "master",
|
|
|
|
Message: "Freakin' macOS isn't fully case-sensitive..",
|
|
|
|
},
|
|
|
|
Build: Build{
|
|
|
|
Number: 101,
|
|
|
|
Status: "success",
|
|
|
|
Link: "https://github.com/appleboy/go-hello",
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: Config{
|
|
|
|
Token: os.Getenv("TELEGRAM_TOKEN"),
|
|
|
|
To: []string{os.Getenv("TELEGRAM_TO")},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := plugin.Exec()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
2019-10-22 18:44:50 +03:00
|
|
|
|
|
|
|
func TestMessageFile(t *testing.T) {
|
|
|
|
plugin := Plugin{
|
|
|
|
Repo: Repo{
|
|
|
|
Name: "go-hello",
|
|
|
|
Namespace: "appleboy",
|
|
|
|
},
|
|
|
|
Commit: Commit{
|
|
|
|
Sha: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2",
|
|
|
|
Author: "Bo-Yi Wu",
|
|
|
|
Branch: "master",
|
|
|
|
Message: "Freakin' macOS isn't fully case-sensitive..",
|
|
|
|
},
|
|
|
|
Build: Build{
|
|
|
|
Number: 101,
|
|
|
|
Status: "success",
|
|
|
|
Link: "https://github.com/appleboy/go-hello",
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: Config{
|
|
|
|
Token: os.Getenv("TELEGRAM_TOKEN"),
|
|
|
|
To: []string{os.Getenv("TELEGRAM_TO")},
|
2019-10-29 05:57:00 +03:00
|
|
|
MessageFile: "tests/message.txt",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := plugin.Exec()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplateVars(t *testing.T) {
|
|
|
|
plugin := Plugin{
|
|
|
|
Repo: Repo{
|
|
|
|
Name: "go-hello",
|
|
|
|
Namespace: "appleboy",
|
|
|
|
},
|
|
|
|
Commit: Commit{
|
|
|
|
Sha: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2",
|
|
|
|
Author: "Bo-Yi Wu",
|
|
|
|
Branch: "master",
|
|
|
|
Message: "This is a test commit msg",
|
|
|
|
},
|
|
|
|
Build: Build{
|
|
|
|
Number: 101,
|
|
|
|
Status: "success",
|
|
|
|
Link: "https://github.com/appleboy/go-hello",
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: Config{
|
|
|
|
Token: os.Getenv("TELEGRAM_TOKEN"),
|
|
|
|
To: []string{os.Getenv("TELEGRAM_TO")},
|
2019-10-30 14:50:31 +03:00
|
|
|
Format: formatMarkdown,
|
2019-10-29 05:57:00 +03:00
|
|
|
MessageFile: "tests/message_template.txt",
|
|
|
|
TemplateVars: `{"env":"testing","version":"1.2.0-SNAPSHOT"}`,
|
2019-10-22 18:44:50 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := plugin.Exec()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
2019-12-07 06:29:47 +03:00
|
|
|
|
|
|
|
func TestProxySendMessage(t *testing.T) {
|
|
|
|
plugin := Plugin{
|
|
|
|
Repo: Repo{
|
|
|
|
Name: "go-hello",
|
|
|
|
Namespace: "appleboy",
|
|
|
|
},
|
|
|
|
Commit: Commit{
|
|
|
|
Sha: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2",
|
|
|
|
Author: "Bo-Yi Wu",
|
|
|
|
Branch: "master",
|
|
|
|
Message: "start use proxy",
|
|
|
|
Email: "test@gmail.com",
|
|
|
|
},
|
|
|
|
Build: Build{
|
|
|
|
Tag: "1.0.0",
|
|
|
|
Number: 101,
|
|
|
|
Status: "success",
|
|
|
|
Link: "https://github.com/appleboy/go-hello",
|
|
|
|
},
|
|
|
|
|
|
|
|
Config: Config{
|
2019-12-07 08:00:35 +03:00
|
|
|
Token: os.Getenv("TELEGRAM_TOKEN"),
|
|
|
|
To: []string{os.Getenv("TELEGRAM_TO")},
|
|
|
|
Message: []string{"Send message from socks5 proxy URL."},
|
|
|
|
Debug: false,
|
|
|
|
Socks5: os.Getenv("SOCKS5"),
|
2019-12-07 06:29:47 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := plugin.Exec()
|
2019-12-07 08:03:59 +03:00
|
|
|
assert.Nil(t, err)
|
2019-12-07 06:29:47 +03:00
|
|
|
}
|