mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-14 22:59:29 +01:00
[TEST] webhook creation payload ref
(cherry picked from commit 9d2919248b
)
This commit is contained in:
parent
7e5fed7e29
commit
4a8eb1caa1
3 changed files with 74 additions and 1 deletions
|
@ -106,7 +106,7 @@ func (t *HookTask) simpleMarshalJSON(v any) string {
|
||||||
return string(p)
|
return string(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HookTasks returns a list of hook tasks by given conditions.
|
// HookTasks returns a list of hook tasks by given conditions, order by ID desc.
|
||||||
func HookTasks(ctx context.Context, hookID int64, page int) ([]*HookTask, error) {
|
func HookTasks(ctx context.Context, hookID int64, page int) ([]*HookTask, error) {
|
||||||
tasks := make([]*HookTask, 0, setting.Webhook.PagingNum)
|
tasks := make([]*HookTask, 0, setting.Webhook.PagingNum)
|
||||||
return tasks, db.GetEngine(ctx).
|
return tasks, db.GetEngine(ctx).
|
||||||
|
|
|
@ -83,6 +83,7 @@ func testPullCleanUp(t *testing.T, session *TestSession, user, repo, pullnum str
|
||||||
return resp
|
return resp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// returns the hook tasks, order by ID desc.
|
||||||
func retrieveHookTasks(t *testing.T, hookID int64, activateWebhook bool) []*webhook.HookTask {
|
func retrieveHookTasks(t *testing.T, hookID int64, activateWebhook bool) []*webhook.HookTask {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
if activateWebhook {
|
if activateWebhook {
|
||||||
|
|
72
tests/integration/webhook_test.go
Normal file
72
tests/integration/webhook_test.go
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package integration
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/models/db"
|
||||||
|
"code.gitea.io/gitea/models/unittest"
|
||||||
|
webhook_model "code.gitea.io/gitea/models/webhook"
|
||||||
|
"code.gitea.io/gitea/modules/json"
|
||||||
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestWebhookPayloadRef(t *testing.T) {
|
||||||
|
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
|
||||||
|
w := unittest.AssertExistsAndLoadBean(t, &webhook_model.Webhook{ID: 1})
|
||||||
|
w.HookEvent = &webhook_module.HookEvent{
|
||||||
|
SendEverything: true,
|
||||||
|
}
|
||||||
|
assert.NoError(t, w.UpdateEvent())
|
||||||
|
assert.NoError(t, webhook_model.UpdateWebhook(db.DefaultContext, w))
|
||||||
|
|
||||||
|
hookTasks := retrieveHookTasks(t, w.ID, true)
|
||||||
|
hookTasksLenBefore := len(hookTasks)
|
||||||
|
|
||||||
|
session := loginUser(t, "user2")
|
||||||
|
// create new branch
|
||||||
|
csrf := GetCSRF(t, session, "user2/repo1")
|
||||||
|
req := NewRequestWithValues(t, "POST", "user2/repo1/branches/_new/branch/master",
|
||||||
|
map[string]string{
|
||||||
|
"_csrf": csrf,
|
||||||
|
"new_branch_name": "arbre",
|
||||||
|
"create_tag": "false",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
session.MakeRequest(t, req, http.StatusSeeOther)
|
||||||
|
// delete the created branch
|
||||||
|
req = NewRequestWithValues(t, "POST", "user2/repo1/branches/delete?name=arbre",
|
||||||
|
map[string]string{
|
||||||
|
"_csrf": csrf,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
session.MakeRequest(t, req, http.StatusOK)
|
||||||
|
|
||||||
|
// check the newly created hooktasks
|
||||||
|
hookTasks = retrieveHookTasks(t, w.ID, false)
|
||||||
|
expected := map[webhook_module.HookEventType]bool{
|
||||||
|
webhook_module.HookEventCreate: true,
|
||||||
|
webhook_module.HookEventPush: true, // the branch creation also creates a push event
|
||||||
|
webhook_module.HookEventDelete: true,
|
||||||
|
}
|
||||||
|
for _, hookTask := range hookTasks[:len(hookTasks)-hookTasksLenBefore] {
|
||||||
|
if !expected[hookTask.EventType] {
|
||||||
|
t.Errorf("unexpected (or duplicated) event %q", hookTask.EventType)
|
||||||
|
}
|
||||||
|
|
||||||
|
var payload struct {
|
||||||
|
Ref string `json:"ref"`
|
||||||
|
}
|
||||||
|
assert.NoError(t, json.Unmarshal([]byte(hookTask.PayloadContent), &payload))
|
||||||
|
assert.Equal(t, "refs/heads/arbre", payload.Ref, "unexpected ref for %q event", hookTask.EventType)
|
||||||
|
delete(expected, hookTask.EventType)
|
||||||
|
}
|
||||||
|
assert.Empty(t, expected)
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in a new issue