Help us shape the future of Go! Take the 2026 Go Developer Survey

testutil

package
v0.44.2 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: MIT Imports: 33 Imported by: 0

Documentation

Overview

Package testutil provides shared helpers for unit + integration tests across the filex codebase.

All helpers are designed for *testing.T-driven setups: they call t.Helper(), t.Cleanup() (instead of returning a manual close fn) and t.Fatalf on failure. They also gate themselves on testing.Short() where network or heavy disk IO is involved, so `go test -short ./...` stays fast.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoginAs

func LoginAs(t *testing.T, srv *httptest.Server, client *http.Client, email, password string) string

LoginAs posts to /api/auth/login and returns the freshly-issued cookie value. The cookie is also stored on the supplied client's jar.

func MustEmbedFS

func MustEmbedFS() embed.FS

MustEmbedFS is a tiny convenience that returns a fresh embed.FS with no files — handy for code that requires an embed.FS but doesn't care about its contents.

func NewAPIToken

func NewAPIToken(t *testing.T, store db.Store, userID int64, scopes string) string

NewAPIToken mints an API token for a user and returns the plaintext secret.

It lives here because three protocol suites need the same thing: a token to authenticate with, with a chosen scope string. Each of them writing its own would mean three places that could drift from how the product actually hashes a token.

func NewTestDB

func NewTestDB(t *testing.T) (*sql.DB, db.Store)

NewTestDB opens an in-memory SQLite DB, runs all migrations, and returns the underlying *sql.DB plus a typed Store. Cleanup is registered via t.Cleanup so tests don't have to remember to close.

func NewTestServer

func NewTestServer(t *testing.T) (*httptest.Server, *http.Client, db.Store)

NewTestServer wires a fully working HTTP server backed by an in-memory SQLite DB plus a tmp-dir local storage. The returned httptest.Server is stopped via t.Cleanup; callers receive a cookie jar pre-installed on the returned http.Client.

LocalAuth is wired so /api/auth/login works.

func NewTestServerCfg

func NewTestServerCfg(t *testing.T, mutate func(*config.Config)) (*httptest.Server, *http.Client, db.Store)

NewTestServerCfg is NewTestServer with a config hook applied before the router is built — for exercising config-dependent behavior (e.g. FILEX_COOKIE_DOMAIN stamping a Domain on the session cookie).

func NewTestServerWith

func NewTestServerWith(t *testing.T, cfgMutate func(*config.Config), depsMutate func(*api.Deps)) (*httptest.Server, *http.Client, db.Store)

NewTestServerWith is NewTestServerCfg plus a hook to mutate api.Deps before the router is built — e.g. to inject an OIDC driver so the full router + middleware chain can exercise /api/auth/oidc/callback end to end.

func ReadJSON

func ReadJSON(t *testing.T, resp *http.Response, out any)

ReadJSON decodes resp.Body into out. Calls t.Fatal on decode failure.

func SeedAdmin

func SeedAdmin(t *testing.T, store db.Store) (string, string)

SeedAdmin creates an admin user with a known password and returns the (email, plaintext password) tuple suitable for LoginAs.

func SeedAdminUser

func SeedAdminUser(t *testing.T, store db.Store) (int64, string)

SeedAdminUser creates an admin user and returns its (id, email). Useful for tests that need the user_id to bind an API token.

func SeedRegularUser

func SeedRegularUser(t *testing.T, store db.Store, email, password string)

SeedRegularUser creates a non-admin user and returns its credentials.

func TagNode

func TagNode(t *testing.T, store db.Store, nodeID, tenant int64, names ...string)

TagNode puts TEAM tags of tenant `tenant` on a node — the shape every tag had before v0.43.0 (one shared label per file) and what migration 00055 turns those into. 0 is "the instance": the tenant a single-tenant install's tags live in. Reuses a tag of the same name (tagname.Key) the way the handler does, so two calls build one vocabulary row, not two.

func TagNodePersonal

func TagNodePersonal(t *testing.T, store db.Store, nodeID, owner int64, names ...string)

TagNodePersonal puts PERSONAL tags owned by `owner` on a node.

func TmpFilePath

func TmpFilePath(t *testing.T, name string) string

TmpFilePath returns a path inside t.TempDir() suitable for file create.

Types

This section is empty.

Directories

Path Synopsis
Package archivetest builds hostile archives for tests.
Package archivetest builds hostile archives for tests.
Package dbtest provides minimal DB+model test helpers without depending on auth/capability/share — so it is safe to import from the test files of those packages without creating an import cycle.
Package dbtest provides minimal DB+model test helpers without depending on auth/capability/share — so it is safe to import from the test files of those packages without creating an import cycle.
Package fakeidp is a minimal OpenID Connect provider for tests: discovery, a JWKS, an authorization endpoint that signs the current identity in at once, a token endpoint that answers with an RS256-signed id_token for that identity, and an end-session endpoint (RP-initiated logout) that records who asked.
Package fakeidp is a minimal OpenID Connect provider for tests: discovery, a JWKS, an authorization endpoint that signs the current identity in at once, a token endpoint that answers with an RS256-signed id_token for that identity, and an end-session endpoint (RP-initiated logout) that records who asked.

Jump to

Keyboard shortcuts

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