webhook: add org tests

(cherry picked from commit 7d95cf6472fbf5b6713a037b52a466e25cef650c)
This commit is contained in:
oliverpool 2024-04-09 11:19:52 +02:00 committed by GitHub
parent f3a4134049
commit 373200e986

View file

@ -288,99 +288,108 @@ func assertInput(t testing.TB, form *goquery.Selection, name string) string {
func testWebhookForms(name string, session *TestSession, validFields map[string]string, invalidPatches ...map[string]string) func(t *testing.T) { func testWebhookForms(name string, session *TestSession, validFields map[string]string, invalidPatches ...map[string]string) func(t *testing.T) {
return func(t *testing.T) { return func(t *testing.T) {
// new webhook form t.Run("repo1", func(t *testing.T) {
resp := session.MakeRequest(t, NewRequest(t, "GET", "/user2/repo1/settings/hooks/"+name+"/new"), http.StatusOK) testWebhookFormsShared(t, "/user2/repo1/settings/hooks", name, session, validFields, invalidPatches...)
htmlForm := NewHTMLParser(t, resp.Body).Find(`form[action^="/user2/repo1/settings/hooks/"]`) })
t.Run("org3", func(t *testing.T) {
testWebhookFormsShared(t, "/org/org3/settings/hooks", name, session, validFields, invalidPatches...)
})
}
}
// fill the form func testWebhookFormsShared(t *testing.T, endpoint, name string, session *TestSession, validFields map[string]string, invalidPatches ...map[string]string) {
payload := map[string]string{ // new webhook form
"_csrf": htmlForm.Find(`input[name="_csrf"]`).AttrOr("value", ""), resp := session.MakeRequest(t, NewRequest(t, "GET", endpoint+"/"+name+"/new"), http.StatusOK)
"events": "send_everything", htmlForm := NewHTMLParser(t, resp.Body).Find(`form[action^="` + endpoint + `/"]`)
}
for k, v := range validFields {
assertInput(t, htmlForm, k)
payload[k] = v
}
if t.Failed() {
t.FailNow() // prevent further execution if the form could not be filled properly
}
// create the webhook (this redirects back to the hook list) // fill the form
resp = session.MakeRequest(t, NewRequestWithValues(t, "POST", "/user2/repo1/settings/hooks/"+name+"/new", payload), http.StatusSeeOther) payload := map[string]string{
assertHasFlashMessages(t, resp, "success") "_csrf": htmlForm.Find(`input[name="_csrf"]`).AttrOr("value", ""),
"events": "send_everything",
}
for k, v := range validFields {
assertInput(t, htmlForm, k)
payload[k] = v
}
if t.Failed() {
t.FailNow() // prevent further execution if the form could not be filled properly
}
// find last created hook in the hook list // create the webhook (this redirects back to the hook list)
// (a bit hacky, but the list should be sorted) resp = session.MakeRequest(t, NewRequestWithValues(t, "POST", endpoint+"/"+name+"/new", payload), http.StatusSeeOther)
resp = session.MakeRequest(t, NewRequest(t, "GET", "/user2/repo1/settings/hooks"), http.StatusOK) assertHasFlashMessages(t, resp, "success")
htmlDoc := NewHTMLParser(t, resp.Body)
editFormURL := htmlDoc.Find(`a[href^="/user2/repo1/settings/hooks/"]`).Last().AttrOr("href", "")
assert.NotEmpty(t, editFormURL)
// edit webhook form // find last created hook in the hook list
resp = session.MakeRequest(t, NewRequest(t, "GET", editFormURL), http.StatusOK) // (a bit hacky, but the list should be sorted)
htmlForm = NewHTMLParser(t, resp.Body).Find(`form[action^="/user2/repo1/settings/hooks/"]`) resp = session.MakeRequest(t, NewRequest(t, "GET", endpoint), http.StatusOK)
editPostURL := htmlForm.AttrOr("action", "") htmlDoc := NewHTMLParser(t, resp.Body)
assert.NotEmpty(t, editPostURL) editFormURL := htmlDoc.Find(`a[href^="`+endpoint+`/"]`).Last().AttrOr("href", "")
assert.NotEmpty(t, editFormURL)
// fill the form // edit webhook form
payload = map[string]string{ resp = session.MakeRequest(t, NewRequest(t, "GET", editFormURL), http.StatusOK)
"_csrf": htmlForm.Find(`input[name="_csrf"]`).AttrOr("value", ""), htmlForm = NewHTMLParser(t, resp.Body).Find(`form[action^="` + endpoint + `/"]`)
"events": "push_only", editPostURL := htmlForm.AttrOr("action", "")
} assert.NotEmpty(t, editPostURL)
for k, v := range validFields {
assert.Equal(t, v, assertInput(t, htmlForm, k), "input %q did not contain value %q", k, v)
payload[k] = v
}
// update the webhook // fill the form
resp = session.MakeRequest(t, NewRequestWithValues(t, "POST", editPostURL, payload), http.StatusSeeOther) payload = map[string]string{
assertHasFlashMessages(t, resp, "success") "_csrf": htmlForm.Find(`input[name="_csrf"]`).AttrOr("value", ""),
"events": "push_only",
}
for k, v := range validFields {
assert.Equal(t, v, assertInput(t, htmlForm, k), "input %q did not contain value %q", k, v)
payload[k] = v
}
// check the updated webhook // update the webhook
resp = session.MakeRequest(t, NewRequest(t, "GET", editFormURL), http.StatusOK) resp = session.MakeRequest(t, NewRequestWithValues(t, "POST", editPostURL, payload), http.StatusSeeOther)
htmlForm = NewHTMLParser(t, resp.Body).Find(`form[action^="/user2/repo1/settings/hooks/"]`) assertHasFlashMessages(t, resp, "success")
for k, v := range validFields {
assert.Equal(t, v, assertInput(t, htmlForm, k), "input %q did not contain value %q", k, v)
}
if len(invalidPatches) > 0 { // check the updated webhook
// check that invalid fields are rejected resp = session.MakeRequest(t, NewRequest(t, "GET", editFormURL), http.StatusOK)
resp := session.MakeRequest(t, NewRequest(t, "GET", "/user2/repo1/settings/hooks/"+name+"/new"), http.StatusOK) htmlForm = NewHTMLParser(t, resp.Body).Find(`form[action^="` + endpoint + `/"]`)
htmlForm := NewHTMLParser(t, resp.Body).Find(`form[action^="/user2/repo1/settings/hooks/"]`) for k, v := range validFields {
assert.Equal(t, v, assertInput(t, htmlForm, k), "input %q did not contain value %q", k, v)
}
for _, invalidPatch := range invalidPatches { if len(invalidPatches) > 0 {
t.Run("invalid", func(t *testing.T) { // check that invalid fields are rejected
// fill the form resp := session.MakeRequest(t, NewRequest(t, "GET", endpoint+"/"+name+"/new"), http.StatusOK)
payload := map[string]string{ htmlForm := NewHTMLParser(t, resp.Body).Find(`form[action^="` + endpoint + `/"]`)
"_csrf": htmlForm.Find(`input[name="_csrf"]`).AttrOr("value", ""),
"events": "send_everything", for _, invalidPatch := range invalidPatches {
} t.Run("invalid", func(t *testing.T) {
for k, v := range validFields { // fill the form
payload := map[string]string{
"_csrf": htmlForm.Find(`input[name="_csrf"]`).AttrOr("value", ""),
"events": "send_everything",
}
for k, v := range validFields {
payload[k] = v
}
for k, v := range invalidPatch {
if v == "" {
delete(payload, k)
} else {
payload[k] = v payload[k] = v
} }
for k, v := range invalidPatch { }
if v == "" {
delete(payload, k)
} else {
payload[k] = v
}
}
resp := session.MakeRequest(t, NewRequestWithValues(t, "POST", "/user2/repo1/settings/hooks/"+name+"/new", payload), http.StatusUnprocessableEntity) resp := session.MakeRequest(t, NewRequestWithValues(t, "POST", endpoint+"/"+name+"/new", payload), http.StatusUnprocessableEntity)
// check that the invalid form is pre-filled // check that the invalid form is pre-filled
htmlForm = NewHTMLParser(t, resp.Body).Find(`form[action^="/user2/repo1/settings/hooks/"]`) htmlForm = NewHTMLParser(t, resp.Body).Find(`form[action^="` + endpoint + `/"]`)
for k, v := range payload { for k, v := range payload {
if k == "_csrf" || k == "events" || v == "" { if k == "_csrf" || k == "events" || v == "" {
// the 'events' is a radio input, which is buggy below // the 'events' is a radio input, which is buggy below
continue continue
}
assert.Equal(t, v, assertInput(t, htmlForm, k), "input %q did not contain value %q", k, v)
} }
if t.Failed() { assert.Equal(t, v, assertInput(t, htmlForm, k), "input %q did not contain value %q", k, v)
t.Log(invalidPatch) }
} if t.Failed() {
}) t.Log(invalidPatch)
} }
})
} }
} }
} }