authtest

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2026 License: Apache-2.0 Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

authtest

Package authtest checks in tests that an application's routes refuse the principals their needs exclude: Routes calls every route of an authhttp.Mux as an anonymous caller, a session in recovery mode, a stale session, and an agent with no scopes. PublicRoutes lists the Public routes to review.

The package documentation is in authtest.go. To read it with the API, run go doc ./authtest.

Contents

Name What it holds
authtest.go Routes, PublicRoutes, and User; the package documentation, with what Routes can't catch

See also

Documentation

Overview

Package authtest checks that an application's routes refuse the principals their needs exclude.

Routes calls every route of an authhttp.Mux as four principals that most routes should refuse, and fails the test when one gets a 2xx it shouldn't:

  • an anonymous caller
  • a human session in recovery mode, signed in with a recovery code alone
  • a human session that signed in a day ago
  • an agent token with no scopes

A principal "shouldn't" get a 2xx when the route's need, tightened by auth.WithNeeds, excludes it. Run it in CI, so a route that forgets its rule fails before it ships.

Routes checks only that each route enforces the need it declares. It can't tell whether that need is right, so it doesn't catch:

  • a need that is too loose, for example a route that should need a human but declares auth.Need{}, or one missing a scope
  • a Public route that should have a need; Routes skips Public routes
  • a handler that checks the wrong thing itself, such as another user's resource, or an agent where only a person should act

Your application's own route tests catch those: call each sensitive route as the principal it must refuse, such as an agent or another user, and expect the refusal. To review Public routes, compare PublicRoutes with the list you expect in a test, so adding one fails until you add it to that list.

Index

Constants

View Source
const User auth.UserID = "authtest-user"

User is the account whose sessions Routes creates.

Variables

This section is empty.

Functions

func PublicRoutes

func PublicRoutes(m *authhttp.Mux) []string

PublicRoutes returns the pattern of every authhttp.Public route of m, in the order they were registered. Routes skips these, so compare the list with the one you expect in a test, and review each route you add.

func Routes

func Routes(tb testing.TB, m *authhttp.Mux)

Routes calls every non-public route of m as each principal in the package documentation, and reports every 2xx that the route's need excludes.

Types

This section is empty.

Jump to

Keyboard shortcuts

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