testkit

package
v0.103.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 28 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package testkit has the helpers shared by the service tests: an isolated Mongo database, an Elasticsearch recorder, token signing and an HTTP client for handlers.

Tests that need Mongo are skipped unless MONGO_TEST_URI is set, e.g. mongodb://localhost:27018/?directConnection=true (make env-up).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisabledAuth

func DisabledAuth(t testing.TB) *auth.Authenticator

DisabledAuth returns an authenticator that accepts every request.

func GuardedMongo

func GuardedMongo(t testing.TB, db *mongo.Database) (*mongo.Database, *mongodb.WriteGuard)

GuardedMongo opens db again on a client of its own with a mongodb.WriteGuard, as app.Run does in dry run.

func Mongo

func Mongo(t testing.TB) *mongo.Database

Mongo returns an empty database for the test, dropped at cleanup.

func MongoClient

func MongoClient(t testing.TB) *mongo.Client

MongoClient returns a client shared by the tests of the package.

func Store

func Store(t testing.TB, collection string) rest.Store

Store wraps the named collection of Mongo(t) for rest.Resource.Store.

Types

type Client

type Client struct {

	// Header is sent with every request (e.g. Authorization).
	Header http.Header
	// contains filtered or unexported fields
}

Client calls a handler in process.

func DryRun

func DryRun(t testing.TB, spec app.Spec, cfg config.Config, authn *auth.Authenticator, db *mongo.Database) *Client

DryRun builds the service again on db, the database of Service, in dry run: the tests prepare the data with the real service and repeat the writes on this one. Background tasks registered with Env.Go and writes outside a transaction (mongodb.WriteGuard, as in app.Run) fail the test.

func NewClient

func NewClient(t testing.TB, handler http.Handler) *Client

NewClient builds a client for handler.

func Service

func Service(t testing.TB, spec app.Spec, cfg config.Config, authn *auth.Authenticator) (*Client, *mongo.Database)

Service builds a whole service (app.Build) on an isolated database and returns a client for it.

func (*Client) Anonymous

func (c *Client) Anonymous() *Client

Anonymous returns a client for the same handler without the headers.

func (*Client) Do

func (c *Client) Do(method, path string, body any, headers ...string) *Response

Do sends a request; body may be a string (sent as is) or a value encoded as JSON. headers are name/value pairs.

type DB

type DB struct {
	*mongo.Database
}

DB adds assertions to a test database.

func (*DB) HasIndex

func (db *DB) HasIndex(t testing.TB, collection, field string) bool

HasIndex reports whether collection has an index whose first key is field.

type ESCall

type ESCall struct {
	Method string
	Path   string
	Query  string
	Body   map[string]any
}

ESCall is a request received by ESRecorder.

type ESRecorder

type ESRecorder struct {
	*httptest.Server

	// Fail makes every request answer 500.
	Fail bool
	// contains filtered or unexported fields
}

ESRecorder is a fake Elasticsearch that records the writes.

func NewES

func NewES(t testing.TB) *ESRecorder

NewES starts the recorder.

func (*ESRecorder) Calls

func (es *ESRecorder) Calls() []ESCall

Calls returns the recorded writes.

func (*ESRecorder) Hosts

func (es *ESRecorder) Hosts() []any

Hosts is the value for elasticsearch.hosts.

func (*ESRecorder) Reset

func (es *ESRecorder) Reset()

Reset forgets the recorded writes.

type Keys

type Keys struct {

	// PublicKeyFile is the PEM file to configure auth.Config.PublicKeyFile.
	PublicKeyFile string
	// contains filtered or unexported fields
}

Keys signs legacy auth-api tokens for tests.

func NewKeys

func NewKeys(t testing.TB) *Keys

NewKeys generates a key pair.

func (*Keys) Authenticator

func (k *Keys) Authenticator(t testing.TB) *auth.Authenticator

Authenticator returns an enabled authenticator that accepts Token.

func (*Keys) Token

func (k *Keys) Token(t testing.TB, user string) string

Token signs a token for user valid for one hour.

func (*Keys) TokenWith

func (k *Keys) TokenWith(t testing.TB, claims map[string]any) string

TokenWith signs a token with claims (plus exp), e.g. preferred_username as in a real Azure AD token.

type Response

type Response struct {
	Status int
	Header http.Header
	Raw    string
	// JSON is the decoded body: map[string]any, []any or nil.
	JSON any
}

Response is a decoded HTTP response.

func (*Response) Map

func (r *Response) Map() map[string]any

Map returns the body as an object.

func (*Response) String

func (r *Response) String(field string) string

String returns the top level field as a string.

Jump to

Keyboard shortcuts

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