testkit

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package testkit - the part of the module SDK a module's tests use, as a module from its own repository can:

  • rendering: the engine's templates with the module's (UseEngineRoot, RenderPageContent, RenderPlacement…), the modules switched on and off (WithModules), a theme or the embedded assets (UseTheme, UseModuleAssets);
  • handler tests: fakes of the core's services (Auth, Users, Nodes), a session with a CSRF token (Session, PostForm), the module's mux (ModuleMux), deleting content (NodeDeleter), the actions recorded for the action log (CaptureActions);
  • the database (built with the dbintegration tag only, run on the servers KOSATKA_TEST_MYSQL_DSN and KOSATKA_TEST_POSTGRES_DSN name - the engine's scripts/test_db.sh starts both; without them a test is skipped): every set's schema installed (SchemaDB) and the core's repositories and services on it (Deps).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaptureActions

func CaptureActions(t *testing.T) *[]modulekit.ActionEvent

CaptureActions - the actions recorded for the action log during the test (modulekit.RecordAction, RecordNodeCreated…), in order; the other listeners are dropped until it ends.

func EngineRoot

func EngineRoot() string

EngineRoot - the engine's source tree, whose templates (web/templates), themes and translations a module's rendering tests read: this package's directory two levels up - the engine's repository, or the engine's module in the module cache for a module from its own repository.

func FileStorageConfig

func FileStorageConfig(publicStorage string, sections map[string]any) modulekit.FileStorageConfig

FileStorageConfig - the config a file storage's Public and Private get on a site whose public files are in publicStorage ("local", or the storage's name), with sections as the config file's files.<section> (e.g. {"s3": {"bucket": "site"}}); Decode reads the environment over them, as on a site.

func Logger

func Logger() *slog.Logger

Logger - a logger writing nowhere.

func ModuleMux

func ModuleMux(mux *http.ServeMux, module string, notFound http.HandlerFunc) modulekit.Mux

ModuleMux - the module's routes' mux on mux, as the site gives it: its routes answer notFound while the module is off.

func NodeDeleter

func NodeDeleter(nodes modulekit.NodesRepository, deleteNode func(ctx context.Context, id int) error) modulekit.NodeDeleter

NodeDeleter - the core's deleting of content of a handler test: nodes finds an item, deleteNode deletes it (its files untouched).

func PageData

func PageData() map[string]any

PageData - a whole page's data with no blocks placed in its regions, for rendering a layout (RenderPageBlock with "layout.html"); more is added to it by the test.

func PostForm

func PostForm(path string, form url.Values, cookies []*http.Cookie) *http.Request

PostForm - a form POST to path (with the session's cookies; the form carries what the test gives, the CSRF token too if it wants it).

func RegisterModules

func RegisterModules()

RegisterModules puts the engine's modules in the registry (a module from its own repository registers itself, imported by the test).

func RenderNamed

func RenderNamed(t testing.TB, name string, data any, files ...string) string

RenderNamed - the template name defined in files (template paths, as the template service resolves them).

func RenderPageBlock

func RenderPageBlock(t testing.TB, page, block string, data any) string

RenderPageBlock - the block of a page template (a template file and what it parses with it: the layout, the partials, the modules' hooks), as rendering the page renders it; a failure fails the test.

func RenderPageContent

func RenderPageContent(t testing.TB, page string, data any) string

RenderPageContent - the "content" block of a page template.

func RenderPlacement

func RenderPlacement(t testing.TB, p modulekit.BlockPlacement, pageData map[string]any) string

RenderPlacement - the region of p with that one block, as a page renders its regions: a plugin block through its module's template (block_<id>), its PluginConfig as placed. pageData - the rest of the page's data the block reads (nil - none).

func ResolveTemplate

func ResolveTemplate(path string) (string, error)

ResolveTemplate - where the template path resolves ("embed:…" for a module's embedded one).

func Session

func Session(t testing.TB) (*sessions.CookieStore, []*http.Cookie, string)

Session - a cookie store with a session holding a CSRF token: its cookies and the token, for a form a test posts.

func Templates

func Templates() modulekit.TemplateService

Templates - the template service a controller of a handler test renders with (modulekit.BaseDeps.Templates): the site's, uncached.

func UseEngineRoot

func UseEngineRoot(t testing.TB)

UseEngineRoot makes the engine's source tree the test's working directory, where the template service reads from, and the modules' files where a site finds them: an engine module's directory under the engine's, a module from its own repository's assets of its registration.

func UseImageStyleFormats

func UseImageStyleFormats(t testing.TB, formats map[string]imagestyle.Format)

UseImageStyleFormats gives the image styles their output formats (as the image styles page sets them) for the test.

func UseImageStyleVersions

func UseImageStyleVersions(t testing.TB, versions map[string]string)

UseImageStyleVersions gives the image styles their versions in the derivatives' URLs (?v=…, as the site sets them from the styles' settings) for the test.

func UseModuleAssets

func UseModuleAssets(t testing.TB, assets map[string]fs.FS)

UseModuleAssets serves the modules' templates and static files from their assets (the binary's embedded files), as on a site, for the test.

func UseTheme

func UseTheme(t testing.TB, themesDir, theme string)

UseTheme makes theme, in themesDir, the site's theme for the test (themesDir "" - the engine's themes directory).

func WithModules

func WithModules(t testing.TB, states map[string]bool)

WithModules switches the modules on (true) and off for the test, the engine's registered; their states are restored after it.

Types

type Auth

type Auth struct {
	modulekit.AuthService
	UserID      int
	Allow       bool
	Permissions map[string]bool
}

Auth - the core's AuthService of a handler test: UserID is the signed-in user (0 - none), Allow answers the content permission checks (create, edit, delete), Permissions the site-wide ones; any other method panics (the embedded nil interface).

func (Auth) CanCreateNode

CanCreateNode - Allow.

func (Auth) CanDeleteNode

func (a Auth) CanDeleteNode(context.Context, *modulekit.Node, *modulekit.User) bool

CanDeleteNode - Allow.

func (Auth) CanEditNode

func (a Auth) CanEditNode(context.Context, *modulekit.Node, *modulekit.User) bool

CanEditNode - Allow.

func (Auth) CanUseSiteInMaintenanceMode

func (a Auth) CanUseSiteInMaintenanceMode(context.Context, *modulekit.User) bool

CanUseSiteInMaintenanceMode - no one.

func (Auth) CanViewNode

func (a Auth) CanViewNode(_ context.Context, n *modulekit.Node, _ *modulekit.User) bool

CanViewNode - a published node, or any while Allow.

func (Auth) ContentVisitor

ContentVisitor - nothing hidden: all the content is open.

func (Auth) CreatableNodeTypes

func (a Auth) CreatableNodeTypes(context.Context, *modulekit.User) []modulekit.CreatableMaterialLink

CreatableNodeTypes - none.

func (Auth) GetUserID

func (a Auth) GetUserID(*http.Request) (int, bool)

GetUserID - UserID, signed in when it isn't 0.

func (Auth) HasPermission

func (a Auth) HasPermission(_ context.Context, _ *modulekit.User, perm string) bool

HasPermission - the site-wide permissions in Permissions (none by default: the pages render as for a regular user).

type Nodes

type Nodes struct {
	modulekit.NodesRepository
	Known map[int]*modulekit.Node
}

Nodes - the core's NodesRepository knowing the content by ID (Known); any other method panics.

func (Nodes) ByID

func (n Nodes) ByID(_ context.Context, id int) (*modulekit.Node, error)

ByID - the content in Known, else ErrEntityNotFound.

type Users

type Users struct {
	modulekit.UserService
	Known map[int]*modulekit.User
}

Users - the core's UserService knowing the users by ID (Known); any other method panics.

func (Users) ByID

func (u Users) ByID(_ context.Context, id int) (*modulekit.User, error)

ByID - the user in Known, else ErrEntityNotFound.

Jump to

Keyboard shortcuts

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