package handlers import ( "html/template" "log/slog" "net/http" "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 event log's body cap // to the handlers_test package. const MaxRenderedBodyBytesForTest = maxRenderedBodyBytes // 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, page int, ) []EventLogView { views, _ := s.loadEventsWithDeliveries(w, webhook, nil, page) return views } // 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 buildURLTargetConfig // with the Slack target parameters for use in the // handlers_test package. func (s *Handlers) BuildSlackTargetConfigForTest( w http.ResponseWriter, r *http.Request, targetURL string, ) (string, error) { return s.buildURLTargetConfig( w, r, targetURL, "webhookUrl", "Webhook URL is required for Slack targets", ) } // BuildDatabaseTargetConfigForTest exposes // buildDatabaseTargetConfig for use in the handlers_test // package. func (s *Handlers) BuildDatabaseTargetConfigForTest( w http.ResponseWriter, expiry string, ) (string, error) { return s.buildDatabaseTargetConfig(w, expiry) }