2019-11-01 23:51:22 +01:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-11-01 23:51:22 +01:00
|
|
|
|
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
2024-04-30 09:29:44 +02:00
|
|
|
"fmt"
|
2019-11-01 23:51:22 +01:00
|
|
|
"testing"
|
|
|
|
|
2022-10-21 18:21:56 +02:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-10 02:27:50 +01:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 15:36:47 +01:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-10 06:13:16 +01:00
|
|
|
webhook_model "code.gitea.io/gitea/models/webhook"
|
2019-11-01 23:51:22 +01:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2023-01-01 16:23:15 +01:00
|
|
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
2021-11-17 13:34:35 +01:00
|
|
|
|
2019-11-01 23:51:22 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 21:42:06 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-11-01 23:51:22 +01:00
|
|
|
)
|
|
|
|
|
2024-03-19 11:59:48 +01:00
|
|
|
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)
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, err)
|
2024-03-19 11:59:48 +01:00
|
|
|
}
|
|
|
|
|
2019-11-01 23:51:22 +01:00
|
|
|
func TestPrepareWebhooks(t *testing.T) {
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2019-11-01 23:51:22 +01:00
|
|
|
|
2022-08-16 04:22:25 +02:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2024-03-19 11:59:48 +01:00
|
|
|
activateWebhook(t, 1)
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
hookTasks := []*webhook_model.HookTask{
|
2023-01-01 16:23:15 +01:00
|
|
|
{HookID: 1, EventType: webhook_module.HookEventPush},
|
2019-11-01 23:51:22 +01:00
|
|
|
}
|
|
|
|
for _, hookTask := range hookTasks {
|
2021-11-16 09:53:21 +01:00
|
|
|
unittest.AssertNotExistsBean(t, hookTask)
|
2019-11-01 23:51:22 +01:00
|
|
|
}
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, PrepareWebhooks(db.DefaultContext, EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{Commits: []*api.PayloadCommit{{}}}))
|
2019-11-01 23:51:22 +01:00
|
|
|
for _, hookTask := range hookTasks {
|
2021-11-16 09:53:21 +01:00
|
|
|
unittest.AssertExistsAndLoadBean(t, hookTask)
|
2019-11-01 23:51:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-30 09:29:44 +02:00
|
|
|
func eventType(p api.Payloader) webhook_module.HookEventType {
|
|
|
|
switch p.(type) {
|
|
|
|
case *api.CreatePayload:
|
|
|
|
return webhook_module.HookEventCreate
|
|
|
|
case *api.DeletePayload:
|
|
|
|
return webhook_module.HookEventDelete
|
|
|
|
case *api.PushPayload:
|
|
|
|
return webhook_module.HookEventPush
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("no event type for payload %T", p))
|
|
|
|
}
|
|
|
|
|
2019-11-01 23:51:22 +01:00
|
|
|
func TestPrepareWebhooksBranchFilterMatch(t *testing.T) {
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2019-11-01 23:51:22 +01:00
|
|
|
|
2024-04-30 09:29:44 +02:00
|
|
|
// branch_filter: {master,feature*}
|
|
|
|
w := unittest.AssertExistsAndLoadBean(t, &webhook_model.Webhook{ID: 4})
|
|
|
|
activateWebhook(t, w.ID)
|
2024-03-19 11:59:48 +01:00
|
|
|
|
2024-04-30 09:29:44 +02:00
|
|
|
for _, p := range []api.Payloader{
|
|
|
|
&api.PushPayload{Ref: "refs/heads/feature/7791"},
|
|
|
|
&api.CreatePayload{Ref: "refs/heads/feature/7791"}, // branch creation
|
|
|
|
&api.DeletePayload{Ref: "refs/heads/feature/7791"}, // branch deletion
|
|
|
|
} {
|
|
|
|
t.Run(fmt.Sprintf("%T", p), func(t *testing.T) {
|
|
|
|
db.DeleteBeans(db.DefaultContext, webhook_model.HookTask{HookID: w.ID})
|
|
|
|
typ := eventType(p)
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, PrepareWebhook(db.DefaultContext, w, typ, p))
|
2024-04-30 09:29:44 +02:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &webhook_model.HookTask{
|
|
|
|
HookID: w.ID,
|
|
|
|
EventType: typ,
|
|
|
|
})
|
|
|
|
})
|
2019-11-01 23:51:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrepareWebhooksBranchFilterNoMatch(t *testing.T) {
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2019-11-01 23:51:22 +01:00
|
|
|
|
2024-04-30 09:29:44 +02:00
|
|
|
// branch_filter: {master,feature*}
|
|
|
|
w := unittest.AssertExistsAndLoadBean(t, &webhook_model.Webhook{ID: 4})
|
|
|
|
activateWebhook(t, w.ID)
|
2019-11-01 23:51:22 +01:00
|
|
|
|
2024-04-30 09:29:44 +02:00
|
|
|
for _, p := range []api.Payloader{
|
|
|
|
&api.PushPayload{Ref: "refs/heads/fix_weird_bug"},
|
|
|
|
&api.CreatePayload{Ref: "refs/heads/fix_weird_bug"}, // branch creation
|
|
|
|
&api.DeletePayload{Ref: "refs/heads/fix_weird_bug"}, // branch deletion
|
|
|
|
} {
|
|
|
|
t.Run(fmt.Sprintf("%T", p), func(t *testing.T) {
|
|
|
|
db.DeleteBeans(db.DefaultContext, webhook_model.HookTask{HookID: w.ID})
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, PrepareWebhook(db.DefaultContext, w, eventType(p), p))
|
2024-04-30 09:29:44 +02:00
|
|
|
unittest.AssertNotExistsBean(t, &webhook_model.HookTask{HookID: w.ID})
|
|
|
|
})
|
2019-11-01 23:51:22 +01:00
|
|
|
}
|
|
|
|
}
|