2023-12-06 20:53:01 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2024-05-10 06:42:59 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-05-17 19:50:20 +00:00
|
|
|
plugin_base "github.com/thegeeklab/wp-plugin-go/v3/plugin"
|
2023-12-06 20:53:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_messageContent(t *testing.T) {
|
|
|
|
//nolint:lll
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
want string
|
|
|
|
template string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "render default template",
|
|
|
|
want: "Status: **success**\nBuild: [octocat/demo](https://ci.example.com) (main) by octobot\nMessage: feat: demo commit title ([source](https://git.example.com))",
|
|
|
|
template: DefaultMessageTemplate,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "render unsafe html template",
|
|
|
|
want: "Status: **success**\nBuild: octocat/demo",
|
2024-05-10 06:42:59 +00:00
|
|
|
template: "Status: **{{ .Pipeline.Status }}**\nBuild: {{ .Repository.Slug }}",
|
2023-12-06 20:53:01 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-05-10 06:42:59 +00:00
|
|
|
p := New(func(_ context.Context) error { return nil })
|
2024-05-17 19:50:20 +00:00
|
|
|
p.Network = plugin_base.Network{
|
2024-05-10 06:42:59 +00:00
|
|
|
Context: context.Background(),
|
|
|
|
Client: &http.Client{},
|
2023-12-06 20:53:01 +00:00
|
|
|
}
|
2024-05-17 19:50:20 +00:00
|
|
|
p.Metadata = plugin_base.Metadata{
|
|
|
|
Curr: plugin_base.Commit{
|
2023-12-06 20:53:01 +00:00
|
|
|
Branch: "main",
|
|
|
|
Title: "feat: demo commit title",
|
|
|
|
URL: "https://git.example.com",
|
2024-05-17 19:50:20 +00:00
|
|
|
Author: plugin_base.Author{
|
2023-12-06 20:53:01 +00:00
|
|
|
Name: "octobot",
|
|
|
|
},
|
|
|
|
},
|
2024-05-17 19:50:20 +00:00
|
|
|
Pipeline: plugin_base.Pipeline{
|
2023-12-06 20:53:01 +00:00
|
|
|
Status: "success",
|
|
|
|
URL: "https://ci.example.com",
|
|
|
|
},
|
2024-05-17 19:50:20 +00:00
|
|
|
Repository: plugin_base.Repository{
|
2023-12-06 20:53:01 +00:00
|
|
|
Slug: "octocat/demo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2024-05-10 06:42:59 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p.Settings.Template = tt.template
|
2023-12-06 20:53:01 +00:00
|
|
|
|
2024-05-10 06:42:59 +00:00
|
|
|
content, err := p.CreateMessage()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, tt.want, content)
|
|
|
|
})
|
2023-12-06 20:53:01 +00:00
|
|
|
}
|
|
|
|
}
|