From 2536c2db89cfe79170f0558e2493745f8d718954 Mon Sep 17 00:00:00 2001 From: oliverpool Date: Tue, 19 Mar 2024 11:59:48 +0100 Subject: [PATCH] fix webhook fixtures --- models/fixtures/webhook.yml | 18 ++++++++------ models/webhook/webhook_test.go | 16 ++++++++++++ services/webhook/webhook_test.go | 11 +++++++++ tests/integration/pull_merge_test.go | 37 ++++++++++++++++------------ 4 files changed, 59 insertions(+), 23 deletions(-) diff --git a/models/fixtures/webhook.yml b/models/fixtures/webhook.yml index f62bae1f31..989bb5657a 100644 --- a/models/fixtures/webhook.yml +++ b/models/fixtures/webhook.yml @@ -1,15 +1,17 @@ - id: 1 repo_id: 1 - url: www.example.com/url1 + url: http://www.example.com/url1 + http_method: POST 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 + is_active: false # disable to prevent sending hook task during unrelated tests - id: 2 repo_id: 1 - url: www.example.com/url2 + url: http://www.example.com/url2 + http_method: POST 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 @@ -18,14 +20,16 @@ id: 3 owner_id: 3 repo_id: 3 - url: www.example.com/url3 + url: http://www.example.com/url3 + http_method: POST 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 + is_active: false - id: 4 repo_id: 2 - url: www.example.com/url4 + url: http://www.example.com/url4 + http_method: POST content_type: 1 # json events: '{"push_only":true,"branch_filter":"{master,feature*}"}' - is_active: true + is_active: false diff --git a/models/webhook/webhook_test.go b/models/webhook/webhook_test.go index f4403776ce..b4f6ffa189 100644 --- a/models/webhook/webhook_test.go +++ b/models/webhook/webhook_test.go @@ -124,6 +124,9 @@ func TestGetWebhookByOwnerID(t *testing.T) { func TestGetActiveWebhooksByRepoID(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) + + activateWebhook(t, 1) + hooks, err := db.Find[Webhook](db.DefaultContext, ListWebhookOptions{RepoID: 1, IsActive: optional.Some(true)}) assert.NoError(t, err) if assert.Len(t, hooks, 1) { @@ -144,6 +147,9 @@ func TestGetWebhooksByRepoID(t *testing.T) { func TestGetActiveWebhooksByOwnerID(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) + + activateWebhook(t, 3) + hooks, err := db.Find[Webhook](db.DefaultContext, ListWebhookOptions{OwnerID: 3, IsActive: optional.Some(true)}) assert.NoError(t, err) if assert.Len(t, hooks, 1) { @@ -152,8 +158,18 @@ func TestGetActiveWebhooksByOwnerID(t *testing.T) { } } +func activateWebhook(t *testing.T, hookID int64) { + t.Helper() + updated, err := db.GetEngine(db.DefaultContext).ID(hookID).Cols("is_active").Update(Webhook{IsActive: true}) + assert.Equal(t, int64(1), updated) + assert.NoError(t, err) +} + func TestGetWebhooksByOwnerID(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) + + activateWebhook(t, 3) + hooks, err := db.Find[Webhook](db.DefaultContext, ListWebhookOptions{OwnerID: 3}) assert.NoError(t, err) if assert.Len(t, hooks, 1) { diff --git a/services/webhook/webhook_test.go b/services/webhook/webhook_test.go index 5f5c146232..1ef152838d 100644 --- a/services/webhook/webhook_test.go +++ b/services/webhook/webhook_test.go @@ -28,10 +28,19 @@ func TestWebhook_GetSlackHook(t *testing.T) { }) } +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) +} + func TestPrepareWebhooks(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) + activateWebhook(t, 1) + hookTasks := []*webhook_model.HookTask{ {HookID: 1, EventType: webhook_module.HookEventPush}, } @@ -48,6 +57,8 @@ func TestPrepareWebhooksBranchFilterMatch(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}) + activateWebhook(t, 4) + hookTasks := []*webhook_model.HookTask{ {HookID: 4, EventType: webhook_module.HookEventPush}, } diff --git a/tests/integration/pull_merge_test.go b/tests/integration/pull_merge_test.go index ee13a4bc57..e79d99b052 100644 --- a/tests/integration/pull_merge_test.go +++ b/tests/integration/pull_merge_test.go @@ -80,10 +80,22 @@ func testPullCleanUp(t *testing.T, session *TestSession, user, repo, pullnum str return resp } +func retrieveHookTasks(t *testing.T, hookID int64, activateWebhook bool) []*webhook.HookTask { + t.Helper() + if activateWebhook { + updated, err := db.GetEngine(db.DefaultContext).ID(hookID).Cols("is_active").Update(webhook.Webhook{IsActive: true}) + assert.Equal(t, int64(1), updated) + assert.NoError(t, err) + } + + hookTasks, err := webhook.HookTasks(db.DefaultContext, hookID, 1) + assert.NoError(t, err) + return hookTasks +} + func TestPullMerge(t *testing.T) { onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { - hookTasks, err := webhook.HookTasks(db.DefaultContext, 1, 1) // Retrieve previous hook number - assert.NoError(t, err) + hookTasks := retrieveHookTasks(t, 1, true) hookTasksLenBefore := len(hookTasks) session := loginUser(t, "user1") @@ -96,16 +108,14 @@ func TestPullMerge(t *testing.T) { assert.EqualValues(t, "pulls", elem[3]) testPullMerge(t, session, elem[1], elem[2], elem[4], repo_model.MergeStyleMerge, false) - hookTasks, err = webhook.HookTasks(db.DefaultContext, 1, 1) - assert.NoError(t, err) + hookTasks = retrieveHookTasks(t, 1, false) assert.Len(t, hookTasks, hookTasksLenBefore+1) }) } func TestPullRebase(t *testing.T) { onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { - hookTasks, err := webhook.HookTasks(db.DefaultContext, 1, 1) // Retrieve previous hook number - assert.NoError(t, err) + hookTasks := retrieveHookTasks(t, 1, true) hookTasksLenBefore := len(hookTasks) session := loginUser(t, "user1") @@ -118,16 +128,14 @@ func TestPullRebase(t *testing.T) { assert.EqualValues(t, "pulls", elem[3]) testPullMerge(t, session, elem[1], elem[2], elem[4], repo_model.MergeStyleRebase, false) - hookTasks, err = webhook.HookTasks(db.DefaultContext, 1, 1) - assert.NoError(t, err) + hookTasks = retrieveHookTasks(t, 1, false) assert.Len(t, hookTasks, hookTasksLenBefore+1) }) } func TestPullRebaseMerge(t *testing.T) { onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { - hookTasks, err := webhook.HookTasks(db.DefaultContext, 1, 1) // Retrieve previous hook number - assert.NoError(t, err) + hookTasks := retrieveHookTasks(t, 1, true) hookTasksLenBefore := len(hookTasks) session := loginUser(t, "user1") @@ -140,16 +148,14 @@ func TestPullRebaseMerge(t *testing.T) { assert.EqualValues(t, "pulls", elem[3]) testPullMerge(t, session, elem[1], elem[2], elem[4], repo_model.MergeStyleRebaseMerge, false) - hookTasks, err = webhook.HookTasks(db.DefaultContext, 1, 1) - assert.NoError(t, err) + hookTasks = retrieveHookTasks(t, 1, false) assert.Len(t, hookTasks, hookTasksLenBefore+1) }) } func TestPullSquash(t *testing.T) { onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { - hookTasks, err := webhook.HookTasks(db.DefaultContext, 1, 1) // Retrieve previous hook number - assert.NoError(t, err) + hookTasks := retrieveHookTasks(t, 1, true) hookTasksLenBefore := len(hookTasks) session := loginUser(t, "user1") @@ -163,8 +169,7 @@ func TestPullSquash(t *testing.T) { assert.EqualValues(t, "pulls", elem[3]) testPullMerge(t, session, elem[1], elem[2], elem[4], repo_model.MergeStyleSquash, false) - hookTasks, err = webhook.HookTasks(db.DefaultContext, 1, 1) - assert.NoError(t, err) + hookTasks = retrieveHookTasks(t, 1, false) assert.Len(t, hookTasks, hookTasksLenBefore+1) }) }