testutil

package
v0.0.0-...-f0f63f4 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2020 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Auth

func Auth(t *testing.T, tr *Request, p endpoint.Core) (userToken string, userID string)

Auth will register a user, login, and return the user token and user ID.

func EnsureBadRequest

func EnsureBadRequest(t *testing.T, w *httptest.ResponseRecorder) *model.BadRequestResponse

EnsureBadRequest tests for HTTP status 400.

func EnsureCreated

func EnsureCreated(t *testing.T, w *httptest.ResponseRecorder) *model.CreatedResponse

EnsureCreated tests for http status 201.

func EnsureInternalServerError

func EnsureInternalServerError(t *testing.T, w *httptest.ResponseRecorder) *model.InternalServerErrorResponse

EnsureInternalServerError tests for http status 500.

func EnsureOK

EnsureOK tests for HTTP status 200.

func EnsureUnauthorized

func EnsureUnauthorized(t *testing.T, w *httptest.ResponseRecorder) *model.UnauthorizedResponse

EnsureUnauthorized tests for HTTP status 401.

func LoadDatabase

func LoadDatabase(ml logger.ILog) *database.DBW

LoadDatabase will set up the DB and apply migrations for the tests.

func Register

func Register(t *testing.T, tr *Request, p endpoint.Core) (userID string)

Register a user and return the user ID.

func TeardownDatabase

func TeardownDatabase(db *database.DBW)

TeardownDatabase will destroy the test database.

func ToJSON

func ToJSON(values url.Values) string

ToJSON converts a url.Values to a JSON string.

Types

type Context

type Context struct {
	mock.Mock
}

Context is a mocked requestcontext.

func (*Context) SetUserID

func (m *Context) SetUserID(r *http.Request, userID string)

SetUserID .

func (*Context) UserID

func (m *Context) UserID(r *http.Request) (userID string, found bool)

UserID .

type CoreTest

type CoreTest struct {
	DB      *database.DBW
	Core    endpoint.Core
	Test    *Mocks
	Request *Request
}

CoreTest is a collection of utilities for testing.

func Setup

func Setup() *CoreTest

Setup will set up the test utilities.

func (*CoreTest) Teardown

func (ct *CoreTest) Teardown()

Teardown will teardown the test utilities - should be called with a defer.

type MockLogger

type MockLogger struct {
	FatalfFunc func(format string, v ...interface{})
	PrintfFunc func(format string, v ...interface{})
}

MockLogger is a mocked logger.

func NewMockLogger

func NewMockLogger() *MockLogger

NewMockLogger is a mocked logger that displays nothing by default.

func (*MockLogger) Fatalf

func (l *MockLogger) Fatalf(format string, v ...interface{})

Fatalf .

func (*MockLogger) Printf

func (l *MockLogger) Printf(format string, v ...interface{})

Printf .

type MockPasshash

type MockPasshash struct {
	HashString string
	HashError  error
	MatchBool  bool
}

MockPasshash is a mocked passhash.

func (*MockPasshash) Hash

func (m *MockPasshash) Hash(password string) (string, error)

Hash .

func (*MockPasshash) Match

func (m *MockPasshash) Match(hash, password string) bool

Match .

type MockToken

type MockToken struct {
	GenerateFunc func(userID string) (string, error)
	VerifyValue  string
	VerifyError  error
}

MockToken is a mocked webtoken.

func NewMockToken

func NewMockToken() *MockToken

NewMockToken returns a new mock token.

func (*MockToken) Generate

func (mt *MockToken) Generate(userID string) (string, error)

Generate .

func (*MockToken) Verify

func (mt *MockToken) Verify(s string) (string, error)

Verify .

type Mocks

type Mocks struct {
	Log  *MockLogger
	Mock *mock.Mocker
}

Mocks contains all the configurable dependencies.

func Services

func Services(db *database.DBW) (endpoint.Core, *Mocks)

Services sets up the test services.

type Request

type Request struct {
	// Request is cleared before use.
	Request *http.Request
	// Header clears after use.
	Header http.Header
	// SkipMiddleware resets to false after use.
	SkipMiddleware bool
	// SkipRoutes resets to false after use.
	SkipRoutes bool
}

Request is a test request.

func NewRequest

func NewRequest() *Request

NewRequest returns a new test request.

func (*Request) SendForm

func (tr *Request) SendForm(t *testing.T, core endpoint.Core, method string, target string,
	v url.Values) *httptest.ResponseRecorder

SendForm is a helper to quickly make a form request.

func (*Request) SendJSON

func (tr *Request) SendJSON(t *testing.T, core endpoint.Core, method string, target string,
	v url.Values) *httptest.ResponseRecorder

SendJSON is a helper to quickly make a JSON request.

Jump to

Keyboard shortcuts

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