envstest

package
v1.17.0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: Apache-2.0 Imports: 48 Imported by: 0

Documentation

Overview

Package envstest defines global test helpers for the entire project.

Index

Constants

View Source
const (
	RealmName  = "e2e-test-realm"
	RegionCode = "e2e-test"

	AdminKeyPrefix  = "e2e-admin-key."
	DeviceKeyPrefix = "e2e-device-key."
	StatsKeyPrefix  = "e2e-stats-key."

	AndroidAppPrefix = "e2e-android-"
	IOSAppPrefix     = "e2e-ios-"
)

Variables

This section is empty.

Functions

func AutoConfirmDialogs added in v0.19.0

func AutoConfirmDialogs(ctx context.Context, b bool) <-chan error

AutoConfirmDialogs automatically clicks "confirm" on popup dialogs from window.Confirm prompts.

func BuildFormRequest added in v0.23.0

func BuildFormRequest(ctx context.Context, tb testing.TB, meth, pth string, v *url.Values) (*httptest.ResponseRecorder, *http.Request)

BuildFormRequest builds an http request and http response recorder for the given form values (expressed as url.Values). It sets the proper headers and response types to post as a form and expect HTML in return.

func BuildJSONRequest added in v0.23.0

func BuildJSONRequest(ctx context.Context, tb testing.TB, meth, pth string, v interface{}) (*httptest.ResponseRecorder, *http.Request)

BuildJSONRequest builds an http request and http response recorder for the given payload. marshaled as JSON. It sets the proper headers and response types.

func CaptureJavascriptErrors added in v0.19.0

func CaptureJavascriptErrors(ctx context.Context) <-chan error

CaptureJavascriptErrors captures any console errors that occur.

func ExerciseBadPagination added in v0.19.0

func ExerciseBadPagination(t *testing.T, membership *database.Membership, h http.Handler)

ExerciseBadPagination tests that the proper response code and HTML error page are rendered when the URL includes pagination parameters that fail to parse.

func ExerciseIDNotFound added in v0.19.0

func ExerciseIDNotFound(t *testing.T, membership *database.Membership, h http.Handler)

ExerciseIDNotFound tests that the proper response code and HTML error page are rendered when the route expects an "id" mux parameter, but the one given does not correspond to an actual record.

func ExerciseMembershipMissing added in v0.19.0

func ExerciseMembershipMissing(t *testing.T, h http.Handler)

ExerciseMembershipMissing tests that the proper response code and HTML error page are rendered with there is no membership in the context. It sets a session in the context.

func ExercisePermissionMissing added in v0.19.0

func ExercisePermissionMissing(t *testing.T, h http.Handler)

ExercisePermissionMissing tests that the proper response code and HTML error page are rendered when the requestor does not have permission to perform this action.

func ExerciseSessionMissing added in v0.19.0

func ExerciseSessionMissing(t *testing.T, h http.Handler)

ExerciseSessionMissing tests that the proper response code and HTML error page are rendered with the context has no session.

func ExerciseUserMissing added in v0.22.0

func ExerciseUserMissing(t *testing.T, h http.Handler)

ExerciseUserMissing tests that the proper response code and HTML error page are rendered with there is no user in the context. It sets a session in the context. This only applies to admin pages

func NewFailingDatabase added in v0.19.0

func NewFailingDatabase() *gorm.DB

NewFailingDatabase database creates a new database connection that fails all requests.

func NewHarnessServer added in v0.19.0

func NewHarnessServer(tb testing.TB, mux http.Handler) *server.Server

NewHarnessServer creates a new server for the mux on a random port. Cleanup is handled automatically.

Types

type APIServerConfigResponse added in v0.19.0

type APIServerConfigResponse struct {
	Config      *config.APIServerConfig
	Database    *database.Database
	BadDatabase *database.Database
	Cacher      cache.Cacher
	KeyManager  keys.KeyManager
	RateLimiter limiter.Store
	Renderer    *render.Renderer
}

APIServerConfigResponse is the response from creating an API server config.

func NewAPIServerConfig added in v0.19.0

func NewAPIServerConfig(tb testing.TB, testDatabaseInstance *database.TestInstance) *APIServerConfigResponse

NewAPIServerConfig creates a new API server configuration.

func (*APIServerConfigResponse) NewServer added in v0.19.0

type APIServerResponse added in v0.19.0

type APIServerResponse struct {
	Config      *config.APIServerConfig
	Database    *database.Database
	BadDatabase *database.Database
	Cacher      cache.Cacher
	KeyManager  keys.KeyManager
	RateLimiter limiter.Store
	Renderer    *render.Renderer
	Server      *server.Server
}

APIServerResponse is the response from a test APIServer instance.

func NewAPIServer added in v0.19.0

func NewAPIServer(tb testing.TB, testDatabaseInstance *database.TestInstance) *APIServerResponse

NewAPIServer creates a new test APIServer server instance. See NewHarnessServer for more information.

type AdminAPIServerConfigResponse added in v0.19.0

type AdminAPIServerConfigResponse struct {
	Config      *config.AdminAPIServerConfig
	Database    *database.Database
	BadDatabase *database.Database
	Cacher      cache.Cacher
	KeyManager  keys.KeyManager
	RateLimiter limiter.Store
	Renderer    *render.Renderer
}

AdminAPIServerConfigResponse is the response from creating an AdminAPI server config.

func NewAdminAPIServerConfig added in v0.19.0

func NewAdminAPIServerConfig(tb testing.TB, testDatabaseInstance *database.TestInstance) *AdminAPIServerConfigResponse

NewAdminAPIServerConfig creates a new API server configuration.

func (*AdminAPIServerConfigResponse) NewServer added in v0.19.0

NewServer creates a new server.

type AdminAPIServerResponse added in v0.19.0

type AdminAPIServerResponse struct {
	Config      *config.AdminAPIServerConfig
	Database    *database.Database
	BadDatabase *database.Database
	Cacher      cache.Cacher
	KeyManager  keys.KeyManager
	RateLimiter limiter.Store
	Renderer    *render.Renderer
	Server      *server.Server
}

AdminAPIServerResponse is the response from a test AdminAPI instance.

func NewAdminAPIServer added in v0.19.0

func NewAdminAPIServer(tb testing.TB, testDatabaseInstance *database.TestInstance) *AdminAPIServerResponse

NewAdminAPIServer creates a new test AdminAPI server instance. See NewHarnessServer for more information.

type BootstrapResponse added in v0.20.0

type BootstrapResponse struct {
	Realm        *database.Realm
	AdminAPIKey  string
	DeviceAPIKey string
	StatsAPIKey  string
	// contains filtered or unexported fields
}

BootstrapResponse is the response from Bootstrap.

func Bootstrap added in v0.20.0

func Bootstrap(ctx context.Context, db *database.Database) (*BootstrapResponse, error)

Bootstrap configures the database with an e2e realm (or re-uses one that already exists), and provisions new authorized apps for accessing the admin apis, device apis, and stats apis.

It also provisions and enables the e2e realm. If the realm already exists, it updates the realms settings to enable settings and configuration that e2e expects.

Callers should always call Cleanup() on the response to ensure temporary resources are purged.

func (*BootstrapResponse) Cleanup added in v0.20.0

func (r *BootstrapResponse) Cleanup() error

Cleanup deletes temporary resources created by the bootstrap.

type ENXRedirectServerConfigResponse added in v0.21.0

type ENXRedirectServerConfigResponse struct {
	Config      *config.RedirectConfig
	Database    *database.Database
	BadDatabase *database.Database
	Cacher      cache.Cacher
	KeyManager  keys.KeyManager
	RateLimiter limiter.Store
	Renderer    *render.Renderer
}

ENXRedirectServerConfigResponse is the response from creating an Redirect server config.

func NewENXRedirectServerConfig added in v0.21.0

func NewENXRedirectServerConfig(tb testing.TB, testDatabaseInstance *database.TestInstance) *ENXRedirectServerConfigResponse

NewENXRedirectServerConfig creates a new ENX redirect server configuration.

func (*ENXRedirectServerConfigResponse) NewServer added in v0.21.0

NewServer creates a new server.

type ENXRedirectServerResponse added in v0.21.0

type ENXRedirectServerResponse struct {
	Config      *config.RedirectConfig
	Database    *database.Database
	BadDatabase *database.Database
	Cacher      cache.Cacher
	Renderer    *render.Renderer
	Server      *server.Server
}

ENXRedirectServerResponse is the response from a test ENX redirect instance.

func NewENXRedirectServer added in v0.21.0

func NewENXRedirectServer(tb testing.TB, testDatabaseInstance *database.TestInstance) *ENXRedirectServerResponse

NewENXRedirectServer creates a new test ENX redirect server instance. See NewHarnessServer for more information.

type IntegrationSuite added in v0.20.0

type IntegrationSuite struct {
	AdminAPIAddress string
	AdminAPIKey     string

	APIServerAddress string
	APIServerKey     string

	KeyServerAddress string
	KeyServerData    *enkstest.BootstrapResponse

	ENXRedirectAddress string
}

IntegrationSuite encompasses a local API server and Admin API server for testing. Both servers run in-memory on the local machine.

func NewIntegrationSuite added in v0.20.0

func NewIntegrationSuite(tb testing.TB, testDatabaseInstance *database.TestInstance, testKeyServerDatabaseInstance *keydatabase.TestInstance) *IntegrationSuite

NewIntegrationSuite creates a new test suite for local integration testing.

type SQLConn added in v0.19.0

type SQLConn struct{}

func (*SQLConn) Begin added in v0.19.0

func (c *SQLConn) Begin() (driver.Tx, error)

func (*SQLConn) BeginTx added in v0.19.0

func (c *SQLConn) BeginTx() (driver.Tx, error)

func (*SQLConn) Close added in v0.19.0

func (c *SQLConn) Close() error

func (*SQLConn) Commit added in v0.19.0

func (c *SQLConn) Commit() error

func (*SQLConn) Exec added in v0.19.0

func (c *SQLConn) Exec(query string, args []driver.Value) (driver.Result, error)

func (*SQLConn) Prepare added in v0.19.0

func (c *SQLConn) Prepare(query string) (driver.Stmt, error)

func (*SQLConn) PrepareContext added in v0.19.0

func (c *SQLConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error)

func (*SQLConn) Query added in v0.19.0

func (c *SQLConn) Query(query string, args []driver.Value) (driver.Rows, error)

func (*SQLConn) QueryContext added in v0.19.0

func (c *SQLConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error)

func (*SQLConn) Rollback added in v0.19.0

func (c *SQLConn) Rollback() error

type SQLDriver added in v0.19.0

type SQLDriver struct{}

SQLDriver is a test SQL driver implementation that returns an error for most queries and operations. It's used to test how the system behaves under connection errors or other database-level failures.

func (*SQLDriver) Connect added in v0.19.0

func (d *SQLDriver) Connect(ctx context.Context) (driver.Conn, error)

func (*SQLDriver) Driver added in v0.19.0

func (d *SQLDriver) Driver() driver.Driver

func (*SQLDriver) Open added in v0.19.0

func (d *SQLDriver) Open(name string) (driver.Conn, error)

type ServerConfigResponse added in v0.19.0

type ServerConfigResponse struct {
	AuthProvider auth.Provider
	Config       *config.ServerConfig
	Database     *database.Database
	BadDatabase  *database.Database
	Cacher       cache.Cacher
	Locales      *i18n.LocaleMap
	KeyManager   keys.KeyManager
	RateLimiter  limiter.Store
	Renderer     *render.Renderer
}

ServerConfigResponse is the response from creating a server config.

func NewServerConfig added in v0.19.0

func NewServerConfig(tb testing.TB, testDatabaseInstance *database.TestInstance) *ServerConfigResponse

NewServerConfig creates a new server configuration. It creates all the keys, databases, and cacher, but does not actually start the server. All cleanup is scheduled by t.Cleanup.

func (*ServerConfigResponse) NewServer added in v0.19.0

func (*ServerConfigResponse) WithCommonMiddlewares added in v0.26.0

func (r *ServerConfigResponse) WithCommonMiddlewares(next http.Handler) http.Handler

WithCommonMiddlewares returns a helper that injects common middleware onto a handler.

type TestHarnessResponse added in v0.19.0

type TestHarnessResponse struct {
	Cacher      cache.Cacher
	CacheConfig *cache.Config

	Database       *database.Database
	DatabaseConfig *database.Config

	BadDatabase *database.Database

	ObservabilityConfig *observability.Config

	KeyManager       keys.KeyManager
	KeyManagerConfig *keys.Config

	RateLimiter       limiter.Store
	RateLimiterConfig *ratelimit.Config
}

func NewTestHarness added in v0.19.0

func NewTestHarness(tb testing.TB, testDatabaseInstance *database.TestInstance) *TestHarnessResponse

type TestServerResponse

type TestServerResponse struct {
	AuthProvider auth.Provider
	Cacher       cache.Cacher
	Config       *config.ServerConfig
	Database     *database.Database
	BadDatabase  *database.Database
	KeyManager   keys.KeyManager
	Locales      *i18n.LocaleMap
	RateLimiter  limiter.Store
	Renderer     *render.Renderer
	Server       *server.Server
}

TestServerResponse is used as the reply to creating a test UI server.

func NewServer

func NewServer(tb testing.TB, testDatabaseInstance *database.TestInstance) *TestServerResponse

NewServer creates a new test UI server instance. See NewHarnessServer for more information.

func (*TestServerResponse) LoggedInSession added in v0.16.0

func (r *TestServerResponse) LoggedInSession(session *sessions.Session, email string) (*sessions.Session, error)

LoggedInSession returns an session with the provided email address logged in. It also stores that email verification and MFA prompting have already occurred for a consistent post-login experience.

The provided email is marked as verified, has MFA enabled, and is not revoked. To test other journeys, manually build the session.

func (*TestServerResponse) ProvisionAndLogin added in v0.19.0

func (r *TestServerResponse) ProvisionAndLogin() (*database.Realm, *database.User, *sessions.Session, error)

ProvisionAndLogin provisions and authenticates the initial user, realm, permissions on the realm, and session cookie.

func (*TestServerResponse) SessionCookie

func (r *TestServerResponse) SessionCookie(session *sessions.Session) (*http.Cookie, error)

SessionCookie returns an encrypted cookie for the given session information, capable of being injected into the browser instance and read by the application. Since the cookie contains the session, it can be used to mutate any server state, including the currently-authenticated user.

Jump to

Keyboard shortcuts

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