check / check (push) Waiting to run
Target.MaxRetries (max_retries) becomes Target.MaxAttempts (max_attempts), and the form field, parser and ceiling are renamed to match. The column is NOT NULL and a check constraint refuses a value below 1, so every target, whatever its type, is stored with at least 1. 1 is a single attempt with no circuit breaker; above 1 retries behind the breaker up to that many attempts. Both target forms refuse 0 and less; a blank field on the add target form gives 1, and on the edit form keeps the stored value. The target page shows the plain number, and the README's two paragraphs on 0 against 1 become one sentence. Model: opus-5-5
176 lines
5.5 KiB
Go
176 lines
5.5 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"html/template"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
"sneak.berlin/go/webhooker/internal/database"
|
|
)
|
|
|
|
// SetLogForTest replaces the handler's logger, so the handlers_test
|
|
// package can assert on what a log line actually contains rather than
|
|
// on what it is meant to contain.
|
|
func (s *Handlers) SetLogForTest(log *slog.Logger) {
|
|
s.log = log
|
|
}
|
|
|
|
// MaxRenderedBodyBytesForTest exposes the body cap of the lists
|
|
// of events to the handlers_test package.
|
|
const MaxRenderedBodyBytesForTest = maxRenderedBodyBytes
|
|
|
|
// NewBodyViewForTest exposes newBodyView for use in the
|
|
// handlers_test package.
|
|
func NewBodyViewForTest(body []byte, size int64) BodyView {
|
|
return newBodyView("/hook/w/events/e", body, size)
|
|
}
|
|
|
|
// MaxRenderedResponseBytesForTest exposes the event log's
|
|
// delivery response cap to the handlers_test package.
|
|
const MaxRenderedResponseBytesForTest = maxRenderedResponseBytes
|
|
|
|
// MaxRenderedAttemptsForTest exposes the event log's
|
|
// per-delivery attempt ceiling to the handlers_test package.
|
|
const MaxRenderedAttemptsForTest = maxRenderedAttempts
|
|
|
|
// MaxTargetAttemptsForTest exposes the target max_attempts ceiling to
|
|
// the handlers_test package, so the tests assert against the constant
|
|
// the handlers enforce rather than a number copied beside it.
|
|
const MaxTargetAttemptsForTest = maxTargetAttempts
|
|
|
|
// EventDBLeftMsgForTest and SidecarLeftMsgForTest expose the two
|
|
// messages the webhook delete handler logs when a file of the event
|
|
// database is left on disk, so a test checking that one is absent
|
|
// checks for the handler's own wording.
|
|
const (
|
|
EventDBLeftMsgForTest = eventDBLeftMsg
|
|
SidecarLeftMsgForTest = sidecarLeftMsg
|
|
)
|
|
|
|
// DummyVerificationsForTest reports how many equivalent-cost
|
|
// verifications were charged for usernames that do not exist. It
|
|
// lets a test prove the anti-enumeration path ran without timing
|
|
// anything.
|
|
func (s *Handlers) DummyVerificationsForTest() uint64 {
|
|
return s.dummyVerifications.Load()
|
|
}
|
|
|
|
// TrimPartialRuneForTest exposes trimPartialRune for use in the
|
|
// handlers_test package.
|
|
func TrimPartialRuneForTest(b []byte) []byte {
|
|
return trimPartialRune(b)
|
|
}
|
|
|
|
// LoadEventLogViewsForTest exposes loadEventsWithDeliveries for
|
|
// use in the handlers_test package. Assertions on the projected
|
|
// body need the bytes as loaded: html/template rewrites invalid
|
|
// UTF-8 on the way out, so the rendered page cannot show whether
|
|
// a binary body survived the projection intact.
|
|
func (s *Handlers) LoadEventLogViewsForTest(
|
|
w http.ResponseWriter,
|
|
webhook database.Webhook,
|
|
) []EventLogView {
|
|
views, _, _ := s.loadEventsWithDeliveries(
|
|
w, newRequestForTest(), webhook, nil, nil,
|
|
)
|
|
|
|
return views
|
|
}
|
|
|
|
// WebhookStatsForTest returns the figures the statistics pane on a
|
|
// webhook's page shows, from the webhook's entrypoints and targets
|
|
// loaded as that page loads them.
|
|
func (s *Handlers) WebhookStatsForTest(webhookID string) *WebhookStats {
|
|
var entrypoints []database.Entrypoint
|
|
|
|
s.db.DB().Where("webhook_id = ?", webhookID).Find(&entrypoints)
|
|
|
|
var targets []database.Target
|
|
|
|
s.db.DB().Where("webhook_id = ?", webhookID).Find(&targets)
|
|
|
|
return s.loadWebhookStats(webhookID, entrypoints, targets)
|
|
}
|
|
|
|
// FinishedByTargetForTest exposes finishedByTarget for use in the
|
|
// handlers_test package.
|
|
func FinishedByTargetForTest(
|
|
webhookDB *gorm.DB, since time.Time,
|
|
) ([]TargetFinished, error) {
|
|
return finishedByTarget(webhookDB, since)
|
|
}
|
|
|
|
// newRequestForTest is the request the helpers here pass on for
|
|
// callers that have none: it is used only to render the error page.
|
|
func newRequestForTest() *http.Request {
|
|
return httptest.NewRequestWithContext(
|
|
context.Background(), http.MethodGet, "/", nil,
|
|
)
|
|
}
|
|
|
|
// AddTemplateForTest registers a template under a page name so that
|
|
// the handlers_test package can drive the render path with a
|
|
// template of its own.
|
|
func (s *Handlers) AddTemplateForTest(
|
|
pageTemplate string,
|
|
tmpl *template.Template,
|
|
) {
|
|
s.templates[pageTemplate] = tmpl
|
|
}
|
|
|
|
// RenderTemplateForTest exposes renderTemplate for use in the
|
|
// handlers_test package.
|
|
func (s *Handlers) RenderTemplateForTest(
|
|
w http.ResponseWriter,
|
|
r *http.Request,
|
|
pageTemplate string,
|
|
data any,
|
|
) {
|
|
s.renderTemplate(w, r, pageTemplate, data)
|
|
}
|
|
|
|
// BuildSlackTargetConfigForTest exposes
|
|
// buildSlackTargetConfig for use in the handlers_test package.
|
|
func (s *Handlers) BuildSlackTargetConfigForTest(
|
|
ctx context.Context,
|
|
targetURL string,
|
|
) (string, string, error) {
|
|
return s.buildSlackTargetConfig(ctx, targetURL)
|
|
}
|
|
|
|
// BuildHTTPTargetConfigForTest exposes buildHTTPTargetConfig
|
|
// for use in the handlers_test package, taking the form fields
|
|
// an HTTP target's configuration is built from.
|
|
func (s *Handlers) BuildHTTPTargetConfigForTest(
|
|
ctx context.Context,
|
|
targetURL, headers, timeout string,
|
|
) (string, string, error) {
|
|
return s.buildHTTPTargetConfig(ctx, targetFormInput{
|
|
URL: targetURL,
|
|
Headers: headers,
|
|
Timeout: timeout,
|
|
})
|
|
}
|
|
|
|
// BuildDatabaseTargetConfigForTest exposes
|
|
// buildDatabaseTargetConfig for use in the handlers_test
|
|
// package.
|
|
func BuildDatabaseTargetConfigForTest(
|
|
expiry, rotation string,
|
|
) (string, string, error) {
|
|
return buildDatabaseTargetConfig(expiry, rotation)
|
|
}
|
|
|
|
// ArchiveFileViewForTest exposes archiveFileView, which describes a
|
|
// database target's archive files as the target list shows them at
|
|
// now.
|
|
func (s *Handlers) ArchiveFileViewForTest(
|
|
webhook *database.Webhook, target *database.Target, now time.Time,
|
|
) *ArchiveFileView {
|
|
return s.archiveFileView(webhook, target, now)
|
|
}
|