Improve testing init, clean up webhook tests (#37412)

Avoid webhook test fixtures affect other tests (be triggered)

Also fixed more testing problems including path init, global config
pollution & conflict

---------

Signed-off-by: silverwind <me@silverwind.io>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: wxiaoguang <2114189+wxiaoguang@users.noreply.github.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
Copilot
2026-04-25 18:55:18 +00:00
committed by GitHub
parent 24b60f8ff9
commit 9b9fb95559
25 changed files with 692 additions and 710 deletions

View File

@@ -1,39 +1,2 @@
-
id: 1
hook_id: 1
uuid: uuid1
is_delivered: true
is_succeed: false
request_content: >
{
"url": "/matrix-delivered",
"http_method":"PUT",
"headers": {
"X-Head": "42"
},
"body": "{}"
}
-
id: 2
hook_id: 1
uuid: uuid2
is_delivered: true
-
id: 3
hook_id: 1
uuid: uuid3
is_delivered: true
is_succeed: true
payload_content: '{"key":"value"}' # legacy task, payload saved in payload_content (and not in request_content)
request_content: >
{
"url": "/matrix-success",
"http_method":"PUT",
"headers": {
"X-Head": "42"
}
}
[]
# DO NOT add more test data in the fixtures, test case should prepare their own test data separately and clearly

View File

@@ -1,54 +1,2 @@
-
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

@@ -75,7 +75,7 @@ func deleteDB() error {
}
db.Close()
// Check if we need to setup a specific schema
// Check if we need to set up a specific schema
if len(setting.Database.Schema) != 0 {
db, err = sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=%s",
setting.Database.User, setting.Database.Passwd, setting.Database.Host, setting.Database.Name, setting.Database.SSLMode))
@@ -91,7 +91,7 @@ func deleteDB() error {
defer schrows.Close()
if !schrows.Next() {
// Create and setup a DB schema
// Create and set up a DB schema
_, err = db.Exec("CREATE SCHEMA " + setting.Database.Schema)
if err != nil {
return err
@@ -134,7 +134,8 @@ func PrepareTestEnv(t *testing.T, skip int, syncModels ...any) (*xorm.Engine, fu
ourSkip := 2
ourSkip += skip
deferFn := testlogger.PrintCurrentTest(t, ourSkip)
require.NoError(t, unittest.SyncDirs(filepath.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
giteaRoot := setting.GetGiteaTestSourceRoot()
require.NoError(t, unittest.SyncDirs(filepath.Join(giteaRoot, "tests/gitea-repositories-meta"), setting.RepoRootPath))
if err := deleteDB(); err != nil {
t.Fatalf("unable to reset database: %v", err)
@@ -166,7 +167,7 @@ func PrepareTestEnv(t *testing.T, skip int, syncModels ...any) (*xorm.Engine, fu
}
}
fixturesDir := filepath.Join(filepath.Dir(setting.AppPath), "models", "migrations", "fixtures", t.Name())
fixturesDir := filepath.Join(giteaRoot, "models", "migrations", "fixtures", t.Name())
if _, err := os.Stat(fixturesDir); err == nil {
t.Logf("initializing fixtures from: %s", fixturesDir)
@@ -202,17 +203,18 @@ func LoadTableSchemasMap(t *testing.T, x *xorm.Engine) map[string]*schemas.Table
func mainTest(m *testing.M) int {
testlogger.Init()
setting.SetupGiteaTestEnv()
tmpDataPath, cleanup, err := tempdir.OsTempDir("gitea-test").MkdirTempRandom("data")
tempWorkPath, cleanup, err := tempdir.OsTempDir("gitea-test").MkdirTempRandom("migration-test-data-")
if err != nil {
testlogger.Panicf("Unable to create temporary data path %v\n", err)
return testlogger.MainErrorf("Unable to create temporary dir for migration test: %v", err)
}
defer cleanup()
setting.AppDataPath = tmpDataPath
setting.MockBuiltinPaths(tempWorkPath, "", "")
setting.SetupGiteaTestEnv()
if err = git.InitFull(); err != nil {
testlogger.Panicf("Unable to InitFull: %v\n", err)
return testlogger.MainErrorf("Unable to InitFull: %v", err)
}
setting.LoadDBSetting()
setting.InitLoggersForTest()

View File

@@ -42,12 +42,20 @@ func MainTest(m *testing.M, testOptsArg ...*TestOptions) {
func mainTest(m *testing.M, testOptsArg ...*TestOptions) int {
testOpts := util.OptionalArg(testOptsArg, &TestOptions{})
tempWorkPath, tempCleanup, err := tempdir.OsTempDir("gitea-test").MkdirTempRandom("unit-test-dir-")
if err != nil {
return testlogger.MainErrorf("Failed to create temp dir for unit test: %v", err)
}
defer tempCleanup()
defer setting.MockBuiltinPaths(tempWorkPath, "", "")()
setting.SetupGiteaTestEnv()
giteaRoot := setting.GetGiteaTestSourceRoot()
fixturesOpts := FixturesOptions{Dir: filepath.Join(giteaRoot, "models", "fixtures"), Files: testOpts.FixtureFiles}
if err := CreateTestEngine(fixturesOpts); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error creating test database engine: %v\n", err)
os.Exit(1)
return testlogger.MainErrorf("Error creating test database engine: %v", err)
}
setting.AppURL = "https://try.gitea.io/"
@@ -59,59 +67,28 @@ func mainTest(m *testing.M, testOptsArg ...*TestOptions) int {
setting.SSH.Domain = "try.gitea.io"
setting.Database.Type = "sqlite3"
setting.Repository.DefaultBranch = "master" // many test code still assume that default branch is called "master"
repoRootPath, cleanup1, err := tempdir.OsTempDir("gitea-test").MkdirTempRandom("repos")
if err != nil {
testlogger.Panicf("TempDir: %v\n", err)
}
defer cleanup1()
setting.RepoRootPath = repoRootPath
appDataPath, cleanup2, err := tempdir.OsTempDir("gitea-test").MkdirTempRandom("appdata")
if err != nil {
testlogger.Panicf("TempDir: %v\n", err)
}
defer cleanup2()
setting.AppDataPath = appDataPath
setting.GravatarSource = "https://secure.gravatar.com/avatar/"
setting.Attachment.Storage.Path = filepath.Join(setting.AppDataPath, "attachments")
setting.LFS.Storage.Path = filepath.Join(setting.AppDataPath, "lfs")
setting.Avatar.Storage.Path = filepath.Join(setting.AppDataPath, "avatars")
setting.RepoAvatar.Storage.Path = filepath.Join(setting.AppDataPath, "repo-avatars")
setting.RepoArchive.Storage.Path = filepath.Join(setting.AppDataPath, "repo-archive")
setting.Packages.Storage.Path = filepath.Join(setting.AppDataPath, "packages")
setting.Actions.LogStorage.Path = filepath.Join(setting.AppDataPath, "actions_log")
setting.Git.HomePath = filepath.Join(setting.AppDataPath, "home")
setting.IncomingEmail.ReplyToAddress = "incoming+%{token}@localhost"
config.SetDynGetter(system.NewDatabaseDynKeyGetter())
if err = cache.Init(); err != nil {
testlogger.Panicf("cache.Init: %v\n", err)
return testlogger.MainErrorf("cache.Init: %v", err)
}
if err = storage.Init(); err != nil {
testlogger.Panicf("storage.Init: %v\n", err)
return testlogger.MainErrorf("storage.Init: %v", err)
}
if err = SyncDirs(filepath.Join(giteaRoot, "tests", "gitea-repositories-meta"), setting.RepoRootPath); err != nil {
testlogger.Panicf("util.SyncDirs: %v\n", err)
return testlogger.MainErrorf("util.SyncDirs: %v", err)
}
if err = git.InitFull(); err != nil {
testlogger.Panicf("git.Init: %v\n", err)
return testlogger.MainErrorf("git.Init: %v", err)
}
if testOpts.SetUp != nil {
if err := testOpts.SetUp(); err != nil {
testlogger.Panicf("set up failed: %v\n", err)
return testlogger.MainErrorf("set up failed: %v", err)
}
}
@@ -119,7 +96,7 @@ func mainTest(m *testing.M, testOptsArg ...*TestOptions) int {
if testOpts.TearDown != nil {
if err := testOpts.TearDown(); err != nil {
testlogger.Panicf("tear down failed: %v\n", err)
return testlogger.MainErrorf("tear down failed: %v", err)
}
}
return exitStatus

View File

@@ -15,5 +15,6 @@ func TestMain(m *testing.M) {
"webhook.yml",
"hook_task.yml",
},
SetUp: prepareWebhookTestData,
})
}

View File

@@ -10,28 +10,28 @@ import (
"code.gitea.io/gitea/modules/optional"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestListSystemWebhookOptions(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hookSystem := unittest.AssertExistsAndLoadBean(t, &Webhook{URL: "https://www.example.com/system"})
hookDefault := unittest.AssertExistsAndLoadBean(t, &Webhook{URL: "https://www.example.com/default"})
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)
}
require.NoError(t, err)
require.Len(t, hooks, 2)
assert.Equal(t, hookSystem.ID, hooks[0].ID)
assert.Equal(t, hookDefault.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)
}
require.NoError(t, err)
require.Len(t, hooks, 1)
assert.Equal(t, hookSystem.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)
}
require.NoError(t, err)
require.Len(t, hooks, 1)
assert.Equal(t, hookDefault.ID, hooks[0].ID)
}

View File

@@ -4,6 +4,7 @@
package webhook
import (
"context"
"testing"
"time"
@@ -14,40 +15,105 @@ import (
"code.gitea.io/gitea/modules/timeutil"
webhook_module "code.gitea.io/gitea/modules/webhook"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"xorm.io/builder"
)
func TestHookContentType_Name(t *testing.T) {
assert.Equal(t, "json", ContentTypeJSON.Name())
assert.Equal(t, "form", ContentTypeForm.Name())
func prepareWebhookTestData() error {
if err := unittest.PrepareTestDatabase(); err != nil {
return err
}
var hooks []*Webhook
hooks = append(hooks, &Webhook{
RepoID: 1,
URL: "https://www.example.com/url1",
ContentType: ContentTypeJSON,
Events: `{"push_only":true,"send_everything":false,"choose_events":false,"events":{"create":false,"push":true,"pull_request":false}}`,
IsActive: true,
})
hooks = append(hooks, &Webhook{
RepoID: 1,
URL: "https://www.example.com/url2",
ContentType: ContentTypeJSON,
Events: `{}`,
IsActive: false,
})
hooks = append(hooks, &Webhook{
OwnerID: 3,
RepoID: 3,
URL: "https://www.example.com/url3",
ContentType: ContentTypeJSON,
Events: `{"push_only":false,"send_everything":false,"choose_events":false,"events":{"create":false,"push":true,"pull_request":true}}`,
IsActive: true,
})
hooks = append(hooks, &Webhook{
OwnerID: 3,
RepoID: 3,
URL: "https://www.example.com/url3",
ContentType: ContentTypeJSON,
Events: `{}`,
})
hooks = append(hooks, &Webhook{
RepoID: 2,
URL: "https://www.example.com/url4",
ContentType: ContentTypeJSON,
Events: `{"push_only":true,"branch_filter":"{master,feature*}"}`,
IsActive: true,
})
hooks = append(hooks, &Webhook{
URL: "https://www.example.com/system",
ContentType: ContentTypeJSON,
Events: `{"push_only":true,"branch_filter":"{master,feature*}"}`,
IsSystemWebhook: true,
})
hooks = append(hooks, &Webhook{
URL: "https://www.example.com/default",
ContentType: ContentTypeJSON,
Events: `{"push_only":true,"branch_filter":"{master,feature*}"}`,
})
ctx := context.Background()
if err := db.TruncateBeans(ctx, &Webhook{}); err != nil {
return err
}
if err := db.Insert(ctx, hooks); err != nil {
return err
}
hook, _, _ := db.Get[Webhook](ctx, builder.Eq{"repo_id": 1, "is_active": true})
var tasks []*HookTask
tasks = append(tasks, &HookTask{HookID: hook.ID, UUID: uuid.New().String()})
tasks = append(tasks, &HookTask{HookID: hook.ID, UUID: uuid.New().String()})
tasks = append(tasks, &HookTask{HookID: hook.ID, UUID: uuid.New().String()})
if err := db.TruncateBeans(ctx, &HookTask{}); err != nil {
return err
}
return db.Insert(ctx, tasks)
}
func TestIsValidHookContentType(t *testing.T) {
func TestWebHookContentType(t *testing.T) {
assert.Equal(t, "json", ContentTypeJSON.Name())
assert.Equal(t, "form", ContentTypeForm.Name())
assert.True(t, IsValidHookContentType("json"))
assert.True(t, IsValidHookContentType("form"))
assert.False(t, IsValidHookContentType("invalid"))
}
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)
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)
}
hook := unittest.AssertExistsAndLoadBean(t, &Webhook{RepoID: 1, IsActive: true})
tasks, err := hook.History(t.Context(), 0)
require.NoError(t, err)
require.Len(t, tasks, 3)
webhook = unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 2})
tasks, err = webhook.History(t.Context(), 0)
hook = unittest.AssertExistsAndLoadBean(t, &Webhook{RepoID: 1, Events: "{}"})
tasks, err = hook.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 := unittest.AssertExistsAndLoadBean(t, &Webhook{RepoID: 1, IsActive: true})
hookEvent := &webhook_module.HookEvent{
PushOnly: true,
SendEverything: false,
@@ -100,10 +166,10 @@ func TestCreateWebhook(t *testing.T) {
}
func TestGetWebhookByRepoID(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hook, err := GetWebhookByRepoID(t.Context(), 1, 1)
hook := unittest.AssertExistsAndLoadBean(t, &Webhook{RepoID: 1, IsActive: true})
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)
@@ -111,10 +177,10 @@ func TestGetWebhookByRepoID(t *testing.T) {
}
func TestGetWebhookByOwnerID(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hook, err := GetWebhookByOwnerID(t.Context(), 3, 3)
assert.NoError(t, err)
assert.Equal(t, int64(3), hook.ID)
hook := unittest.AssertExistsAndLoadBean(t, &Webhook{OwnerID: 3})
loaded, err := GetWebhookByOwnerID(t.Context(), 3, hook.ID)
require.NoError(t, err)
require.Equal(t, hook.ID, loaded.ID)
_, err = GetWebhookByOwnerID(t.Context(), unittest.NonexistentID, unittest.NonexistentID)
assert.Error(t, err)
@@ -122,48 +188,45 @@ func TestGetWebhookByOwnerID(t *testing.T) {
}
func TestGetActiveWebhooksByRepoID(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hook := unittest.AssertExistsAndLoadBean(t, &Webhook{RepoID: 1, IsActive: true})
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.True(t, hooks[0].IsActive)
}
require.NoError(t, err)
require.Len(t, hooks, 1)
assert.Equal(t, hook.ID, hooks[0].ID)
assert.True(t, hooks[0].IsActive)
}
func TestGetWebhooksByRepoID(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
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)
}
require.NoError(t, err)
require.Len(t, hooks, 2)
assert.Equal(t, int64(1), hooks[0].RepoID)
assert.True(t, hooks[0].IsActive)
assert.Equal(t, int64(1), hooks[1].RepoID)
assert.False(t, hooks[1].IsActive)
}
func TestGetActiveWebhooksByOwnerID(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
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.True(t, hooks[0].IsActive)
}
require.NoError(t, err)
require.Len(t, hooks, 1)
assert.Equal(t, int64(3), hooks[0].OwnerID)
assert.True(t, hooks[0].IsActive)
}
func TestGetWebhooksByOwnerID(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
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.True(t, hooks[0].IsActive)
}
require.NoError(t, err)
require.Len(t, hooks, 2)
assert.Equal(t, int64(3), hooks[0].OwnerID)
assert.True(t, hooks[0].IsActive)
assert.Equal(t, int64(3), hooks[1].OwnerID)
assert.False(t, hooks[1].IsActive)
}
func TestUpdateWebhook(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hook := unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 2})
hook := unittest.AssertExistsAndLoadBean(t, &Webhook{RepoID: 1, Events: `{}`})
require.False(t, hook.IsActive)
hook.IsActive = true
hook.ContentType = ContentTypeForm
unittest.AssertNotExistsBean(t, hook)
@@ -172,48 +235,36 @@ 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 := unittest.AssertExistsAndLoadBean(t, &Webhook{RepoID: 1, Events: `{}`})
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)
assert.True(t, IsErrWebhookNotExist(err))
}
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 := unittest.AssertExistsAndLoadBean(t, &Webhook{OwnerID: 3, Events: `{}`})
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)
assert.True(t, IsErrWebhookNotExist(err))
}
func TestHookTasks(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hookTasks, err := HookTasks(t.Context(), 1, 1)
hook := unittest.AssertExistsAndLoadBean(t, &Webhook{RepoID: 1, IsActive: true})
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.Len(t, hookTasks, 3)
hookTasks, err = HookTasks(t.Context(), unittest.NonexistentID, 1)
assert.NoError(t, err)
assert.Empty(t, hookTasks)
}
func TestCreateHookTask(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hookTask := &HookTask{
HookID: 3,
PayloadVersion: 2,
}
hook := unittest.AssertExistsAndLoadBean(t, &Webhook{OwnerID: 3, IsActive: true})
hookTask := &HookTask{HookID: hook.ID, PayloadVersion: 2}
unittest.AssertNotExistsBean(t, hookTask)
_, err := CreateHookTask(t.Context(), hookTask)
assert.NoError(t, err)
@@ -221,20 +272,23 @@ func TestCreateHookTask(t *testing.T) {
}
func TestUpdateHookTask(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hook := unittest.AssertExistsAndLoadBean(t, &Webhook{OwnerID: 3, IsActive: true})
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}`}
require.NoError(t, db.Insert(t.Context(), hook))
hookTask := &HookTask{
HookID: 3,
HookID: hook.ID,
IsDelivered: true,
Delivered: timeutil.TimeStampNanoNow(),
PayloadVersion: 2,
@@ -249,9 +303,10 @@ 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}`}
require.NoError(t, db.Insert(t.Context(), hook))
hookTask := &HookTask{
HookID: 4,
HookID: hook.ID,
IsDelivered: false,
PayloadVersion: 2,
}
@@ -265,9 +320,10 @@ 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}`}
require.NoError(t, db.Insert(t.Context(), hook))
hookTask := &HookTask{
HookID: 4,
HookID: hook.ID,
IsDelivered: true,
Delivered: timeutil.TimeStampNanoNow(),
PayloadVersion: 2,
@@ -282,9 +338,10 @@ 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}`}
require.NoError(t, db.Insert(t.Context(), hook))
hookTask := &HookTask{
HookID: 3,
HookID: hook.ID,
IsDelivered: true,
Delivered: timeutil.TimeStampNano(time.Now().AddDate(0, 0, -8).UnixNano()),
PayloadVersion: 2,
@@ -299,9 +356,10 @@ 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}`}
require.NoError(t, db.Insert(t.Context(), hook))
hookTask := &HookTask{
HookID: 4,
HookID: hook.ID,
IsDelivered: false,
PayloadVersion: 2,
}
@@ -315,9 +373,10 @@ 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}`}
require.NoError(t, db.Insert(t.Context(), hook))
hookTask := &HookTask{
HookID: 4,
HookID: hook.ID,
IsDelivered: true,
Delivered: timeutil.TimeStampNano(time.Now().AddDate(0, 0, -6).UnixNano()),
PayloadVersion: 2,