Files
webhooker/internal/session/testing.go
T
sneak fcee8ae182
check / check (push) Waiting to run
Build the middleware test cookie stores with the production constructor (closes #154)
session.NewStore moves from internal/session/export_test.go into
internal/session/testing.go, so packages outside session can build the
store the application runs with. The two middleware test helpers that
assembled their own store by assigning store.Options now call it, which
puts their securecookie codecs on the same 7-day cap as production
instead of the library's 30-day default.

Model: opus-5-5
2026-10-02 11:20:29 +00:00

47 lines
1.2 KiB
Go

package session
import (
"log/slog"
"time"
"github.com/gorilla/sessions"
"sneak.berlin/go/webhooker/internal/config"
)
// NewStore exposes the production cookie-store constructor so tests
// exercise the store the application actually runs with, rather than a
// lookalike assembled in the test.
func NewStore(key []byte) *sessions.CookieStore {
return newStore(key)
}
// 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,
}
}