Skip to content

Commit 9a94019

Browse files
committed
webhook: add admin-hooks tests
1 parent e0b5f2d commit 9a94019

5 files changed

Lines changed: 40 additions & 20 deletions

File tree

models/webhook/webhook_system.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@ import (
88
"fmt"
99

1010
"code.gitea.io/gitea/models/db"
11-
"code.gitea.io/gitea/modules/optional"
1211
)
1312

1413
// GetDefaultWebhooks returns all admin-default webhooks.
1514
func GetDefaultWebhooks(ctx context.Context) ([]*Webhook, error) {
16-
webhooks := make([]*Webhook, 0, 5)
17-
return webhooks, db.GetEngine(ctx).
18-
Where("repo_id=? AND owner_id=? AND is_system_webhook=?", 0, 0, false).
19-
Find(&webhooks)
15+
return getAdminWebhooks(ctx, false)
2016
}
2117

2218
// GetSystemOrDefaultWebhook returns admin system or default webhook by given ID.
@@ -34,15 +30,21 @@ func GetSystemOrDefaultWebhook(ctx context.Context, id int64) (*Webhook, error)
3430
}
3531

3632
// GetSystemWebhooks returns all admin system webhooks.
37-
func GetSystemWebhooks(ctx context.Context, isActive optional.Option[bool]) ([]*Webhook, error) {
33+
func GetSystemWebhooks(ctx context.Context, onlyActive bool) ([]*Webhook, error) {
34+
return getAdminWebhooks(ctx, true, onlyActive)
35+
}
36+
37+
func getAdminWebhooks(ctx context.Context, systemWebhooks bool, onlyActive ...bool) ([]*Webhook, error) {
3838
webhooks := make([]*Webhook, 0, 5)
39-
if !isActive.Has() {
39+
if len(onlyActive) > 0 && onlyActive[0] {
4040
return webhooks, db.GetEngine(ctx).
41-
Where("repo_id=? AND owner_id=? AND is_system_webhook=?", 0, 0, true).
41+
Where("repo_id=? AND owner_id=? AND is_system_webhook=? AND is_active = ?", 0, 0, systemWebhooks, true).
42+
OrderBy("id").
4243
Find(&webhooks)
4344
}
4445
return webhooks, db.GetEngine(ctx).
45-
Where("repo_id=? AND owner_id=? AND is_system_webhook=? AND is_active = ?", 0, 0, true, isActive.Value()).
46+
Where("repo_id=? AND owner_id=? AND is_system_webhook=?", 0, 0, systemWebhooks).
47+
OrderBy("id").
4648
Find(&webhooks)
4749
}
4850

routers/api/v1/admin/hooks.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"net/http"
99

1010
"code.gitea.io/gitea/models/webhook"
11-
"code.gitea.io/gitea/modules/optional"
1211
"code.gitea.io/gitea/modules/setting"
1312
api "code.gitea.io/gitea/modules/structs"
1413
"code.gitea.io/gitea/modules/util"
@@ -38,7 +37,7 @@ func ListHooks(ctx *context.APIContext) {
3837
// "200":
3938
// "$ref": "#/responses/HookList"
4039

41-
sysHooks, err := webhook.GetSystemWebhooks(ctx, optional.None[bool]())
40+
sysHooks, err := webhook.GetSystemWebhooks(ctx, false)
4241
if err != nil {
4342
ctx.Error(http.StatusInternalServerError, "GetSystemWebhooks", err)
4443
return

routers/web/admin/hooks.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"code.gitea.io/gitea/models/webhook"
1010
"code.gitea.io/gitea/modules/base"
11-
"code.gitea.io/gitea/modules/optional"
1211
"code.gitea.io/gitea/modules/setting"
1312
"code.gitea.io/gitea/services/context"
1413
webhook_service "code.gitea.io/gitea/services/webhook"
@@ -36,7 +35,7 @@ func DefaultOrSystemWebhooks(ctx *context.Context) {
3635

3736
sys["Title"] = ctx.Tr("admin.systemhooks")
3837
sys["Description"] = ctx.Tr("admin.systemhooks.desc")
39-
sys["Webhooks"], err = webhook.GetSystemWebhooks(ctx, optional.None[bool]())
38+
sys["Webhooks"], err = webhook.GetSystemWebhooks(ctx, false)
4039
sys["BaseLink"] = setting.AppSubURL + "/admin/hooks"
4140
sys["BaseLinkNew"] = setting.AppSubURL + "/admin/system-hooks"
4241
sys["WebhookList"] = webhook_service.List()

services/webhook/webhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func PrepareWebhooks(ctx context.Context, source EventSource, event webhook_modu
243243
}
244244

245245
// Add any admin-defined system webhooks
246-
systemHooks, err := webhook_model.GetSystemWebhooks(ctx, optional.Some(true))
246+
systemHooks, err := webhook_model.GetSystemWebhooks(ctx, true)
247247
if err != nil {
248248
return fmt.Errorf("GetSystemWebhooks: %w", err)
249249
}

tests/integration/repo_webhook_test.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func TestNewWebHookLink(t *testing.T) {
8787
func TestWebhookForms(t *testing.T) {
8888
defer tests.PrepareTestEnv(t)()
8989

90-
session := loginUser(t, "user2")
90+
session := loginUser(t, "user1")
9191

9292
t.Run("forgejo/required", testWebhookForms("forgejo", session, map[string]string{
9393
"payload_url": "https://forgejo.example.com",
@@ -299,7 +299,9 @@ func assertInput(t testing.TB, form *goquery.Selection, name string) string {
299299
t.Helper()
300300
input := form.Find(`input[name="` + name + `"]`)
301301
if input.Length() != 1 {
302-
t.Log(form.Html())
302+
form.Find("input").Each(func(i int, s *goquery.Selection) {
303+
t.Logf("found <input name=%q />", s.AttrOr("name", ""))
304+
})
303305
t.Errorf("field <input name=%q /> found %d times, expected once", name, input.Length())
304306
}
305307
switch input.AttrOr("type", "") {
@@ -321,6 +323,12 @@ func testWebhookForms(name string, session *TestSession, validFields map[string]
321323
t.Run("org3", func(t *testing.T) {
322324
testWebhookFormsShared(t, "/org/org3/settings/hooks", name, session, validFields, invalidPatches...)
323325
})
326+
t.Run("system", func(t *testing.T) {
327+
testWebhookFormsShared(t, "/admin/system-hooks", name, session, validFields, invalidPatches...)
328+
})
329+
t.Run("default", func(t *testing.T) {
330+
testWebhookFormsShared(t, "/admin/default-hooks", name, session, validFields, invalidPatches...)
331+
})
324332
}
325333
}
326334

@@ -345,17 +353,29 @@ func testWebhookFormsShared(t *testing.T, endpoint, name string, session *TestSe
345353
// create the webhook (this redirects back to the hook list)
346354
resp = session.MakeRequest(t, NewRequestWithValues(t, "POST", endpoint+"/"+name+"/new", payload), http.StatusSeeOther)
347355
assertHasFlashMessages(t, resp, "success")
356+
listEndpoint := resp.Header().Get("Location")
357+
updateEndpoint := endpoint + "/"
358+
if endpoint == "/admin/system-hooks" || endpoint == "/admin/default-hooks" {
359+
updateEndpoint = "/admin/hooks/"
360+
}
348361

349362
// find last created hook in the hook list
350363
// (a bit hacky, but the list should be sorted)
351-
resp = session.MakeRequest(t, NewRequest(t, "GET", endpoint), http.StatusOK)
364+
resp = session.MakeRequest(t, NewRequest(t, "GET", listEndpoint), http.StatusOK)
352365
htmlDoc := NewHTMLParser(t, resp.Body)
353-
editFormURL := htmlDoc.Find(`a[href^="`+endpoint+`/"]`).Last().AttrOr("href", "")
366+
selector := `a[href^="` + updateEndpoint + `"]`
367+
if endpoint == "/admin/system-hooks" {
368+
// system-hooks and default-hooks are listed on the same page
369+
// add a specifier to select the latest system-hooks
370+
// (the default-hooks are at the end, so no further specifier needed)
371+
selector = `.admin-setting-content > div:first-of-type ` + selector
372+
}
373+
editFormURL := htmlDoc.Find(selector).Last().AttrOr("href", "")
354374
assert.NotEmpty(t, editFormURL)
355375

356376
// edit webhook form
357377
resp = session.MakeRequest(t, NewRequest(t, "GET", editFormURL), http.StatusOK)
358-
htmlForm = NewHTMLParser(t, resp.Body).Find(`form[action^="` + endpoint + `/"]`)
378+
htmlForm = NewHTMLParser(t, resp.Body).Find(`form[action^="` + updateEndpoint + `"]`)
359379
editPostURL := htmlForm.AttrOr("action", "")
360380
assert.NotEmpty(t, editPostURL)
361381

@@ -375,7 +395,7 @@ func testWebhookFormsShared(t *testing.T, endpoint, name string, session *TestSe
375395

376396
// check the updated webhook
377397
resp = session.MakeRequest(t, NewRequest(t, "GET", editFormURL), http.StatusOK)
378-
htmlForm = NewHTMLParser(t, resp.Body).Find(`form[action^="` + endpoint + `/"]`)
398+
htmlForm = NewHTMLParser(t, resp.Body).Find(`form[action^="` + updateEndpoint + `"]`)
379399
for k, v := range validFields {
380400
assert.Equal(t, v, assertInput(t, htmlForm, k), "input %q did not contain value %q", k, v)
381401
}

0 commit comments

Comments
 (0)