Documentation ¶
Overview ¶
Package authtest implements some interfaces used by auth package to simplify unit testing.
Index ¶
- Variables
- func MockAuthConfig(c context.Context) context.Context
- type FakeAuth
- type FakeDB
- func (db FakeDB) CheckMembership(c context.Context, id identity.Identity, groups []string) (out []string, err error)
- func (db FakeDB) GetAuthServiceURL(c context.Context) (string, error)
- func (db FakeDB) GetCertificates(c context.Context, id identity.Identity) (*signing.PublicCertificates, error)
- func (db FakeDB) GetTokenServiceURL(c context.Context) (string, error)
- func (db FakeDB) GetWhitelistForIdentity(c context.Context, ident identity.Identity) (string, error)
- func (db FakeDB) IsAllowedOAuthClientID(c context.Context, email, clientID string) (bool, error)
- func (db FakeDB) IsInWhitelist(c context.Context, ip net.IP, whitelist string) (bool, error)
- func (db FakeDB) IsInternalService(c context.Context, hostname string) (bool, error)
- func (db FakeDB) IsMember(c context.Context, id identity.Identity, groups []string) (bool, error)
- func (db FakeDB) Use(c context.Context) context.Context
- type FakeErroringDB
- type FakeState
- func (s *FakeState) Authenticator() *auth.Authenticator
- func (s *FakeState) DB() authdb.DB
- func (s *FakeState) Method() auth.Method
- func (s *FakeState) PeerIP() net.IP
- func (s *FakeState) PeerIdentity() identity.Identity
- func (s *FakeState) User() *auth.User
- func (s *FakeState) UserCredentials() (*oauth2.Token, error)
- type MemorySessionStore
- func (s *MemorySessionStore) CloseSession(c context.Context, sessionID string) error
- func (s *MemorySessionStore) GetSession(c context.Context, sessionID string) (*auth.Session, error)
- func (s *MemorySessionStore) OpenSession(c context.Context, userID string, u *auth.User, exp time.Time) (string, error)
Constants ¶
This section is empty.
Variables ¶
var ErrAuthenticationError = errors.New("authtest: fake Authenticate error")
ErrAuthenticationError is returned by FakeAuth.Authenticate.
Functions ¶
Types ¶
type FakeAuth ¶
FakeAuth implements auth.Method's Authenticate by returning predefined user.
func (FakeAuth) Authenticate ¶
Authenticate returns predefined User object (if it is not nil) or error.
type FakeDB ¶
FakeDB implements user group checking part of db.DB (IsMember).
It is a mapping "identity -> list of its groups". Intended to be used mostly for testing request handlers, thus all other DB methods are hardcoded to implement some default behavior sufficient for fake requests to pass authentication.
func (FakeDB) CheckMembership ¶
func (db FakeDB) CheckMembership(c context.Context, id identity.Identity, groups []string) (out []string, err error)
CheckMembership is part of authdb.DB interface.
It returns a list of groups the identity belongs to.
func (FakeDB) GetAuthServiceURL ¶
GetAuthServiceURL is part of authdb.DB interface.
func (FakeDB) GetCertificates ¶
func (db FakeDB) GetCertificates(c context.Context, id identity.Identity) (*signing.PublicCertificates, error)
GetCertificates is part of authdb.DB interface.
func (FakeDB) GetTokenServiceURL ¶
GetTokenServiceURL is part of authdb.DB interface.
func (FakeDB) GetWhitelistForIdentity ¶
func (db FakeDB) GetWhitelistForIdentity(c context.Context, ident identity.Identity) (string, error)
GetWhitelistForIdentity is part of authdb.DB interface.
func (FakeDB) IsAllowedOAuthClientID ¶
IsAllowedOAuthClientID is part of authdb.DB interface.
func (FakeDB) IsInWhitelist ¶
IsInWhitelist is part of authdb.DB interface.
func (FakeDB) IsInternalService ¶
IsInternalService is part of authdb.DB interface.
type FakeErroringDB ¶
FakeErroringDB is authdb.DB with IsMember returning an error.
func (*FakeErroringDB) CheckMembership ¶
func (db *FakeErroringDB) CheckMembership(c context.Context, id identity.Identity, groups []string) ([]string, error)
CheckMembership is part of authdb.DB interface.
It returns db.Error if it is not nil.
type FakeState ¶
type FakeState struct { // Identity is main identity associated with the request. // // identity.AnonymousIdentity if not set. Identity identity.Identity // IdentityGroups is list of groups the calling identity belongs to. IdentityGroups []string // Error if not nil is returned by IsMember checks. Error error // FakeDB is a mock authdb.DB implementation to use. // // If not nil, overrides 'IdentityGroups' and 'Error'. FakeDB authdb.DB // PeerIdentityOverride may be set for PeerIdentity() to return custom value. // // By default PeerIdentity() returns Identity (i.e. no delegation is // happening). PeerIdentityOverride identity.Identity // PeerIPOverride may be set for PeerIP() to return custom value. // // By default PeerIP() returns "127.0.0.1". PeerIPOverride net.IP // UserCredentialsOverride may be set to override UserCredentials(). // // By default UserCredentials() returns ErrNoForwardableCreds error. UserCredentialsOverride *oauth2.Token }
FakeState implements auth.State returning predefined values.
Inject it into the context when testing handlers that expect auth state:
ctx = auth.WithState(ctx, &authtest.FakeState{ Identity: "user:user@example.com", IdentityGroups: []string{"admins"}, }) auth.IsMember(ctx, "admins") -> returns true.
func (*FakeState) Authenticator ¶
func (s *FakeState) Authenticator() *auth.Authenticator
Authenticator is part of State interface.
func (*FakeState) PeerIdentity ¶
PeerIdentity is part of State interface.
type MemorySessionStore ¶
type MemorySessionStore struct {
// contains filtered or unexported fields
}
MemorySessionStore implement auth.SessionStore.
func (*MemorySessionStore) CloseSession ¶
func (s *MemorySessionStore) CloseSession(c context.Context, sessionID string) error
CloseSession closes a session given its ID. Does nothing if session is already closed or doesn't exist. Returns only transient errors.
func (*MemorySessionStore) GetSession ¶
GetSession returns existing non-expired session given its ID. Returns nil if session doesn't exist, closed or expired. Returns only transient errors.