apptest

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: AGPL-3.0 Imports: 30 Imported by: 0

Documentation

Overview

Package apptest runs the whole HTTP stack against a fresh test database so API behaviour can be tested end to end: middleware, authz, handlers, SQL.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MidMonth

func MidMonth() time.Time

MidMonth is a moment in the past when the sample's design days (the 1st to the 15th of a month, "today" the 15th) fit in one calendar month: the 20th of last month at noon in New York. A test whose numbers count "this month" (the SMS spend) seeds there with SeedDemoAt, so it holds on any day of the month.

Types

type Client

type Client struct {

	// Bearer is sent as Authorization when set (API keys, agents).
	Bearer string
	// Header is added to every request (Accept-Language…).
	Header http.Header
	// contains filtered or unexported fields
}

Client is a browser-like client with its own cookie jar.

func (*Client) Do

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

Do sends a request with a JSON body (nil for none) to path under /api/v1.

func (*Client) DoRaw

func (c *Client) DoRaw(method, path, contentType string, body io.Reader) *Response

DoRaw sends a request with a raw body and content type (multipart uploads) to path under /api/v1, or to a non-API path as is ("/media/…").

func (*Client) SignIn

func (c *Client) SignIn(email, password string)

SignIn signs a client in with a password (no second factor).

func (*Client) SignInDemo

func (c *Client) SignInDemo(email string)

SignInDemo signs in as a demo user. Demo users with an authenticator get a session directly (tests don't type TOTP codes).

func (*Client) Stream

func (c *Client) Stream(ctx context.Context, path string) (*http.Response, error)

Stream opens a server-sent events stream at path under /api/v1 with the client's session. The caller reads the body and closes it; the stream stays open until ctx ends.

type Env

type Env struct {
	T      testing.TB
	App    *app.App
	Server *httptest.Server
}

Env is a running test instance.

func New

func New(t testing.TB) *Env

New starts the API on a fresh database.

func (*Env) AddMember

func (e *Env) AddMember(ws store.Workspace, user uuid.UUID, roleKey string)

AddMember adds user to ws with a built-in role key.

func (*Env) Client

func (e *Env) Client() *Client

Client returns a new client with an empty cookie jar.

func (*Env) CreateUser

func (e *Env) CreateUser(o UserOpts) store.User

CreateUser inserts a user directly.

func (*Env) CreateWorkspace

func (e *Env) CreateWorkspace(name string, owner uuid.UUID) store.Workspace

CreateWorkspace creates a workspace owned by owner.

func (*Env) SeedDemo

func (e *Env) SeedDemo()

SeedDemo loads the Acme demo data into the test database.

func (*Env) SeedDemoAt

func (e *Env) SeedDemoAt(at time.Time)

SeedDemoAt loads the Acme demo as seeded at a given moment and runs the app's clock (App.Now) from there, so the services that read it see the sample's days where they were seeded.

func (*Env) SeedNorthwind

func (e *Env) SeedNorthwind()

SeedNorthwind loads the agency sample (Northwind Studio and its seven clients) into the test database instead of the Acme demo.

type Response

type Response struct {
	Status int
	Header http.Header
	Body   []byte
}

Response is a decoded API response.

func (*Response) JSON

func (r *Response) JSON(t testing.TB, v any)

JSON decodes the body into v.

func (*Response) MustStatus

func (r *Response) MustStatus(t testing.TB, want int) *Response

MustStatus fails the test unless the response has the given status.

func (*Response) Problem

func (r *Response) Problem(t testing.TB) map[string]any

Problem decodes an error body.

type SMTPSink

type SMTPSink struct {
	Addr string
	// Reject makes AUTH fail with 535.
	Reject bool
	// contains filtered or unexported fields
}

SMTPSink is a minimal SMTP server for tests: it accepts EHLO, AUTH PLAIN or LOGIN (any credentials unless Reject is set), MAIL, RCPT and DATA, and keeps every message it received and every password it was given.

func NewSMTPSink

func NewSMTPSink(t testing.TB) *SMTPSink

NewSMTPSink starts a sink on a free local port; it stops with the test.

func (*SMTPSink) Messages

func (s *SMTPSink) Messages() []SinkMessage

Messages returns what the sink received so far.

func (*SMTPSink) Passwords

func (s *SMTPSink) Passwords() []string

Passwords returns the passwords clients logged in with so far, in order.

type SinkMessage

type SinkMessage struct {
	From string
	To   []string
	Data string
}

SinkMessage is one message the sink received.

type UserOpts

type UserOpts struct {
	Email, Name, Password string
	InstanceAdmin         bool
	MustChange            bool
}

UserOpts describes a test user.

Jump to

Keyboard shortcuts

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