Added tests package with helpers. Started on middleware tests.
This commit is contained in:
parent
0e2625bf51
commit
58ba9f6dcc
9 changed files with 197 additions and 66 deletions
|
|
@ -6,21 +6,18 @@ import (
|
|||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"goweb/config"
|
||||
"goweb/middleware"
|
||||
"goweb/msg"
|
||||
"goweb/services"
|
||||
"goweb/tests"
|
||||
|
||||
"github.com/eko/gocache/v2/store"
|
||||
|
||||
"github.com/eko/gocache/v2/marshaler"
|
||||
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/labstack/echo-contrib/session"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
|
@ -50,21 +47,8 @@ func TestMain(m *testing.M) {
|
|||
os.Exit(exitVal)
|
||||
}
|
||||
|
||||
func newContext(url string) (echo.Context, *httptest.ResponseRecorder) {
|
||||
req := httptest.NewRequest(http.MethodGet, url, strings.NewReader(""))
|
||||
rec := httptest.NewRecorder()
|
||||
return c.Web.NewContext(req, rec), rec
|
||||
}
|
||||
|
||||
func initSesssion(t *testing.T, ctx echo.Context) {
|
||||
// Simulate an HTTP request through the session middleware to initiate the session
|
||||
mw := session.Middleware(sessions.NewCookieStore([]byte("secret")))
|
||||
handler := mw(echo.NotFoundHandler)
|
||||
assert.Error(t, handler(ctx))
|
||||
}
|
||||
|
||||
func TestController_Redirect(t *testing.T) {
|
||||
ctx, _ := newContext("/abc")
|
||||
ctx, _ := tests.NewContext(c.Web, "/abc")
|
||||
ctr := NewController(c)
|
||||
err := ctr.Redirect(ctx, "home")
|
||||
require.NoError(t, err)
|
||||
|
|
@ -81,8 +65,8 @@ func TestController_SetValidationErrorMessages(t *testing.T) {
|
|||
err := v.Struct(e)
|
||||
require.Error(t, err)
|
||||
|
||||
ctx, _ := newContext("/")
|
||||
initSesssion(t, ctx)
|
||||
ctx, _ := tests.NewContext(c.Web, "/")
|
||||
tests.InitSession(ctx)
|
||||
ctr := NewController(c)
|
||||
ctr.SetValidationErrorMessages(ctx, err, e)
|
||||
|
||||
|
|
@ -93,8 +77,8 @@ func TestController_SetValidationErrorMessages(t *testing.T) {
|
|||
|
||||
func TestController_RenderPage(t *testing.T) {
|
||||
setup := func() (echo.Context, *httptest.ResponseRecorder, Controller, Page) {
|
||||
ctx, rec := newContext("/test/TestController_RenderPage")
|
||||
initSesssion(t, ctx)
|
||||
ctx, rec := tests.NewContext(c.Web, "/test/TestController_RenderPage")
|
||||
tests.InitSession(ctx)
|
||||
ctr := NewController(c)
|
||||
|
||||
p := NewPage(ctx)
|
||||
|
|
|
|||
|
|
@ -6,13 +6,14 @@ import (
|
|||
|
||||
"goweb/context"
|
||||
"goweb/msg"
|
||||
"goweb/tests"
|
||||
|
||||
echomw "github.com/labstack/echo/v4/middleware"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNewPage(t *testing.T) {
|
||||
ctx, _ := newContext("/")
|
||||
ctx, _ := tests.NewContext(c.Web, "/")
|
||||
p := NewPage(ctx)
|
||||
assert.Same(t, ctx, p.Context)
|
||||
assert.NotNil(t, p.ToURL)
|
||||
|
|
@ -27,7 +28,7 @@ func TestNewPage(t *testing.T) {
|
|||
assert.Empty(t, p.RequestID)
|
||||
assert.False(t, p.Cache.Enabled)
|
||||
|
||||
ctx, _ = newContext("/abc?def=123")
|
||||
ctx, _ = tests.NewContext(c.Web, "/abc?def=123")
|
||||
ctx.Set(context.AuthenticatedUserKey, 1)
|
||||
ctx.Set(echomw.DefaultCSRFConfig.ContextKey, "csrf")
|
||||
p = NewPage(ctx)
|
||||
|
|
@ -39,9 +40,9 @@ func TestNewPage(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestPage_GetMessages(t *testing.T) {
|
||||
ctx, _ := newContext("/")
|
||||
ctx, _ := tests.NewContext(c.Web, "/")
|
||||
tests.InitSession(ctx)
|
||||
p := NewPage(ctx)
|
||||
initSesssion(t, ctx)
|
||||
|
||||
// Set messages
|
||||
msgTests := make(map[msg.Type][]string)
|
||||
|
|
|
|||
|
|
@ -4,28 +4,30 @@ import (
|
|||
"fmt"
|
||||
"testing"
|
||||
|
||||
"goweb/tests"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNewPager(t *testing.T) {
|
||||
ctx, _ := newContext("/")
|
||||
ctx, _ := tests.NewContext(c.Web, "/")
|
||||
pgr := NewPager(ctx, 10)
|
||||
assert.Equal(t, 10, pgr.ItemsPerPage)
|
||||
assert.Equal(t, 1, pgr.Page)
|
||||
assert.Equal(t, 0, pgr.Items)
|
||||
assert.Equal(t, 0, pgr.Pages)
|
||||
|
||||
ctx, _ = newContext(fmt.Sprintf("/abc?%s=%d", PageQueryKey, 2))
|
||||
ctx, _ = tests.NewContext(c.Web, fmt.Sprintf("/abc?%s=%d", PageQueryKey, 2))
|
||||
pgr = NewPager(ctx, 10)
|
||||
assert.Equal(t, 2, pgr.Page)
|
||||
|
||||
ctx, _ = newContext(fmt.Sprintf("/abc?%s=%d", PageQueryKey, -2))
|
||||
ctx, _ = tests.NewContext(c.Web, fmt.Sprintf("/abc?%s=%d", PageQueryKey, -2))
|
||||
pgr = NewPager(ctx, 10)
|
||||
assert.Equal(t, 1, pgr.Page)
|
||||
}
|
||||
|
||||
func TestPager_SetItems(t *testing.T) {
|
||||
ctx, _ := newContext("/")
|
||||
ctx, _ := tests.NewContext(c.Web, "/")
|
||||
pgr := NewPager(ctx, 20)
|
||||
pgr.SetItems(100)
|
||||
assert.Equal(t, 100, pgr.Items)
|
||||
|
|
@ -33,7 +35,7 @@ func TestPager_SetItems(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestPager_IsBeginning(t *testing.T) {
|
||||
ctx, _ := newContext("/")
|
||||
ctx, _ := tests.NewContext(c.Web, "/")
|
||||
pgr := NewPager(ctx, 20)
|
||||
pgr.Pages = 10
|
||||
assert.True(t, pgr.IsBeginning())
|
||||
|
|
@ -44,7 +46,7 @@ func TestPager_IsBeginning(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestPager_IsEnd(t *testing.T) {
|
||||
ctx, _ := newContext("/")
|
||||
ctx, _ := tests.NewContext(c.Web, "/")
|
||||
pgr := NewPager(ctx, 20)
|
||||
pgr.Pages = 10
|
||||
assert.False(t, pgr.IsEnd())
|
||||
|
|
@ -55,7 +57,7 @@ func TestPager_IsEnd(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestPager_GetOffset(t *testing.T) {
|
||||
ctx, _ := newContext("/")
|
||||
ctx, _ := tests.NewContext(c.Web, "/")
|
||||
pgr := NewPager(ctx, 20)
|
||||
assert.Equal(t, 0, pgr.GetOffset())
|
||||
pgr.Page = 2
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue