Files
webhooker/internal/session/testing.go
clawbot 032f265d69
All checks were successful
check / check (push) Successful in 2m56s
Derive cookie Secure and CSRF strictness from the request transport (closes #269)
2026-08-24 03:01:37 +02:00

40 lines
1008 B
Go

package session
import (
"log/slog"
"time"
"github.com/gorilla/sessions"
"sneak.berlin/go/webhooker/internal/config"
)
// NewForTest creates a Session with a pre-configured cookie store for use
// in tests. This bypasses the fx lifecycle and database dependency, allowing
// middleware and handler tests to use real session functionality. The key
// parameter is the raw 32-byte authentication key used for session encryption
// and CSRF cookie signing.
//
// The idle timeout is taken from cfg.SessionIdleTimeout, exactly as in
// production. The now parameter supplies the clock used for expiry
// checks so tests can advance time without sleeping; pass nil for the
// real clock.
func NewForTest(
store *sessions.CookieStore,
cfg *config.Config,
log *slog.Logger,
key []byte,
now func() time.Time,
) *Session {
if now == nil {
now = time.Now
}
return &Session{
store: store,
key: key,
log: log,
idleTimeout: cfg.SessionIdleTimeout,
now: now,
}
}