sitetest

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Overview

Package sitetest starts a complete WebThings app for end-to-end tests and drives its admin like a browser: sign in, submit forms, publish.

Its own templates are only the generic ones (layout, page, 404 and search). A module's tests bring the module's templates in their testdata folder:

s := sitetest.StartWith(t, sitetest.Options{Templates: "testdata/templates"}, publishing.Module("news", "News"))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasToast

func HasToast(body, level, text string) bool

HasToast reports whether an admin page shows text as a toast of level ("success", "info", "warn" or "error").

func JPEG

func JPEG(t testing.TB, width, height int) []byte

JPEG returns a grey JPEG image of width × height pixels.

func PNG

func PNG(t testing.TB, width, height int) []byte

PNG returns a blank PNG image of width × height pixels.

Types

type File

type File struct {
	Field string
	Name  string
	Data  []byte
}

File is a file in a form PostFiles sends, as a browser sends the file of a file input: Field is the input's name.

type Options

type Options struct {
	// Templates is a template folder used on top of sitetest's generic
	// templates, holding the module's own, for example comments/list.gohtml.
	Templates string
	// Files replace sitetest's templates entirely, for example a folder of
	// themes.
	Files fs.FS
	// Prepare runs after the modules are added and before the app starts, for
	// example to write templates with App.WriteTemplates.
	Prepare func(app *webthings.App) error
	// Configure changes the app's configuration before it is created, for
	// example its CacheTTL. It runs after OutputCache is applied, so it can
	// set Config.OutputCacheTTL itself.
	Configure func(config *webthings.Config)
	// OutputCache keeps the output cache on, as the site setting says
	// (see webthings.Config.OutputCacheTTL). Sites in tests have it off,
	// so a test that changes content from Go, outside a request, sees the
	// change on the next page also where the app is not told about it.
	OutputCache bool
	// StartingPoints names module instances, such as "news", whose starting
	// points App.WriteTemplates generates for the test. They come below
	// Templates and above sitetest's generic templates, and as they are
	// written for the instance key they take precedence over templates in
	// Templates for the module type. Tests then render the generated
	// templates, which can never drift from the generator, and need no
	// copies of their own.
	StartingPoints []string
	// NoAccount starts the site without accounts, as a fresh install that
	// offers the first-run setup; StartWith then returns a browser that is
	// not signed in. The setup link is <AdminPath>/setup?token=<token> with
	// the token from App.SetupToken, also posted as the form's Token field.
	NoAccount bool
}

Options change how StartWith sets up the site.

type Site

type Site struct {
	App *webthings.App
	// Mail holds the e-mail the site sent.
	Mail *mail.Memory
	// contains filtered or unexported fields
}

Site is a running app with a signed-in admin browser.

func Start

func Start(t *testing.T, modules ...webthings.Module) *Site

Start runs an app with the given modules on SQLite, signed in as admin.

func StartWith

func StartWith(t *testing.T, options Options, modules ...webthings.Module) *Site

StartWith is Start with options.

func (*Site) As

func (s *Site) As(email, password string) *Site

As returns a second browser on the same site, signed in with another account.

func (*Site) CrossSite

func (s *Site) CrossSite(path string, values url.Values) int

CrossSite posts to path as a page on another website would and returns the status.

func (*Site) Dashboard

func (s *Site) Dashboard() string

Dashboard returns the admin dashboard with its panels loaded, as the browser shows it once they arrived.

func (*Site) DraftPage

func (s *Site) DraftPage(title, draft, slug string)

DraftPage saves a new, unpublished English title and slug for the page titled title, leaving its published version as it is.

func (*Site) Expect

func (s *Site) Expect(path string, status int, parts ...string)

Expect checks that a public page answers with status and contains all parts.

func (*Site) ExpectMissing

func (s *Site) ExpectMissing(path, part string)

ExpectMissing checks that a page does not contain part.

func (*Site) Fragment

func (s *Site) Fragment(path, id string) (int, string)

Fragment returns the status and body of the part of the page at path with element ID id, asked for as HTMX does for an element that loads on its own, such as a panel of the admin dashboard.

func (*Site) Get

func (s *Site) Get(path string) (int, string)

Get returns the status and body of a page.

func (*Site) Mails

func (s *Site) Mails(count int) []mail.Message

Mails waits until the site has sent at least count e-mails, as some are sent in the background, and returns all it sent. It fails the test after five seconds.

func (*Site) Mount

func (s *Site) Mount(module, title, slug string) string

Mount creates and publishes a page with a module below Home, in English, and returns its editor address.

func (*Site) MountIn

func (s *Site) MountIn(module string, translations ...Translation) string

MountIn creates a page with a module below Home with a translation in every given language, publishes it and returns its editor address. The page is created in the first language.

func (*Site) Post

func (s *Site) Post(formPage, action string, values url.Values) string

Post opens the page at formPage and sends values to action from it, as a browser submits a form of that page.

func (*Site) PostFiles

func (s *Site) PostFiles(formPage, action string, values url.Values, files ...File) (int, string)

PostFiles opens formPage and sends values and files to action from it as a multipart form, as a browser submits a form with a file input. It returns the status and body after redirects instead of failing the test.

func (*Site) PostStatus

func (s *Site) PostStatus(formPage, action string, values url.Values) (int, string)

PostStatus is Post for submissions that may fail: it returns the status and body instead of failing the test.

func (*Site) Publish

func (s *Site) Publish(path string)

Publish presses the publish button in the editor at path.

func (*Site) Request

func (s *Site) Request(request *http.Request) *http.Response

Request sends request with the browser's cookies, its URL being a path of the site such as App.AdminURL("admin.presence"), and returns the response unread, for streams and other responses a test reads itself. Close its body.

func (*Site) Response

func (s *Site) Response(path string) (*http.Response, string)

Response returns the response to a GET of path, with its headers, and its body.

func (*Site) Send

func (s *Site) Send(path string, values url.Values) (int, string, string)

Send posts a visitor's form to path, as a public form of the site does, without opening a page first, and returns the status, body and final path.

func (*Site) SendHTMX

func (s *Site) SendHTMX(path string, values url.Values, target string) (int, string, string)

SendHTMX is Send for a form that posts with HTMX and swaps the element with ID target: the post and the redirect HTMX follows carry its request headers. It returns the status, the body, which is a part of the page without the layout, and the final path.

func (*Site) Submit

func (s *Site) Submit(path string, values url.Values) string

Submit opens the form at path, posts values to it and returns the page it ended on. It fails the test on any error status.

func (*Site) Upload

func (s *Site) Upload(name string, data []byte) media.Asset

Upload adds a file to the site's media library, as the admin's upload does, and returns the new asset.

func (*Site) Visitor

func (s *Site) Visitor() *Site

Visitor returns a browser on the same site that is not signed in.

type Translation

type Translation struct {
	Locale string
	Title  string
	Slug   string
}

Translation is a page's title and slug in one language, for MountIn.

Directories

Path Synopsis
Package catalogtest checks in tests that the admin's catalogues translate every text of a section's forms in each language the admin comes with, so a label or description added to a form cannot go untranslated:
Package catalogtest checks in tests that the admin's catalogues translate every text of a section's forms in each language the admin comes with, so a label or description added to a form cannot go untranslated:

Jump to

Keyboard shortcuts

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