2017-01-25 05:37:35 -05:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-01-25 05:37:35 -05:00
|
|
|
|
2021-11-10 00:13:16 -05:00
|
|
|
package webhook
|
2017-01-25 05:37:35 -05:00
|
|
|
|
|
|
|
import (
|
2021-01-26 16:02:42 -05:00
|
|
|
"context"
|
2017-01-25 05:37:35 -05:00
|
|
|
"testing"
|
2021-01-26 16:02:42 -05:00
|
|
|
"time"
|
2017-01-25 05:37:35 -05:00
|
|
|
|
2021-09-19 06:49:59 -05:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-12 09:36:47 -05:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-07-24 11:03:58 -05:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2019-05-11 05:21:34 -05:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2023-05-03 18:53:43 -05:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2021-08-12 07:43:08 -05:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2023-01-01 10:23:15 -05:00
|
|
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
2017-01-25 05:37:35 -05:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHookContentType_Name(t *testing.T) {
|
|
|
|
assert.Equal(t, "json", ContentTypeJSON.Name())
|
|
|
|
assert.Equal(t, "form", ContentTypeForm.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsValidHookContentType(t *testing.T) {
|
|
|
|
assert.True(t, IsValidHookContentType("json"))
|
|
|
|
assert.True(t, IsValidHookContentType("form"))
|
|
|
|
assert.False(t, IsValidHookContentType("invalid"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebhook_History(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 21:22:25 -05:00
|
|
|
webhook := unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 1})
|
2017-01-25 05:37:35 -05:00
|
|
|
tasks, err := webhook.History(0)
|
|
|
|
assert.NoError(t, err)
|
2017-08-28 04:17:45 -05:00
|
|
|
if assert.Len(t, tasks, 1) {
|
|
|
|
assert.Equal(t, int64(1), tasks[0].ID)
|
|
|
|
}
|
2017-01-25 05:37:35 -05:00
|
|
|
|
2022-08-15 21:22:25 -05:00
|
|
|
webhook = unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 2})
|
2017-01-25 05:37:35 -05:00
|
|
|
tasks, err = webhook.History(0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, tasks, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebhook_UpdateEvent(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 21:22:25 -05:00
|
|
|
webhook := unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 1})
|
2023-01-01 10:23:15 -05:00
|
|
|
hookEvent := &webhook_module.HookEvent{
|
2017-01-25 05:37:35 -05:00
|
|
|
PushOnly: true,
|
|
|
|
SendEverything: false,
|
|
|
|
ChooseEvents: false,
|
2023-01-01 10:23:15 -05:00
|
|
|
HookEvents: webhook_module.HookEvents{
|
2017-01-25 05:37:35 -05:00
|
|
|
Create: false,
|
|
|
|
Push: true,
|
|
|
|
PullRequest: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
webhook.HookEvent = hookEvent
|
|
|
|
assert.NoError(t, webhook.UpdateEvent())
|
|
|
|
assert.NotEmpty(t, webhook.Events)
|
2023-01-01 10:23:15 -05:00
|
|
|
actualHookEvent := &webhook_module.HookEvent{}
|
2017-01-25 05:37:35 -05:00
|
|
|
assert.NoError(t, json.Unmarshal([]byte(webhook.Events), actualHookEvent))
|
|
|
|
assert.Equal(t, *hookEvent, *actualHookEvent)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebhook_EventsArray(t *testing.T) {
|
2021-03-14 13:52:12 -05:00
|
|
|
assert.Equal(t, []string{
|
|
|
|
"create", "delete", "fork", "push",
|
2020-03-06 00:10:48 -05:00
|
|
|
"issues", "issue_assign", "issue_label", "issue_milestone", "issue_comment",
|
|
|
|
"pull_request", "pull_request_assign", "pull_request_label", "pull_request_milestone",
|
|
|
|
"pull_request_comment", "pull_request_review_approved", "pull_request_review_rejected",
|
2022-09-04 14:54:23 -05:00
|
|
|
"pull_request_review_comment", "pull_request_sync", "wiki", "repository", "release",
|
2023-05-24 21:06:27 -05:00
|
|
|
"package", "pull_request_review_request",
|
2021-03-14 13:52:12 -05:00
|
|
|
},
|
2017-01-25 05:37:35 -05:00
|
|
|
(&Webhook{
|
2023-01-01 10:23:15 -05:00
|
|
|
HookEvent: &webhook_module.HookEvent{SendEverything: true},
|
2017-01-25 05:37:35 -05:00
|
|
|
}).EventsArray(),
|
|
|
|
)
|
|
|
|
|
|
|
|
assert.Equal(t, []string{"push"},
|
|
|
|
(&Webhook{
|
2023-01-01 10:23:15 -05:00
|
|
|
HookEvent: &webhook_module.HookEvent{PushOnly: true},
|
2017-01-25 05:37:35 -05:00
|
|
|
}).EventsArray(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateWebhook(t *testing.T) {
|
|
|
|
hook := &Webhook{
|
|
|
|
RepoID: 3,
|
|
|
|
URL: "www.example.com/unit_test",
|
|
|
|
ContentType: ContentTypeJSON,
|
|
|
|
Events: `{"push_only":false,"send_everything":false,"choose_events":false,"events":{"create":false,"push":true,"pull_request":true}}`,
|
|
|
|
}
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertNotExistsBean(t, hook)
|
2021-11-10 00:13:16 -05:00
|
|
|
assert.NoError(t, CreateWebhook(db.DefaultContext, hook))
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertExistsAndLoadBean(t, hook)
|
2017-01-25 05:37:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetWebhookByRepoID(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-25 05:37:35 -05:00
|
|
|
hook, err := GetWebhookByRepoID(1, 1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(1), hook.ID)
|
|
|
|
|
2021-11-16 03:53:21 -05:00
|
|
|
_, err = GetWebhookByRepoID(unittest.NonexistentID, unittest.NonexistentID)
|
2017-01-25 05:37:35 -05:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrWebhookNotExist(err))
|
|
|
|
}
|
|
|
|
|
2023-03-10 09:28:32 -05:00
|
|
|
func TestGetWebhookByOwnerID(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2023-03-10 09:28:32 -05:00
|
|
|
hook, err := GetWebhookByOwnerID(3, 3)
|
2017-01-25 05:37:35 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(3), hook.ID)
|
|
|
|
|
2023-03-10 09:28:32 -05:00
|
|
|
_, err = GetWebhookByOwnerID(unittest.NonexistentID, unittest.NonexistentID)
|
2017-01-25 05:37:35 -05:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrWebhookNotExist(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetActiveWebhooksByRepoID(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-05-20 09:08:52 -05:00
|
|
|
hooks, err := ListWebhooksByOpts(db.DefaultContext, &ListWebhookOptions{RepoID: 1, IsActive: util.OptionalBoolTrue})
|
2017-01-25 05:37:35 -05:00
|
|
|
assert.NoError(t, err)
|
2017-08-28 04:17:45 -05:00
|
|
|
if assert.Len(t, hooks, 1) {
|
|
|
|
assert.Equal(t, int64(1), hooks[0].ID)
|
|
|
|
assert.True(t, hooks[0].IsActive)
|
|
|
|
}
|
2017-01-25 05:37:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetWebhooksByRepoID(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-05-20 09:08:52 -05:00
|
|
|
hooks, err := ListWebhooksByOpts(db.DefaultContext, &ListWebhookOptions{RepoID: 1})
|
2017-01-25 05:37:35 -05:00
|
|
|
assert.NoError(t, err)
|
2017-08-28 04:17:45 -05:00
|
|
|
if assert.Len(t, hooks, 2) {
|
|
|
|
assert.Equal(t, int64(1), hooks[0].ID)
|
|
|
|
assert.Equal(t, int64(2), hooks[1].ID)
|
|
|
|
}
|
2017-01-25 05:37:35 -05:00
|
|
|
}
|
|
|
|
|
2023-03-10 09:28:32 -05:00
|
|
|
func TestGetActiveWebhooksByOwnerID(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2023-03-10 09:28:32 -05:00
|
|
|
hooks, err := ListWebhooksByOpts(db.DefaultContext, &ListWebhookOptions{OwnerID: 3, IsActive: util.OptionalBoolTrue})
|
2017-01-25 05:37:35 -05:00
|
|
|
assert.NoError(t, err)
|
2017-08-28 04:17:45 -05:00
|
|
|
if assert.Len(t, hooks, 1) {
|
|
|
|
assert.Equal(t, int64(3), hooks[0].ID)
|
|
|
|
assert.True(t, hooks[0].IsActive)
|
|
|
|
}
|
2017-01-25 05:37:35 -05:00
|
|
|
}
|
|
|
|
|
2023-03-10 09:28:32 -05:00
|
|
|
func TestGetWebhooksByOwnerID(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2023-03-10 09:28:32 -05:00
|
|
|
hooks, err := ListWebhooksByOpts(db.DefaultContext, &ListWebhookOptions{OwnerID: 3})
|
2017-01-25 05:37:35 -05:00
|
|
|
assert.NoError(t, err)
|
2017-08-28 04:17:45 -05:00
|
|
|
if assert.Len(t, hooks, 1) {
|
|
|
|
assert.Equal(t, int64(3), hooks[0].ID)
|
|
|
|
assert.True(t, hooks[0].IsActive)
|
|
|
|
}
|
2017-01-25 05:37:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateWebhook(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 21:22:25 -05:00
|
|
|
hook := unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 2})
|
2017-01-25 05:37:35 -05:00
|
|
|
hook.IsActive = true
|
|
|
|
hook.ContentType = ContentTypeForm
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertNotExistsBean(t, hook)
|
2017-01-25 05:37:35 -05:00
|
|
|
assert.NoError(t, UpdateWebhook(hook))
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertExistsAndLoadBean(t, hook)
|
2017-01-25 05:37:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteWebhookByRepoID(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 2, RepoID: 1})
|
2017-01-25 05:37:35 -05:00
|
|
|
assert.NoError(t, DeleteWebhookByRepoID(1, 2))
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertNotExistsBean(t, &Webhook{ID: 2, RepoID: 1})
|
2017-01-25 05:37:35 -05:00
|
|
|
|
2021-11-16 03:53:21 -05:00
|
|
|
err := DeleteWebhookByRepoID(unittest.NonexistentID, unittest.NonexistentID)
|
2017-01-25 05:37:35 -05:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrWebhookNotExist(err))
|
|
|
|
}
|
|
|
|
|
2023-03-10 09:28:32 -05:00
|
|
|
func TestDeleteWebhookByOwnerID(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2023-03-10 09:28:32 -05:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 3, OwnerID: 3})
|
|
|
|
assert.NoError(t, DeleteWebhookByOwnerID(3, 3))
|
|
|
|
unittest.AssertNotExistsBean(t, &Webhook{ID: 3, OwnerID: 3})
|
2017-01-25 05:37:35 -05:00
|
|
|
|
2023-03-10 09:28:32 -05:00
|
|
|
err := DeleteWebhookByOwnerID(unittest.NonexistentID, unittest.NonexistentID)
|
2017-01-25 05:37:35 -05:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrWebhookNotExist(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHookTasks(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-25 05:37:35 -05:00
|
|
|
hookTasks, err := HookTasks(1, 1)
|
|
|
|
assert.NoError(t, err)
|
2017-08-28 04:17:45 -05:00
|
|
|
if assert.Len(t, hookTasks, 1) {
|
|
|
|
assert.Equal(t, int64(1), hookTasks[0].ID)
|
|
|
|
}
|
2017-01-25 05:37:35 -05:00
|
|
|
|
2021-11-16 03:53:21 -05:00
|
|
|
hookTasks, err = HookTasks(unittest.NonexistentID, 1)
|
2017-01-25 05:37:35 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, hookTasks, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateHookTask(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-25 05:37:35 -05:00
|
|
|
hookTask := &HookTask{
|
|
|
|
HookID: 3,
|
|
|
|
Payloader: &api.PushPayload{},
|
|
|
|
}
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertNotExistsBean(t, hookTask)
|
2022-10-21 11:21:56 -05:00
|
|
|
_, err := CreateHookTask(db.DefaultContext, hookTask)
|
|
|
|
assert.NoError(t, err)
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertExistsAndLoadBean(t, hookTask)
|
2017-01-25 05:37:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateHookTask(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-25 05:37:35 -05:00
|
|
|
|
2022-08-15 21:22:25 -05:00
|
|
|
hook := unittest.AssertExistsAndLoadBean(t, &HookTask{ID: 1})
|
2017-01-25 05:37:35 -05:00
|
|
|
hook.PayloadContent = "new payload content"
|
|
|
|
hook.IsDelivered = true
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertNotExistsBean(t, hook)
|
2017-01-25 05:37:35 -05:00
|
|
|
assert.NoError(t, UpdateHookTask(hook))
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertExistsAndLoadBean(t, hook)
|
2017-01-25 05:37:35 -05:00
|
|
|
}
|
2021-01-26 16:02:42 -05:00
|
|
|
|
|
|
|
func TestCleanupHookTaskTable_PerWebhook_DeletesDelivered(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-01-26 16:02:42 -05:00
|
|
|
hookTask := &HookTask{
|
|
|
|
HookID: 3,
|
|
|
|
Payloader: &api.PushPayload{},
|
|
|
|
IsDelivered: true,
|
2023-05-03 18:53:43 -05:00
|
|
|
Delivered: timeutil.TimeStampNanoNow(),
|
2021-01-26 16:02:42 -05:00
|
|
|
}
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertNotExistsBean(t, hookTask)
|
2022-10-21 11:21:56 -05:00
|
|
|
_, err := CreateHookTask(db.DefaultContext, hookTask)
|
|
|
|
assert.NoError(t, err)
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertExistsAndLoadBean(t, hookTask)
|
2021-01-26 16:02:42 -05:00
|
|
|
|
|
|
|
assert.NoError(t, CleanupHookTaskTable(context.Background(), PerWebhook, 168*time.Hour, 0))
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertNotExistsBean(t, hookTask)
|
2021-01-26 16:02:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCleanupHookTaskTable_PerWebhook_LeavesUndelivered(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-01-26 16:02:42 -05:00
|
|
|
hookTask := &HookTask{
|
|
|
|
HookID: 4,
|
|
|
|
Payloader: &api.PushPayload{},
|
|
|
|
IsDelivered: false,
|
|
|
|
}
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertNotExistsBean(t, hookTask)
|
2022-10-21 11:21:56 -05:00
|
|
|
_, err := CreateHookTask(db.DefaultContext, hookTask)
|
|
|
|
assert.NoError(t, err)
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertExistsAndLoadBean(t, hookTask)
|
2021-01-26 16:02:42 -05:00
|
|
|
|
|
|
|
assert.NoError(t, CleanupHookTaskTable(context.Background(), PerWebhook, 168*time.Hour, 0))
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertExistsAndLoadBean(t, hookTask)
|
2021-01-26 16:02:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCleanupHookTaskTable_PerWebhook_LeavesMostRecentTask(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-01-26 16:02:42 -05:00
|
|
|
hookTask := &HookTask{
|
|
|
|
HookID: 4,
|
|
|
|
Payloader: &api.PushPayload{},
|
|
|
|
IsDelivered: true,
|
2023-05-03 18:53:43 -05:00
|
|
|
Delivered: timeutil.TimeStampNanoNow(),
|
2021-01-26 16:02:42 -05:00
|
|
|
}
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertNotExistsBean(t, hookTask)
|
2022-10-21 11:21:56 -05:00
|
|
|
_, err := CreateHookTask(db.DefaultContext, hookTask)
|
|
|
|
assert.NoError(t, err)
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertExistsAndLoadBean(t, hookTask)
|
2021-01-26 16:02:42 -05:00
|
|
|
|
|
|
|
assert.NoError(t, CleanupHookTaskTable(context.Background(), PerWebhook, 168*time.Hour, 1))
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertExistsAndLoadBean(t, hookTask)
|
2021-01-26 16:02:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCleanupHookTaskTable_OlderThan_DeletesDelivered(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-01-26 16:02:42 -05:00
|
|
|
hookTask := &HookTask{
|
|
|
|
HookID: 3,
|
|
|
|
Payloader: &api.PushPayload{},
|
|
|
|
IsDelivered: true,
|
2023-05-03 18:53:43 -05:00
|
|
|
Delivered: timeutil.TimeStampNano(time.Now().AddDate(0, 0, -8).UnixNano()),
|
2021-01-26 16:02:42 -05:00
|
|
|
}
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertNotExistsBean(t, hookTask)
|
2022-10-21 11:21:56 -05:00
|
|
|
_, err := CreateHookTask(db.DefaultContext, hookTask)
|
|
|
|
assert.NoError(t, err)
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertExistsAndLoadBean(t, hookTask)
|
2021-01-26 16:02:42 -05:00
|
|
|
|
|
|
|
assert.NoError(t, CleanupHookTaskTable(context.Background(), OlderThan, 168*time.Hour, 0))
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertNotExistsBean(t, hookTask)
|
2021-01-26 16:02:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCleanupHookTaskTable_OlderThan_LeavesUndelivered(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-01-26 16:02:42 -05:00
|
|
|
hookTask := &HookTask{
|
|
|
|
HookID: 4,
|
|
|
|
Payloader: &api.PushPayload{},
|
|
|
|
IsDelivered: false,
|
|
|
|
}
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertNotExistsBean(t, hookTask)
|
2022-10-21 11:21:56 -05:00
|
|
|
_, err := CreateHookTask(db.DefaultContext, hookTask)
|
|
|
|
assert.NoError(t, err)
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertExistsAndLoadBean(t, hookTask)
|
2021-01-26 16:02:42 -05:00
|
|
|
|
|
|
|
assert.NoError(t, CleanupHookTaskTable(context.Background(), OlderThan, 168*time.Hour, 0))
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertExistsAndLoadBean(t, hookTask)
|
2021-01-26 16:02:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCleanupHookTaskTable_OlderThan_LeavesTaskEarlierThanAgeToDelete(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-01-26 16:02:42 -05:00
|
|
|
hookTask := &HookTask{
|
|
|
|
HookID: 4,
|
|
|
|
Payloader: &api.PushPayload{},
|
|
|
|
IsDelivered: true,
|
2023-05-03 18:53:43 -05:00
|
|
|
Delivered: timeutil.TimeStampNano(time.Now().AddDate(0, 0, -6).UnixNano()),
|
2021-01-26 16:02:42 -05:00
|
|
|
}
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertNotExistsBean(t, hookTask)
|
2022-10-21 11:21:56 -05:00
|
|
|
_, err := CreateHookTask(db.DefaultContext, hookTask)
|
|
|
|
assert.NoError(t, err)
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertExistsAndLoadBean(t, hookTask)
|
2021-01-26 16:02:42 -05:00
|
|
|
|
|
|
|
assert.NoError(t, CleanupHookTaskTable(context.Background(), OlderThan, 168*time.Hour, 0))
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertExistsAndLoadBean(t, hookTask)
|
2021-01-26 16:02:42 -05:00
|
|
|
}
|