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 ¶
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.