2019-11-02 04:21:22 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2021-09-19 17:19:59 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-12 20:06:47 +05:30
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-10 10:43:16 +05:30
|
|
|
webhook_model "code.gitea.io/gitea/models/webhook"
|
2019-11-02 04:21:22 +05:30
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2019-11-04 03:43:25 +05:30
|
|
|
func TestWebhook_GetSlackHook(t *testing.T) {
|
2021-11-10 10:43:16 +05:30
|
|
|
w := &webhook_model.Webhook{
|
2019-11-04 03:43:25 +05:30
|
|
|
Meta: `{"channel": "foo", "username": "username", "color": "blue"}`,
|
|
|
|
}
|
|
|
|
slackHook := GetSlackHook(w)
|
|
|
|
assert.Equal(t, *slackHook, SlackMeta{
|
|
|
|
Channel: "foo",
|
|
|
|
Username: "username",
|
|
|
|
Color: "blue",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-02 04:21:22 +05:30
|
|
|
func TestPrepareWebhooks(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-11-02 04:21:22 +05:30
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
|
2021-11-10 10:43:16 +05:30
|
|
|
hookTasks := []*webhook_model.HookTask{
|
|
|
|
{RepoID: repo.ID, HookID: 1, EventType: webhook_model.HookEventPush},
|
2019-11-02 04:21:22 +05:30
|
|
|
}
|
|
|
|
for _, hookTask := range hookTasks {
|
2021-09-19 17:19:59 +05:30
|
|
|
db.AssertNotExistsBean(t, hookTask)
|
2019-11-02 04:21:22 +05:30
|
|
|
}
|
2021-11-10 10:43:16 +05:30
|
|
|
assert.NoError(t, PrepareWebhooks(repo, webhook_model.HookEventPush, &api.PushPayload{Commits: []*api.PayloadCommit{{}}}))
|
2019-11-02 04:21:22 +05:30
|
|
|
for _, hookTask := range hookTasks {
|
2021-09-19 17:19:59 +05:30
|
|
|
db.AssertExistsAndLoadBean(t, hookTask)
|
2019-11-02 04:21:22 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrepareWebhooksBranchFilterMatch(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-11-02 04:21:22 +05:30
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository)
|
2021-11-10 10:43:16 +05:30
|
|
|
hookTasks := []*webhook_model.HookTask{
|
|
|
|
{RepoID: repo.ID, HookID: 4, EventType: webhook_model.HookEventPush},
|
2019-11-02 04:21:22 +05:30
|
|
|
}
|
|
|
|
for _, hookTask := range hookTasks {
|
2021-09-19 17:19:59 +05:30
|
|
|
db.AssertNotExistsBean(t, hookTask)
|
2019-11-02 04:21:22 +05:30
|
|
|
}
|
|
|
|
// this test also ensures that * doesn't handle / in any special way (like shell would)
|
2021-11-10 10:43:16 +05:30
|
|
|
assert.NoError(t, PrepareWebhooks(repo, webhook_model.HookEventPush, &api.PushPayload{Ref: "refs/heads/feature/7791", Commits: []*api.PayloadCommit{{}}}))
|
2019-11-02 04:21:22 +05:30
|
|
|
for _, hookTask := range hookTasks {
|
2021-09-19 17:19:59 +05:30
|
|
|
db.AssertExistsAndLoadBean(t, hookTask)
|
2019-11-02 04:21:22 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrepareWebhooksBranchFilterNoMatch(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-11-02 04:21:22 +05:30
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository)
|
2021-11-10 10:43:16 +05:30
|
|
|
hookTasks := []*webhook_model.HookTask{
|
|
|
|
{RepoID: repo.ID, HookID: 4, EventType: webhook_model.HookEventPush},
|
2019-11-02 04:21:22 +05:30
|
|
|
}
|
|
|
|
for _, hookTask := range hookTasks {
|
2021-09-19 17:19:59 +05:30
|
|
|
db.AssertNotExistsBean(t, hookTask)
|
2019-11-02 04:21:22 +05:30
|
|
|
}
|
2021-11-10 10:43:16 +05:30
|
|
|
assert.NoError(t, PrepareWebhooks(repo, webhook_model.HookEventPush, &api.PushPayload{Ref: "refs/heads/fix_weird_bug"}))
|
2019-11-02 04:21:22 +05:30
|
|
|
|
|
|
|
for _, hookTask := range hookTasks {
|
2021-09-19 17:19:59 +05:30
|
|
|
db.AssertNotExistsBean(t, hookTask)
|
2019-11-02 04:21:22 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO TestHookTask_deliver
|
|
|
|
|
|
|
|
// TODO TestDeliverHooks
|