// Copyright 2019 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package webhook
import (
"testing"
webhook_model "gitea.dev/models/webhook"
"gitea.dev/modules/json"
api "gitea.dev/modules/structs"
webhook_module "gitea.dev/modules/webhook"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTelegramPayload(t *testing.T) {
tc := telegramConvertor{}
t.Run("Correct webhook params", func(t *testing.T) {
p := createTelegramPayloadHTML(`testMsg `)
assert.Equal(t, TelegramPayload{
RichMessage: InputRichMessage{
HTML: `testMsg`,
},
}, p)
})
t.Run("Line breaks are kept in rich messages", func(t *testing.T) {
// Rich messages collapse bare newlines, so they must become
.
p := createTelegramPayloadHTML("first line\r\nsecond line\nthird line")
assert.Equal(t, "first line
second line
third line", p.RichMessage.HTML)
})
for _, testCase := range []struct {
name string
call func() (TelegramPayload, error)
text string
}{
{
"Create", func() (TelegramPayload, error) { return tc.Create(createTestPayload()) },
`[test/repo] branch test created`,
},
{
"Delete", func() (TelegramPayload, error) { return tc.Delete(deleteTestPayload()) },
`[test/repo] branch test deleted`,
},
{
"Fork", func() (TelegramPayload, error) { return tc.Fork(forkTestPayload()) },
`test/repo2 is forked to test/repo`,
},
{
"Push", func() (TelegramPayload, error) { return tc.Push(pushTestPayload()) },
`[test/repo:test] 2 new commits
[2020558] commit message - user1
[2020558] commit message - user1`,
},
{
"IssueComment", func() (TelegramPayload, error) { return tc.IssueComment(issueCommentTestPayload()) },
`[test/repo] New comment on issue #2 crash by user1
more info needed`,
},
{
"PullRequest", func() (TelegramPayload, error) { return tc.PullRequest(pullRequestTestPayload()) },
`[test/repo] Pull request opened: #12 Fix bug by user1
fixes bug #2`,
},
{
"PullRequestComment", func() (TelegramPayload, error) { return tc.IssueComment(pullRequestCommentTestPayload()) },
`[test/repo] New comment on pull request #12 Fix bug by user1
changes requested`,
},
{
"Review", func() (TelegramPayload, error) {
p := pullRequestTestPayload()
p.Action = api.HookIssueReviewed
return tc.Review(p, webhook_module.HookEventPullRequestReviewApproved)
},
`[test/repo] Pull request review approved: #12 Fix bug
good job`,
},
{
"Repository", func() (TelegramPayload, error) { return tc.Repository(repositoryTestPayload()) },
`[test/repo] Repository created`,
},
{
"Package", func() (TelegramPayload, error) { return tc.Package(packageTestPayload()) },
`Package created: GiteaContainer:latest by user1`,
},
{
"Release", func() (TelegramPayload, error) { return tc.Release(pullReleaseTestPayload()) },
`[test/repo] Release created: v1.0 by user1`,
},
} {
t.Run(testCase.name, func(t *testing.T) {
pl, err := testCase.call()
require.NoError(t, err)
assert.Equal(t, testCase.text, pl.RichMessage.HTML)
})
}
t.Run("Issue", func(t *testing.T) {
p := issueTestPayload()
for _, testCase := range []struct {
action api.HookIssueAction
text string
}{
{api.HookIssueOpened, `[test/repo] Issue opened: #2 crash by user1
issue body`},
{api.HookIssueClosed, `[test/repo] Issue closed: #2 crash by user1`},
} {
p.Action = testCase.action
pl, err := tc.Issue(p)
require.NoError(t, err)
assert.Equal(t, testCase.text, pl.RichMessage.HTML)
}
})
t.Run("Wiki", func(t *testing.T) {
p := wikiTestPayload()
for _, testCase := range []struct {
action api.HookWikiAction
text string
}{
{api.HookWikiCreated, `[test/repo] New wiki page 'index' (Wiki change comment) by user1`},
{api.HookWikiEdited, `[test/repo] Wiki page 'index' edited (Wiki change comment) by user1`},
{api.HookWikiDeleted, `[test/repo] Wiki page 'index' deleted by user1`},
} {
p.Action = testCase.action
pl, err := tc.Wiki(p)
require.NoError(t, err)
assert.Equal(t, testCase.text, pl.RichMessage.HTML)
}
})
}
func TestTelegramJSONPayload(t *testing.T) {
p := pushTestPayload()
data, err := p.JSONPayload()
require.NoError(t, err)
hook := &webhook_model.Webhook{
RepoID: 3,
IsActive: true,
Type: webhook_module.TELEGRAM,
URL: "https://telegram.example.com/sendMessage",
Meta: ``,
HTTPMethod: "POST",
}
task := &webhook_model.HookTask{
HookID: hook.ID,
EventType: webhook_module.HookEventPush,
PayloadContent: string(data),
PayloadVersion: 2,
}
req, reqBody, err := newTelegramRequest(t.Context(), hook, task)
require.NotNil(t, req)
require.NotNil(t, reqBody)
require.NoError(t, err)
assert.Equal(t, "POST", req.Method)
assert.Equal(t, "https://telegram.example.com/sendRichMessage", req.URL.String())
assert.Equal(t, "sha256=", req.Header.Get("X-Hub-Signature-256"))
assert.Equal(t, "application/json", req.Header.Get("Content-Type"))
var body TelegramPayload
err = json.NewDecoder(req.Body).Decode(&body)
assert.NoError(t, err)
assert.Equal(t, `[test/repo:test] 2 new commits
[2020558] commit message - user1
[2020558] commit message - user1`, body.RichMessage.HTML)
}