sighttest

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: Apache-2.0, MIT Imports: 22 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 sighttest drives admin panels in tests: a header-based authenticator, a fixed subject table for switching roles, an HTTP client that handles CSRF, and a Live client for index and form events over the SSE+POST transport admin panels serve.

Index

Constants

View Source
const UserHeader = "X-Admintest-User"

UserHeader carries the user ID that Auth authenticates.

Variables

This section is empty.

Functions

func Auth

func Auth() auth.Authenticator

Auth returns an authenticator that trusts UserHeader. Use it in tests only.

func StateStoreSuite

func StateStoreSuite(t *testing.T, newStore func(t *testing.T) sight.StateStore)

StateStoreSuite checks a StateStore against the contract admin relies on. newStore returns an empty store for each subtest.

func Subjects

func Subjects(m map[string]rbac.Subject) func(ctx context.Context, userID string) (rbac.Subject, error)

Subjects resolves user IDs through a fixed table, for sight.WithSubject. Unknown users get a subject with no roles.

Types

type Client

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

Client is an HTTP client for one test server with a cookie jar.

func New

func New(tb testing.TB, h http.Handler) *Client

New serves h for the length of the test.

func (*Client) As

func (c *Client) As(user string) *Client

As returns a client that sends requests as user, sharing the cookie jar.

func (*Client) CSRFToken

func (c *Client) CSRFToken(path string) string

CSRFToken returns the token from the CSRF cookie the panel set for path, or "".

func (*Client) Get

func (c *Client) Get(path string) *Response

Get requests path.

func (*Client) Live

func (c *Client) Live(path string) *Live

Live opens the Live page at path and joins its session. It fails the test unless the page answers 200 with a Live root.

func (*Client) Post

func (c *Client) Post(path string, form url.Values) *Response

Post submits form to path with the CSRF token, fetching a page first when the jar has no CSRF cookie yet.

func (*Client) PostRaw

func (c *Client) PostRaw(path string, form url.Values) *Response

PostRaw submits form as is, without adding a CSRF token.

func (*Client) URL

func (c *Client) URL() string

URL is the server's base URL.

type Frame

type Frame struct {
	Raw      string
	Err      map[string]string
	Redirect string
	Events   map[string]json.RawMessage
}

Frame is the server's answer to one event.

type LeakOption

type LeakOption func(*leakConfig)

LeakOption configures LeakScan.

func MaxPages

func MaxPages(n int) LeakOption

MaxPages caps the pages crawled per user. Default 400.

func Probe

func Probe(paths ...string) LeakOption

Probe adds paths every user is sent to directly, such as another tenant's records or exports. A 403 or 404 is fine; a sentinel in the answer is not.

func Searches

func Searches(terms ...string) LeakOption

Searches adds terms typed into every index search and the Cmd+K palette. LeakScan always searches the first half of each sentinel, so a match shows the full value only if the row holding it was found.

type LeakReport

type LeakReport struct {
	Pages, LiveViews, Searches, Exports, Relations, Probes int
}

LeakReport counts what one scan exercised, so a test can check the scan reached the screens it means to cover.

func LeakScan

func LeakScan(t testing.TB, c *Client, prefix string, users []string, sentinels []string, opts ...LeakOption) LeakReport

LeakScan proves that what users must not see never reaches them. It signs in as each user and crawls every page of the panel under prefix reachable from its home: it follows links (record pages and CSV exports included), joins every Live view over the SSE+POST transport, lists 100 rows per index page and follows those rows too, opens every relation tab, searches every index and the Cmd+K palette, and sends the probe paths. It fails t when any sentinel appears in a page, a Live frame, a CSV, a JSON answer, or a toast, in plain, HTML-escaped, or JSON-escaped form.

type Live

type Live struct {

	// HTML is the dead render.
	HTML string
	// Frames holds every frame received, in order.
	Frames []string
	// contains filtered or unexported fields
}

Live is a Live page joined the way the browser runtime joins an admin panel: an SSE stream for the server's frames and a POST, carrying the page's CSRF token, for each event.

func (*Live) AwaitEvent

func (l *Live) AwaitEvent(name string, d time.Duration) (payload json.RawMessage, ok bool)

AwaitEvent returns the payload of the next pushed event called name that the test has not awaited yet, such as the toast of a queued run that finished on a worker, waiting up to d for it. ok is false on timeout.

func (*Live) Click

func (l *Live) Click(handler string) Frame

Click clicks the element whose data-f-click reference is handler.

func (*Live) HandlerRef

func (l *Live) HandlerRef(event string, n int) string

HandlerRef returns the n-th (from 0) data-f-<event> handler reference in the page's first render, such as HandlerRef("submit", 0). It fails the test when there is none.

func (*Live) Push

func (l *Live) Push(name string, payload any) Frame

Push sends a named event to the view's HandleEvent and returns the server's answer: its diff or error frame, a following redirect, and the events the view pushed.

func (*Live) Put

func (l *Live) Put(path, contentType string, body []byte) *Response

Put sends body to path as the page would: with its CSRF token and the user, the way the upload component sends a direct upload.

func (*Live) Submit

func (l *Live) Submit(handler string, form map[string]string) Frame

Submit submits the form whose data-f-submit reference is handler, as the page would, with form as its fields, and returns the answer like Push. Find handler in HTML with HandlerRef.

type Response

type Response struct {
	Status int
	Header http.Header
	Body   string
}

Response is a finished request.

Jump to

Keyboard shortcuts

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