[TESTS] webhook forms keep submitted data when invalid

This commit is contained in:
oliverpool 2024-03-23 22:43:44 +01:00
parent 1e050d01c0
commit 635230ca5d
3 changed files with 42 additions and 15 deletions

View file

@ -219,6 +219,22 @@ func WebhookCreate(ctx *context.Context) {
ctx.Data["BaseLinkNew"] = orCtx.LinkNew
if ctx.HasError() {
// pre-fill the form with the submitted data
var w webhook.Webhook
w.URL = fields.URL
w.ContentType = fields.ContentType
w.Secret = fields.Secret
w.HookEvent = ParseHookEvent(fields.WebhookForm)
w.IsActive = fields.WebhookForm.Active
w.HTTPMethod = fields.HTTPMethod
err := w.SetHeaderAuthorization(fields.WebhookForm.AuthorizationHeader)
if err != nil {
ctx.ServerError("SetHeaderAuthorization", err)
return
}
ctx.Data["Webhook"] = w
ctx.Data["HookMetadata"] = fields.Metadata
ctx.HTML(http.StatusUnprocessableEntity, orCtx.NewTemplate)
return
}
@ -284,13 +300,27 @@ func WebhookUpdate(ctx *context.Context) {
middleware.Validate(errs, ctx.Data, form, ctx.Locale) // error checked below in ctx.HasError
})
// pre-fill the form with the submitted data
w.URL = fields.URL
w.ContentType = fields.ContentType
w.Secret = fields.Secret
w.HookEvent = ParseHookEvent(fields.WebhookForm)
w.IsActive = fields.WebhookForm.Active
w.HTTPMethod = fields.HTTPMethod
err := w.SetHeaderAuthorization(fields.WebhookForm.AuthorizationHeader)
if err != nil {
ctx.ServerError("SetHeaderAuthorization", err)
return
}
if ctx.HasError() {
ctx.Data["HookMetadata"] = fields.Metadata
ctx.HTML(http.StatusUnprocessableEntity, orCtx.NewTemplate)
return
}
var meta []byte
var err error
if fields.Metadata != nil {
meta, err = json.Marshal(fields.Metadata)
if err != nil {
@ -299,20 +329,8 @@ func WebhookUpdate(ctx *context.Context) {
}
}
w.URL = fields.URL
w.ContentType = fields.ContentType
w.Secret = fields.Secret
w.HookEvent = ParseHookEvent(fields.WebhookForm)
w.IsActive = fields.WebhookForm.Active
w.HTTPMethod = fields.HTTPMethod
w.Meta = string(meta)
err = w.SetHeaderAuthorization(fields.WebhookForm.AuthorizationHeader)
if err != nil {
ctx.ServerError("SetHeaderAuthorization", err)
return
}
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return

View file

@ -259,7 +259,7 @@
</div>
<!-- Authorization Header -->
<div class="field{{if eq .HookType "matrix"}} required{{end}}">
<div class="field{{if eq .HookType "matrix"}} required{{end}} {{if .Err_AuthorizationHeader}}error{{end}}">
<label for="authorization_header">{{ctx.Locale.Tr "repo.settings.authorization_header"}}</label>
<input id="authorization_header" name="authorization_header" type="text" value="{{.Webhook.HeaderAuthorization}}"{{if eq .HookType "matrix"}} placeholder="Bearer $access_token" required{{end}}>
{{if ne .HookType "matrix"}}{{/* Matrix doesn't make the authorization optional but it is implied by the help string, should be changed.*/}}

View file

@ -330,7 +330,16 @@ func testWebhookForms(name string, session *TestSession, validFields map[string]
}
}
session.MakeRequest(t, NewRequestWithValues(t, "POST", "/user2/repo1/settings/hooks/"+name+"/new", payload), http.StatusUnprocessableEntity)
resp := session.MakeRequest(t, NewRequestWithValues(t, "POST", "/user2/repo1/settings/hooks/"+name+"/new", payload), http.StatusUnprocessableEntity)
// check that the invalid form is pre-filled
htmlForm = NewHTMLParser(t, resp.Body).Find(`form[action^="/user2/repo1/settings/hooks/"]`)
for k, v := range payload {
if k == "_csrf" || k == "events" || v == "" {
// the 'events' is a radio input, which is buggy below
continue
}
assert.Equal(t, v, assertInput(t, htmlForm, k), "input %q did not contain value %q", k, v)
}
if t.Failed() {
t.Log(invalidPatch)
}