testing

package
v0.0.0-...-60192f8 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: AGPL-3.0 Imports: 32 Imported by: 0

Documentation

Overview

Package testing provides tools to tests the HTTP routes, the message bus, email sending, etc.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunRequestSequence

func RunRequestSequence(t *testing.T, c *Client, user string, tests ...RequestTest)

RunRequestSequence performs a serie of requests using RequestTest instances.

func Store

func Store() *superbus.MemStore

Store return the MemStore.

Types

type Client

type Client struct {
	*testing.T

	URL       *url.URL
	Jar       http.CookieJar
	CsrfToken string
	// contains filtered or unexported fields
}

Client is a thin HTTP client over the main server router.

func NewClient

func NewClient(t *testing.T, app *TestApp) *Client

NewClient creates a new Client instance.

func (*Client) Cookies

func (c *Client) Cookies() []*http.Cookie

Cookies returns the stored cookie for the current client session.

func (*Client) Get

func (c *Client) Get(target string) *Response

Get performs a GET request on the given path.

func (*Client) Login

func (c *Client) Login(username, password string)

Login logs in the given user.

func (*Client) Logout

func (c *Client) Logout()

Logout empties the client's cookie jar.

func (*Client) NewFormRequest

func (c *Client) NewFormRequest(method, target string, data url.Values) *http.Request

NewFormRequest returns a new http.Request instance to be used for sending form data.

func (*Client) NewJSONRequest

func (c *Client) NewJSONRequest(method, target string, data interface{}) *http.Request

NewJSONRequest returns a new http.Request instance to be used for sending and receiving JSON data.

func (*Client) NewRequest

func (c *Client) NewRequest(method, target string, body io.Reader) *http.Request

NewRequest returns a new http.Request instance ready for tests.

func (*Client) PostForm

func (c *Client) PostForm(target string, data url.Values) *Response

PostForm performs a POST request on the given path with some data. If available, the CSRF token is automaticaly sent with the data.

func (*Client) RenderTemplate

func (c *Client) RenderTemplate(src string, extra map[string]interface{}) (string, error)

RenderTemplate executes a template string using some properties of the client. (Users, URL). Extra data can be sent using the extra map.

func (*Client) Request

func (c *Client) Request(req *http.Request) *Response

Request performs a request using httptest tools. It returns a Response instance that can be evaluated for testing purposes.

func (*Client) RequestJSON

func (c *Client) RequestJSON(method string, target string, data interface{}) *Response

RequestJSON performs a JSON HTTP requests (sending and receiving data).

type EventManager

type EventManager struct {
	// contains filtered or unexported fields
}

EventManager is a in memory event manager that stores every event in a list.

func Events

func Events() *EventManager

Events returns the EventManager.

func NewEventManager

func NewEventManager() *EventManager

NewEventManager returns a new EventManager instance.

func (*EventManager) Clear

func (m *EventManager) Clear()

Clear empties the event queue.

func (*EventManager) Listen

func (m *EventManager) Listen()

Listen does nothing.

func (*EventManager) On

On registers an event handler. In this case, it does nothing.

func (*EventManager) Push

func (m *EventManager) Push(name string, value []byte) error

Push adds an event to the queue.

func (*EventManager) Records

func (m *EventManager) Records(name string) [][]byte

Records returns the recorded events for a given event name.

func (*EventManager) Stop

func (m *EventManager) Stop()

Stop stops the Event manager. In this case it empties the queue of recorded events.

type RequestTest

type RequestTest struct {
	Method         string
	Target         string
	Form           url.Values
	JSON           interface{}
	ExpectStatus   int
	ExpectRedirect string
	ExpectJSON     string
	ExpectJQ       []any
	ExpectContains string
	Assert         func(*testing.T, *Response)
}

RequestTest contains data that are used to perform requests and assert some results.

type Response

type Response struct {
	*http.Response
	URL       *url.URL
	Redirect  string
	Body      []byte
	HTML      *html.Node
	JSON      any
	CsrfToken string
}

Response is a wrapper around http.Response where the body is stored and the HTML (when applicable) is parsed in advance.

func NewResponse

func NewResponse(rec *httptest.ResponseRecorder, req *http.Request) (*Response, error)

NewResponse returns a Response instance based on the ResponseRecorder given in input.

func (*Response) AssertJQ

func (r *Response) AssertJQ(t *testing.T, q string, expected ...any)

AssertJQ performs the JQ query and check every expected result argument. The test fails when the returned results count differs from the expected arguments.

func (*Response) AssertJSON

func (r *Response) AssertJSON(t *testing.T, expected string)

AssertJSON checks that the response's JSON matches what we expect.

func (*Response) AssertRedirect

func (r *Response) AssertRedirect(t *testing.T, expected string)

AssertRedirect checks that the expected target is present in a Location header.

func (*Response) AssertStatus

func (r *Response) AssertStatus(t *testing.T, expected int)

AssertStatus checks the response's expected status.

func (*Response) Path

func (r *Response) Path() string

Path returns the path and querystring of the response URL.

type TestApp

type TestApp struct {
	TmpDir    string
	Srv       *server.Server
	Users     map[string]*TestUser
	Bookmarks map[string]*bookmarks.Bookmark
	LastEmail string
}

TestApp holds information of the application for testing.

func NewTestApp

func NewTestApp(t *testing.T) *TestApp

NewTestApp initializes TestApp with a default configuration, some users, and an http muxer ready to accept requests.

func (*TestApp) Close

func (ta *TestApp) Close(t *testing.T)

Close removes artifacts that were needed for testing.

func (*TestApp) SendEmail

func (ta *TestApp) SendEmail(m *mail.Email) error

SendEmail implements email.sender interface and stores the last sent message.

type TestUser

type TestUser struct {
	User      *users.User
	Token     *tokens.Token
	Bookmarks []*bookmarks.Bookmark
	// contains filtered or unexported fields
}

TestUser contains the user data that we can use during tests.

func NewTestUser

func NewTestUser(name, email, password, group string) (*TestUser, error)

NewTestUser creates a new user for testing.

func (*TestUser) JWT

func (tu *TestUser) JWT() string

JWT returns the user's API token.

func (*TestUser) Login

func (tu *TestUser) Login(c *Client)

Login performs the login for the user.

func (*TestUser) Password

func (tu *TestUser) Password() string

Password returns the user's password.

Jump to

Keyboard shortcuts

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