Skip to content
Gasv0.10.0

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 served

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.

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 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 (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 (m *MockAuthenticator) CallCount(method string) int

CallCount returns how many times the named method was called.

func (m *MockAuthenticator) Calls() []Call

Calls returns a copy of the recorded calls, in the order they were made.

func (m *MockAuthenticator) Reset()

Reset forgets the recorded calls.

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 (m *MockAuthorizer) Authorize(
ctx context.Context,
principal gas.Principal,
action, resource string,
) error

Authorize records the call and returns what AuthorizeFn returns, or auth.ErrForbidden when it is nil.

func (m *MockAuthorizer) CallCount(method string) int

CallCount returns how many times the named method was called.

func (m *MockAuthorizer) Calls() []Call

Calls returns a copy of the recorded calls, in the order they were made.

func (m *MockAuthorizer) Reset()

Reset forgets the recorded calls.

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 (m *MockRevoker) CallCount(method string) int

CallCount returns how many times the named method was called.

func (m *MockRevoker) Calls() []Call

Calls returns a copy of the recorded calls, in the order they were made.

func (m *MockRevoker) Reset()

Reset forgets the recorded calls.

func (m *MockRevoker) Revoke(ctx context.Context, principal gas.Principal) error

Revoke records the call and returns what RevokeFn returns, or nil when it is nil.

func (m *MockRevoker) RevokeAll(ctx context.Context, subject string) error

RevokeAll records the call and returns what RevokeAllFn returns, or nil when it is nil.

func (m *MockRevoker) RevokeAllByScheme(ctx context.Context, subject, scheme string) error

RevokeAllByScheme records the call and returns what RevokeAllBySchemeFn returns, or nil when it is nil.