authtest

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package authtest checks auth.Authenticator implementations against the rules every authenticator shares. Call Run from the authenticator's tests with factories for a request without credentials, one with valid credentials, and, through WithInvalid, one with rejected credentials.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run[U any](t *testing.T, authenticate auth.Authenticator[U], newAnonymous, newValid func(t *testing.T) *http.Request, opts ...Option[U])

Run checks that authenticate follows the auth.Authenticator contract: a request without credentials yields auth.ErrNoCredentials and passes auth.Middleware anonymously, a request with valid credentials yields a user that the middleware stores in the context, and, with WithInvalid, rejected credentials yield auth.ErrUnauthenticated and stop the request.

newAnonymous and newValid are called once per subtest; each request must carry whatever context the authenticator reads, such as a session.

Example

func TestBearer(t *testing.T) {
	authenticate := auth.Bearer(verify)
	authtest.Run(t, authenticate, anonymousRequest, validRequest,
		authtest.WithInvalid[User](forgedRequest))
}

Types

type Option

type Option[U any] func(*suite[U])

Option changes one conformance suite setting.

func WithInvalid

func WithInvalid[U any](newRequest func(t *testing.T) *http.Request) Option[U]

WithInvalid supplies a factory for a request carrying credentials the authenticator rejects, such as an expired or forged token. It enables the check that rejected credentials yield auth.ErrUnauthenticated. Leave it out for authenticators without a notion of invalid credentials, such as auth.SessionIdentity.Authenticator.

func WithUser

func WithUser[U any](check func(t *testing.T, user U)) Option[U]

WithUser supplies a check run on the user resolved from the valid request, for example comparing its identifier.

Jump to

Keyboard shortcuts

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