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 ¶
- func LoginAs(t *testing.T, srv *httptest.Server, client *http.Client, ...) string
- func LoginAsIn(t *testing.T, srv *httptest.Server, client *http.Client, ...) string
- func MustEmbedFS() embed.FS
- func NewAPIToken(t *testing.T, store db.Store, userID int64, scopes string) string
- func NewTestDB(t *testing.T) (*sql.DB, db.Store)
- func NewTestServer(t *testing.T) (*httptest.Server, *http.Client, db.Store)
- func NewTestServerCfg(t *testing.T, mutate func(*config.Config)) (*httptest.Server, *http.Client, db.Store)
- func NewTestServerWith(t *testing.T, cfgMutate func(*config.Config), depsMutate func(*api.Deps)) (*httptest.Server, *http.Client, db.Store)
- func ReadJSON(t *testing.T, resp *http.Response, out any)
- func SeedAdmin(t *testing.T, store db.Store) (string, string)
- func SeedAdminUser(t *testing.T, store db.Store) (int64, string)
- func SeedRegularUser(t *testing.T, store db.Store, email, password string)
- func TagNode(t *testing.T, store db.Store, nodeID, tenant int64, names ...string)
- func TagNodePersonal(t *testing.T, store db.Store, nodeID, owner int64, names ...string)
- func TmpFilePath(t *testing.T, name string) string
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.
On a multi-tenant server it signs the person in the way a tenant's person signs in on the platform's page: with THEIR OWN tenant's realm in the Realm field (docs/MULTI-TENANCY.md, Realms) — found from the account in the server's store, for a server this package built. The realm rules themselves (an empty realm is the platform's own tenant, another tenant's account is refused) are measured by requests that spell the realm out, never through this helper.
func LoginAsIn ¶ added in v0.50.0
func LoginAsIn(t *testing.T, srv *httptest.Server, client *http.Client, realm, email, password string) string
LoginAsIn is LoginAs in a tenant realm: what a tenant's person types in the sign-in form's Realm field on the platform's page of a multi-tenant install (docs/MULTI-TENANCY.md, Realms). An empty realm is the platform's own; a single-tenant server ignores the field.
func MustEmbedFS ¶
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 ¶
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 ¶
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 ¶
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 SeedAdmin ¶
SeedAdmin creates an admin user with a known password and returns the (email, plaintext password) tuple suitable for LoginAs.
func SeedAdminUser ¶
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 ¶
SeedRegularUser creates a non-admin user and returns its credentials.
func TagNode ¶
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 ¶
TagNodePersonal puts PERSONAL tags owned by `owner` on a node.
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. |
|
Package fakemagick writes a stand-in for ImageMagick: a shell script that answers the way an ImageMagick of a given kind does, so the code that runs ImageMagick (enginebin.HEIC, thumb.generateHEIF) is tested through a real exec, on a machine that has no ImageMagick at all.
|
Package fakemagick writes a stand-in for ImageMagick: a shell script that answers the way an ImageMagick of a given kind does, so the code that runs ImageMagick (enginebin.HEIC, thumb.generateHEIF) is tested through a real exec, on a machine that has no ImageMagick at all. |
|
Package gatetest catches a two-step change half way and asks whether the storage's row gate (internal/rowgate) is held there.
|
Package gatetest catches a two-step change half way and asks whether the storage's row gate (internal/rowgate) is held there. |
|
Package lives3 tells the suites that talk to a REAL S3 server which one: the FILEX_TEST_S3_* variables, read in one place.
|
Package lives3 tells the suites that talk to a REAL S3 server which one: the FILEX_TEST_S3_* variables, read in one place. |
|
Package wasmfixture guards the tests that load the echo app fixture (internal/wasmplugin/testdata/echo/echo.wasm): the module is built, and it was built from the sources beside it.
|
Package wasmfixture guards the tests that load the echo app fixture (internal/wasmplugin/testdata/echo/echo.wasm): the module is built, and it was built from the sources beside it. |