import "go.chromium.org/luci/server/auth/authtest"
Package authtest implements some interfaces used by auth package to simplify unit testing.
config.go db.go doc.go method.go session.go state.go
ErrAuthenticationError is returned by FakeAuth.Authenticate.
MockAuthConfig configures auth library for unit tests environment.
If modifies the configure stored in the context. See auth.Initialize for more info.
FakeAuth implements auth.Method's Authenticate by returning predefined user.
Authenticate returns predefined User object (if it is not nil) or error.
LoginURL returns fake login URL.
LogoutURL returns fake logout URL.
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 (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.
GetAuthServiceURL is part of authdb.DB interface.
func (db FakeDB) GetCertificates(c context.Context, id identity.Identity) (*signing.PublicCertificates, error)
GetCertificates is part of authdb.DB interface.
GetTokenServiceURL is part of authdb.DB interface.
func (db FakeDB) GetWhitelistForIdentity(c context.Context, ident identity.Identity) (string, error)
GetWhitelistForIdentity is part of authdb.DB interface.
IsAllowedOAuthClientID is part of authdb.DB interface.
IsInWhitelist is part of authdb.DB interface.
IsInternalService is part of authdb.DB interface.
IsMember is part of authdb.DB interface.
It returns true if any of 'groups' is listed in db[id].
Use installs the fake db into the context.
FakeErroringDB is authdb.DB with IsMember returning an error.
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.
func (db *FakeErroringDB) IsMember(c context.Context, id identity.Identity, groups []string) (bool, error)
IsMember is part of authdb.DB interface.
It returns db.Error if it is not nil.
Use installs the fake db into the context.
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 (s *FakeState) Authenticator() *auth.Authenticator
Authenticator is part of State interface.
DB is part of State interface.
Method is part of State interface.
PeerIP is part of State interface.
PeerIdentity is part of State interface.
User is part of State interface.
UserCredentials is part of State interface.
type MemorySessionStore struct {
// contains filtered or unexported fields
}
MemorySessionStore implement auth.SessionStore.
CloseSession closes a session given its ID. Does nothing if session is already closed or doesn't exist. Returns only transient errors.
func (s *MemorySessionStore) GetSession(c context.Context, sessionID string) (*auth.Session, error)
GetSession returns existing non-expired session given its ID. Returns nil if session doesn't exist, closed or expired. Returns only transient errors.
func (s *MemorySessionStore) OpenSession(c context.Context, userID string, u *auth.User, exp time.Time) (string, error)
OpenSession create a new session for a user with given expiration time. It returns unique session ID.
Package authtest imports 15 packages (graph) and is imported by 5 packages. Updated 2019-11-22. Refresh now. Tools for package owners.