package handlers_test import ( "net/http" "net/http/httptest" "net/url" "regexp" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "sneak.berlin/go/webhooker/internal/database" ) // expiryNever is the archive expiry that keeps archived events // forever. const expiryNever = "never" // matched returns what the one group of pattern matched in page, at // each match. func matched(pattern, page string) []string { matches := regexp.MustCompile(pattern).FindAllStringSubmatch(page, -1) groups := make([]string, 0, len(matches)) for _, m := range matches { groups = append(groups, m[1]) } return groups } // expiryShown returns the archive expiries the webhook page's target // list shows. func expiryShown( t *testing.T, env *sourceTestEnv, webhookID string, ) []string { t.Helper() w := httptest.NewRecorder() env.handlers.HandleSourceDetail().ServeHTTP(w, getRequest( t, "/hook/"+webhookID, env.cookies, map[string]string{sourceIDParam: webhookID}, )) require.Equal(t, http.StatusOK, w.Code) return matched( `Archive Expiry:\s*([^<]*)`, w.Body.String(), ) } // expirySelected returns the target edit page and the expiries its // select starts on. func expirySelected( t *testing.T, env *sourceTestEnv, webhookID, targetID string, ) (string, []string) { t.Helper() w := serveTarget( env, http.MethodGet, "/hook/"+webhookID+"/targets/"+targetID+"/edit", nil, ) require.Equal(t, http.StatusOK, w.Code) page := w.Body.String() return page, matched(`