models/fixtures: remove test data from webhook.yml, use code to insert in tests

Agent-Logs-Url: https://github.com/go-gitea/gitea/sessions/52d595da-6991-440e-9cae-a4e070410229

Co-authored-by: wxiaoguang <2114189+wxiaoguang@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-04-25 04:18:19 +00:00
committed by GitHub
parent 683f8c0014
commit 66e0e1e2c9
5 changed files with 301 additions and 124 deletions

View File

@@ -1,54 +1,3 @@
-
id: 1
repo_id: 1
url: https://www.example.com/url1
content_type: 1 # json
events: '{"push_only":true,"send_everything":false,"choose_events":false,"events":{"create":false,"push":true,"pull_request":false}}'
is_active: true
-
id: 2
repo_id: 1
url: https://www.example.com/url2
content_type: 1 # json
events: '{"push_only":false,"send_everything":false,"choose_events":false,"events":{"create":false,"push":true,"pull_request":true}}'
is_active: false
-
id: 3
owner_id: 3
repo_id: 3
url: https://www.example.com/url3
content_type: 1 # json
events: '{"push_only":false,"send_everything":false,"choose_events":false,"events":{"create":false,"push":true,"pull_request":true}}'
is_active: true
-
id: 4
repo_id: 2
url: https://www.example.com/url4
content_type: 1 # json
events: '{"push_only":true,"branch_filter":"{master,feature*}"}'
is_active: true
-
id: 5
repo_id: 0
owner_id: 0
url: https://www.example.com/url5
content_type: 1 # json
events: '{"push_only":true,"branch_filter":"{master,feature*}"}'
is_active: true
is_system_webhook: true
-
id: 6
repo_id: 0
owner_id: 0
url: https://www.example.com/url6
content_type: 1 # json
events: '{"push_only":true,"branch_filter":"{master,feature*}"}'
is_active: true
is_system_webhook: false
[]
# DO NOT add more test data in the fixtures, test case should prepare their own test data separately and clearly

View File

@@ -14,24 +14,44 @@ import (
func TestListSystemWebhookOptions(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
systemHook := &Webhook{
URL: "https://www.example.com/system",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
IsActive: true,
IsSystemWebhook: true,
}
assert.NoError(t, CreateWebhook(t.Context(), systemHook))
defaultHook := &Webhook{
URL: "https://www.example.com/default",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
IsActive: true,
IsSystemWebhook: false,
}
assert.NoError(t, CreateWebhook(t.Context(), defaultHook))
opts := ListSystemWebhookOptions{IsSystem: optional.None[bool]()}
hooks, _, err := GetGlobalWebhooks(t.Context(), &opts)
assert.NoError(t, err)
if assert.Len(t, hooks, 2) {
assert.Equal(t, int64(5), hooks[0].ID)
assert.Equal(t, int64(6), hooks[1].ID)
assert.Equal(t, systemHook.ID, hooks[0].ID)
assert.Equal(t, defaultHook.ID, hooks[1].ID)
}
opts.IsSystem = optional.Some(true)
hooks, _, err = GetGlobalWebhooks(t.Context(), &opts)
assert.NoError(t, err)
if assert.Len(t, hooks, 1) {
assert.Equal(t, int64(5), hooks[0].ID)
assert.Equal(t, systemHook.ID, hooks[0].ID)
}
opts.IsSystem = optional.Some(false)
hooks, _, err = GetGlobalWebhooks(t.Context(), &opts)
assert.NoError(t, err)
if assert.Len(t, hooks, 1) {
assert.Equal(t, int64(6), hooks[0].ID)
assert.Equal(t, defaultHook.ID, hooks[0].ID)
}
}

View File

@@ -30,24 +30,52 @@ func TestIsValidHookContentType(t *testing.T) {
func TestWebhook_History(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
webhook := unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 1})
tasks, err := webhook.History(t.Context(), 0)
hook1 := &Webhook{
RepoID: 3,
URL: "https://www.example.com/history1",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
IsActive: true,
}
assert.NoError(t, CreateWebhook(t.Context(), hook1))
task1, err := CreateHookTask(t.Context(), &HookTask{HookID: hook1.ID, IsDelivered: true, PayloadVersion: 2})
assert.NoError(t, err)
task2, err := CreateHookTask(t.Context(), &HookTask{HookID: hook1.ID, IsDelivered: true, PayloadVersion: 2})
assert.NoError(t, err)
task3, err := CreateHookTask(t.Context(), &HookTask{HookID: hook1.ID, IsDelivered: true, PayloadVersion: 2})
assert.NoError(t, err)
tasks, err := hook1.History(t.Context(), 0)
assert.NoError(t, err)
if assert.Len(t, tasks, 3) {
assert.Equal(t, int64(3), tasks[0].ID)
assert.Equal(t, int64(2), tasks[1].ID)
assert.Equal(t, int64(1), tasks[2].ID)
assert.Equal(t, task3.ID, tasks[0].ID)
assert.Equal(t, task2.ID, tasks[1].ID)
assert.Equal(t, task1.ID, tasks[2].ID)
}
webhook = unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 2})
tasks, err = webhook.History(t.Context(), 0)
hook2 := &Webhook{
RepoID: 3,
URL: "https://www.example.com/history2",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
IsActive: false,
}
assert.NoError(t, CreateWebhook(t.Context(), hook2))
tasks, err = hook2.History(t.Context(), 0)
assert.NoError(t, err)
assert.Empty(t, tasks)
}
func TestWebhook_UpdateEvent(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
webhook := unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 1})
webhook := &Webhook{
RepoID: 3,
URL: "https://www.example.com/update_event",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
}
assert.NoError(t, CreateWebhook(t.Context(), webhook))
hookEvent := &webhook_module.HookEvent{
PushOnly: true,
SendEverything: false,
@@ -101,9 +129,17 @@ func TestCreateWebhook(t *testing.T) {
func TestGetWebhookByRepoID(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hook, err := GetWebhookByRepoID(t.Context(), 1, 1)
hook := &Webhook{
RepoID: 1,
URL: "https://www.example.com/by_repo_id",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
}
assert.NoError(t, CreateWebhook(t.Context(), hook))
loaded, err := GetWebhookByRepoID(t.Context(), 1, hook.ID)
assert.NoError(t, err)
assert.Equal(t, int64(1), hook.ID)
assert.Equal(t, hook.ID, loaded.ID)
_, err = GetWebhookByRepoID(t.Context(), unittest.NonexistentID, unittest.NonexistentID)
assert.Error(t, err)
@@ -112,9 +148,18 @@ func TestGetWebhookByRepoID(t *testing.T) {
func TestGetWebhookByOwnerID(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hook, err := GetWebhookByOwnerID(t.Context(), 3, 3)
hook := &Webhook{
OwnerID: 3,
URL: "https://www.example.com/by_owner_id",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
IsActive: true,
}
assert.NoError(t, CreateWebhook(t.Context(), hook))
loaded, err := GetWebhookByOwnerID(t.Context(), 3, hook.ID)
assert.NoError(t, err)
assert.Equal(t, int64(3), hook.ID)
assert.Equal(t, hook.ID, loaded.ID)
_, err = GetWebhookByOwnerID(t.Context(), unittest.NonexistentID, unittest.NonexistentID)
assert.Error(t, err)
@@ -123,47 +168,106 @@ func TestGetWebhookByOwnerID(t *testing.T) {
func TestGetActiveWebhooksByRepoID(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hook1 := &Webhook{
RepoID: 1,
URL: "https://www.example.com/active1",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
IsActive: true,
}
assert.NoError(t, CreateWebhook(t.Context(), hook1))
hook2 := &Webhook{
RepoID: 1,
URL: "https://www.example.com/inactive1",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
IsActive: false,
}
assert.NoError(t, CreateWebhook(t.Context(), hook2))
hooks, err := db.Find[Webhook](t.Context(), ListWebhookOptions{RepoID: 1, IsActive: optional.Some(true)})
assert.NoError(t, err)
if assert.Len(t, hooks, 1) {
assert.Equal(t, int64(1), hooks[0].ID)
assert.Equal(t, hook1.ID, hooks[0].ID)
assert.True(t, hooks[0].IsActive)
}
}
func TestGetWebhooksByRepoID(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hook1 := &Webhook{
RepoID: 1,
URL: "https://www.example.com/repo1_hook1",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
IsActive: true,
}
assert.NoError(t, CreateWebhook(t.Context(), hook1))
hook2 := &Webhook{
RepoID: 1,
URL: "https://www.example.com/repo1_hook2",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
IsActive: false,
}
assert.NoError(t, CreateWebhook(t.Context(), hook2))
hooks, err := db.Find[Webhook](t.Context(), ListWebhookOptions{RepoID: 1})
assert.NoError(t, err)
if assert.Len(t, hooks, 2) {
assert.Equal(t, int64(1), hooks[0].ID)
assert.Equal(t, int64(2), hooks[1].ID)
assert.Equal(t, hook1.ID, hooks[0].ID)
assert.Equal(t, hook2.ID, hooks[1].ID)
}
}
func TestGetActiveWebhooksByOwnerID(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hook := &Webhook{
OwnerID: 3,
URL: "https://www.example.com/owner_active",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
IsActive: true,
}
assert.NoError(t, CreateWebhook(t.Context(), hook))
hooks, err := db.Find[Webhook](t.Context(), ListWebhookOptions{OwnerID: 3, IsActive: optional.Some(true)})
assert.NoError(t, err)
if assert.Len(t, hooks, 1) {
assert.Equal(t, int64(3), hooks[0].ID)
assert.Equal(t, hook.ID, hooks[0].ID)
assert.True(t, hooks[0].IsActive)
}
}
func TestGetWebhooksByOwnerID(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hook := &Webhook{
OwnerID: 3,
URL: "https://www.example.com/owner_hook",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
IsActive: true,
}
assert.NoError(t, CreateWebhook(t.Context(), hook))
hooks, err := db.Find[Webhook](t.Context(), ListWebhookOptions{OwnerID: 3})
assert.NoError(t, err)
if assert.Len(t, hooks, 1) {
assert.Equal(t, int64(3), hooks[0].ID)
assert.Equal(t, hook.ID, hooks[0].ID)
assert.True(t, hooks[0].IsActive)
}
}
func TestUpdateWebhook(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hook := unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 2})
hook := &Webhook{
RepoID: 1,
URL: "https://www.example.com/update",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
IsActive: false,
}
assert.NoError(t, CreateWebhook(t.Context(), hook))
hook.IsActive = true
hook.ContentType = ContentTypeForm
unittest.AssertNotExistsBean(t, hook)
@@ -173,9 +277,16 @@ func TestUpdateWebhook(t *testing.T) {
func TestDeleteWebhookByRepoID(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 2, RepoID: 1})
assert.NoError(t, DeleteWebhookByRepoID(t.Context(), 1, 2))
unittest.AssertNotExistsBean(t, &Webhook{ID: 2, RepoID: 1})
hook := &Webhook{
RepoID: 1,
URL: "https://www.example.com/delete_by_repo",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
}
assert.NoError(t, CreateWebhook(t.Context(), hook))
unittest.AssertExistsAndLoadBean(t, &Webhook{ID: hook.ID, RepoID: 1})
assert.NoError(t, DeleteWebhookByRepoID(t.Context(), 1, hook.ID))
unittest.AssertNotExistsBean(t, &Webhook{ID: hook.ID, RepoID: 1})
err := DeleteWebhookByRepoID(t.Context(), unittest.NonexistentID, unittest.NonexistentID)
assert.Error(t, err)
@@ -184,9 +295,16 @@ func TestDeleteWebhookByRepoID(t *testing.T) {
func TestDeleteWebhookByOwnerID(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 3, OwnerID: 3})
assert.NoError(t, DeleteWebhookByOwnerID(t.Context(), 3, 3))
unittest.AssertNotExistsBean(t, &Webhook{ID: 3, OwnerID: 3})
hook := &Webhook{
OwnerID: 3,
URL: "https://www.example.com/delete_by_owner",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
}
assert.NoError(t, CreateWebhook(t.Context(), hook))
unittest.AssertExistsAndLoadBean(t, &Webhook{ID: hook.ID, OwnerID: 3})
assert.NoError(t, DeleteWebhookByOwnerID(t.Context(), 3, hook.ID))
unittest.AssertNotExistsBean(t, &Webhook{ID: hook.ID, OwnerID: 3})
err := DeleteWebhookByOwnerID(t.Context(), unittest.NonexistentID, unittest.NonexistentID)
assert.Error(t, err)
@@ -195,12 +313,26 @@ func TestDeleteWebhookByOwnerID(t *testing.T) {
func TestHookTasks(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hookTasks, err := HookTasks(t.Context(), 1, 1)
hook := &Webhook{
RepoID: 3,
URL: "https://www.example.com/hook_tasks",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
}
assert.NoError(t, CreateWebhook(t.Context(), hook))
task1, err := CreateHookTask(t.Context(), &HookTask{HookID: hook.ID, IsDelivered: true, PayloadVersion: 2})
assert.NoError(t, err)
task2, err := CreateHookTask(t.Context(), &HookTask{HookID: hook.ID, IsDelivered: true, PayloadVersion: 2})
assert.NoError(t, err)
task3, err := CreateHookTask(t.Context(), &HookTask{HookID: hook.ID, IsDelivered: true, PayloadVersion: 2})
assert.NoError(t, err)
hookTasks, err := HookTasks(t.Context(), hook.ID, 1)
assert.NoError(t, err)
if assert.Len(t, hookTasks, 3) {
assert.Equal(t, int64(3), hookTasks[0].ID)
assert.Equal(t, int64(2), hookTasks[1].ID)
assert.Equal(t, int64(1), hookTasks[2].ID)
assert.Equal(t, task3.ID, hookTasks[0].ID)
assert.Equal(t, task2.ID, hookTasks[1].ID)
assert.Equal(t, task1.ID, hookTasks[2].ID)
}
hookTasks, err = HookTasks(t.Context(), unittest.NonexistentID, 1)
@@ -210,8 +342,15 @@ func TestHookTasks(t *testing.T) {
func TestCreateHookTask(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hook := &Webhook{
RepoID: 3,
URL: "https://www.example.com/create_task",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
}
assert.NoError(t, CreateWebhook(t.Context(), hook))
hookTask := &HookTask{
HookID: 3,
HookID: hook.ID,
PayloadVersion: 2,
}
unittest.AssertNotExistsBean(t, hookTask)
@@ -222,19 +361,35 @@ func TestCreateHookTask(t *testing.T) {
func TestUpdateHookTask(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hook := &Webhook{
RepoID: 3,
URL: "https://www.example.com/update_task",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
}
assert.NoError(t, CreateWebhook(t.Context(), hook))
hookTask := &HookTask{HookID: hook.ID, PayloadVersion: 2}
_, err := CreateHookTask(t.Context(), hookTask)
assert.NoError(t, err)
hook := unittest.AssertExistsAndLoadBean(t, &HookTask{ID: 1})
hook.PayloadContent = "new payload content"
hook.IsDelivered = true
unittest.AssertNotExistsBean(t, hook)
assert.NoError(t, UpdateHookTask(t.Context(), hook))
unittest.AssertExistsAndLoadBean(t, hook)
hookTask.PayloadContent = "new payload content"
hookTask.IsDelivered = true
unittest.AssertNotExistsBean(t, hookTask)
assert.NoError(t, UpdateHookTask(t.Context(), hookTask))
unittest.AssertExistsAndLoadBean(t, hookTask)
}
func TestCleanupHookTaskTable_PerWebhook_DeletesDelivered(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hook := &Webhook{
RepoID: 3,
URL: "https://www.example.com/cleanup1",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
}
assert.NoError(t, CreateWebhook(t.Context(), hook))
hookTask := &HookTask{
HookID: 3,
HookID: hook.ID,
IsDelivered: true,
Delivered: timeutil.TimeStampNanoNow(),
PayloadVersion: 2,
@@ -250,8 +405,15 @@ func TestCleanupHookTaskTable_PerWebhook_DeletesDelivered(t *testing.T) {
func TestCleanupHookTaskTable_PerWebhook_LeavesUndelivered(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hook := &Webhook{
RepoID: 3,
URL: "https://www.example.com/cleanup2",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
}
assert.NoError(t, CreateWebhook(t.Context(), hook))
hookTask := &HookTask{
HookID: 4,
HookID: hook.ID,
IsDelivered: false,
PayloadVersion: 2,
}
@@ -266,8 +428,15 @@ func TestCleanupHookTaskTable_PerWebhook_LeavesUndelivered(t *testing.T) {
func TestCleanupHookTaskTable_PerWebhook_LeavesMostRecentTask(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hook := &Webhook{
RepoID: 3,
URL: "https://www.example.com/cleanup3",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
}
assert.NoError(t, CreateWebhook(t.Context(), hook))
hookTask := &HookTask{
HookID: 4,
HookID: hook.ID,
IsDelivered: true,
Delivered: timeutil.TimeStampNanoNow(),
PayloadVersion: 2,
@@ -283,8 +452,15 @@ func TestCleanupHookTaskTable_PerWebhook_LeavesMostRecentTask(t *testing.T) {
func TestCleanupHookTaskTable_OlderThan_DeletesDelivered(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hook := &Webhook{
RepoID: 3,
URL: "https://www.example.com/cleanup4",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
}
assert.NoError(t, CreateWebhook(t.Context(), hook))
hookTask := &HookTask{
HookID: 3,
HookID: hook.ID,
IsDelivered: true,
Delivered: timeutil.TimeStampNano(time.Now().AddDate(0, 0, -8).UnixNano()),
PayloadVersion: 2,
@@ -300,8 +476,15 @@ func TestCleanupHookTaskTable_OlderThan_DeletesDelivered(t *testing.T) {
func TestCleanupHookTaskTable_OlderThan_LeavesUndelivered(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hook := &Webhook{
RepoID: 3,
URL: "https://www.example.com/cleanup5",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
}
assert.NoError(t, CreateWebhook(t.Context(), hook))
hookTask := &HookTask{
HookID: 4,
HookID: hook.ID,
IsDelivered: false,
PayloadVersion: 2,
}
@@ -316,8 +499,15 @@ func TestCleanupHookTaskTable_OlderThan_LeavesUndelivered(t *testing.T) {
func TestCleanupHookTaskTable_OlderThan_LeavesTaskEarlierThanAgeToDelete(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hook := &Webhook{
RepoID: 3,
URL: "https://www.example.com/cleanup6",
ContentType: ContentTypeJSON,
Events: `{"push_only":true}`,
}
assert.NoError(t, CreateWebhook(t.Context(), hook))
hookTask := &HookTask{
HookID: 4,
HookID: hook.ID,
IsDelivered: true,
Delivered: timeutil.TimeStampNano(time.Now().AddDate(0, 0, -6).UnixNano()),
PayloadVersion: 2,

View File

@@ -4,6 +4,7 @@
package repo
import (
"fmt"
"net/http"
"testing"
@@ -17,8 +18,17 @@ import (
func TestTestHook(t *testing.T) {
unittest.PrepareTestEnv(t)
hook := &webhook.Webhook{
RepoID: 1,
URL: "https://www.example.com/test_hook",
ContentType: webhook.ContentTypeJSON,
Events: `{"push_only":true}`,
IsActive: true,
}
assert.NoError(t, webhook.CreateWebhook(t.Context(), hook))
ctx, _ := contexttest.MockAPIContext(t, "user2/repo1/wiki/_pages")
ctx.SetPathParam("id", "1")
ctx.SetPathParam("id", fmt.Sprintf("%d", hook.ID))
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2)
@@ -26,6 +36,6 @@ func TestTestHook(t *testing.T) {
assert.Equal(t, http.StatusNoContent, ctx.Resp.WrittenStatus())
unittest.AssertExistsAndLoadBean(t, &webhook.HookTask{
HookID: 1,
HookID: hook.ID,
}, unittest.Cond("is_delivered=?", false))
}

View File

@@ -37,50 +37,58 @@ func TestPrepareWebhooks(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
hookTasks := []*webhook_model.HookTask{
{HookID: 1, EventType: webhook_module.HookEventPush},
}
for _, hookTask := range hookTasks {
unittest.AssertNotExistsBean(t, hookTask)
hook := &webhook_model.Webhook{
RepoID: repo.ID,
URL: "https://www.example.com/prepare_webhooks",
ContentType: webhook_model.ContentTypeJSON,
Events: `{"push_only":true}`,
IsActive: true,
}
assert.NoError(t, webhook_model.CreateWebhook(t.Context(), hook))
hookTask := &webhook_model.HookTask{HookID: hook.ID, EventType: webhook_module.HookEventPush}
unittest.AssertNotExistsBean(t, hookTask)
assert.NoError(t, PrepareWebhooks(t.Context(), EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{Commits: []*api.PayloadCommit{{}}}))
for _, hookTask := range hookTasks {
unittest.AssertExistsAndLoadBean(t, hookTask)
}
unittest.AssertExistsAndLoadBean(t, hookTask)
}
func TestPrepareWebhooksBranchFilterMatch(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
hookTasks := []*webhook_model.HookTask{
{HookID: 4, EventType: webhook_module.HookEventPush},
}
for _, hookTask := range hookTasks {
unittest.AssertNotExistsBean(t, hookTask)
hook := &webhook_model.Webhook{
RepoID: repo.ID,
URL: "https://www.example.com/branch_filter_match",
ContentType: webhook_model.ContentTypeJSON,
Events: `{"push_only":true,"branch_filter":"{master,feature*}"}`,
IsActive: true,
}
assert.NoError(t, webhook_model.CreateWebhook(t.Context(), hook))
hookTask := &webhook_model.HookTask{HookID: hook.ID, EventType: webhook_module.HookEventPush}
unittest.AssertNotExistsBean(t, hookTask)
// this test also ensures that * doesn't handle / in any special way (like shell would)
assert.NoError(t, PrepareWebhooks(t.Context(), EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{Ref: "refs/heads/feature/7791", Commits: []*api.PayloadCommit{{}}}))
for _, hookTask := range hookTasks {
unittest.AssertExistsAndLoadBean(t, hookTask)
}
unittest.AssertExistsAndLoadBean(t, hookTask)
}
func TestPrepareWebhooksBranchFilterNoMatch(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
hookTasks := []*webhook_model.HookTask{
{HookID: 4, EventType: webhook_module.HookEventPush},
hook := &webhook_model.Webhook{
RepoID: repo.ID,
URL: "https://www.example.com/branch_filter_no_match",
ContentType: webhook_model.ContentTypeJSON,
Events: `{"push_only":true,"branch_filter":"{master,feature*}"}`,
IsActive: true,
}
for _, hookTask := range hookTasks {
unittest.AssertNotExistsBean(t, hookTask)
}
assert.NoError(t, PrepareWebhooks(t.Context(), EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{Ref: "refs/heads/fix_weird_bug"}))
assert.NoError(t, webhook_model.CreateWebhook(t.Context(), hook))
for _, hookTask := range hookTasks {
unittest.AssertNotExistsBean(t, hookTask)
}
hookTask := &webhook_model.HookTask{HookID: hook.ID, EventType: webhook_module.HookEventPush}
unittest.AssertNotExistsBean(t, hookTask)
assert.NoError(t, PrepareWebhooks(t.Context(), EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{Ref: "refs/heads/fix_weird_bug"}))
unittest.AssertNotExistsBean(t, hookTask)
}
func TestWebhookUserMail(t *testing.T) {