Documentation
¶
Overview ¶
Package authtest tests code protected by the auth package without real tokens, keys or network: the Go side of Powertools Python's mock_claims.
A Verifier answers every token with the same claims. Everything around the verification still runs: Require still extracts the bearer token and checks the scopes, the Authorize functions still build their policies.
verifier := authtest.NewVerifier(map[string]any{"sub": "user-1", "scope": "orders:read"})
protect, _ := auth.Require(verifier, auth.WithScopes("orders:read"))
It bypasses every security check, so use it in tests only, and keep separate tests for the token profiles the application accepts.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Verifier ¶
type Verifier struct {
// contains filtered or unexported fields
}
Verifier is an auth.Verifier that trusts every token.
func NewVerifier ¶
NewVerifier returns a Verifier whose Verify returns claims, a map or a struct that marshals to a JSON object. It panics otherwise, as befits a test helper given a constant.
Example ¶
package main
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"github.com/alessandrolattao/powertools-lambda-go/auth"
"github.com/alessandrolattao/powertools-lambda-go/auth/authtest"
)
func main() {
verifier := authtest.NewVerifier(map[string]any{"sub": "test-user", "scope": "orders:read"})
protect, err := auth.Require(verifier, auth.WithScopes("orders:read"))
if err != nil {
panic(err)
}
handler := protect(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
claims, _ := auth.ClaimsFromContext(r.Context())
w.Header().Set("X-Subject", claims.Subject())
}))
r := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/orders", http.NoBody)
r.Header.Set("Authorization", "Bearer any-token")
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
fmt.Println(w.Code, w.Header().Get("X-Subject"))
}
Output: 200 test-user