giteabot backport / giteabot (push) Canceled after 0s
giteabot / giteabot (push) Canceled after 0s
release-nightly / nightly-binary (push) Canceled after 0s
release-nightly / nightly-container (push) Canceled after 0s
cache-seeder / gobuild (push) Canceled after 0s
cache-seeder / lint (bindata, lint-backend) (push) Canceled after 0s
release-nightly-snapcraft / build-and-publish (push) Canceled after 0s
Includes direct password setup links in registration emails. Assisted-by: Codex:GPT-5
83 lines
2.3 KiB
Go
83 lines
2.3 KiB
Go
// Copyright 2025 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package utils
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
"gitea.dev/models/unittest"
|
|
"gitea.dev/modules/structs"
|
|
"gitea.dev/services/contexttest"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestTestHookValidation(t *testing.T) {
|
|
unittest.PrepareTestEnv(t)
|
|
|
|
t.Run("Test Validation", func(t *testing.T) {
|
|
ctx, _ := contexttest.MockAPIContext(t, "user2/repo1/hooks")
|
|
contexttest.LoadRepo(t, ctx, 1)
|
|
contexttest.LoadRepoCommit(t, ctx)
|
|
contexttest.LoadUser(t, ctx, 2)
|
|
|
|
checkCreateHookOption(ctx, &structs.CreateHookOption{
|
|
Type: "gitea",
|
|
Config: map[string]string{
|
|
"content_type": "json",
|
|
"url": "https://example.com/webhook",
|
|
},
|
|
})
|
|
assert.Equal(t, 0, ctx.Resp.WrittenStatus()) // not written yet
|
|
})
|
|
|
|
t.Run("Test Validation with invalid URL", func(t *testing.T) {
|
|
ctx, _ := contexttest.MockAPIContext(t, "user2/repo1/hooks")
|
|
contexttest.LoadRepo(t, ctx, 1)
|
|
contexttest.LoadRepoCommit(t, ctx)
|
|
contexttest.LoadUser(t, ctx, 2)
|
|
|
|
checkCreateHookOption(ctx, &structs.CreateHookOption{
|
|
Type: "gitea",
|
|
Config: map[string]string{
|
|
"content_type": "json",
|
|
"url": "example.com/webhook",
|
|
},
|
|
})
|
|
assert.Equal(t, http.StatusUnprocessableEntity, ctx.Resp.WrittenStatus())
|
|
})
|
|
|
|
t.Run("Test Validation with invalid webhook type", func(t *testing.T) {
|
|
ctx, _ := contexttest.MockAPIContext(t, "user2/repo1/hooks")
|
|
contexttest.LoadRepo(t, ctx, 1)
|
|
contexttest.LoadRepoCommit(t, ctx)
|
|
contexttest.LoadUser(t, ctx, 2)
|
|
|
|
checkCreateHookOption(ctx, &structs.CreateHookOption{
|
|
Type: "unknown",
|
|
Config: map[string]string{
|
|
"content_type": "json",
|
|
"url": "example.com/webhook",
|
|
},
|
|
})
|
|
assert.Equal(t, http.StatusUnprocessableEntity, ctx.Resp.WrittenStatus())
|
|
})
|
|
|
|
t.Run("Test Validation with empty content type", func(t *testing.T) {
|
|
ctx, _ := contexttest.MockAPIContext(t, "user2/repo1/hooks")
|
|
contexttest.LoadRepo(t, ctx, 1)
|
|
contexttest.LoadRepoCommit(t, ctx)
|
|
contexttest.LoadUser(t, ctx, 2)
|
|
|
|
checkCreateHookOption(ctx, &structs.CreateHookOption{
|
|
Type: "unknown",
|
|
Config: map[string]string{
|
|
"url": "https://example.com/webhook",
|
|
},
|
|
})
|
|
assert.Equal(t, http.StatusUnprocessableEntity, ctx.Resp.WrittenStatus())
|
|
})
|
|
}
|