## Search

Page index: https://gasmod.dev/llms.txt. Add index.md to any page URL for its Markdown.

This site has a search endpoint: https://context7.com/api/v2/ask?siteKey=ask_b845b566cb63bd033e9c576d&query=<URL-encoded question>. It returns documentation that answers the question, with a source link for each part. No API key is needed. If nothing matches, it says so.

# 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.

The Markdown of https://gasmod.dev/reference/auth/authtest/ from the Gas documentation, whose index for LLMs is https://gasmod.dev/llms.txt

```go
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](#MockAuthenticator), [MockAuthorizer](#MockAuthorizer) and [MockRevoker](#MockRevoker).

## Usage

A mock records each call, and returns what the function in the field
named after the method returns:

```go
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
```

## 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.

## Calls

Each method appends a [Call](#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

<a id="Call"></a>

### type Call

```go
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.

<a id="MockAuthenticator"></a>

### type MockAuthenticator

```go
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.

<a id="MockAuthenticator.Authenticate"></a>

#### func (*MockAuthenticator) Authenticate

```go
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.

<a id="MockAuthenticator.CallCount"></a>

#### func (*MockAuthenticator) CallCount

```go
func (m *MockAuthenticator) CallCount(method string) int
```

CallCount returns how many times the named method was called.

<a id="MockAuthenticator.Calls"></a>

#### func (*MockAuthenticator) Calls

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

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

<a id="MockAuthenticator.Reset"></a>

#### func (*MockAuthenticator) Reset

```go
func (m *MockAuthenticator) Reset()
```

Reset forgets the recorded calls.

<a id="MockAuthorizer"></a>

### type MockAuthorizer

```go
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.

<a id="MockAuthorizer.Authorize"></a>

#### func (*MockAuthorizer) Authorize

```go
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.

<a id="MockAuthorizer.CallCount"></a>

#### func (*MockAuthorizer) CallCount

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

CallCount returns how many times the named method was called.

<a id="MockAuthorizer.Calls"></a>

#### func (*MockAuthorizer) Calls

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

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

<a id="MockAuthorizer.Reset"></a>

#### func (*MockAuthorizer) Reset

```go
func (m *MockAuthorizer) Reset()
```

Reset forgets the recorded calls.

<a id="MockRevoker"></a>

### type MockRevoker

```go
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.

<a id="MockRevoker.CallCount"></a>

#### func (*MockRevoker) CallCount

```go
func (m *MockRevoker) CallCount(method string) int
```

CallCount returns how many times the named method was called.

<a id="MockRevoker.Calls"></a>

#### func (*MockRevoker) Calls

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

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

<a id="MockRevoker.Reset"></a>

#### func (*MockRevoker) Reset

```go
func (m *MockRevoker) Reset()
```

Reset forgets the recorded calls.

<a id="MockRevoker.Revoke"></a>

#### func (*MockRevoker) Revoke

```go
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.

<a id="MockRevoker.RevokeAll"></a>

#### func (*MockRevoker) RevokeAll

```go
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.

<a id="MockRevoker.RevokeAllByScheme"></a>

#### func (*MockRevoker) RevokeAllByScheme

```go
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.
