authtest

package
v3.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package authtest provides synthetic deterministic fixtures via Principal, NewClock/NewAuthenticator and HTTP helpers. Callers own fixture/error privacy, request mutation and any body lifecycle. These helpers are not hostile- collaborator or provider-conformance guarantees; scripted state is serialized.

Index

Constants

This section is empty.

Variables

View Source
var Epoch = time.Unix(0, 0).UTC()

Epoch is the mutable default fixture timestamp. Set it only before concurrent fixture use; the package does not synchronize writes to this exported variable.

Functions

func Principal

Principal constructs a tested principal and supplies Epoch when no authentication time was specified.

func RequireFailure

func RequireFailure(testing TestingT, err error, kind authentication.FailureKind)

RequireFailure requires a classified authentication failure.

func RequirePrincipal

func RequirePrincipal(testing TestingT, ctx context.Context, subject string)

RequirePrincipal requires an authenticated subject in ctx.

func Result

Result constructs a tested authenticated result.

Types

type Authenticator

type Authenticator struct {
	// contains filtered or unexported fields
}

Authenticator serializes scripted outcomes and call recording under an instance mutex. It borrows errors and trusts credential.Kind: that callback runs under the mutex and must not block, panic or reenter this fixture. Custom kinds are not redacted; fixtures must remain synthetic/non-secret.

func NewAuthenticator

func NewAuthenticator(outcomes ...Outcome) *Authenticator

NewAuthenticator creates a deterministic scripted authenticator.

func (*Authenticator) Authenticate

func (a *Authenticator) Authenticate(ctx context.Context, credential authentication.Credential) (authentication.Result, error)

Authenticate records Kind before checking cancellation, then consumes one outcome only for an active context. Empty scripts fail unavailable; nil credential is recorded with empty kind. Nil context/provider panics propagate. Calls/outcomes are serialized, but no general collaborator recovery is provided.

func (*Authenticator) Calls

func (a *Authenticator) Calls() []Call

Calls returns a copy of recorded non-secret call metadata.

type Call

type Call struct {
	// Kind is raw caller routing metadata, not confidentiality-screened.
	Kind authentication.CredentialKind
}

Call records only credential kind, never credential contents. Custom Kind strings remain caller-asserted non-secret fixture data.

func (Call) String

func (c Call) String() string

String formats the raw Kind; do not use confidential custom fixture labels.

type Clock

type Clock struct {
	// contains filtered or unexported fields
}

Clock is a concurrency-safe deterministic clock.

func NewClock

func NewClock(instant time.Time) *Clock

NewClock creates a clock at instant.

func (*Clock) Advance

func (c *Clock) Advance(duration time.Duration) time.Time

Advance moves the clock by duration and returns the new instant.

func (*Clock) Now

func (c *Clock) Now() time.Time

Now returns the current deterministic instant.

type HTTPFixture

type HTTPFixture struct {
	// Request is the caller-owned mutable httptest request; it may retain the supplied body.
	Request *http.Request
	// Recorder is the caller-owned mutable response recorder, not concurrent-safe.
	Recorder *httptest.ResponseRecorder
}

HTTPFixture contains a real request and response recorder.

func NewHTTPFixture

func NewHTTPFixture(method, target string, body io.Reader) HTTPFixture

NewHTTPFixture creates a deterministic real HTTP fixture.

type Outcome

type Outcome struct {
	// Result is the immutable scripted result returned unchanged.
	Result authentication.Result
	// Err is a borrowed scripted error, not automatically sanitized by fixtures.
	Err error
}

Outcome is one scripted authentication result.

type TestingT

type TestingT interface {
	// Helper marks assertion call sites without changing the scripted result.
	Helper()
	// Fatalf reports a trusted-fixture assertion failure; conventional tests stop
	// execution, but construction helpers return zero values if Fatalf returns.
	Fatalf(string, ...any)
}

TestingT is the subset of testing.TB required by fixture assertions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL