mock

package
v0.0.0-...-d430ac9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package mock contains basic mocks and test doubles.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock struct {
	// contains filtered or unexported fields
}

Clock is a deterministic clock for unit tests. It starts at the Unix epoch and only advances when Clock.StepBy() is called.

func NewClock

func NewClock() *Clock

NewClock starts a new Clock at the Unix epoch.

func (*Clock) AddListener

func (c *Clock) AddListener(callback func(time.Time))

AddListener registers a callback that will be called whenever the clock is advanced. It will also be called once immediately.

func (*Clock) Now

func (c *Clock) Now() time.Time

Now reads the clock. This function can be used as a test double for time.Now().

func (*Clock) StepBy

func (c *Clock) StepBy(d time.Duration)

StepBy advances the clock by the given duration.

type Enforcer

type Enforcer struct {
	// contains filtered or unexported fields
}

Enforcer implements the gopherpolicy.Enforcer interface. During enforcement, all accesses are allowed by default. More restrictive policies can be configured with Forbid() and Allow(). Request attributes cannot be checked.

func NewEnforcer

func NewEnforcer() *Enforcer

NewEnforcer initializes an Enforcer instance.

func (*Enforcer) Allow

func (e *Enforcer) Allow(rule string)

Allow reverses a previous Forbid call and allows the given policy rule.

func (*Enforcer) Enforce

func (e *Enforcer) Enforce(rule string, ctx policy.Context) bool

Enforce implements the gopherpolicy.Enforcer interface.

func (*Enforcer) Forbid

func (e *Enforcer) Forbid(rule string)

Forbid will cause all subsequent calls to Enforce() to return false when called for this rule.

type Validator

type Validator[E gopherpolicy.Enforcer] struct {
	Enforcer E
	Auth     map[string]string
}

Validator implements the gopherpolicy.Validator and gopherpolicy.Enforcer interfaces.

During validation, the X-Auth-Token header on the request is not inspected at all. Instead, auth success is always assumed and a token is built from the Auth parameters provided during New(), using the mock itself as Enforcer.

During enforcement, all accesses are allowed by default. More restrictive policies can be configured with Forbid() and Allow().

func NewValidator

func NewValidator[E gopherpolicy.Enforcer](enforcer E, auth map[string]string) *Validator[E]

NewValidator initializes a new Validator. The provided auth variables will be mirrored into all gopherpolicy.Token instances returned by this Validator.

func (*Validator[E]) CheckToken

func (v *Validator[E]) CheckToken(r *http.Request) *gopherpolicy.Token

CheckToken implements the gopherpolicy.Validator interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL