2019-11-01 16:51:22 -06:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-11-01 16:51:22 -06:00
|
|
|
|
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-10-21 10:21:56 -06:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-09 18:27:50 -07:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 07:36:47 -07:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-09 22:13:16 -07:00
|
|
|
webhook_model "code.gitea.io/gitea/models/webhook"
|
2019-11-01 16:51:22 -06:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2023-01-01 08:23:15 -07:00
|
|
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
2021-11-17 05:34:35 -07:00
|
|
|
|
2019-11-01 16:51:22 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2019-11-03 15:13:25 -07:00
|
|
|
func TestWebhook_GetSlackHook(t *testing.T) {
|
2021-11-09 22:13:16 -07:00
|
|
|
w := &webhook_model.Webhook{
|
2019-11-03 15:13:25 -07:00
|
|
|
Meta: `{"channel": "foo", "username": "username", "color": "blue"}`,
|
|
|
|
}
|
|
|
|
slackHook := GetSlackHook(w)
|
2024-12-15 03:41:29 -07:00
|
|
|
assert.Equal(t, SlackMeta{
|
2019-11-03 15:13:25 -07:00
|
|
|
Channel: "foo",
|
|
|
|
Username: "username",
|
|
|
|
Color: "blue",
|
2024-12-15 03:41:29 -07:00
|
|
|
}, *slackHook)
|
2019-11-03 15:13:25 -07:00
|
|
|
}
|
|
|
|
|
2019-11-01 16:51:22 -06:00
|
|
|
func TestPrepareWebhooks(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-11-01 16:51:22 -06:00
|
|
|
|
2022-08-15 20:22:25 -06:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2021-11-09 22:13:16 -07:00
|
|
|
hookTasks := []*webhook_model.HookTask{
|
2023-01-01 08:23:15 -07:00
|
|
|
{HookID: 1, EventType: webhook_module.HookEventPush},
|
2019-11-01 16:51:22 -06:00
|
|
|
}
|
|
|
|
for _, hookTask := range hookTasks {
|
2021-11-16 01:53:21 -07:00
|
|
|
unittest.AssertNotExistsBean(t, hookTask)
|
2019-11-01 16:51:22 -06:00
|
|
|
}
|
2023-01-01 08:23:15 -07:00
|
|
|
assert.NoError(t, PrepareWebhooks(db.DefaultContext, EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{Commits: []*api.PayloadCommit{{}}}))
|
2019-11-01 16:51:22 -06:00
|
|
|
for _, hookTask := range hookTasks {
|
2021-11-16 01:53:21 -07:00
|
|
|
unittest.AssertExistsAndLoadBean(t, hookTask)
|
2019-11-01 16:51:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrepareWebhooksBranchFilterMatch(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-11-01 16:51:22 -06:00
|
|
|
|
2022-08-15 20:22:25 -06:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
|
2021-11-09 22:13:16 -07:00
|
|
|
hookTasks := []*webhook_model.HookTask{
|
2023-01-01 08:23:15 -07:00
|
|
|
{HookID: 4, EventType: webhook_module.HookEventPush},
|
2019-11-01 16:51:22 -06:00
|
|
|
}
|
|
|
|
for _, hookTask := range hookTasks {
|
2021-11-16 01:53:21 -07:00
|
|
|
unittest.AssertNotExistsBean(t, hookTask)
|
2019-11-01 16:51:22 -06:00
|
|
|
}
|
|
|
|
// this test also ensures that * doesn't handle / in any special way (like shell would)
|
2023-01-01 08:23:15 -07:00
|
|
|
assert.NoError(t, PrepareWebhooks(db.DefaultContext, EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{Ref: "refs/heads/feature/7791", Commits: []*api.PayloadCommit{{}}}))
|
2019-11-01 16:51:22 -06:00
|
|
|
for _, hookTask := range hookTasks {
|
2021-11-16 01:53:21 -07:00
|
|
|
unittest.AssertExistsAndLoadBean(t, hookTask)
|
2019-11-01 16:51:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrepareWebhooksBranchFilterNoMatch(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-11-01 16:51:22 -06:00
|
|
|
|
2022-08-15 20:22:25 -06:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
|
2021-11-09 22:13:16 -07:00
|
|
|
hookTasks := []*webhook_model.HookTask{
|
2023-01-01 08:23:15 -07:00
|
|
|
{HookID: 4, EventType: webhook_module.HookEventPush},
|
2019-11-01 16:51:22 -06:00
|
|
|
}
|
|
|
|
for _, hookTask := range hookTasks {
|
2021-11-16 01:53:21 -07:00
|
|
|
unittest.AssertNotExistsBean(t, hookTask)
|
2019-11-01 16:51:22 -06:00
|
|
|
}
|
2023-01-01 08:23:15 -07:00
|
|
|
assert.NoError(t, PrepareWebhooks(db.DefaultContext, EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{Ref: "refs/heads/fix_weird_bug"}))
|
2019-11-01 16:51:22 -06:00
|
|
|
|
|
|
|
for _, hookTask := range hookTasks {
|
2021-11-16 01:53:21 -07:00
|
|
|
unittest.AssertNotExistsBean(t, hookTask)
|
2019-11-01 16:51:22 -06:00
|
|
|
}
|
|
|
|
}
|