Started on controller tests.
This commit is contained in:
parent
098d1b7eb2
commit
c6c9ed7fd2
3 changed files with 51 additions and 10 deletions
|
|
@ -192,7 +192,12 @@ func (t *Controller) Redirect(c echo.Context, route string, routeParams ...inter
|
||||||
// - FirstName string `form:"first-name" validate:"required" label:"First name"`
|
// - FirstName string `form:"first-name" validate:"required" label:"First name"`
|
||||||
// Only a few validator tags are supported below. Expand them as needed.
|
// Only a few validator tags are supported below. Expand them as needed.
|
||||||
func (t *Controller) SetValidationErrorMessages(c echo.Context, err error, data interface{}) {
|
func (t *Controller) SetValidationErrorMessages(c echo.Context, err error, data interface{}) {
|
||||||
for _, ve := range err.(validator.ValidationErrors) {
|
ves, ok := err.(validator.ValidationErrors)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, ve := range ves {
|
||||||
var message string
|
var message string
|
||||||
|
|
||||||
// Default the field label to the name of the struct field
|
// Default the field label to the name of the struct field
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,17 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"goweb/config"
|
"goweb/config"
|
||||||
|
"goweb/msg"
|
||||||
"goweb/services"
|
"goweb/services"
|
||||||
|
|
||||||
|
"github.com/gorilla/sessions"
|
||||||
|
"github.com/labstack/echo-contrib/session"
|
||||||
|
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -33,3 +42,38 @@ func newContext(url string) echo.Context {
|
||||||
req := httptest.NewRequest(http.MethodGet, url, strings.NewReader(""))
|
req := httptest.NewRequest(http.MethodGet, url, strings.NewReader(""))
|
||||||
return c.Web.NewContext(req, httptest.NewRecorder())
|
return c.Web.NewContext(req, httptest.NewRecorder())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
ctr := NewController(c)
|
||||||
|
err := ctr.Redirect(ctx, "home")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "", ctx.Response().Header().Get(echo.HeaderLocation))
|
||||||
|
assert.Equal(t, http.StatusFound, ctx.Response().Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestController_SetValidationErrorMessages(t *testing.T) {
|
||||||
|
type example struct {
|
||||||
|
Name string `validate:"required" label:"Label test"`
|
||||||
|
}
|
||||||
|
e := example{}
|
||||||
|
v := validator.New()
|
||||||
|
err := v.Struct(e)
|
||||||
|
require.Error(t, err)
|
||||||
|
|
||||||
|
ctx := newContext("/")
|
||||||
|
initSesssion(t, ctx)
|
||||||
|
ctr := NewController(c)
|
||||||
|
ctr.SetValidationErrorMessages(ctx, err, e)
|
||||||
|
|
||||||
|
msgs := msg.Get(ctx, msg.TypeDanger)
|
||||||
|
require.Len(t, msgs, 1)
|
||||||
|
assert.Equal(t, "<strong>Label test</strong> is required.", msgs[0])
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,10 +7,6 @@ import (
|
||||||
"goweb/context"
|
"goweb/context"
|
||||||
"goweb/msg"
|
"goweb/msg"
|
||||||
|
|
||||||
"github.com/gorilla/sessions"
|
|
||||||
"github.com/labstack/echo-contrib/session"
|
|
||||||
"github.com/labstack/echo/v4"
|
|
||||||
|
|
||||||
echomw "github.com/labstack/echo/v4/middleware"
|
echomw "github.com/labstack/echo/v4/middleware"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
@ -45,11 +41,7 @@ func TestNewPage(t *testing.T) {
|
||||||
func TestPage_GetMessages(t *testing.T) {
|
func TestPage_GetMessages(t *testing.T) {
|
||||||
ctx := newContext("/")
|
ctx := newContext("/")
|
||||||
p := NewPage(ctx)
|
p := NewPage(ctx)
|
||||||
|
initSesssion(t, ctx)
|
||||||
// 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))
|
|
||||||
|
|
||||||
// Set messages
|
// Set messages
|
||||||
msgTests := make(map[msg.Type][]string)
|
msgTests := make(map[msg.Type][]string)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue