gas/auth
auth/authtest
import "github.com/gasmod/gas/auth/authtest"Package authtest holds mocks of the interfaces in package auth, for tests of code that authenticates, authorizes or revokes: MockAuthenticator, MockAuthorizer and MockRevoker.
A mock records each call, and returns what the function in the field named after the method returns:
m := &authtest.MockAuthenticator{ AuthenticateFn: func(context.Context, *http.Request) (gas.Principal, error) { return auth.NewPrincipal("user-1", auth.SchemeJWT, "token-1", nil), nil },}handler := auth.Middleware(m)(next)// m.CallCount("Authenticate") is how many requests handler servedDefaults
Section titled “Defaults”With the field nil, a mock fails closed, so a test that forgets to set it never lets a request through by accident:
- MockAuthenticator.Authenticate returns nil and auth.ErrNoCredentials.
- MockAuthorizer.Authorize returns auth.ErrForbidden.
- MockRevoker’s methods return nil, as revoking nothing succeeds.
Each method appends a Call with its name and arguments, the context included, before it calls its field. Every mock reads them back with:
- Calls, which returns a copy of them in the order they were made.
- CallCount, which counts those of one method.
- Reset, which forgets them all.
The methods may be called from several goroutines at once, as the calls are kept under a lock, which Calls, CallCount and Reset take too. Setting a field while a method runs is a data race.
API reference
Section titled “API reference”type Call
Section titled “type Call”type Call struct { // Method is the name of the method called. Method string // Args are the method's arguments, in order. Args []any}Call is one call of a mock’s method.
type MockAuthenticator
Section titled “type MockAuthenticator”type MockAuthenticator struct { // AuthenticateFn is what Authenticate returns. When it is nil, // Authenticate returns auth.ErrNoCredentials. AuthenticateFn func(ctx context.Context, r *http.Request) (gas.Principal, error) // contains filtered or unexported fields}MockAuthenticator is an auth.Authenticator that records its calls, as described in the package documentation.
func (*MockAuthenticator) Authenticate
Section titled “func (*MockAuthenticator) Authenticate”func (m *MockAuthenticator) Authenticate( ctx context.Context, r *http.Request,) (gas.Principal, error)Authenticate records the call and returns what AuthenticateFn returns, or nil and auth.ErrNoCredentials when it is nil.
func (*MockAuthenticator) CallCount
Section titled “func (*MockAuthenticator) CallCount”func (m *MockAuthenticator) CallCount(method string) intCallCount returns how many times the named method was called.
func (*MockAuthenticator) Calls
Section titled “func (*MockAuthenticator) Calls”func (m *MockAuthenticator) Calls() []CallCalls returns a copy of the recorded calls, in the order they were made.
func (*MockAuthenticator) Reset
Section titled “func (*MockAuthenticator) Reset”func (m *MockAuthenticator) Reset()Reset forgets the recorded calls.
type MockAuthorizer
Section titled “type MockAuthorizer”type MockAuthorizer struct { // AuthorizeFn is what Authorize returns. When it is nil, Authorize // returns auth.ErrForbidden. AuthorizeFn func(ctx context.Context, principal gas.Principal, action, resource string) error // contains filtered or unexported fields}MockAuthorizer is an auth.Authorizer that records its calls, as described in the package documentation.
func (*MockAuthorizer) Authorize
Section titled “func (*MockAuthorizer) Authorize”func (m *MockAuthorizer) Authorize( ctx context.Context, principal gas.Principal, action, resource string,) errorAuthorize records the call and returns what AuthorizeFn returns, or auth.ErrForbidden when it is nil.
func (*MockAuthorizer) CallCount
Section titled “func (*MockAuthorizer) CallCount”func (m *MockAuthorizer) CallCount(method string) intCallCount returns how many times the named method was called.
func (*MockAuthorizer) Calls
Section titled “func (*MockAuthorizer) Calls”func (m *MockAuthorizer) Calls() []CallCalls returns a copy of the recorded calls, in the order they were made.
func (*MockAuthorizer) Reset
Section titled “func (*MockAuthorizer) Reset”func (m *MockAuthorizer) Reset()Reset forgets the recorded calls.
type MockRevoker
Section titled “type MockRevoker”type MockRevoker struct { // RevokeFn is what Revoke returns. When it is nil, Revoke returns nil. RevokeFn func(ctx context.Context, principal gas.Principal) error // RevokeAllFn is what RevokeAll returns. When it is nil, RevokeAll // returns nil. RevokeAllFn func(ctx context.Context, subject string) error // RevokeAllBySchemeFn is what RevokeAllByScheme returns. When it is // nil, RevokeAllByScheme returns nil. RevokeAllBySchemeFn func(ctx context.Context, subject, scheme string) error // contains filtered or unexported fields}MockRevoker is an auth.PrincipalRevoker that records its calls, as described in the package documentation.
func (*MockRevoker) CallCount
Section titled “func (*MockRevoker) CallCount”func (m *MockRevoker) CallCount(method string) intCallCount returns how many times the named method was called.
func (*MockRevoker) Calls
Section titled “func (*MockRevoker) Calls”func (m *MockRevoker) Calls() []CallCalls returns a copy of the recorded calls, in the order they were made.
func (*MockRevoker) Reset
Section titled “func (*MockRevoker) Reset”func (m *MockRevoker) Reset()Reset forgets the recorded calls.
func (*MockRevoker) Revoke
Section titled “func (*MockRevoker) Revoke”func (m *MockRevoker) Revoke(ctx context.Context, principal gas.Principal) errorRevoke records the call and returns what RevokeFn returns, or nil when it is nil.
func (*MockRevoker) RevokeAll
Section titled “func (*MockRevoker) RevokeAll”func (m *MockRevoker) RevokeAll(ctx context.Context, subject string) errorRevokeAll records the call and returns what RevokeAllFn returns, or nil when it is nil.
func (*MockRevoker) RevokeAllByScheme
Section titled “func (*MockRevoker) RevokeAllByScheme”func (m *MockRevoker) RevokeAllByScheme(ctx context.Context, subject, scheme string) errorRevokeAllByScheme records the call and returns what RevokeAllBySchemeFn returns, or nil when it is nil.