testhelpers

package
v0.0.0-...-58993a3 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: GPL-3.0 Imports: 16 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func InsertAPIKey

func InsertAPIKey(t *testing.T, pool *pgxpool.Pool, ctx context.Context, name string) string

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

func MigrateWithRetry(databaseURL string, migrationsPath string, timeout time.Duration) error

MigrateWithRetry runs MigrateUp, retrying until timeout if the DB isn't ready yet.

func ProjectRoot

func ProjectRoot(t *testing.T) string

ProjectRoot walks up from the caller's file location to return the repository root.

func ResetDatabase

func ResetDatabase(t *testing.T, pool *pgxpool.Pool)

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

func TestConfig(dbURL string) config.Config

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

func TestLogger() zerolog.Logger

TestLogger returns a no-op logger suitable for tests.

Types

This section is empty.

Jump to

Keyboard shortcuts

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