0
0
mirror of https://github.com/thegeeklab/wp-matrix.git synced 2024-11-13 03:30:39 +00:00
wp-matrix/plugin/impl_test.go
renovate[bot] b0214b66b4
chore(deps): update golang devdeps non-major (#88)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Robert Kaussow <mail@thegeeklab.de>
2024-02-09 22:39:01 +01:00

79 lines
2.0 KiB
Go

package plugin
import (
"context"
"net/http"
"testing"
wp "github.com/thegeeklab/wp-plugin-go/plugin"
)
func Test_messageContent(t *testing.T) {
//nolint:lll
tests := []struct {
name string
want string
unsafe bool
template string
meta wp.Metadata
}{
{
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",
unsafe: true,
template: "Status: **{{ .Pipeline.Status }}**<br>Build: {{ .Repository.Slug }}",
},
{
name: "render html xss template",
want: "Status: **success**\nBuild: octocat/demo",
unsafe: true,
template: "Status: **{{ .Pipeline.Status }}**<br>Build: <a href=\"javascript:alert('XSS1')\" onmouseover=\"alert('XSS2')\">{{ .Repository.Slug }}<a>",
},
{
name: "render markdown xss template",
want: "Status: **success**\nBuild: octocat/demo",
unsafe: true,
template: "Status: **{{ .Pipeline.Status }}**<br>Build: [{{ .Repository.Slug }}](javascript:alert(XSS1'))",
},
}
options := wp.Options{
Name: "wp-matrix",
Execute: func(_ context.Context) error { return nil },
}
p := New(options, &Settings{})
p.Metadata = wp.Metadata{
Curr: wp.Commit{
Branch: "main",
Title: "feat: demo commit title",
URL: "https://git.example.com",
Author: wp.Author{
Name: "octobot",
},
},
Pipeline: wp.Pipeline{
Status: "success",
URL: "https://ci.example.com",
},
Repository: wp.Repository{
Slug: "octocat/demo",
},
}
for _, tt := range tests {
p.Settings.Template = tt.template
p.Settings.TemplateUnsafe = tt.unsafe
content, _ := p.messageContent(context.Background(), http.Client{})
if content.Body != tt.want {
t.Errorf("messageContent: %q got: %q, want: %q", tt.name, content.Body, tt.want)
}
}
}