2020-03-22 22:07:22 +08:00
|
|
|
package repo
|
|
|
|
|
|
|
|
|
|
import (
|
2026-01-30 22:27:59 +08:00
|
|
|
"net/http"
|
2020-03-22 22:07:22 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
2024-02-18 19:39:41 -05:00
|
|
|
"gogs.io/gogs/internal/database"
|
2020-04-06 18:35:10 +08:00
|
|
|
"gogs.io/gogs/internal/mocks"
|
2020-03-22 22:07:22 +08:00
|
|
|
)
|
|
|
|
|
|
2026-01-30 22:27:59 +08:00
|
|
|
func TestValidateWebhook(t *testing.T) {
|
2020-04-06 18:35:10 +08:00
|
|
|
l := &mocks.Locale{
|
|
|
|
|
MockLang: "en",
|
2023-02-02 21:25:25 +08:00
|
|
|
MockTr: func(s string, _ ...any) string {
|
2020-04-06 18:35:10 +08:00
|
|
|
return s
|
|
|
|
|
},
|
|
|
|
|
}
|
2020-03-22 22:07:22 +08:00
|
|
|
|
|
|
|
|
tests := []struct {
|
2026-01-30 22:27:59 +08:00
|
|
|
name string
|
|
|
|
|
actor *database.User
|
|
|
|
|
webhook *database.Webhook
|
|
|
|
|
wantField string
|
|
|
|
|
wantMsg string
|
|
|
|
|
wantStatus int
|
2020-03-22 22:07:22 +08:00
|
|
|
}{
|
|
|
|
|
{
|
2026-01-30 22:27:59 +08:00
|
|
|
name: "admin bypass local address check",
|
|
|
|
|
webhook: &database.Webhook{URL: "https://www.google.com"},
|
|
|
|
|
wantStatus: http.StatusOK,
|
2020-03-22 22:07:22 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
{
|
2026-01-30 22:27:59 +08:00
|
|
|
name: "local address not allowed",
|
|
|
|
|
webhook: &database.Webhook{URL: "http://localhost:3306"},
|
|
|
|
|
wantField: "PayloadURL",
|
|
|
|
|
wantMsg: "repo.settings.webhook.url_resolved_to_blocked_local_address",
|
|
|
|
|
wantStatus: http.StatusForbidden,
|
2020-03-22 22:07:22 +08:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
for _, test := range tests {
|
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2026-01-30 22:27:59 +08:00
|
|
|
field, msg, status := validateWebhook(l, test.webhook)
|
|
|
|
|
assert.Equal(t, test.wantStatus, status)
|
|
|
|
|
assert.Equal(t, test.wantMsg, msg)
|
|
|
|
|
assert.Equal(t, test.wantField, field)
|
2020-03-22 22:07:22 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|