Documentation
¶
Overview ¶
Package testhelpers provides shared test utilities for integration, contract, and batch test packages. Functions here are pure utilities that take explicit DB/context arguments rather than a package-specific testEnv struct.
Index ¶
- func InsertAPIKey(t *testing.T, pool *pgxpool.Pool, ctx context.Context, name string) string
- func InsertAPIKeyWithRole(t *testing.T, pool *pgxpool.Pool, ctx context.Context, name, role string) string
- func InsertAdminUser(t *testing.T, pool *pgxpool.Pool, ctx context.Context, ...)
- func MigrateWithRetry(databaseURL string, migrationsPath string, timeout time.Duration) error
- func ProjectRoot(t *testing.T) string
- func ResetDatabase(t *testing.T, pool *pgxpool.Pool)
- func TestConfig(dbURL string) config.Config
- func TestLogger() zerolog.Logger
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InsertAPIKey ¶
InsertAPIKey creates a test API key with a random prefix and SHA-256 hash, inserts it into the database, and returns the raw key string. Uses crypto/rand (not ULID) to avoid prefix collisions when many keys are created in rapid succession. Uses SHA-256 (not bcrypt) to avoid ~300ms/hash overhead; ValidateAPIKey supports both hash versions.
func InsertAPIKeyWithRole ¶
func InsertAPIKeyWithRole(t *testing.T, pool *pgxpool.Pool, ctx context.Context, name, role string) string
InsertAPIKeyWithRole inserts an API key with a specific role. Unlike InsertAPIKey (which defaults to 'agent'), this allows creating admin keys for token exchange tests. Uses crypto/rand + SHA-256 hashing (same as InsertAPIKey).
func InsertAdminUser ¶
func InsertAdminUser(t *testing.T, pool *pgxpool.Pool, ctx context.Context, username, password, email, role string)
InsertAdminUser inserts a user with a bcrypt-hashed password. Note: bcrypt is unavoidable here because the production login handler verifies passwords with bcrypt. Use sparingly — it adds ~100ms per call.
func MigrateWithRetry ¶
MigrateWithRetry runs MigrateUp, retrying until timeout if the DB isn't ready yet.
func ProjectRoot ¶
ProjectRoot walks up from the caller's file location to return the repository root.
func ResetDatabase ¶
ResetDatabase truncates all public tables (except schema_migrations and river_migration) and restarts sequences. Call at the start of each test to ensure isolation.
func TestConfig ¶
TestConfig returns a standard config.Config for tests using the given DB URL. Rate limits are set high so they never interfere with test logic. AllowTestDomains is always true so test code constructing example.com URLs passes validation.
func TestLogger ¶
TestLogger returns a no-op logger suitable for tests.
Types ¶
This section is empty.