Documentation ¶
Overview ¶
Package authtest implements some interfaces used by auth package to simplify unit testing.
Index ¶
- Variables
- func MockAuthConfig(ctx context.Context) context.Context
- type FakeAuth
- type FakeDB
- func (db *FakeDB) AddMocks(mocks ...MockedDatum)
- func (db *FakeDB) CheckMembership(ctx context.Context, id identity.Identity, groups []string) (out []string, err error)
- func (db *FakeDB) GetAuthServiceURL(ctx context.Context) (string, error)
- func (db *FakeDB) GetCertificates(ctx context.Context, id identity.Identity) (*signing.PublicCertificates, error)
- func (db *FakeDB) GetRealmData(ctx context.Context, realm string) (*protocol.RealmData, error)
- func (db *FakeDB) GetTokenServiceURL(ctx context.Context) (string, error)
- func (db *FakeDB) GetWhitelistForIdentity(ctx context.Context, ident identity.Identity) (string, error)
- func (db *FakeDB) HasPermission(ctx context.Context, id identity.Identity, perm realms.Permission, ...) (bool, error)
- func (db *FakeDB) IsAllowedOAuthClientID(ctx context.Context, email, clientID string) (bool, error)
- func (db *FakeDB) IsInWhitelist(ctx context.Context, ip net.IP, whitelist string) (bool, error)
- func (db *FakeDB) IsInternalService(ctx context.Context, hostname string) (bool, error)
- func (db *FakeDB) IsMember(ctx context.Context, id identity.Identity, groups []string) (bool, error)
- func (db *FakeDB) Use(ctx context.Context) context.Context
- 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(ctx context.Context, sessionID string) error
- func (s *MemorySessionStore) GetSession(ctx context.Context, sessionID string) (*auth.Session, error)
- func (s *MemorySessionStore) OpenSession(ctx context.Context, userID string, u *auth.User, exp time.Time) (string, error)
- type MockedDatum
- func MockError(err error) MockedDatum
- func MockIPWhitelist(ip, whitelist string) MockedDatum
- func MockMembership(id identity.Identity, group string) MockedDatum
- func MockPermission(id identity.Identity, realm string, perm realms.Permission) MockedDatum
- func MockRealmData(realm string, data *protocol.RealmData) MockedDatum
- type RealmPermission
Constants ¶
This section is empty.
Variables ¶
var ErrAuthenticationError = errors.New("authtest: fake Authenticate error")
ErrAuthenticationError is returned by FakeAuth.Authenticate.
Functions ¶
func MockAuthConfig ¶
MockAuthConfig configures the auth library for unit tests environment.
You need this *only* if your tests call auth.Authenticate(...) or auth.GetRPCTransport(...). If your tests only check groups or permissions (for example when testing bodies of request handlers), use FakeState instead. See its docs for some examples.
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 ¶
type FakeDB struct {
// contains filtered or unexported fields
}
FakeDB implements authdb.DB by mocking membership and permission checks.
Initialize it with a bunch of mocks like:
db := authtest.NewFakeDB(
authtest.MockMembership("user:a@example.com", "group"), authtest.MockPermission("user:a@example.com", "proj:realm", perm), ...
)
The list of mocks can also be extended later via db.AddMocks(...).
func NewFakeDB ¶
func NewFakeDB(mocks ...MockedDatum) *FakeDB
NewFakeDB creates a FakeDB populated with the given mocks.
Construct mocks using MockMembership, MockPermission, MockIPWhitelist and MockError functions.
func (*FakeDB) AddMocks ¶
func (db *FakeDB) AddMocks(mocks ...MockedDatum)
AddMocks applies a bunch of mocks to the state in the db.
func (*FakeDB) CheckMembership ¶
func (db *FakeDB) CheckMembership(ctx context.Context, id identity.Identity, groups []string) (out []string, err error)
CheckMembership is part of authdb.DB interface.
func (*FakeDB) GetAuthServiceURL ¶
GetAuthServiceURL is part of authdb.DB interface.
func (*FakeDB) GetCertificates ¶
func (db *FakeDB) GetCertificates(ctx context.Context, id identity.Identity) (*signing.PublicCertificates, error)
GetCertificates is part of authdb.DB interface.
func (*FakeDB) GetRealmData ¶
GetRealmData is part of authdb.DB interface.
func (*FakeDB) GetTokenServiceURL ¶
GetTokenServiceURL is part of authdb.DB interface.
func (*FakeDB) GetWhitelistForIdentity ¶
func (db *FakeDB) GetWhitelistForIdentity(ctx context.Context, ident identity.Identity) (string, error)
GetWhitelistForIdentity is part of authdb.DB interface.
func (*FakeDB) HasPermission ¶
func (db *FakeDB) HasPermission(ctx context.Context, id identity.Identity, perm realms.Permission, realm string) (bool, error)
HasPermission 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 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 // IdentityPermissions is a list of (realm, permission) tuples that define // caller's permissions. IdentityPermissions []RealmPermission // PeerIPWhitelists is a list of IP whitelists the caller IP belongs to. PeerIPWhitelists []string // Error, if not nil, is returned by auth DB checks. Error error // FakeDB is an authdb.DB implementation to use. // // If not nil, takes precedence over IdentityGroups, IdentityPermissions, // PeerIPWhitelists 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 }
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(ctx 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 ¶
func (s *MemorySessionStore) GetSession(ctx 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.
type MockedDatum ¶
type MockedDatum struct {
// contains filtered or unexported fields
}
MockedDatum is a return value of various Mock* constructors.
func MockError ¶
func MockError(err error) MockedDatum
MockError modifies db to make its methods return this error.
`err` may be nil, in which case the previously mocked error is removed.
func MockIPWhitelist ¶
func MockIPWhitelist(ip, whitelist string) MockedDatum
MockIPWhitelist modifies db to make IsInWhitelist(ip, whitelist) == true.
Panics if `ip` is not a valid IP address.
func MockMembership ¶
func MockMembership(id identity.Identity, group string) MockedDatum
MockMembership modifies db to make IsMember(id, group) == true.
func MockPermission ¶
func MockPermission(id identity.Identity, realm string, perm realms.Permission) MockedDatum
MockPermission modifies db to make HasPermission(id, realm, perm) == true.
Panics if `realm` is not a valid globally scoped realm, i.e. it doesn't look like "<project>:<realm>".
func MockRealmData ¶
func MockRealmData(realm string, data *protocol.RealmData) MockedDatum
MockRealmData modifies what db's GetRealmData returns.
Panics if `realm` is not a valid globally scoped realm, i.e. it doesn't look like "<project>:<realm>".
type RealmPermission ¶
type RealmPermission struct { Realm string Permission realms.Permission }
RealmPermission is used to populate IdentityPermissions in FakeState.