2019-11-02 04:21:22 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-11-02 04:21:22 +05:30
|
|
|
|
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-10-21 21:51:56 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
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"
|
2023-01-01 20:53:15 +05:30
|
|
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
2021-11-17 18:04:35 +05:30
|
|
|
|
2019-11-02 04:21:22 +05:30
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2024-03-19 16:29:48 +05:30
|
|
|
func activateWebhook(t *testing.T, hookID int64) {
|
|
|
|
t.Helper()
|
|
|
|
updated, err := db.GetEngine(db.DefaultContext).ID(hookID).Cols("is_active").Update(webhook_model.Webhook{IsActive: true})
|
|
|
|
assert.Equal(t, int64(1), updated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2024-03-19 16:29:48 +05:30
|
|
|
activateWebhook(t, 1)
|
|
|
|
|
2021-11-10 10:43:16 +05:30
|
|
|
hookTasks := []*webhook_model.HookTask{
|
2023-01-01 20:53:15 +05:30
|
|
|
{HookID: 1, EventType: webhook_module.HookEventPush},
|
2019-11-02 04:21:22 +05:30
|
|
|
}
|
|
|
|
for _, hookTask := range hookTasks {
|
2021-11-16 14:23:21 +05:30
|
|
|
unittest.AssertNotExistsBean(t, hookTask)
|
2019-11-02 04:21:22 +05:30
|
|
|
}
|
2023-01-01 20:53:15 +05:30
|
|
|
assert.NoError(t, PrepareWebhooks(db.DefaultContext, EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{Commits: []*api.PayloadCommit{{}}}))
|
2019-11-02 04:21:22 +05:30
|
|
|
for _, hookTask := range hookTasks {
|
2021-11-16 14:23:21 +05:30
|
|
|
unittest.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
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
|
2024-03-19 16:29:48 +05:30
|
|
|
activateWebhook(t, 4)
|
|
|
|
|
2021-11-10 10:43:16 +05:30
|
|
|
hookTasks := []*webhook_model.HookTask{
|
2023-01-01 20:53:15 +05:30
|
|
|
{HookID: 4, EventType: webhook_module.HookEventPush},
|
2019-11-02 04:21:22 +05:30
|
|
|
}
|
|
|
|
for _, hookTask := range hookTasks {
|
2021-11-16 14:23:21 +05:30
|
|
|
unittest.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)
|
2023-01-01 20:53:15 +05:30
|
|
|
assert.NoError(t, PrepareWebhooks(db.DefaultContext, EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{Ref: "refs/heads/feature/7791", Commits: []*api.PayloadCommit{{}}}))
|
2019-11-02 04:21:22 +05:30
|
|
|
for _, hookTask := range hookTasks {
|
2021-11-16 14:23:21 +05:30
|
|
|
unittest.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
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
|
2021-11-10 10:43:16 +05:30
|
|
|
hookTasks := []*webhook_model.HookTask{
|
2023-01-01 20:53:15 +05:30
|
|
|
{HookID: 4, EventType: webhook_module.HookEventPush},
|
2019-11-02 04:21:22 +05:30
|
|
|
}
|
|
|
|
for _, hookTask := range hookTasks {
|
2021-11-16 14:23:21 +05:30
|
|
|
unittest.AssertNotExistsBean(t, hookTask)
|
2019-11-02 04:21:22 +05:30
|
|
|
}
|
2023-01-01 20:53:15 +05:30
|
|
|
assert.NoError(t, PrepareWebhooks(db.DefaultContext, EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{Ref: "refs/heads/fix_weird_bug"}))
|
2019-11-02 04:21:22 +05:30
|
|
|
|
|
|
|
for _, hookTask := range hookTasks {
|
2021-11-16 14:23:21 +05:30
|
|
|
unittest.AssertNotExistsBean(t, hookTask)
|
2019-11-02 04:21:22 +05:30
|
|
|
}
|
|
|
|
}
|